I'm writing a function with my schoolmates that will convert various files (like shapefile to CSV, CSV to TopoJSON etc, we focus on geospatial data) - we are trying to learn Python. My part right now is to write two functions for CSV format (to read and write file) - we write two for each format and then we use them in the main function. This is what I came up with for CSV:
import pandas as pd import geopandas as gpd #read file def from_csv(file, x, y): df = pd.read_csv(file) gdf = gpd.GeoDataFrame(df, geometry = gpd.points_from_xy(df[x], df[y])) return gdf #write file def to_csv(gdf, filename): gdf.to_csv(filename + '.csv')
I tried to use Pandas to read file and then used GeoPandas to create gdf from df - I want user to write which columns are longitude and latitude (to set it as a geometry while creating the GeoDataFrame). Also I know that DataFrame may have coordinates in WKT format. I don't really know how to even start with including that.
To give you an idea - here is a little snippet of our function, I cut out a lot and only left CSV, trying to make it as short as I can. There are more formats that we already included - like GeoJSON. All other functions work (like converting GeoJSON to shapefile, no problems with that). I tried to convert GeoJSON to CSV and it worked, but I cannot convert CSV files to any other format.
valid_formats = ["csv"] valid_extensions = [".csv"] def convert(file, target_format, x = None, y = None): filename, file_extension = os.path.splitext(file) if file_extension in valid_extensions and target_format in valid_formats: if file_extension == ".csv": if x != None and y != None: gdf = from_csv(file, x, y) if target_format == "csv": to_csv(gdf, filename) else: print("wrong input data")