1

I have written Code to establish Point in Polygon in Python, the program uses a shapefile that I read in as the Polygons. I now have a dataframe I read in with a column containing the Polygon e.g [[28.050815,-26.242253],[28.050085,-26.25938],[28.011934,-26.25888],[28.020216,-26.230127],[28.049828,-26.230704],[28.050815,-26.242253]]. I want to transform this column into a polygon in order to perform Point in Polygon, but all the examples use geometry = [Point(xy) for xy in zip(dataPoints['Long'], dataPoints['Lat'])] but mine is already zip? How would I go about achieving this?

Thanks

1 Answers1

0

taking your example above you could do the following:

list_coords = [[28.050815,-26.242253],[28.050085,-26.25938],[28.011934,-26.25888],[28.020216,-26.230127],[28.049828,-26.230704],[28.050815,-26.242253]]
from shapely.geometry import Point, Polygon

# Create a list of point objects using list comprehension

point_list = [Point(x,y) for [x,y] in list_coords]

# Create a polygon object from the list of Point objects

polygon_feature = Polygon([[poly.x, poly.y] for poly in point_list])

And if you would like to apply it to a dataframe you could do the following:

import pandas as pd
import geopandas as gpd

df = pd.DataFrame({'coords': [list_coords]})

def get_polygon(list_coords):

    point_list = [Point(x,y) for [x,y] in list_coords]

    polygon_feature = Polygon([[poly.x, poly.y] for poly in point_list])

    return polygon_feature

df['geom'] = df['coords'].apply(get_polygon)

However, there might be geopandas built-in functions in order to avoid "reinventing the wheel", so let's see if anyone else has a suggestion :)