|
| 1 | +from xml.sax.saxutils import escape |
| 2 | + |
| 3 | +import overpy |
| 4 | + |
| 5 | + |
| 6 | +def dump(result, fp): |
| 7 | + """ |
| 8 | +
|
| 9 | + :param result: |
| 10 | + :type result: overpy.Result |
| 11 | + :param fp: |
| 12 | + :return: |
| 13 | + """ |
| 14 | + fp.write('<?xml version="1.0" encoding="UTF-8"?>\n') |
| 15 | + fp.write('<osm version="0.6" generator="OverPy {0}">\n'.format(overpy.__version__)) |
| 16 | + lat_min = result.nodes[0].lat |
| 17 | + lat_max = lat_min |
| 18 | + lon_min = result.nodes[0].lon |
| 19 | + lon_max = lon_min |
| 20 | + for node in result.nodes: |
| 21 | + if node.lat < lat_min: |
| 22 | + lat_min = node.lat |
| 23 | + elif node.lat > lat_max: |
| 24 | + lat_max = node.lat |
| 25 | + |
| 26 | + if node.lon < lon_min: |
| 27 | + lon_min = node.lon |
| 28 | + elif node.lon > lon_max: |
| 29 | + lon_max = node.lon |
| 30 | + |
| 31 | + fp.write( |
| 32 | + '<bounds minlat="{0:f}" minlon="{1:f}" maxlat="{2:f}" maxlon="{3:f}"/>\n'.format( |
| 33 | + lat_min, |
| 34 | + lat_max, |
| 35 | + lon_min, |
| 36 | + lon_max |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + for node in result.nodes: |
| 41 | + fp.write( |
| 42 | + '<node id="{0:d}" lat="{1:f}" lon="{2:f}"'.format( |
| 43 | + node.id, |
| 44 | + node.lat, |
| 45 | + node.lon |
| 46 | + ) |
| 47 | + ) |
| 48 | + if len(node.tags) == 0: |
| 49 | + fp.write('/>\n') |
| 50 | + continue |
| 51 | + fp.write('>\n') |
| 52 | + for k, v in node.tags.items(): |
| 53 | + fp.write( |
| 54 | + '<tag k="{0:s}" v="{1:s}"/>\n'.format( |
| 55 | + escape(k), |
| 56 | + escape(v) |
| 57 | + ) |
| 58 | + ) |
| 59 | + fp.write('</node>\n') |
| 60 | + |
| 61 | + for way in result.ways: |
| 62 | + fp.write('<way id="{0:d}"'.format(node.id)) |
| 63 | + if len(way.nodes) == 0 and len(way.tags) == 0: |
| 64 | + fp.write('/>\n') |
| 65 | + continue |
| 66 | + fp.write('>\n') |
| 67 | + for node in way.nodes: |
| 68 | + fp.write('<nd ref="{0:d}"/>\n'.format(node.id)) |
| 69 | + |
| 70 | + for k, v in way.tags.items(): |
| 71 | + fp.write( |
| 72 | + '<tag k="{0:s}" v="{1:s}"/>\n'.format( |
| 73 | + escape(k), |
| 74 | + escape(v) |
| 75 | + ) |
| 76 | + ) |
| 77 | + |
| 78 | + fp.write('</way>\n') |
| 79 | + |
| 80 | + for relation in result.relations: |
| 81 | + fp.write('<relation id="{0:d}'.format(relation.id)) |
| 82 | + if len(relation.tags) == 0 and len(relation.members) == 0: |
| 83 | + fp.write('/>\n') |
| 84 | + |
| 85 | + for member in relation.members: |
| 86 | + if not isinstance(member, overpy.RelationMember): |
| 87 | + continue |
| 88 | + fp.write( |
| 89 | + '<member type="{0:s}" ref="{1:d}" role="{2:s}"/>\n'.format( |
| 90 | + member._type_value, |
| 91 | + member.ref, |
| 92 | + member.role |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + for k, v in relation.tags.items(): |
| 97 | + fp.write( |
| 98 | + '<tag k="{0:s}" v="{1:s}"/>\n'.format( |
| 99 | + escape(k), |
| 100 | + escape(v) |
| 101 | + ) |
| 102 | + ) |
| 103 | + |
| 104 | + fp.write('</relation>\n') |
| 105 | + |
| 106 | + fp.write('</osm>') |
0 commit comments