Home > Back-end >  How select cities from country (geopandas)?
How select cities from country (geopandas)?

Time:10-19

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 ?

CodePudding user response:

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
)
  • Related