DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Shapefile2KML

11.16.2005
| 4095 views |
  • submit to reddit
        
#requires and ArcGIS 9.x licenses
#import standard libraries
import sys, os, win32com.client

#import the kml library
import pyKML

#create gp object
gp = win32com.client.Dispatch("esriGeoprocessing.GPDispatch.1")

#get the input layer path
layer = "C:/myProjects/CUAHSI/WebServices/NCDC/asos-20051110/asos-20051110.shp" #sys.argv[1]

#get the lat, lon, name, and id fields
lat_field = "LATITUDE"
lon_field = "LONGITUDE"
elev_field = "STN_ELEV"
name_field = "LOCATION"
id_field = "CALLSIGN"

#create a cursor on this layer 
feats = gp.SearchCursor(layer)
feat = feats.Next()

#open a kml file to write points into
kml = pyKML.KML_File("C:/myProjects/CUAHSI/WebServices/NCDC/asos-20051110/test.kml")
kml.open_folder("Weather_Stations")

#loop through features
while feat:

    shape1 = feat.shape
    part = shape1.GetPart(0)
    
    #get he features attributes
    lat =  part.y
    lon = part.x
    elev = feat.GetValue(elev_field)
    name = feat.GetValue(name_field)
    id = feat.GetValue(id_field)

    #create a description from the name and id attributes                         
    description = name + "<a href='http://www.nws.noaa.gov/asos/'>http://www.nws.noaa.gov/asos/</a>"

    #create a kml placemaker object    
    kml.add_placemarker(lat,lon,0.0,description,id)
                           
    feat = feats.Next()

#close the folder
kml.close_folder()

#close file
kml.close()