Commit d98e5f8d authored by Eliot Blennerhassett's avatar Eliot Blennerhassett

Simpler iteration through places

(runs slower though!)
parent 0da92809
......@@ -22,6 +22,10 @@ import pyspatialite.dbapi2 as db
import sys
def add_tag(node, k, v):
ET.SubElement(node, "tag", k=k, v=v)
def generate_osmchange(dbfile, outdir,
changeset = 1,
min_nodes = 1, max_nodes = 8000,
......@@ -32,71 +36,68 @@ def generate_osmchange(dbfile, outdir,
except OSError:
pass
def add_tag(node, k, v):
ET.SubElement(node, "tag", k=k, v=v)
con = db.connect(dbfile)
cur = con.cursor()
q = '''select * from linz_not_osm'''
# Select subsets by place
q = 'select * from places'
places = list(cur.execute(q))
rows = cur.execute(q)
for place in places:
generate_osmchange_place(place, cur, outdir,
changeset, include_location)
finished = False
prev_suburb = None
num_nodes = 0
block = 0
osmchange = None
while True:
try:
r = rows.next()
linz_id, housenumber, street, x, y, suburb, city = r
num_nodes += 1
except StopIteration:
finished = True
def generate_changeset_place(place, fn, outdir='.'):
osm = ET.Element('osm')
changeset = ET.SubElement(osm, 'changeset')
add_tag(changeset, "created_by", "https://git.nzoss.org.nz/ewblen/osmlinzaddr/blob/master/osmlinzaddr.py")
add_tag(changeset, "attribution", "https://wiki.openstreetmap.org/wiki/Contributors#LINZ")
add_tag(changeset, "source", "https://data.linz.govt.nz/layer/3353-nz-street-address")
add_tag(changeset, "comment", "LINZ address import %s" % fn)
tree = ET.ElementTree(osm)
path = outdir + "/linz_addr_%s.osm" % fn
tree.write(path, pretty_print=True)
if (osmchange is None
or finished
or ((suburb != prev_suburb) and (num_nodes >= min_nodes))
or (num_nodes >= max_nodes)):
def generate_osmchange_place(place, dbcursor, outdir,
changeset, include_location):
if place[0] is None:
q = '''select * from linz_not_osm
where c is NULL and s="{1}"
'''.format(*(place))
else:
q = '''select * from linz_not_osm
where c="{0}" and s="{1}"
'''.format(*(place))
if osmchange is not None:
osm = ET.Element('osm')
changeset = ET.SubElement(osm, 'changeset')
add_tag(changeset, "created_by", "https://git.nzoss.org.nz/ewblen/osmlinzaddr/blob/master/osmlinzaddr.py")
add_tag(changeset, "attribution", "https://wiki.openstreetmap.org/wiki/Contributors#LINZ")
add_tag(changeset, "source", "https://data.linz.govt.nz/layer/3353-nz-street-address")
add_tag(changeset, "comment", "LINZ address import %s" % fn)
tree = ET.ElementTree(osm)
path = outdir + "/linz_addr_%04d_%s.osm" % (block, fn)
tree.write(path, pretty_print=True)
rows = dbcursor.execute(q)
tree = ET.ElementTree(osmchange)
path = outdir + "/linz_addr_%04d_%s.osc" % (block, fn)
print(fn, ":", num_nodes, "nodes")
tree.write(path, pretty_print=True)
lrows = list(rows)
if not len(lrows):
print("No addresses in %s!?" % str(place))
return
if finished:
break
city, suburb = place[0:2]
block += 1
if city is None or city == suburb:
fn = suburb
else:
fn = city + '-' + suburb
fn = fn.replace(' ', '_')
fn = fn.replace('/', '_')
if city is None or city == suburb:
fn = suburb
else:
fn = city + '-' + suburb
fn = fn.replace(' ', '_')
fn = fn.replace('/', '_')
path = outdir + "/linz_addr_%s.osc" % fn
print("%s : %d nodes" % (fn, len(lrows)))
num_nodes = 1
generate_changeset_place(place, fn, outdir)
osmchange = ET.Element('osmChange', version="0.6",
generator="osmlinzaddr.py")
osmchange = ET.Element('osmChange', version="0.6",
generator="osmlinzaddr.py")
prev_suburb = suburb
for r in lrows:
linz_id, housenumber, street, x, y, suburb, city = r
create = ET.SubElement(osmchange, "create")
dnode = {
......@@ -110,6 +111,7 @@ def generate_osmchange(dbfile, outdir,
add_tag(node, "LINZ:address_id", str(linz_id))
add_tag(node, "addr:housenumber", housenumber)
add_tag(node, "addr:street", street)
if include_location:
if city is not None:
add_tag(node, "addr:city", city)
......@@ -117,8 +119,8 @@ def generate_osmchange(dbfile, outdir,
else:
add_tag(node, "addr:hamlet", suburb)
if finished:
break
tree = ET.ElementTree(osmchange)
tree.write(path, pretty_print=True)
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment