2

I searched for my problem and found this question which is different from my issue.

I have two geo data frames, one contains houses locations as points (~700 points) and the other contains suburbs names and their polygon (~2973 polygons). I want to link each point to a polygon to assign each house to the correct suburb.

sample of my geo dataframe

polygon

import geopandas as gpd
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon

#creating geo series
polys = gpd.GeoSeries({
    '6672': Polygon([(142.92288, -37.97886,), (141.74552, -35.07202), (141.74748, -35.06367)]),
    '6372': Polygon([(148.66850, -37.40622), (148.66883, -37.40609), (148.66920, -37.40605)]),
})

#creating geo dataframe
polysgdf = gpd.GeoDataFrame(geometry=gpd.GeoSeries(polys))
polysgdf

Which produces the following(my original geo dataframe also includes a suburb column that contains the suburb name but I couldn't add it to my sample, you can only see the suburb ID below)

        geometry
6672    POLYGON ((142.92288 -37.97886, 141.74552 -35.07202, 141.74748 -35.06367, 142.92288 -37.97886))
6372    POLYGON ((148.66850 -37.40622, 148.66883 -37.40609, 148.66920 -37.40605, 148.66850 -37.40622))

sample of the points geo dataframe

points

points=[Point(145.103,-37.792), Point(145.09720, -37.86400), 
        Point(145.02190, -37.85450)]

pointsDF = gpd.GeoDataFrame(geometry=points,
                                  index=['house1_ID', 'house2_ID', 'house3_ID'])

pointsDF

Which produces the following

            geometry
house1_ID   POINT (145.10300 -37.79200)
house2_ID   POINT (145.09720 -37.86400)
house3_ID   POINT (145.02190 -37.85450)

I would like the final output to be the pointsDF geo dataframe with each house assigned to the corresponding suburb. As a result of matching the points and the polygons.

Example:

suburbID subrubName    house_ID
6672      south apple  house1_ID
6372      water garden house2_ID

I am new to GeoPandas, I tried to explain my question in the clearest way possible. I am happy to clarify any point. Thank you.

Community
  • 1
  • 1
leena
  • 563
  • 1
  • 8
  • 25
  • 2
    Does this answer your question? [geopandas point in polygon](https://stackoverflow.com/questions/48097742/geopandas-point-in-polygon) – Borealis Feb 23 '20 at 21:00

2 Answers2

9

I found a way to accomplish this by joining the two data frames using a spatial join

joinDF=gpd.sjoin(pointsDF, polysgdf, how='left',op="within")
Georgy
  • 12,464
  • 7
  • 65
  • 73
leena
  • 563
  • 1
  • 8
  • 25
  • Thanks, I had to install "pygeos" module separately and set the option "gpd.options.use_pygeos=True" before creating both geoDataFrame, after that worked fine. Much cleaner and simpler – aesutsha Aug 04 '20 at 14:33
1

Use shapely's Point-in-Polygon analysis using .contains function as follows.

import geopandas as gpd
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon

polys = gpd.GeoSeries({
    '6672': Polygon([(0, 0), (0, 1), (1, 0)]),
    '6372': Polygon([(0, 1), (1, 1), (1, 0)]),
})

#creating geo dataframe
polysgdf = gpd.GeoDataFrame(geometry=gpd.GeoSeries(polys))
polysgdf
Out[48]: 
                            geometry
6672  POLYGON ((0 0, 0 1, 1 0, 0 0))
6372  POLYGON ((0 1, 1 1, 1 0, 0 1))

points=[Point(0.25,0.25), Point(0.75,0.75), 
        Point(145.02190, -37.85450)]

pointsDF = gpd.GeoDataFrame(geometry=points,
                                  index=['house1_ID', 'house2_ID', 'house3_ID'])

pointsDF
Out[49]: 
                            geometry
house1_ID          POINT (0.25 0.25)
house2_ID          POINT (0.75 0.75)
house3_ID  POINT (145.0219 -37.8545)

polysgdf['house_ID'] = ''
for i in range(0,len(pointsDF)):
    print('Check for house '+str(pointsDF.index.values.astype(str)[i]))
    for j in range(0,len(polysgdf)):
        print('Check for suburb '+str(polysgdf.index.values.astype(str)[j]))
        if polysgdf['geometry'][j].contains(pointsDF['geometry'][i]) == True:
            polysgdf['house_ID'][j] = pointsDF.index.values.astype(str)[i]

print(polysgdf)
                            geometry   house_ID
6672  POLYGON ((0 0, 0 1, 1 0, 0 0))  house1_ID
6372  POLYGON ((0 1, 1 1, 1 0, 0 1))  house2_ID
Debjit Bhowmick
  • 920
  • 7
  • 20
  • Thank you Debjit. But this code does not return the desired output. However, I was able to modify it. Thanks again. – leena Oct 23 '19 at 02:12