I have a CSV file with records in below format.
area | confidence | boundary |
---|---|---|
159.1512 | 0.8084 | POLYGON((87.7185491791352 22.5667828312016, 87.7184590804737 22.5667864311825, 87.7184519272495 22.5666320082646, 87.7185420258111 22.5666284082901, 87.7185491791352 22.5667828312016)) |
I am using the following python code which uses the boundary column to get the polygon shapefile from it.The code works fine however, when I add the exported the shapefile into ArcGIS the polygons are not falling on correct location.
import geopandas as gpd
import pandas as pd
from shapely.geometry import Polygon
df = pd.read_csv(r"/content/Test_Polygon.csv")
df.boundary = df.boundary.str.findall('\d+\.\d+').apply(lambda x: list(map(float, x))) #Extract numbers from boundary string
def givepoly(col):
"""Function to return a shapely polygon from a string of lat and longs"""
return Polygon([[col[i+1], col[i]] for i in range(0, len(col)-1,2)])
df['geometry'] = df.apply(lambda row: givepoly(row['boundary']), axis=1)
df2 = gpd.GeoDataFrame(df, geometry='geometry', crs="EPSG:4326")
df2.drop('boundary', inplace=True, axis=1) #A shapefile cant have a list as data type
df2.to_file(r"/content/test3.shp")
Am not sure where am going wrong in this. I used the following link for reference : Converting CSV file with polygons in dictionary format into GeoDataFrame