Deprecated: Assigning the return value of new by reference is deprecated in /web/htdocs/www.flaviocdc.net/home/wiki/inc/parserutils.php on line 208 Deprecated: Assigning the return value of new by reference is deprecated in /web/htdocs/www.flaviocdc.net/home/wiki/inc/parserutils.php on line 211 Deprecated: Assigning the return value of new by reference is deprecated in /web/htdocs/www.flaviocdc.net/home/wiki/inc/parserutils.php on line 421 Deprecated: Assigning the return value of new by reference is deprecated in /web/htdocs/www.flaviocdc.net/home/wiki/inc/parserutils.php on line 594 Deprecated: Function split() is deprecated in /web/htdocs/www.flaviocdc.net/home/wiki/inc/auth.php on line 154 Python Shapefile to CSV converter [Flavio Casadei Della Chiesa Wiki]
 
Il contenuto di questo sito è rilasciato sotto licenza Creative Commons License se non specificato diversamente

Python Shapefile to CSV converter

This script exports the data content (tabular data) of a shapefile and the bounding box of every object to csv format

"""
Shapefile export
"""
import shapelib, dbflib, string
 
SHAPEFILE="shpname"
 
shp = shapelib.ShapeFile(SHAPEFILE)
dbf = dbflib.DBFFile(SHAPEFILE)
 
sCount = shp.info()[0]
dCount = dbf.record_count()
 
if sCount <> dCount:
	raise "Error: shape  number %s (%d) differs from  dbf records (%d) " %(SHAPEFILE,sCount,dCount)
 
 
#print l'header!
header = dbf.read_record(0)
#print header.keys()
intestazione = string.join(header.keys(),"|")
intestazione += "|XMIN|YMIN|XMAX|YMAX"
 
print intestazione
 
#read the dbf file 
for i in range (dbf.record_count() ):
	rc = dbf.read_record(i)
	sc = shp.read_object(i)
	row = ""
	for value in rc.values():
		row += "%s|" %(value)
	#print the  extent/envelope
	(min,max) = sc.extents()
	xmin = min[0]
	ymin = min[1]
	xmax = max[0]
	ymax = max[1]
 
	row += "%f|%f|%f|%f" %(xmin,ymin,xmax,ymax)
	print row
	#print "\n"
 
python/en/export_a_shapefile_to_csv.txt · Ultima modifica: 2008/12/30 23:04 da fcasadei
 
Recent changes RSS feed