I have a shapefile containing hundreds of polygons. I need to create a kml of each of the polygon. Is there any way that I may not have to deal each polygon individually and my polygon file may give me individual kml of each polygon.

I'm using ArcGIS 9.3 or QGIS.

  • You will probably have to write a Python script. Why do you want an individual KML file per feature? Jan 24, 2013 at 13:52
  • Do you want a vector or raster kml?
    – Mapperz
    Jan 24, 2013 at 14:34
  • actually I have mapped features individually and formed one file for all of them... I have all of the features placed individually but creating their kml in a single go will save my hours... :) I am not familier with python or any other programming language at all... thx a lot for ur help... :)
    – Ade'l
    Jan 26, 2013 at 10:19
  • I have vector data... :)
    – Ade'l
    Jan 26, 2013 at 10:19

3 Answers 3


With an arcpy.SearchCursor go through your shape file and for each row, append the let's say OBJECTID to a python list. Now loop through that list and for every entry, create a table view on your shape file with the list entry as query condition. Now I am not sure but I think you need the arcpy.LayerToKML_conversion function. Hopefully you can use the table view as an input for this function, otherwise I think you will need to create temporary shapefiles for each view and use them as input for arcpy.LayerToKML_conversion.

LayerToKML_conversion : http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00120000004n000000

I hope these functions are available in 9.3!


If you want full control, here's a script to convert shapefile polygons to kml, I've just been piping the output of the script to a file, windows: c:\script.py > c:\output.kml. The script also creates a popup and link to a url. If you want separate files, you'll need to open, write to, and close a file on each iteration. Something to start with...Have fun!

import arcpy
import sys

class CursorManager:
    def __init__(self, *cursors):
        self.cursors = cursors

    def __enter__(self):

    def __exit__(self, exc_type, exc_value, traceback):
        for cur in self.cursors:
            del cur

class CentroidLabel:
    def __init__(self, label, pos, large, extent):
        self.label  = label
        self.pos    = pos
        self.large  = large
        self.extent = extent

#change these paths and file names to appropriate feature classes
fc_arterial = sys.path[0] + r"\arterials_nad83.shp"

#change string substitution into this pdf_link_template to appropriate string
pdf_link_template = """http://YOURSERVER/maplinks/%s/"""

info_popup_template = """<![CDATA[<div style="margin:10px 10px 10px 10px;">%s</div>]]>"""

grid_poly_template = """    <Placemark>
      <name><![CDATA[<span style="font-size:24px">%s</span>]]></name>

label_point_template = """    <Placemark>
        <coordinates> %f,%f </coordinates>

#start creation of kml
print """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="crsmaps">
  <name>CRS Maps</name>

print """  <Folder id="arterial">
print """  <Folder id="arterial_grid">

#process arterial grid feature class
centroids = []
inCur = arcpy.SearchCursor(fc_arterial)
with CursorManager(inCur):
    for row in inCur:
        poly = row.Shape        
        path = poly.getPart(0)
        outer_cell = row.ARTERIAL
        file_dir  = pdf_link_template % outer_cell.lower()

        if (outer_cell != None and outer_cell.strip() != ""):
            outer_link = file_dir + "%s.pdf" % outer_cell.lower()
            centroid = CentroidLabel(outer_cell, poly.trueCentroid, False, poly.extent)

            link_text = 'Get Arterial Map <a href="%s">%s</a><br/>' % (outer_link, outer_cell)

        allPoints = []
        for point in path:
            if point != None:

        if len(allPoints) > 0 and allPoints[0].X != allPoints[-1].X or allPoints[0].Y != allPoints[-1].Y:

        if len(allPoints) >= 4:
            coord_str = ""
            firstPt = allPoints[0]
            for i in range(len(allPoints)):
                point = allPoints[i]
                coord_str = coord_str + " %f,%f" % (point.X, point.Y)
                if i > 0 and firstPt.X == point.X and firstPt.Y == point.Y:

            name = outer_cell
            desc = info_popup_template % link_text
            style = "ag"
            place_str = grid_poly_template % (name, style, desc, coord_str)
            print place_str

print """  </Folder>
  <Folder id="arterial_labels">

#Create labels for arterial grid
pixels = 50
style  = "a"
for p in centroids:
    point_str = label_point_template % (p.label, style, p.pos.X, p.pos.Y, p.extent.YMax, p.extent.YMin, p.extent.XMax, p.extent.XMin, pixels)
    print point_str

print """  </Folder>

#create styles - short style names reduce kml size
#a = arterial label style
#ag = arterial polygon grid style
print """  <Style id="a">
  <Style id="ag">
  • thx a lot for the help... since i have least background in programming i am unable to make it up... but keeping in view the lengthy code u have written for me ill definately try this out... :) I suppose it wodny effect the precisions of the lines... :)
    – Ade'l
    Jan 26, 2013 at 10:59

You could also write a simple python script for OSGeo4W, loop through your features and create a kml for each. For a model to start from, see page 3 of this pdf: http://www.pygis.de/media/GDAL%20-%20Python%20GIS.pdf

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.