1

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? – MappaGnosis Jan 24 '13 at 13:52
  • Do you want a vector or raster kml? – Mapperz Jan 24 '13 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 '13 at 10:19
  • I have vector data... :) – Ade'l Jan 26 '13 at 10:19
1

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!

1

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):
        pass

    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>
      <styleUrl>#%s</styleUrl>
      <description>%s</description>
      <MultiGeometry>
        <Polygon>
        <tessellate>1</tessellate>
        <extrude>0</extrude>
        <altitudeMode>clampToGround</altitudeMode>
        <outerBoundaryIs>
        <LinearRing>
           <coordinates>%s</coordinates>
        </LinearRing>
        </outerBoundaryIs>
        </Polygon>
      </MultiGeometry>
    </Placemark>"""

label_point_template = """    <Placemark>
      <name>%s</name>
      <styleUrl>#%s</styleUrl>
      <Point>
        <coordinates> %f,%f </coordinates>
      </Point>
      <Region> 
        <LatLonAltBox> 
          <north>%f</north>
          <south>%f</south>
          <east>%f</east>
          <west>%f</west>
        </LatLonAltBox> 
        <Lod>
          <minLodPixels>%d</minLodPixels>
        </Lod>
      </Region>
    </Placemark>"""

#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>
  <Snippet></Snippet>"""

print """  <Folder id="arterial">
    <name>Arterial</name>
    <Snippet></Snippet>"""
print """  <Folder id="arterial_grid">
    <name>Grid</name>
    <Snippet></Snippet>"""

#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:
                allPoints.append(point)

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

        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:
                    break

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

print """  </Folder>
  <Folder id="arterial_labels">
    <name>Labels</name>
    <Snippet></Snippet>"""

#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>
  </Folder>"""


#create styles - short style names reduce kml size
#a = arterial label style
#ag = arterial polygon grid style
print """  <Style id="a">
    <LabelStyle>
      <color>ff0066ff</color>
      <scale>1.5</scale>
    </LabelStyle>
    <IconStyle><Icon/></IconStyle>
  </Style>
  <Style id="ag">
    <LineStyle>
      <color>ff0066ff</color>
      <width>2</width>
    </LineStyle>
    <PolyStyle>
      <fill>0</fill>
      <outline>1</outline>
    </PolyStyle>
  </Style>
</Document>
</kml>
"""
  • 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 '13 at 10:59
0

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, privacy policy and cookie policy

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