Issue
I am working with GeoPandas
and I have two GeoDataframes
with the same CRS
. One of them contains a geometry
column with a polygon geometry
, the other one a column with point geometry
. I want to check which points are inside the polygon.
Naively I tried
shape.contains(points)
This gave me
> The indices of the two GeoSeries are different
I do not understand this message. When I check the documentation, it says
We can also check two GeoSeries against each other, row by row. The GeoSeries above have different indices. We can either align both GeoSeries based on index values and compare elements with the same index using align=True or ignore index and compare elements based on their matching order using align=False:
What are these Indices? Why are they checked against each other and not the geometry columns
?
Online I read, I have to convert my geometries into shapely
geometries. But isn't the whole point of using GeoPandas
that I can perfrom geographical operations on the data?
I am confused about this. How to check if the geometries
in shape
contain any of the geometries
in points
?
Solution
What you are describing is effectively a spatial join. Below example constructs points from lon/lat of cities in UK and then finds which administrational area polygon the city is in. This is an NxM comparison
import pandas as pd
import numpy as np
import geopandas as gpd
import shapely.geometry
import requests
# source some points and polygons
# fmt: off
dfp = pd.read_html("https://www.latlong.net/category/cities-235-15.html")[0]
dfp = gpd.GeoDataFrame(dfp, geometry=dfp.loc[:,["Longitude", "Latitude",]].apply(shapely.geometry.Point, axis=1))
res = requests.get("https://opendata.arcgis.com/datasets/69dc11c7386943b4ad8893c45648b1e1_0.geojson")
df_poly = gpd.GeoDataFrame.from_features(res.json())
# fmt: on
gpd.sjoin(dfp, df_poly)
Answered By - Rob Raymond
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.