Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to create shapefile from csv data using shapefile.py but i am constantly getting this error,

Traceback (most recent call last):
  File "createShp_Davis.py", line 31, in <module>
    w.save(File_loc)    
  File "/usr/lib/python2.7/dist-packages/shapefile.py", line 862, in save
    self.saveShp(target)
  File "/usr/lib/python2.7/dist-packages/shapefile.py", line 829, in saveShp
    self.__shapefileHeader(self.shp, headerType='shp')
  File "/usr/lib/python2.7/dist-packages/shapefile.py", line 586, in __shapefileHeader
    raise ShapefileException("Failed to write shapefile bounding box. Floats required.")
shapefile.ShapefileException: Failed to write shapefile bounding box. Floats required.

For some reason it is not saving the shapefile properly, it is able to create .shp file but that's it. There are no .dbf or .shx. I believe i am making some mistake somewhere in the code, but not sure where. Here is my code.

#!/usr/bin/python
import csv
import shapefile
import BIL

File_loc = raw_input("Enter the file name and folder location: ")

w = shapefile.Writer(shapefile.POINT)
w.field('Species_Id')
w.field('Species')
w.field('MeanAnnTemp')
w.field('TempSeasonality')
w.field('MinTempColdestMonth')
w.field('AnnPrecipitation')
#w.field('Elevation')
w.field('Latitude')
w.field('Longitude')

with open('treeMap.csv', 'rb') as f:
    reader = csv.reader(f)
    for rec in reader:
        w.point(rec[6],rec[5])
        lat=[];lon=[]
        lat.append(rec[5]);lon.append(rec[6])
        lat=[float(i) for i in lat];lon=[float(i) for i in lon]
        MAT=BIL.lookupBIL(r'./worldclim/bio1',lat,lon)
        TSD=BIL.lookupBIL(r'./worldclim/bio4',lat,lon)
        AnnTMin=BIL.lookupBIL(r'./worldclim/bio6',lat,lon)
        ANNPREC=BIL.lookupBIL(r'./worldclim/bio12',lat,lon)
        w.record(rec[2], rec[3], MAT[0], TSD[0], AnnTMin[0], ANNPREC[0], rec[5], rec[6])
    w.save(File_loc)    


print "Shapefile Successfully created"    
share|improve this question

2 Answers 2

up vote 6 down vote accepted

You need to make some explicit float casts when creating the point:

        w.point(float(rec[6]), float(rec[5]))
share|improve this answer
    
That worked like a charm thanks @Mike –  Sam007 Jun 21 '12 at 16:01

Shapefile field names must be not exceed 10 characters in length. Also, shouldn't you define each field data type? I'm not sure what the default type is. You can also step-into shapefile.py to view the variables during execution. I recommend PyScripter.

share|improve this answer
    
+1 for suggesting PyScripter –  mapoholic Jun 21 '12 at 6:35
    
Actually the shapefile.py handles the truncating automatically so we do not need to worry about that. Is PyScripter not there for Linux? I was not able to find it on my Synaptic or Ubuntu Software Center. –  Sam007 Jun 21 '12 at 16:11
    
Seems PyScripter is windows only. Eric IDE is very powerful and supports Ubuntu/Linux. Current version is Eric5. –  klewis Jun 22 '12 at 15:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.