forked from micolous/geojsontools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson2osm.py
81 lines (59 loc) · 2.04 KB
/
geojson2osm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
"""
geojson2osm
Converts a GeoJSON shapefile to OSM XML. WIP PROBABLY BROKEN
Copyright 2014 Michael Farrell <http://micolous.id.au>
License: 3-clause BSD, see COPYING
"""
import geojson, argparse
import xml.etree.ElementTree as ET
def osmme(input_f, output_f, name_field, all_tags=False):
output_dom = ET.Element('osm', dict(version='0.5', generator='geojson2osm'))
layer = geojson.load(input_f, use_decimal=True)
# Note: does not check CRS, assumes WGS84!
for gid, point in enumerate(layer.features):
# Only supports points!
if point.geometry.type != 'Point':
continue
node = ET.SubElement(output_dom, 'node', dict(
id=unicode(-gid),
visible='true',
lat=unicode(point.geometry.coordinates[1]),
lon=unicode(point.geometry.coordinates[0])
))
ET.SubElement(node, 'tag', dict(k='name', v=unicode(point.properties[name_field])))
ET.SubElement(node, 'tag', dict(k='note', v=unicode(point.properties[name_field])))
# so that this doesn't show up in the output again.
del point.properties[name_field]
if all_tags:
# write out other properties as custom tags
for k, v in point.properties.iteritems():
ET.SubElement(node, 'tag', dict(k=u'custom:' + k, v=unicode(v)))
# write XML
output_f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
output_f.write(ET.tostring(output_dom, encoding='utf-8'))
output_f.write('\n')
output_f.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('input', nargs=1,
type=argparse.FileType('rb'),
help='Input GeoJSON file'
)
parser.add_argument('-o', '--output',
required=True,
type=argparse.FileType('wb'),
help='Output OSM file'
)
parser.add_argument('-n', '--name-field',
default='name',
help='Field to use for the "name" tag [default: %(default)s]'
)
parser.add_argument('-a', '--all-tags',
action='store_true',
help='Include all properties of the GeoJSON file as tags'
)
options = parser.parse_args()
osmme(options.input[0], options.output, options.name_field, options.all_tags)
if __name__ == '__main__':
main()