1

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")
nmtoken
  • 13,355
  • 5
  • 38
  • 87
Edyficjum
  • 11
  • 1
  • 1
    Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short [tour] to learn about our focussed Q&A format. You had a tag for PyQGIS but are not importing and using that, and so I've removed it. What is your question? Without a single question mark in the body of your question it is hard to tell. – PolyGeo Apr 05 '20 at 08:29
  • 1
    take a look perhaps at GDAL, no need to reinvent the wheel. – nmtoken Apr 05 '20 at 09:28
  • Have you looked at this Gist?: https://gist.github.com/nygeog/2731427a74ed66ca0e420eaa7bcd0d2b – Aaron Apr 05 '20 at 20:11

1 Answers1

1

As @nmtoken said, you can take a look at GDAL. There is the ogr2ogr command line utility which, amongst other things, lets you convert between multiple file formats. Here is a great example of how to convert a CSV file to a Shapefile using ogr2ogr.

If you want to execute this from a Python script, you can use the subprocess module.

For example:

import subprocess

subprocess.call('ogr2ogr ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:3857 -oo X_POSSIBLE_NAMES=Lon* -oo Y_POSSIBLE_NAMES=Lat*  -f "ESRI Shapefile" test.shp test.csv')
Marcelo Villa
  • 5,928
  • 2
  • 19
  • 38