1

Extract polygon name if the geo-point is inside polygon ?. I have two dataset one with polygon name and polygon and other with location name and latitude and longitude.

Data 1 (Geopandas Dataframe)

COMMUNITY NAME   POLYGON
New York         MULTIPOLYGON (((55.1993358199345 25.20971347951325, 
                 55.19385836251354 25.20134197109752.... 25.20971347951325)))
Chennai          MULTIPOLYGON (((65.1993358199345 22.20871347951325, 
                 55.19325836251354 15.20132197109752 .... 15.20971347951325)))        

Data 2 (Data Frame)

STOP NAME            LONGITUDE       LANGITUDE
Chennai main stop    55.307228       25.248844
Cabra stop           55.278824       25.205862
USA stop NY          55.069368       24.973946

If the data 2 (stop_name) is inside in the data 1 (polygon) need to extract the name of the polygon. ie. if the USA Stop NY is present in any "New York" need to add the name in the new column in data2.

Sample code :

from shapely.geometry import Point, Polygon
# Create Point objects
p1 = Point(55.230830, 25.128038)
p2 = Point(24.976567, 60.1612500)
# Create a Polygon
coords = [(55.199335819934504,25.209713479513255),(55.193858362513538,25.20134197109752),(55.187450889885667,25.195407028080979 )]
poly = Polygon(coords)
p1.within(poly)

Update 1

Data 1 (KML converted into Json, Json converted into Dataframe)

 import geopandas as gpd
data_poly = gpd.read_file(path + "Data_community_file.geojson")
Kum_R
  • 368
  • 2
  • 19
  • Are you just posting your homework or work assignment? Where exactly is your problem? What have you tried? – Joe Apr 12 '20 at 15:05
  • Ok, let's see. Where are you getting the data from? File, website? How are you accessing it? – Joe Apr 12 '20 at 15:27
  • Do you have some function that downloads it and puts it to a file or database or are you processing on-the-go? – Joe Apr 12 '20 at 15:33
  • Are you using Pandas? or JSON? – Joe Apr 12 '20 at 15:33
  • Or maybe GeoPandas? – Joe Apr 12 '20 at 15:35
  • That should help: https://stackoverflow.com/questions/58513123/geopandas-check-if-point-is-in-polygon?rq=1 – Joe Apr 12 '20 at 15:36
  • Loading data in geopandas (data 1) and (data 2) is a normal dataframe – Kum_R Apr 12 '20 at 15:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211483/discussion-between-rvknlp-and-joe). – Kum_R Apr 12 '20 at 15:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211490/discussion-between-kum-r-and-joe). – Kum_R Apr 12 '20 at 16:29

2 Answers2

1

Here is the answer to the above question.

Install these two packages to avoid the "Error"

    #!pip install rtree
    #conda install -c conda-forge libspatialindex 

Polygon Data (GeoDataFrame)
data_poly = gpd.read_file("data.geojson")
# Readonly the required columns 
# Drop NAN

Location Data (GeoDataFrame)
bus = pd.read_Csv(busstop.csv)

#convert dataframe to geodatframe
gdf = geopandas.GeoDataFrame(
    bus, geometry=geopandas.points_from_xy(bus.stop_location_longitiude, bus.stop_location_latitiude))

#Output
joined_gdf = gpd.sjoin(gdf, data_poly, op='within')
Kum_R
  • 368
  • 2
  • 19
0

I found an interesting article describing how to extract the geopoint from the polygon.
http://archived.mhermans.net/geojson-shapely-geocoding.html

 import json
    from shapely.geometry import shape, Point
    # depending on your version, use: from shapely.geometry import shape, Point

    # load GeoJSON file containing sectors
    with open('sectors.json') as f:
        js = json.load(f)

    # construct point based on lon/lat returned by geocoder
    point = Point(-122.7924463, 45.4519896)

    # check each polygon to see if it contains the point
for feature in js['features']:
    polygon = shape(feature['geometry'])
    if polygon.contains(point):
        print(feature)

It is able to extract the matched polygon form the geojson. Issue : If we use the point from the data frame its throwing error

AttributeError: 'Series' object has no attribute '_geom'
Kum_R
  • 368
  • 2
  • 19