Search code examples
pandasgisgeopandaspoint-in-polygon

How Can I Classify Points Under Sub-Regions on a Map in GeoPandas?


I have a map in geopandas and have overlayed it with a scatterplot of events:

enter image description here

The shape file breaks the geography into sub-regions (as shown in the picture above). However, the scatter plot points do not have information on the subregions to which they belong. I want to use the coordinates to assign the name of the subregion to the pandas row of the event. How could I go about this please? I suspect I need to use a for loop of some kind, but I'm not sure how to check if a point lies within a specific polygon.


Solution

  • Use Spatial Join:

    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.

    import geopandas as gpd
    region = gpd.read_file(r"/home/bera/Desktop/GIStest/regions.geojson")
    # region.head(1)
    #    id Region                                           geometry
    # 0  15      A  POLYGON ((481571.645 7195961.743, 487345.148 7..
    
    point = gpd.read_file(r"/home/bera/Desktop/GIStest/regionpoints.geojson")
    
    # point.head()
    #    id                        geometry
    # 0   0  POINT (529586.940 7163711.300)
    # 1   1  POINT (505472.742 7192060.117)
    # 2   2  POINT (493737.417 7209323.959)
    # 3   3  POINT (489694.110 7172779.451)
    # 4   4  POINT (540742.212 7170046.145)
    
    #I only want to join the Region column to the points
    sj = gpd.sjoin(left_df=point, right_df=region[["Region", "geometry"]], how="left", predicate="intersects")
    sj = sj.drop(columns=["index_right"])
    
    #sj.head()
    #    id                        geometry Region
    # 0   0  POINT (529586.940 7163711.300)    NaN
    # 1   1  POINT (505472.742 7192060.117)      D
    # 2   2  POINT (493737.417 7209323.959)    NaN
    # 3   3  POINT (489694.110 7172779.451)      B
    # 4   4  POINT (540742.212 7170046.145)    NaN
    
    #Not all points are within a Region, they get NaN:
    

    enter image description here