-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src - Add initial support for osm xml (Ref: #12)
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from xml.sax.saxutils import escape | ||
|
||
import overpy | ||
|
||
|
||
def dump(result, fp): | ||
""" | ||
:param result: | ||
:type result: overpy.Result | ||
:param fp: | ||
:return: | ||
""" | ||
fp.write('<?xml version="1.0" encoding="UTF-8"?>\n') | ||
fp.write('<osm version="0.6" generator="OverPy {0}">\n'.format(overpy.__version__)) | ||
lat_min = result.nodes[0].lat | ||
lat_max = lat_min | ||
lon_min = result.nodes[0].lon | ||
lon_max = lon_min | ||
for node in result.nodes: | ||
if node.lat < lat_min: | ||
lat_min = node.lat | ||
elif node.lat > lat_max: | ||
lat_max = node.lat | ||
|
||
if node.lon < lon_min: | ||
lon_min = node.lon | ||
elif node.lon > lon_max: | ||
lon_max = node.lon | ||
|
||
fp.write( | ||
'<bounds minlat="{0:f}" minlon="{1:f}" maxlat="{2:f}" maxlon="{3:f}"/>\n'.format( | ||
lat_min, | ||
lat_max, | ||
lon_min, | ||
lon_max | ||
) | ||
) | ||
|
||
for node in result.nodes: | ||
fp.write( | ||
'<node id="{0:d}" lat="{1:f}" lon="{2:f}"'.format( | ||
node.id, | ||
node.lat, | ||
node.lon | ||
) | ||
) | ||
if len(node.tags) == 0: | ||
fp.write('/>\n') | ||
continue | ||
fp.write('>\n') | ||
for k, v in node.tags.items(): | ||
fp.write( | ||
'<tag k="{0:s}" v="{1:s}"/>\n'.format( | ||
escape(k), | ||
escape(v) | ||
) | ||
) | ||
fp.write('</node>\n') | ||
|
||
for way in result.ways: | ||
fp.write('<way id="{0:d}"'.format(node.id)) | ||
if len(way.nodes) == 0 and len(way.tags) == 0: | ||
fp.write('/>\n') | ||
continue | ||
fp.write('>\n') | ||
for node in way.nodes: | ||
fp.write('<nd ref="{0:d}"/>\n'.format(node.id)) | ||
|
||
for k, v in way.tags.items(): | ||
fp.write( | ||
'<tag k="{0:s}" v="{1:s}"/>\n'.format( | ||
escape(k), | ||
escape(v) | ||
) | ||
) | ||
|
||
fp.write('</way>\n') | ||
|
||
for relation in result.relations: | ||
fp.write('<relation id="{0:d}'.format(relation.id)) | ||
if len(relation.tags) == 0 and len(relation.members) == 0: | ||
fp.write('/>\n') | ||
|
||
for member in relation.members: | ||
if not isinstance(member, overpy.RelationMember): | ||
continue | ||
fp.write( | ||
'<member type="{0:s}" ref="{1:d}" role="{2:s}"/>\n'.format( | ||
member._type_value, | ||
member.ref, | ||
member.role | ||
) | ||
) | ||
|
||
for k, v in relation.tags.items(): | ||
fp.write( | ||
'<tag k="{0:s}" v="{1:s}"/>\n'.format( | ||
escape(k), | ||
escape(v) | ||
) | ||
) | ||
|
||
fp.write('</relation>\n') | ||
|
||
fp.write('</osm>') |
07b638a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the script, unfortunately, it has a bug in line 62: fp.write('<way id="{0:d}"'.format(node.id))
It should be "WAY.id" instead of node.id.
I changed it on my computer locally and now I can download and parse the file with GDAL. Before that, I had a bunch of GDAL errors due to multiple ways having the same ID - I checked it out on OSM and this is how I found the bug.
07b638a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another bug, line 31:
min and max are written out in the wrong order