From 764607da7deceecb3b4deea30d8c3fd12007ca3d Mon Sep 17 00:00:00 2001 From: PhiBo Date: Sun, 10 May 2015 22:55:33 +0200 Subject: [PATCH] src - Initial support for GeoJSON (Ref: #13) --- overpy/format/geojson.py | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 overpy/format/geojson.py diff --git a/overpy/format/geojson.py b/overpy/format/geojson.py new file mode 100644 index 0000000..75dd116 --- /dev/null +++ b/overpy/format/geojson.py @@ -0,0 +1,53 @@ +import json + +import overpy + + +def dump(result, fp, nodes=False, ways=False, json_args={}): + """ + + :param result: + :type result: overpy.Result + :param fp: + :return: + """ + features = [] + if nodes: + for node in result.nodes: + properties = {} + features.append({ + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + float(node.lon), + float(node.lat) + ] + }, + "properties": properties + }) + + if ways: + for way in result.ways: + properties = {} + coordinates = [] + for node in way.nodes: + coordinates.append([ + float(node.lon), + float(node.lat) + ]) + features.append({ + "type": "Feature", + "geometry": { + "type": "LineString", + "coordinates": coordinates + }, + "properties": properties + }) + + geojson = { + "type": "FeatureCollection", + "features": features + } + + json.dump(geojson, fp, **json_args) \ No newline at end of file