Skip to content
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

How to create geometry from relation? #80

Closed
njanakiev opened this issue Oct 27, 2018 · 6 comments
Closed

How to create geometry from relation? #80

njanakiev opened this issue Oct 27, 2018 · 6 comments

Comments

@njanakiev
Copy link

I tried to get geometries from relations but I was getting ArgumentError for both .create_linestring() and .create_multipolygon(). I also tried both methods for each r.memberelement but with the same error. Both area and way geometries are working. Here is a snippet of my code:

import osmium as osm
import shapely.wkb

wkb_factory = osm.geom.WKBFactory()

class OSMHandler(osm.SimpleHandler):
    def __init__(self):
        osm.SimpleHandler.__init__(self)
        self.amenities = []

    def relation(self, r):        
        if 'amenity' in r.tags:
            wkb = wkb_factory.create_linestring(r)
            multipolygon = shapely.wkb.loads(wkb, hex=True)
            lon, lat = line.centroid.x, line.centroid.y
            
            self.amenities.append([r.id, r.tags['amenity'], lon, lat])
                
handler = OSMHandler()
handler.apply_file('liechtenstein-latest.osm.pbf', locations=True)
ArgumentError: Python argument types in
WKBFactory.create_multipolygon(WKBFactory, Relation)
did not match C++ signature:
create_multipolygon(WKBFactory {lvalue} self, osmium::Area area)

Is there a proper way to get the geometry from a relation in pyosmium? Thank you in advance.

@wiktorn
Copy link
Contributor

wiktorn commented Oct 28, 2018

Relations as is do not have geometries. This is something that you can infer from tags. This is done for you in osmium so for relations that are multipolygons you will get an area and that what works for you.

As I understand - you would also like to have similar support for relations that represent line objects (such as routes).

As far as I'm familiar with pyosmium and osmium, this is not implemented yet.

@njanakiev
Copy link
Author

Thank you, @wiktorn. This means that the geometries of relations that are not tagged with area=yes are not accessible by pyosmium?

Yes, I think it would be useful to be able to get the geometry from a relation as a multi polygon or multiline string. But I suppose that issue exists for osmium already?

@lonvia
Copy link
Member

lonvia commented Oct 29, 2018

You can get multi-polygons for all non-broken relations tagged type=multipolygon or type=boundary. Use the area() callback instead of the relation() callback. Be aware though, that this also returns closed ways (i.e. it returns everything that represents an area in OSM)).

There is no way to return multi-linestrings from relation. This is covered as a feature request in #50 already.

@njanakiev
Copy link
Author

Thank you for clarification, @lonvia

@aginpatrick
Copy link

@njanakiev Did you find a way to solve your problem ? I need the same to build a multi-linestring from relation

@keroe
Copy link

keroe commented Mar 29, 2023

I wrote an example which shows how you can get shapely polygons from relations. It is a bit clunky, but I had to do it this way, because I have a history file which is not compatible with the area callback. This can be adapted for linestrings from routes as well. Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants