I have written some Python code to convert different input files to a shapefile using geopandas
.
import pandas as pd
import os
import geopandas as gpd
from shapely.geometry import Point #convert to 3D GeoPandas GeoDataFrame
#-------INPUT FILE TO SHAPEFILE---------
input_file = "C:/input_file.xyz" # hard-coded for the minute - to be changed
file_extension = os.path.splitext(input_file)[-1].lower()
if file_extension == ".xyz":
df = pd.read_table(input_file, skiprows=2, sep=r'\,|\t', engine='python', names=['x', 'y', 'z'])
df.columns = ["x", "y", "z"]
elif file_extension == ".txt" or ".csv":
df = pd.read_csv(input_file, sep='\,|\t')
df.columns = ["x", "y", "z"]
gdf = gpd.GeoDataFrame(df, geometry=df.apply(lambda row: Point(row.x,row.y,row.z), axis=1))
gdf.to_file("C:/new_shapefile.shp") # hard-coded for the minute - to be changed
print("Shapefile Created!")
The code successfully runs and creates a shapefile, but I'm wondering is there any way to automatically include the coordinate system information? The coordinate systems will obviously vary depending on the input file.