1

I have a function which takes the coordinate argument from the user and converts it to a shapefile, and finally saves that shapefile to the filepath that the user gives. This is the code...

epsg_code = int(input('Enter epsg code: '))
outfilepath = input('Enter filepath for where the shapefile should be stored(r"\\...\\..\\"): ')

def geodf(coords):
    from shapely.geometry import Polygon
    import geopandas as gpd
    from fiona.crs import from_epsg
    import os

    coords_gdf = gpd.GeoDataFrame()
    coords_poly = Polygon(coords)
    coords_poly

    coords_gdf.loc[0, 'geometry'] = coords_poly
    coords_gdf.crs = from_epsg(epsg_code)
    coords_gdf.crs
    return coords_gdf.plot(color = 'brown')

    #coords_gdf.to_file(os.path.join(outfilepath))#, *coords_gdf.shp ))
    coords_gdf.to_file(outfilepath + coords.shp)
    #outfilepath + "\\coords_gdf.shp")

a_coords = [(659337, 737770), (659335, 737703), (659329, 737702) ,(659328, 737700), (659282, 737700), (659268, 737749), (659296, 737767)] #example

epsg_code = 32630

geodf(a_coords)

But the shapefile doesn't get saved to the filepath I put in.

5
  • Use os.path.join to combine path and filename instead of outfilepath + coords.shp.
    – BERA
    Mar 6, 2020 at 20:16
  • Thanks @BERA, I just tried ` coords_gdf.to_file(os.path.join(outfilepath, coords_gdf.shp))` but still doesn't get saved there. Mar 6, 2020 at 20:32
  • coords.shp needs to be passed as string - 'coords.shp' Mar 7, 2020 at 11:03
  • [@martinfleis], i just passed it to a string and got the error: "CPLE_AppDefinedError: Failed to create file r'C:\\Users\\...'\coords_gdf.shp: No error" Mar 7, 2020 at 16:05
  • @user2856, the shapefile isn't saved to the filepath given. Outfilepath is the variable which stores the path where the shapefile should be saved. Mar 7, 2020 at 16:07

1 Answer 1

3
  1. You have a return statement before you write out your file, so the coords_gdf.to_file(etc...) will never get run as the geodf function has already returned.
  2. Then you'll get NameError: name 'coords.shp' is not defined because python will interpret coords.shp as a variable.attribute instead of a string. Wrap it in quotes and use os.path.join
  3. You try to do too much in your geodf function, just have it create and return a GeoDataframe and then do whatever you want with it later.

For example:

from shapely.geometry import Polygon
import geopandas as gpd
from fiona.crs import from_epsg
import os


def geodf(coords, epsg_code):

    coords_gdf = gpd.GeoDataFrame()
    coords_poly = Polygon(coords)
    coords_poly

    coords_gdf.loc[0, 'geometry'] = coords_poly
    coords_gdf.crs = from_epsg(epsg_code)
    coords_gdf.crs

    return coords_gdf

outdir = '/tmp'
outfilepath = os.path.join(outdir, 'coords_gdf.shp')
a_coords = [(659337, 737770), (659335, 737703), (659329, 737702) ,(659328, 737700), (659282, 737700), (659268, 737749), (659296, 737767)] #example
epsg_code = 32630

coords_gdf = geodf(a_coords, epsg_code)
coords_gdf.plot(color='brown')
coords_gdf.to_file(outfilepath)
1
  • This was helpful, thank you. Mar 23, 2020 at 15:12

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.