From 9cddd40055ad78e44ef91ffeb41b227f689f80cc Mon Sep 17 00:00:00 2001 From: PhiBo Date: Sun, 10 May 2015 22:54:09 +0200 Subject: [PATCH 01/16] src - Add initial support for osm xml (Ref: #12) --- overpy/format/__init__.py | 0 overpy/format/osm_xml.py | 106 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 overpy/format/__init__.py create mode 100644 overpy/format/osm_xml.py diff --git a/overpy/format/__init__.py b/overpy/format/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/overpy/format/osm_xml.py b/overpy/format/osm_xml.py new file mode 100644 index 0000000..d3a5153 --- /dev/null +++ b/overpy/format/osm_xml.py @@ -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('\n') + fp.write('\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( + '\n'.format( + lat_min, + lat_max, + lon_min, + lon_max + ) + ) + + for node in result.nodes: + fp.write( + '\n') + continue + fp.write('>\n') + for k, v in node.tags.items(): + fp.write( + '\n'.format( + escape(k), + escape(v) + ) + ) + fp.write('\n') + + for way in result.ways: + fp.write('\n') + continue + fp.write('>\n') + for node in way.nodes: + fp.write('\n'.format(node.id)) + + for k, v in way.tags.items(): + fp.write( + '\n'.format( + escape(k), + escape(v) + ) + ) + + fp.write('\n') + + for relation in result.relations: + fp.write('\n'.format( + member._type_value, + member.ref, + member.role + ) + ) + + for k, v in relation.tags.items(): + fp.write( + '\n'.format( + escape(k), + escape(v) + ) + ) + + fp.write('\n') + + fp.write('') From 05800429e29b1aec0c26553c3336c35e94827428 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Sun, 10 May 2015 22:55:33 +0200 Subject: [PATCH 02/16] 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 From e6c233af009b8ad257a5081189774a45168ed2ce Mon Sep 17 00:00:00 2001 From: PhiBo Date: Fri, 7 Apr 2017 15:04:27 +0200 Subject: [PATCH 03/16] src - Fix order of min/max values in osm_xml export --- overpy/format/osm_xml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/overpy/format/osm_xml.py b/overpy/format/osm_xml.py index d3a5153..b9860d7 100644 --- a/overpy/format/osm_xml.py +++ b/overpy/format/osm_xml.py @@ -31,8 +31,8 @@ def dump(result, fp): fp.write( '\n'.format( lat_min, - lat_max, lon_min, + lat_max, lon_max ) ) From 7963e4543b5f1429e70914bbb10229003beed5a4 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Fri, 7 Apr 2017 15:05:52 +0200 Subject: [PATCH 04/16] src - Replace node.id with way.id in osm_xml export --- overpy/format/osm_xml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/overpy/format/osm_xml.py b/overpy/format/osm_xml.py index b9860d7..a67b445 100644 --- a/overpy/format/osm_xml.py +++ b/overpy/format/osm_xml.py @@ -59,7 +59,7 @@ def dump(result, fp): fp.write('\n') for way in result.ways: - fp.write('\n') continue From f182f480d50cd55cfab65e28980fc92ab8caee6a Mon Sep 17 00:00:00 2001 From: PhiBo Date: Fri, 7 Apr 2017 15:19:52 +0200 Subject: [PATCH 05/16] src - Update docstring for geojson export --- overpy/format/geojson.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/overpy/format/geojson.py b/overpy/format/geojson.py index 75dd116..fd9dbe6 100644 --- a/overpy/format/geojson.py +++ b/overpy/format/geojson.py @@ -5,10 +5,21 @@ def dump(result, fp, nodes=False, ways=False, json_args={}): """ + Use the result from the Overpass API to generate GeoJSON. + + More information: - :param result: + * http://geojson.org/ + + :param result: The result from the Overpass API :type result: overpy.Result - :param fp: + :param fp: Filepointer to use + :param nodes: Export nodes + :type nodes: bool + :param ways: Export ways + :type ways: bool + :param json_args: Additional arguments passed to json.dump(...) + :type json_args: dict :return: """ features = [] @@ -50,4 +61,4 @@ def dump(result, fp, nodes=False, ways=False, json_args={}): "features": features } - json.dump(geojson, fp, **json_args) \ No newline at end of file + json.dump(geojson, fp, **json_args) From 16d6a60fd5fe49c817c2cccca0589200ecfbb11f Mon Sep 17 00:00:00 2001 From: PhiBo Date: Fri, 7 Apr 2017 15:25:47 +0200 Subject: [PATCH 06/16] src - Don't use dict as default argument in geojson export --- overpy/format/geojson.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/overpy/format/geojson.py b/overpy/format/geojson.py index fd9dbe6..d9745d6 100644 --- a/overpy/format/geojson.py +++ b/overpy/format/geojson.py @@ -3,7 +3,7 @@ import overpy -def dump(result, fp, nodes=False, ways=False, json_args={}): +def dump(result, fp, nodes=False, ways=False, json_args=None): """ Use the result from the Overpass API to generate GeoJSON. @@ -61,4 +61,6 @@ def dump(result, fp, nodes=False, ways=False, json_args={}): "features": features } + if json_args is None: + json_args = {} json.dump(geojson, fp, **json_args) From 22a8912e9b370b1880e0c5ec80061c8c046e14e6 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Fri, 7 Apr 2017 15:26:09 +0200 Subject: [PATCH 07/16] src - Update docstring for osm_xml export --- overpy/format/osm_xml.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/overpy/format/osm_xml.py b/overpy/format/osm_xml.py index a67b445..4f6bc5a 100644 --- a/overpy/format/osm_xml.py +++ b/overpy/format/osm_xml.py @@ -5,10 +5,15 @@ def dump(result, fp): """ + Use the result from the Overpass API to generate OSM XML - :param result: + More information: + + * http://wiki.openstreetmap.org/wiki/OSM_XML + + :param result: The result from the Overpass API :type result: overpy.Result - :param fp: + :param fp: Filepointer to use :return: """ fp.write('\n') From 9f3c4ebb6b4e2686dd6df27375ae57ab26c545ac Mon Sep 17 00:00:00 2001 From: PhiBo Date: Fri, 7 Apr 2017 15:26:33 +0200 Subject: [PATCH 08/16] doc - Add geojson and osm_xml to sphinx documentation --- docs/source/api.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/source/api.rst b/docs/source/api.rst index a2ffb6d..8276aa6 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -64,3 +64,13 @@ Helper .. automodule:: overpy.helper :members: + + +Format/Export +------------- + +.. automodule:: overpy.format.geojson + :members: + +.. automodule:: overpy.format.osm_xml + :members: From 759f4e4124d13bcdb2c64cab83e6d3a22983f396 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Fri, 7 Apr 2017 15:27:05 +0200 Subject: [PATCH 09/16] test - Basic test for geojson export --- tests/test_export.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/test_export.py diff --git a/tests/test_export.py b/tests/test_export.py new file mode 100644 index 0000000..092e9d6 --- /dev/null +++ b/tests/test_export.py @@ -0,0 +1,20 @@ +import pytest + +import overpy +from overpy import PY2 +from overpy.format import geojson + +from tests.base_class import read_file + +if PY2: + from StringIO import StringIO +else: + from io import StringIO + + +class TestGeoJSON(object): + def test_node01(self): + api = overpy.Overpass() + result = api.parse_json(read_file("json/node-01.json")) + fp = StringIO() + geojson.dump(result, fp, nodes=True, ways=True) From c5beac0e8e569c338a69b3df98b5996bd0ecb7ef Mon Sep 17 00:00:00 2001 From: PhiBo Date: Fri, 7 Apr 2017 15:28:15 +0200 Subject: [PATCH 10/16] test - Basic test for osm_xml export --- tests/test_export.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_export.py b/tests/test_export.py index 092e9d6..bcb42cc 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -2,7 +2,7 @@ import overpy from overpy import PY2 -from overpy.format import geojson +from overpy.format import geojson, osm_xml from tests.base_class import read_file @@ -18,3 +18,11 @@ def test_node01(self): result = api.parse_json(read_file("json/node-01.json")) fp = StringIO() geojson.dump(result, fp, nodes=True, ways=True) + + +class TestOSMXML(object): + def test_node01(self): + api = overpy.Overpass() + result = api.parse_json(read_file("json/node-01.json")) + fp = StringIO() + osm_xml.dump(result, fp) From 69801dc3a5d0540cd4dc1553a29456b704b74352 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 22 Apr 2021 23:25:01 +0200 Subject: [PATCH 11/16] test - Remove Py2 and fix import - Remove Python2 support from tests - Fix update import --- tests/test_export.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/test_export.py b/tests/test_export.py index bcb42cc..d72e5fd 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -1,15 +1,11 @@ import pytest import overpy -from overpy import PY2 from overpy.format import geojson, osm_xml -from tests.base_class import read_file +from tests import read_file -if PY2: - from StringIO import StringIO -else: - from io import StringIO +from io import StringIO class TestGeoJSON(object): From 8bbff4075ba68bce235ab94fab7a9289322d64a7 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 29 Apr 2021 21:30:40 +0200 Subject: [PATCH 12/16] src - Remove whitespaces --- overpy/format/geojson.py | 2 +- overpy/format/osm_xml.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/overpy/format/geojson.py b/overpy/format/geojson.py index d9745d6..781ff83 100644 --- a/overpy/format/geojson.py +++ b/overpy/format/geojson.py @@ -6,7 +6,7 @@ def dump(result, fp, nodes=False, ways=False, json_args=None): """ Use the result from the Overpass API to generate GeoJSON. - + More information: * http://geojson.org/ diff --git a/overpy/format/osm_xml.py b/overpy/format/osm_xml.py index 4f6bc5a..5b6a9e3 100644 --- a/overpy/format/osm_xml.py +++ b/overpy/format/osm_xml.py @@ -8,7 +8,7 @@ def dump(result, fp): Use the result from the Overpass API to generate OSM XML More information: - + * http://wiki.openstreetmap.org/wiki/OSM_XML :param result: The result from the Overpass API From 33b3473d31af351fdd4a1c9aa91ffba12ae8e7d6 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 29 Apr 2021 21:43:34 +0200 Subject: [PATCH 13/16] src - Add type hints --- overpy/format/geojson.py | 7 ++----- overpy/format/osm_xml.py | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/overpy/format/geojson.py b/overpy/format/geojson.py index 781ff83..65a5794 100644 --- a/overpy/format/geojson.py +++ b/overpy/format/geojson.py @@ -1,9 +1,10 @@ import json +from typing import Optional, TextIO import overpy -def dump(result, fp, nodes=False, ways=False, json_args=None): +def dump(result: overpy.Result, fp: TextIO, nodes: bool = False, ways: bool = False, json_args: Optional[dict] = None): """ Use the result from the Overpass API to generate GeoJSON. @@ -12,14 +13,10 @@ def dump(result, fp, nodes=False, ways=False, json_args=None): * http://geojson.org/ :param result: The result from the Overpass API - :type result: overpy.Result :param fp: Filepointer to use :param nodes: Export nodes - :type nodes: bool :param ways: Export ways - :type ways: bool :param json_args: Additional arguments passed to json.dump(...) - :type json_args: dict :return: """ features = [] diff --git a/overpy/format/osm_xml.py b/overpy/format/osm_xml.py index 5b6a9e3..1e51d21 100644 --- a/overpy/format/osm_xml.py +++ b/overpy/format/osm_xml.py @@ -1,9 +1,10 @@ +from typing import TextIO from xml.sax.saxutils import escape import overpy -def dump(result, fp): +def dump(result: overpy.Result, fp: TextIO): """ Use the result from the Overpass API to generate OSM XML @@ -12,7 +13,6 @@ def dump(result, fp): * http://wiki.openstreetmap.org/wiki/OSM_XML :param result: The result from the Overpass API - :type result: overpy.Result :param fp: Filepointer to use :return: """ From 07c1bc927e930182f8f78b313c1cabfb02cbf7e9 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 29 Apr 2021 21:45:47 +0200 Subject: [PATCH 14/16] test - Remove unused import --- tests/test_export.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_export.py b/tests/test_export.py index d72e5fd..867cc64 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -1,5 +1,3 @@ -import pytest - import overpy from overpy.format import geojson, osm_xml From 737dc2e1af852f314fc82dbce5b8bdd92d89415d Mon Sep 17 00:00:00 2001 From: PhiBo Date: Thu, 29 Apr 2021 22:07:48 +0200 Subject: [PATCH 15/16] src - Add type hints --- overpy/format/geojson.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/overpy/format/geojson.py b/overpy/format/geojson.py index 65a5794..018bd33 100644 --- a/overpy/format/geojson.py +++ b/overpy/format/geojson.py @@ -1,5 +1,5 @@ import json -from typing import Optional, TextIO +from typing import Any, Dict, Optional, TextIO import overpy @@ -22,7 +22,7 @@ def dump(result: overpy.Result, fp: TextIO, nodes: bool = False, ways: bool = Fa features = [] if nodes: for node in result.nodes: - properties = {} + properties: Dict[str, Any] = {} features.append({ "type": "Feature", "geometry": { From bb4dfc0aa82aa4caf1fcd781f5a9899147cbe4b7 Mon Sep 17 00:00:00 2001 From: PhiBo Date: Wed, 26 May 2021 22:48:39 +0200 Subject: [PATCH 16/16] doc - Add warning about pre alpha state of export functions --- docs/source/api.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/source/api.rst b/docs/source/api.rst index 8276aa6..81158bf 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -69,6 +69,10 @@ Helper Format/Export ------------- +.. warning:: + + This feature is in pre alpha stage. Please report any issues. + .. automodule:: overpy.format.geojson :members: