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

#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()

KML Utility Class

__author__ = "Jon Goodall <jon.goodall@gmail.com> - http://www.duke.edu/~jgl34"
__version__ = "0.0.1"
__license__ = ""
__copyright__ =""

class KML_File:
    "For creating KML files used for Google Earth"
    def __init__(self, filepath):
        self.filepath = filepath
        "adds the kml header to a file (includes a default style)"
        file = open(filepath,"w")
        file.write(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"\
        "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"\
        "<Document>\n"\
        "<Style id='normalPlaceMarker'>\n"\
        "  <IconStyle>\n"\
        "    <Icon>\n"\
        "      <href>root://icons/palette-3.png</href>\n"\
        "      <x>96</x>\n"\
        "      <y>160</y>\n"\
        "      <w>32</w>\n"\
        "      <h>32</h>\n"\
        "    </Icon>\n"\
        "  </IconStyle>\n"\
        "</Style>\n")
        file.close()

    def close(self):
        file = open(self.filepath,"a")
        file.write(
        "</Document>\n"\
        "</kml>")
        file.close()
        
    def open_folder(self, name):
        file = open(self.filepath,"a")
        file.write(
        "<Folder>\n"\
        "  <name>" + name + "</name>\n")
        file.close()

    def close_folder(self):
        file = open(self.filepath,"a")
        file.write(
        "</Folder>\n")
        file.close()
        
    def add_placemarker(self, latitude, longitude, altitude = 0.0, description = " ", name = " ", range = 6000, tilt = 45, heading = 0):
        "adds the point to a kml file"
        file = open(self.filepath,"a")
        file.write(
        "<Placemark>\n"\
        "  <description>" + description + "</description>\n"\
        "  <name>" + name + "</name>\n"\
        "  <styleUrl>#normalPlaceMarker</styleUrl>" + 
        "  <LookAt>\n"\
        "    <longitude>" + str(longitude) + "</longitude>\n"\
        "    <latitude>" + str(latitude) + "</latitude>\n"\
        "    <range>" + str(range) + "</range>\n"\
        "    <tilt>" + str(tilt) + "</tilt>\n"\
        "    <heading>" + str(heading) + "</heading>\n"\
        "  </LookAt>\n"\
        "  <visibility>0</visibility>\n"\
        "   <Point>\n"\
        "    <extrude>1</extrude>\n"\
        "    <altitudeMode>relativeToGround</altitudeMode>\n"\
        "    <coordinates>" + str(longitude) + "," + str(latitude) +", " +  str(altitude) + "</coordinates>\n"\
        "   </Point>\n"\
        " </Placemark>\n")
        file.close()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS