import os
import pandas as pd
from shapely.geometry import Polygon, mapping
from fiona import collection
# Define path
path = os.path.abspath(os.path.dirname(__file__))
# Set working directory
os.chdir(path)
# Define file to convert
file = 'points.ascii'
# Define shp file schema
schema = { 'geometry': 'Polygon', 'properties': { 'Name': 'str' } }
# Read in data
data = pd.read_csv(file, sep='\\t')
# Define shp file to write to
shpOut = 'poly.shp'
# Create array for storing vertices
polyPoints = []
# Create shp file
with collection(shpOut, "w", "ESRI Shapefile", schema) as output:
# Loop through dataframe and populate shp file
for index, row in data.iterrows():
# Add points to polyPoints
polyPoints.append([row['Longitude'], row['Latitude']])
# Define polygon
polygon = Polygon(polyPoints)
# Write output
output.write({
'properties': {'Name': 'Polygon_from_points' },
'geometry': mapping(polygon)
})