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?


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
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
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. –  Nick Ochoski Jan 24 '12 at 17:11

3 Answers 3

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

#Get field names
for i in range(nfields):
csvwriter = csv.DictWriter(csvfile, fields)
try:csvwriter.writeheader() #python 2.7+

# Write attributes and kml out to csv
for feat in lyr:

#clean up
del csvwriter,lyr,ds

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

import ogr,csv,sys,os

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


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])

del lyr,ds
share|improve this answer

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

1) Experiment with SQL to test the output:


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

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


For more information on Spatialite:


Spatialite SQL functions:


share|improve this answer

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.


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

Your Answer


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.