'Using pandas in python to convert file to shapefile with XYZ columns
I have the following python code to read in an .xyz, .txt or .csv extension file and convert it to a shapefile with the headers as X, Y and Z
import pandas as pd
import os
import geopandas as gpd
from shapely.geometry import Point #convert to 3D GeoPandas GeoDataFrame
input_file = "C:/test/input_xyz.xyz"
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:/test/output_shp.shp")
print("Shapefile Created!")
However, I seem to be struggling with the X, Y, Z headers throwing the conversion of for each of the file types.
For example:
The above .xyz file is in this format
625372.73 234629.36 10.50
625373.35 234630.42 10.35
625374.47 234627.45 10.79
625374.44 234628.46 10.59
625374.45 234629.48 10.44
but if I run my code I get the error TypeError: must be real number, not str
Similarly, one of my CSVs is the format below:
X Y Z date
310746.25 681561.75 -8.82 26/02/2022
310745.75 681561.75 -8.85 26/02/2022
310745.25 681561.75 -8.74 26/02/2022
and when I run my code I get the error ValueError: Length mismatch: Expected axis has 4 elements, new values have 3 elements
I need a way to run my code so that it recognises the file types and adds in x y z as the column headers regardless of the current headers/no. of columns
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
