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
Eliminating sites from searches
-site:experts-exchange.com "convert cstring to int "
Or you can only seach a specified site by removing the dash
site:planetargon.com "rails hosting"
Tip by xinu
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()
wsdl the google API (search google with ruby)
require 'soap/wsdlDriver' $KCODE = "UTF8" key = 'LVJnAm5QFHblahblahblah your key here' #create driver wsdl = "http://api.google.com/GoogleSearch.wsdl" driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver query = "your query string here" start = 0 max = 10 # see http://dev.ctor.org/soap4r/browser/trunk/sample/wsdl/googleSearch/wsdlDriver.rb @results = driver.doGoogleSearch( key, query, start, max, true, "", true, 'lang_en', '','') snippets = @results.resultElements.collect { |r| r.snippet } # you can get all kinds'a' info here self.update_attribute(:html, snippets.join("\n")) # or whatever
Google map pin
def pin(x,y,char=None): # top-left is x-5, y-17 points = [ (0,0), (-2,-6), (-5,-10), (-5,-14), (-2,-17), \ (2,-17), (5,-14), (5,-10), (2,-6)] c.polygon([(x+dx,y+dy) for dx,dy in points], 0, (255,119,107), 1) if char: c.text((x-2, y-7), unicode(char)) else: c.point((x,y-12), 0, width=5)
Instead of adding a pin image to the program, this function
will draw a google map pin at point (x,y) instead.
If you give it a char, it will put that char in the middle of
the pin as well
pin(20,20,'A') # A in the pin pin(40,40) # just bull-eye
Send message with Google Talk
http://www.larsen-b.com/Article/214.html
import xmpp login = 'Your.Login' # @gmail.com pwd = 'YourPassword' cnx = xmpp.Client('gmail.com') cnx.connect( server=('talk.google.com',5223) ) cnx.auth(login,pwd, 'botty') cnx.send( xmpp.Message( "YourFriend@gmail.com" ,"Hello World form Python" ) )
Using Google Maps EZ
Here's the simplest example
<html> <head> <script src="http://maps.google.com/maps?file=api&v=1&key=yourgmapskey"></script> <script src="http://bluweb.com/chouser/gmapez/gmapez.js"></script> </head> <body> <div class="GMapEZ" style="width: 300px; height: 300px;"></div> </body> </html>
See more examples here
http://bluweb.com/us/chouser/gmapez/docs.html
Converting between 2 google map tile types
I try to program a prototype of it on pys60
http://bigbold.com/snippets/posts/show/458
There I only show the default map, not the satellite images.
Retrieving a different mode isn't that difficult.
I read the info from here
http://intepid.com/2005-07-17/21.50/
Then I begin comparing the 2 tile types of the same area
(around California)
http://mt.google.com/mt?v=w2.5&x=20&y=49&zoom=10 (map)
http://kh.google.com/kh?v=3&t=tqtsqrqt (satellite)
Here's the conversion routine between x,y,zoom and quadtree
def quadtree(x,y, zoom): out = [] m = {(0,0):'q', (0,1):'t', (1,0):'r', (1,1):'s'} for i in range(17-zoom): x, rx = divmod(x, 2) y, ry = divmod(y, 2) out.insert(0, m[(rx,ry)]) return 't' + ''.join(out)
Then to convert back
def xyzoom(quad): x, y, z = 0, 0, 17 m = {'q':(0,0), 't':(0,1), 'r':(1,0), 's':(1,1)} for c in quad[1:]: x = x*2 + m[c][0] y = y*2 + m[c][1] z -= 1 return x, y, z
Using them is simple
>>> quadtree(20,49,10) 'tqtsqrqt' >>> xyzoom('tqtsqrqt') (20, 49, 10) >>> sat_url = 'http://kh.google.com/kh?v=3&t=' + quadtree(20,49,10)