0

I'm trying to convert a file.csv to a file.shp through the pyshp / shapefile module. The content of the CSV is:

Lon,Lat,Vel,Dir
15.9576198620869825,41.1791119428255143,9.3949995040893555,92.0000000000000000
15.9598119351079415,41.1791119428255143,9.3949995040893555,92.0000000000000000
15.9620040081288987,41.1791119428255143,..., ...

the code I wrote is as follows:

import csv
import shapefile as shp  

points = shp.Writer(shp.POINT)
with open(csv_path + '.csv', 'rb') as csvfile:
     csvreader = csv.DictReader(csvfile)
     header = csvreader.fieldnames
     [points.field(field) for field in header]
     for row in csvreader:
        points.point((float(row['Lon'])),(float(row['Lat'])))
        points.record(*tuple([row[f] for f in header]))
points.save(r'path/to/output/file.shp')

I always used this code without problems, but I don't understand why in this case it return:

Traceback (most recent call last):
  File "my_script.py", line 110, in <module>
    points = shp.Writer(shp.POINT)
  File "/home/username/.local/lib/python2.7/site-packages/shapefile.py", line 1018, in __init__
    self.shp = self.__getFileObj(os.path.splitext(target)[0] + '.shp')
  File "/usr/lib/python2.7/posixpath.py", line 98, in splitext
    return genericpath._splitext(p, sep, altsep, extsep)
  File "/usr/lib/python2.7/genericpath.py", line 99, in _splitext
    sepIndex = p.rfind(sep)
AttributeError: 'int' object has no attribute 'rfind'
  • 2
    Looks like shapefile.Writer() takes the path to the shapefile, not the type of shapefile. shapefile.POINT is likely an integer "constant" representing that geometry type, hence the error complaining about an int – mikewatt Nov 6 '18 at 20:27
1

It is a bug in pyshp version 2.0.0 (the version I was using which came out a coupt of days ago). I rolled back to version 1.2.12 and it's working again. More details for it at the link. It will be amended in the next rollout:

https://github.com/pytroll/pycoast/issues/17

1

This worked for me using the latest version of pyshp and without using GDAL:

replace

points = shp.Writer(shp.POINT)

with

points = shp.Writer('your_shapefile_name_here', shapeType = shp.POINT)

and (you may not need this)

with open('testing.csv', 'rb') as csvfile:

with

with open('testing.csv', 'r') as csvfile:

(I personally received an error asking me if I meant to read in text instead of read in binary);

and finally, delete

    points.save(r'path/to/output/file.shp')

since your points will be saved as they write, and the shapefile is already named ('your_shapefile_name_here') above.

0

I solved using the features of ogr2ogr:

import gdal
import os
os.system('ogr2ogr -f "ESRI Shapefile" -a_srs EPSG:4326 '\
          '-oo X_POSSIBLE_NAMES=Lon -oo Y_POSSIBLE_NAMES=Lat '\
          '-oo KEEP_GEOM_COLUMNS=NO '\
          '/path/to/output/shapefile.shp '
          'path/to/input/file.csv')

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.