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.

  • @user2856 even with doing this my shapefile is located in the wrong place May 9 at 19:35

1 Answer 1


Set the CRS when you create the GeoDataframe:

gdf = gpd.GeoDataFrame(df, geometry=etc..., crs="the CRS of your xy coordinates") 

Note, this requires you know the CRS your coordinates are in and it doesn't reproject them to a new coordinate system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.