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"
2 Answers 2
You need to make some explicit float casts when creating the point:
w.point(float(rec[6]), float(rec[5]))
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.
-
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– Sam0072012年06月21日 16:11:15 +00:00Commented Jun 21, 2012 at 16:11
-
Seems PyScripter is windows only. Eric IDE is very powerful and supports Ubuntu/Linux. Current version is Eric5.klewis– klewis2012年06月22日 15:02:25 +00:00Commented Jun 22, 2012 at 15:02