Never been to DZone Snippets before?

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

Shapefile2KML (See related posts)

   1  
   2  #requires and ArcGIS 9.x licenses
   3  #import standard libraries
   4  import sys, os, win32com.client
   5  
   6  #import the kml library
   7  import pyKML
   8  
   9  #create gp object
  10  gp = win32com.client.Dispatch("esriGeoprocessing.GPDispatch.1")
  11  
  12  #get the input layer path
  13  layer = "C:/myProjects/CUAHSI/WebServices/NCDC/asos-20051110/asos-20051110.shp" #sys.argv[1]
  14  
  15  #get the lat, lon, name, and id fields
  16  lat_field = "LATITUDE"
  17  lon_field = "LONGITUDE"
  18  elev_field = "STN_ELEV"
  19  name_field = "LOCATION"
  20  id_field = "CALLSIGN"
  21  
  22  #create a cursor on this layer 
  23  feats = gp.SearchCursor(layer)
  24  feat = feats.Next()
  25  
  26  #open a kml file to write points into
  27  kml = pyKML.KML_File("C:/myProjects/CUAHSI/WebServices/NCDC/asos-20051110/test.kml")
  28  kml.open_folder("Weather_Stations")
  29  
  30  #loop through features
  31  while feat:
  32  
  33      shape1 = feat.shape
  34      part = shape1.GetPart(0)
  35      
  36      #get he features attributes
  37      lat =  part.y
  38      lon = part.x
  39      elev = feat.GetValue(elev_field)
  40      name = feat.GetValue(name_field)
  41      id = feat.GetValue(id_field)
  42  
  43      #create a description from the name and id attributes                         
  44      description = name + "<a href='http://www.nws.noaa.gov/asos/'>http://www.nws.noaa.gov/asos/</a>"
  45  
  46      #create a kml placemaker object    
  47      kml.add_placemarker(lat,lon,0.0,description,id)
  48                             
  49      feat = feats.Next()
  50  
  51  #close the folder
  52  kml.close_folder()
  53  
  54  #close file
  55  kml.close()
  56  

You need to create an account or log in to post comments to this site.


Click here to browse all 5350 code snippets

Related Posts