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

About this user

Jon http://www.duke.edu/~jlg34

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Shapefile2KML

   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  

KML Utility Class

   1  
   2  __author__ = "Jon Goodall <jon.goodall@gmail.com> - http://www.duke.edu/~jgl34"
   3  __version__ = "0.0.1"
   4  __license__ = ""
   5  __copyright__ =""
   6  
   7  class KML_File:
   8      "For creating KML files used for Google Earth"
   9      def __init__(self, filepath):
  10          self.filepath = filepath
  11          "adds the kml header to a file (includes a default style)"
  12          file = open(filepath,"w")
  13          file.write(
  14          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"\
  15          "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"\
  16          "<Document>\n"\
  17          "<Style id='normalPlaceMarker'>\n"\
  18          "  <IconStyle>\n"\
  19          "    <Icon>\n"\
  20          "      <href>root://icons/palette-3.png</href>\n"\
  21          "      <x>96</x>\n"\
  22          "      <y>160</y>\n"\
  23          "      <w>32</w>\n"\
  24          "      <h>32</h>\n"\
  25          "    </Icon>\n"\
  26          "  </IconStyle>\n"\
  27          "</Style>\n")
  28          file.close()
  29  
  30      def close(self):
  31          file = open(self.filepath,"a")
  32          file.write(
  33          "</Document>\n"\
  34          "</kml>")
  35          file.close()
  36          
  37      def open_folder(self, name):
  38          file = open(self.filepath,"a")
  39          file.write(
  40          "<Folder>\n"\
  41          "  <name>" + name + "</name>\n")
  42          file.close()
  43  
  44      def close_folder(self):
  45          file = open(self.filepath,"a")
  46          file.write(
  47          "</Folder>\n")
  48          file.close()
  49          
  50      def add_placemarker(self, latitude, longitude, altitude = 0.0, description = " ", name = " ", range = 6000, tilt = 45, heading = 0):
  51          "adds the point to a kml file"
  52          file = open(self.filepath,"a")
  53          file.write(
  54          "<Placemark>\n"\
  55          "  <description>" + description + "</description>\n"\
  56          "  <name>" + name + "</name>\n"\
  57          "  <styleUrl>#normalPlaceMarker</styleUrl>" + 
  58          "  <LookAt>\n"\
  59          "    <longitude>" + str(longitude) + "</longitude>\n"\
  60          "    <latitude>" + str(latitude) + "</latitude>\n"\
  61          "    <range>" + str(range) + "</range>\n"\
  62          "    <tilt>" + str(tilt) + "</tilt>\n"\
  63          "    <heading>" + str(heading) + "</heading>\n"\
  64          "  </LookAt>\n"\
  65          "  <visibility>0</visibility>\n"\
  66          "   <Point>\n"\
  67          "    <extrude>1</extrude>\n"\
  68          "    <altitudeMode>relativeToGround</altitudeMode>\n"\
  69          "    <coordinates>" + str(longitude) + "," + str(latitude) +", " +  str(altitude) + "</coordinates>\n"\
  70          "   </Point>\n"\
  71          " </Placemark>\n")
  72          file.close()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS