2

I followed the guide given here Automate the writing records from csv to shapefile using pyshp module. I follow this procedure and the code doesn't give me any error, but it doesn't produce any output. What can I do to get an output file.shp to load in QGIS? My example file.csv is:

y,x,day1,day2,day3
37.0925854357,14.3860984446,0.0000079572,0.0000939491,0.0000028935
37.0978554286,14.5199850398,0.0000041594,0.0000821038,0.0000015805

This is the code I wrote:

import csv, shapefile
points = shapefile.Writer(shapefile.POINT)
with open("/path/to/file.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['y'])),float(row['x']))
        points.record(*tuple([row[f] for f in header]))
points.save('points')
  • So you're not getting any errors. What have you tried already to get it to load in QGIS? What happens when you try to load the shapefile? – PhilippNagel Dec 14 '17 at 21:12
  • @PhilippNagel the code runs but does not produce any output. – Domenico Fuoco Dec 14 '17 at 21:14
  • 2
    Have you searched your whole file system for a file named 'points'? Because it looks like this code might be writing to a file named 'points' in whatever directory you're running this code in. – Brynne Dec 14 '17 at 21:18
  • @PhilippNagel Sorry, I found it. Is there a way to change destination path? – Domenico Fuoco Dec 14 '17 at 21:21
3

From the comments, it seems the issue was that the file was being output to an unknown location. To remedy this, modify the code as below to specify the output location.

For example, if you want to save the file to C:\Users\DF\points.shp (Windows), your last line should be:

points.save(r'C:\Users\DF\points')

Or on Linux:

points.save(r'/home/DF/points')
  • Sorry, my initial answer added .shp to the filename, which should not be there and will cause an error. Try the above code samples. – PhilippNagel Dec 14 '17 at 21:33
  • I did it (on Linux): points.save(r'/home/DF/points.shp'). It works, thank you – Domenico Fuoco Dec 14 '17 at 21:37

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.