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 have a .shp file with 60k+ entries, all of which are polygons with corresponding attributes (acreage totals, landowner names, tax ID #s, etc.). What I ultimately need is a .csv file with all of these attributes and their corresponding geometry (in the .kml compatible xyz format, that is, NOT the WKT format).

I know that I can open the .dbf file in excel and get the attributes. I also know that I can open the shape file in QGIS and copy the data into excel, which gets me attributes and WKT geometry. Is there a simple way to convert the .shp to .csv (openable in excel) with attribute and Google Earth friendly geometry?

Thanks!

share|improve this question
    
To meet your specifications, an application or a script would have to be built. I don't think it's difficult, but I don't think it's non trivial either. If I was task to do it, I probably would estimate 1 week fulltime for such a job. –  Stephen Quan Jan 23 '12 at 21:52
2  
Thanks, this is helpful. This is kind of crazy though. If my file size weren't so big I could do it automatically through Google. How I typically do this is by importing a .shp into Google Earth and saving as a .kml. I then upload the .kml into Google Fusion Tables (FYI: Google Fusion Tables has a 100 mb upload limit) and export to .csv. I can then play around with the .csv however I want - change attributes, add new polygons that I have the coordinates for, etc., etc. - and then I reupload the newly saved .csv to Google Fusion Tables, export to .kml, and open in Google Earth. –  Rikk Jan 23 '12 at 22:53
    
So I guess the question then becomes... how do I split a .shp file into two parts? This would allow me to get the individual .kml files under the 100 mb limit and I can use the above-mentioned method to convert. –  Rikk Jan 23 '12 at 22:54
1  
I've answered the question as asked below, but just saw your comment. Why do you need csv at all? Why not do your attribute editing, adding new polygons, etc. in the GIS (you said you had QGIS) and then export that to your final KML? –  Luke Jan 24 '12 at 1:16
    
@Rikk - in response to the question in your comment: A simple way to split a shapefile would be to do a spatial selection on part of your shapefile and then right-clicking your layer and selecting "Export". You could then simply export the selected features to a new, smaller shapefile. This is by no means scientific, but it's simple. Alternatively you could select by some attributes if you wanted a more organized approach. –  Radar Jan 24 '12 at 17:11
add comment

3 Answers

Here's a simple script that uses the OGR python bindings:

import ogr,csv,sys

shpfile=r'C:\Temp\test.shp' #sys.argv[1]
csvfile=r'C:\Temp\test.csv' #sys.argv[2]

#Open files
csvfile=open(csvfile,'wb')
ds=ogr.Open(shpfile)
lyr=ds.GetLayer()

#Get field names
dfn=lyr.GetLayerDefn()
nfields=dfn.GetFieldCount()
fields=[]
for i in range(nfields):
    fields.append(dfn.GetFieldDefn(i).GetName())
fields.append('kmlgeometry')
csvwriter = csv.DictWriter(csvfile, fields)
try:csvwriter.writeheader() #python 2.7+
except:csvfile.write(','.join(fields)+'\n')

# Write attributes and kml out to csv
for feat in lyr:
    attributes=feat.items()
    geom=feat.GetGeometryRef()
    attributes['kmlgeometry']=geom.ExportToKML()
    csvwriter.writerow(attributes)

#clean up
del csvwriter,lyr,ds
csvfile.close()

EDIT: and another script to convert from your CSV to KML

import ogr,csv,sys,os
ogr.UseExceptions()

csvfile=r'C:\temp\test.csv' #sys.argv[1]
kmlfile=r'C:\temp\test.kml' #sys.argv[2]

csvreader=csv.reader(open(csvfile,'rb'))
headers=csvreader.next()

ds = ogr.GetDriverByName('KML').CreateDataSource(kmlfile)
lyr = ds.CreateLayer(os.path.splitext(os.path.basename(kmlfile))[0])

for field in headers[:-1]: #skip kmlgeometry
    field_def = ogr.FieldDefn(field)
    print lyr.CreateField(field_def)

for rec in csvreader:
    feat = ogr.Feature(lyr.GetLayerDefn())
    for i,field in enumerate(headers[:-1]): #skip kmlgeometry
        feat.SetField(field, rec[i])
    feat.SetGeometry(ogr.CreateGeometryFromGML(rec[-1]))
    lyr.CreateFeature(feat)

del lyr,ds
share|improve this answer
add comment

If you convert your shapefile to spatialite, you should be able to do the following:

1) Experiment with SQL to test the output:

ex.

SELECT col1, col2, col3, AsKml(geometry_column) FROM tab

2) Once you are satisfied with the result, you can export it to CSV format:

http://stackoverflow.com/questions/5776660/export-from-sqlite-to-csv-using-shell-script

For more information on Spatialite:

https://www.gaia-gis.it/fossil/libspatialite/index

Spatialite SQL functions:

http://www.gaia-gis.it/gaia-sins/spatialite-sql-3.0.0.html

share|improve this answer
add comment

there is a tool on the 'Geoprocessing Model and Script Tool Gallery' that does what you're looking for. it can export to excel or csv.

http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=95009B25-1422-2418-7FB5-B8638ECB2FA9

share|improve this answer
    
I had a look at that, it doesn't export the geometry in any format, let alone KML. –  Luke Jan 24 '12 at 19:01
add comment

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.