Spatial join points to polygons python
I want to create a simple spatial join between points and polygons using GeoPandas but I think GeoPandas has bug? Show GeoPandas code:
Error :
And if I change the imports with the some code:
I get the following error:
Any ideas why? Note Interactive online version: A spatial join uses binary predicates such as A common use case might be a spatial join between a point layer and a polygon layer where you want to retain the point geometries and grab the attributes of the intersecting polygons. Types of spatial joins¶We currently support the following methods of spatial joins. We refer to the left_df and right_df which are the correspond to the two dataframes passed in as args. Left outer join¶In a LEFT OUTER JOIN ( This is equivalent to the PostGIS query: SELECT pts.geom, pts.id as ptid, polys.id as polyid FROM pts LEFT OUTER JOIN polys ON ST_Intersects(pts.geom, polys.geom); geom | ptid | polyid --------------------------------------------+------+-------- 010100000040A9FBF2D88AD03F349CD47D796CE9BF | 4 | 10 010100000048EABE3CB622D8BFA8FBF2D88AA0E9BF | 3 | 10 010100000048EABE3CB622D8BFA8FBF2D88AA0E9BF | 3 | 20 0101000000F0D88AA0E1A4EEBF7052F7E5B115E9BF | 2 | 20 0101000000818693BA2F8FF7BF4ADD97C75604E9BF | 1 | (5 rows) Right outer join¶In a RIGHT OUTER JOIN ( This is equivalent to the PostGIS query: SELECT polys.geom, pts.id as ptid, polys.id as polyid FROM pts RIGHT OUTER JOIN polys ON ST_Intersects(pts.geom, polys.geom); geom | ptid | polyid ----------+------+-------- 01...9BF | 4 | 10 01...9BF | 3 | 10 02...7BF | 3 | 20 02...7BF | 2 | 20 00...5BF | | 30 (5 rows) Inner join¶In an INNER JOIN ( This is equivalent to the PostGIS query: SELECT pts.geom, pts.id as ptid, polys.id as polyid FROM pts INNER JOIN polys ON ST_Intersects(pts.geom, polys.geom); geom | ptid | polyid --------------------------------------------+------+-------- 010100000040A9FBF2D88AD03F349CD47D796CE9BF | 4 | 10 010100000048EABE3CB622D8BFA8FBF2D88AA0E9BF | 3 | 10 010100000048EABE3CB622D8BFA8FBF2D88AA0E9BF | 3 | 20 0101000000F0D88AA0E1A4EEBF7052F7E5B115E9BF | 2 | 20 (4 rows) Spatial Joins between two GeoDataFrames¶Let’s take a look at how we’d implement these using %matplotlib inline from shapely.geometry import Point from geopandas import datasets, GeoDataFrame, read_file # NYC Boros zippath = datasets.get_path('nybb') polydf = read_file(zippath) # Generate some points b = [int(x) for x in polydf.total_bounds] N = 8 pointdf = GeoDataFrame([ {'geometry': Point(x, y), 'value1': x + y, 'value2': x - y} for x, y in zip(range(b[0], b[2], int((b[2] - b[0]) / N)), range(b[1], b[3], int((b[3] - b[1]) / N)))]) # Make sure they're using the same projection reference pointdf.crs = polydf.crs
Joins¶join_left_df = pointdf.sjoin(polydf, how="left") join_left_df # Note the NaNs where the point did not intersect a boro
join_right_df = pointdf.sjoin(polydf, how="right") join_right_df # Note Staten Island is repeated
join_inner_df = pointdf.sjoin(polydf, how="inner") join_inner_df # Note the lack of NaNs; dropped anything that didn't intersect
We’re not limited to using the pointdf.sjoin(polydf, how="left", predicate="within")
We can also conduct a nearest neighbour join with pointdf.sjoin_nearest(polydf, how="left", distance_col="Distances") # Note the optional Distances column with computed distances between each point # and the nearest polydf geometry.
How do you do spatial join points and polygons?To begin making a spatial join between a polygon layer and a point layer, right click on the polygon layer in the table of contents, and choose Joins and Relates>Join. The join dialogue opens. Make sure that in the dropdown at the top you have chosen “Join data from another layer based on spatial location.”
What is spatial join in Geopandas?A spatial join uses binary predicates such as intersects and crosses to combine two GeoDataFrames based on the spatial relationship between their geometries.
How do you join polygons?3) Select the polygons you want to merge (hold the Shift key while selecting the features so that you can select more than one), click the drop down arrow next to “Editor” on the Editor toolbar and click Merge.
|