I'm working on a script, where I want to import a CSV file with coordinates and an ID which should be used to make a polygon shapefile from those.
CSV file looks like this (example):
ID, X, Y
10,15.116686,61.483157
10,17.114749,62.483098
10,17.113456,62.492142
11,14.123456,61.123456
12,12.345678,61.123456
…..
Etc.
So far I managed to make a point output using only the coordinates, but not the ID:
import pandas as pd
from geopandas import GeoDataFrame as gdf
from shapely.geometry import Point, Polygon
temp = r'C:\Temp\%s.csv'
df = pd.read_csv(temp % 'csvfile')
geometry = [Point(xy) for xy in zip(df.x, df.y)]
geo_df.to_file(filename = 'datatest2.shp', driver ='ESRI Shapefile')
This turns out great. But what should i do if i want all the ID's with 10, gather those coordinate points, and then make a polygon out of those coordinates? – and ofc. with ID 11,12,13 etc. as well?
Best Answer
Load the csv file with Pandas:
Compute the geometry column
Convert the DataFrame to a GeoDataFrame with GeoPandas
Select the rows/points where ID = 10:
Convert to Polygon
But you can do it directly without using a GeoDataFrame
New
If you want to do all the ID's at once, use the pandas.DataFrame.groupby command