How select cities from country (geopandas)?

Question:

I would like to select cities from polygon (Poland), but filter points in polygon doesn’t work.
I have code:

import geopandas
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world=world[world.name == 'Poland']
cities = geopandas.read_file(geopandas.datasets.get_path('naturalearth_cities'))
world['geometry']
cities['geometry']
c=world['geometry'].contains(cities['geometry'])
cities[c.values]

Unfortunately the results are only "False". Could You help me ? Why "contains" is not working ?
Is another way to select cities in geopandas ?

Asked By: Maciej

||

Answers:

Doing read_file() with mask argument or an sjoin() should be the same. Currently due to an issues with compatibility of geos and gdal versions these are not the same on my environment.

import geopandas

world = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
world = world[world.name == "Poland"]
cities = geopandas.read_file(geopandas.datasets.get_path("naturalearth_cities"))
cities = geopandas.read_file(
    geopandas.datasets.get_path("naturalearth_cities"), mask=world
)
Answered By: Rob Raymond
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.