mocnik-science / osm-python-tools

A library to access OpenStreetMap related services
GNU General Public License v3.0
441 stars 48 forks source link

Get center coordinates of a way #41

Closed korridor closed 3 years ago

korridor commented 3 years ago

First of all, thank you for this very practical package. I would like to do an overpass query that returns the center of ways as well. This basically works fine but I can't read the center coordinates from the result element. The query I performed looks something like this:

(
  node["some"="type"];
  way["some"="type"];
  relation["some"="type"];
);

out center;
out body;
mocnik-science commented 3 years ago

Dear @korridor,

Thank you very much for your kind words. It was actually not possible to access the center coordinates in an easy way. What you can always do is to access the raw json data like follows:

result.elements()[0]._json

However, it is a good idea to include this functionality. This is why I have added it (will be in the release this night). The following code should now work:

zandvoort = nominatim.query('Zandvoort')
query = overpassQueryBuilder(area=zandvoort, elementType='way', selector='"highway"', includeCenter=True)
# or, alternatively: query = overpassQueryBuilder(area=zandvoort, elementType='way', selector='"highway"', out=['center', 'body'])
result = overpass.query(query)
``
Then, the center can be obtained as follows:
```python
result.elements()[0].centerLat()
# 52.3657055
result.elements()[0].centerLon()
# 4.5598827

I hope this is what you were looking for.

Best!

korridor commented 3 years ago

Thanks, just tried the new release and it works great!