Tải bản đầy đủ - 0 (trang)
Hack 121. Create Google Maps Overlays On the Fly

Hack 121. Create Google Maps Overlays On the Fly

Tải bản đầy đủ - 0trang

Andyoucanfillitwithdatausingthiscommand:



INSERTINTOplacemarksVALUES

(NULL,"RestaurantTaoTao","GoodChinesefood",48.775855,

(NULL,"RestaurantSushi&Wok","GoodThaifood",48.776039,



IfyouthenissuethecommandSELECT*FROMplacemarks;

youshouldseethefollowing:



+----+------------------------+-------------------+-----------+

|id|name|description|latitude|

+----+------------------------+-------------------+-----------+

|1|RestaurantTaoTao|GoodChinesefood|48.775855|

|2|RestaurantSushi&Wok|GoodThaifood|48.776039|

+----+------------------------+-------------------+-----------+



Now,thetrickistogetthatdatabaseintoKML.ThetargetKML

thatyouneedyourPythoncodetogenerateisasfollows:









RestaurantTaoTao

GoodChinesefood



9.176846,48.775855,0







RestaurantSushi&Wok

GoodThaifood



9.176491,48.776039,0













ALLABOUTPYTHONANDKML

YoucanlearnmoreaboutPythonat

http://docs.python.org/tut/,andfindoutmoreabouttheKML

formatat

http://code.google.com/apis/kml/documentation/kml_tut.html.



ThePythonscript

StartoffthePythonscriptbyimportingthelibrariesforXMLand

MySQLhandling:

#!/usr/bin/python

fromxml.dom.minidomimportDocument

importMySQLdb



Nowcomesthemainfunction:getKml,whichaccessesthe

databasetoturnitintoXML.ReplacethevaluesYOUR_HOST,

YOUR_USER_NAME,YOUR_PASSWORD,andYOUR_DATABASEwiththe

correctvaluesforyourdatabase:

CodeView:



defgetKml():

#First,theXMLdocumentiscreated,andaKMLrootelemen

doc=Document()

kml=doc.createElement('kml')

doc.appendChild(kml)









CodeView:



















kml.setAttribute('xmlns','http://earth.google.com/kml/2.2')

document=doc.createElement('Document')

kml.appendChild(document)



#Nowyoucanopenthedatabaseconnectionwithyourvalues

conn=MySQLdb.connect(host='YOUR_HOST',

user='YOUR_USER_NAME',

passwd='YOUR_PASSWORD',

db='YOUR_DATABASE')

cursor=conn.cursor(MySQLdb.cursors.DictCursor)



#Youarenowselectingtheappropriatevalueslikethepla

anddescription,

#topassthemtotheaddPlacemarkfunction

cursor.execute('SELECTname,description,latitude,longitu

'FROMplacemarks')

rows=cursor.fetchall()

forrowinrows:

addPlacemark(doc,

document,

row['name'],

row['description'],

row['latitude'],

row['longitude'])

cursor.close()

conn.close()

returndoc.toprettyxml(indent='')

























TheprecedinglistingcalledanaddPlacemarkfunction,which

handlesgeneratingthenecessaryXMLtoshow.Thisisthecode

forit:



CodeView:



defaddPlacemark(doc,document,name,description,latitude,lo

placemark=doc.createElement('Placemark')

document.appendChild(placemark)

nameElement=doc.createElement('name')

placemark.appendChild(nameElement)

nameElement.appendChild(doc.createTextNode(name))



descriptionElement=doc.createElement('description')

placemark.appendChild(descriptionElement)

descriptionElement.appendChild(doc.createTextNode(descript

point=doc.createElement('Point')

placemark.appendChild(point)



#Addtheelementtotheelement;coo

valuesarelongitude,

#latitudeandaltitudeinthisorder(thoughyoucanleave

coordinates=doc.createElement('coordinates')

point.appendChild(coordinates)

coordinatesText=str(longitude)+','+str(latitude)+",

coordinates.appendChild(doc.createTextNode(coordinatesText

)

























Finally,youjustneedtooutputtheKMLtothebrowser:

print'Content-type:text/xml\n'

printgetKml()



IftheURLofyourscriptis(forexample)

http://example.com/cgi-bin/map.py,thenyoucannowcopy

andpastethatURLintoGoogleMaps.Theresultwillbesimilar



totheonepicturedinFigure11-24.

ThedynamicKMLfiledisplayedonGoogleMaps



Hack122.AddaSkytoGoogleEarth

Weknowtheskyisblue,butit'snotalwayssupposedtobethat

blue.

IfyoucreatescreenshotsbasedonGoogleEarthviews,likethe

oneshowingNewYorkinFigure11-25,youwillnotice

somethingisawkwardlyabsent:clouds.Granted,mostofus

preferaclearskytoacloudyone,butacoupleofcloudsmight

givetheresultingviewamorerealistictouch.

Here'showtoaddaskytoGoogleEarth.ProvidedthatGoogle

Earthisalreadyinstalledonyoursystem,startoutbyentering

thefollowinginyourbrowser'saddressbar:

http://barnabu.co.uk/files/skydome/skydome-nl.kml

WhenyouareaskedwhethertoopenthisinGoogleEarthor

downloadit,choosetoopeninitGoogleEarth(or,depending

onyourbrowser,youmayalsofirstneedtodownloaditand

thenopenthedownload).

OnceGoogleEarthislaunchedandtheKML(KeyholeMarkup

Language)filehasfinishedloading,youwillnoticeanewlist

entryreading"Skydome"inthePlacespanetotheleft.Check

theentryandallofitslistchildren.Thiswilltriggeranother

download,whichmighttakeafewseconds.

WhatisGoogleEarthloadinghere?Toexplain,theskydomeis

actuallyatextured3DmodelcreatedinGoogleSketchUp,as

showninFigure11-26.It'sdesignedbyJamesStafford,who

publishesmanymoreinterestingGoogleEarthmodelsand

hacksathisblog,http://barnabu.co.uk.(Thecloudtexture

originatedwithPhilippeHurbainfromFrance,whohasan

interestinpanoramicphotography,showcasedat

http://philohome.com/panorama.htm.)Becauseofthismodel's

largedomeshape,whichcompletely"wraps"theoutlook

towardsthehorizon,itcanserveasabackgroundimagein

GoogleEarth.



Theskyisblue



Now,zoomdownontothelocationyouwanttotarget.Note

thattheskydomemaynotbeimmediatelyvisible.Tomakeit

appear,trytotiltyourcurrentGoogleEarthviewtotheside,or

upwardsanddownwards.Oncethedometextureiscorrectly

aligned,theskywillbeshownbehindotherbuildingmodels,as

picturedinFigure11-27.Or,morepoeticallyexpressedinthe

wordsofJohnMilton,"Nowglow'dthefirmamentwithliving

sapphires."Andifyouwanttoreplacetheskytexturewith

anotherone,youcanfollowthestepsoutlinedbyJamesat

http://barnabu.co.uk/sketchup-skydome/.

ThedomemodelinGoogle'sSketchUpprogram



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 121. Create Google Maps Overlays On the Fly

Tải bản đầy đủ ngay(0 tr)

×