I have a shapefile 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 shapefile in QGIS and copy the data into Excel, which gets me attributes and WKT geometry.

Is there a simple way to convert the shapefile to CSV (openable in Excel) with attribute and Google Earth friendly geometry?

  • 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. Jan 23, 2012 at 21:52
  • 3
    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, 2012 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, 2012 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?
    – user2856
    Jan 24, 2012 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, 2012 at 17:11

6 Answers 6


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
  • Can you describe more about how to use those bindings as that glosses over important information your readers would need to know.
    – Andrew S
    Aug 15, 2018 at 19:35

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:



If you are working on QGIS, you can instantly generate a CSV document by right clicking on the layer -> Save as -> CSV.

If you are working with ArcMap, then you can export the KML by using the tool Layer to KML (Go to Search option in the program). For some reasons it generates a KMZ instead of an KML (atleast that's what happened on my case).

In order to convert a KMZ to KML:

  • Import your KMZ file to Google Earth and right click on your layer and save it as KML
  • Open QGIS and drag and drop the KML file - it will automatically load the layer (>QGIS 2.10 PISA)
  • Right click on the file and save it as CSV

This procedure is longer if you are working with ArcGIS but in QGIS it can be done in no time. You will have to install QGIS in any case.

  • Tested your solution for QGIS and it does not work. Save as csv does not preserve spatial component. Nov 8, 2016 at 20:18
  • QGIS gives you an option to select WKT during the export - this export the geometry (spatial component) in WKT format along with the rest of the features in the shape file.
    – kozyr
    Aug 22, 2017 at 21:30
  • From QGIS 3.0 you will look for the ”geometry” dropdown list under ”Layer Options” in the export dialogue, and select AS_XY, AS_XYZ or AS_WKT
    – leo
    Jul 1, 2018 at 9:11

At the command line you can use ogr2ogr, like this:

ogr2ogr -f CSV output.csv input.shp -lco GEOMETRY=AS_XYZ

lco is for “layer creation options”, i think. Other available options are documented here: http://www.gdal.org/drv_csv.html

  • Leo, I get "Error 1 could'nt fetch requested layer 'GEOMETRY=AS_XYZ'. I can bring the file up in QGIS and clearly see a vector containing country outlines.
    – Andrew S
    Aug 15, 2018 at 23:00

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.


  • 1
    I had a look at that, it doesn't export the geometry in any format, let alone KML.
    – user2856
    Jan 24, 2012 at 19:01

simply go to conversion tool in arc map. then go to table to excel. an excel file will made. convert that excel doc to .csv file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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