Last active
July 26, 2022 10:45
-
-
Save dimejimudele/ddb7f16578acfaa4348ebe39145a186d to your computer and use it in GitHub Desktop.
Batch conversion of geocoded CSV files to ESRI shapefiles using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1 Import Libraries | |
import pandas as pd | |
from geopandas import GeoDataFrame | |
from shapely.geometry import Point | |
import os | |
import fiona | |
# 2 Directory containing your .csv files | |
directory = r'C:\user\my_csv_files\...' | |
# 3 Read files sequentially | |
for file in os.listdir(directory): | |
filename = os.fsdecode(file) | |
if file.endswith(".csv"): #Ignoring files with endings different from .csv in your folder | |
input_file = directory + filename | |
df = pd.read_csv(input_file) #Reading your csv file with pandas | |
# 4 Create tuples of geometry by zipping Longitude and latitude columns in your csv file | |
geometry = [Point(xy) for xy in zip(df.Longitude, df.Latitude)] | |
# 5 Define coordinate reference system on which to project your resulting shapefile | |
crs = {'init': 'epsg:4326'} | |
# 6 Convert pandas object (containing your csv) to geodataframe object using geopandas | |
gdf = GeoDataFrame(df, crs = crs, geometry=geometry) | |
# 7 Save file to local destination | |
output_filename = directory + "shape files\\" + filename[:-4] + ".shp" | |
gdf.to_file(filename= output_filename, driver='ESRI Shapefile') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment