-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WiP] Export results #66
Open
phibos
wants to merge
16
commits into
master
Choose a base branch
from
export
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9cddd40
src - Add initial support for osm xml (Ref: #12)
phibos 0580042
src - Initial support for GeoJSON (Ref: #13)
phibos e6c233a
src - Fix order of min/max values in osm_xml export
phibos 7963e45
src - Replace node.id with way.id in osm_xml export
phibos f182f48
src - Update docstring for geojson export
phibos 16d6a60
src - Don't use dict as default argument in geojson export
phibos 22a8912
src - Update docstring for osm_xml export
phibos 9f3c4eb
doc - Add geojson and osm_xml to sphinx documentation
phibos 759f4e4
test - Basic test for geojson export
phibos c5beac0
test - Basic test for osm_xml export
phibos 69801dc
test - Remove Py2 and fix import
phibos 8bbff40
src - Remove whitespaces
phibos 33b3473
src - Add type hints
phibos 07c1bc9
test - Remove unused import
phibos 737dc2e
src - Add type hints
phibos bb4dfc0
doc - Add warning about pre alpha state of export functions
phibos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,63 @@ | ||
import json | ||
from typing import Any, Dict, Optional, TextIO | ||
|
||
import overpy | ||
|
||
|
||
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. | ||
|
||
More information: | ||
|
||
* http://geojson.org/ | ||
|
||
:param result: The result from the Overpass API | ||
:param fp: Filepointer to use | ||
:param nodes: Export nodes | ||
:param ways: Export ways | ||
:param json_args: Additional arguments passed to json.dump(...) | ||
:return: | ||
""" | ||
features = [] | ||
if nodes: | ||
for node in result.nodes: | ||
properties: Dict[str, Any] = {} | ||
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 | ||
} | ||
|
||
if json_args is None: | ||
json_args = {} | ||
json.dump(geojson, fp, **json_args) |
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,111 @@ | ||
from typing import TextIO | ||
from xml.sax.saxutils import escape | ||
|
||
import overpy | ||
|
||
|
||
def dump(result: overpy.Result, fp: TextIO): | ||
""" | ||
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 | ||
:param fp: Filepointer to use | ||
: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, | ||
lon_min, | ||
lat_max, | ||
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(way.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>') |
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,22 @@ | ||
import overpy | ||
from overpy.format import geojson, osm_xml | ||
|
||
from tests import read_file | ||
|
||
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) | ||
|
||
|
||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this excellent PR!
Just one brief suggestion - Some
way
's have duplicate first and last coordinates and so they ought to be treated asPolygon
rather thanLineString
.For more info check out https://wiki.openstreetmap.org/wiki/Way#Types_of_way
and
https://wiki.openstreetmap.org/wiki/Overpass_turbo/Polygon_Features (this info feels a bit more interpretable)
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 information. I will have a look at the documentation.