Docstoc

KML_scripting

Document Sample
KML_scripting Powered By Docstoc
					KML Scripting

The Keyhole Markup Language (KML) was developed by Google Labs. KML is a
markup language that allows users to create interactive points, lines, and polygons within
a sphere supplied by a client application called Google Earth.

The following four examples will take you through the steps for creating a simple point,
line, and polygon. Google Earth also provides these same tools on the toolbar, depending
on the type of application you have either downloaded or installed. Google Earth
promotes various tiers for the application.

Points

Start up Google Earth and zoom into any location. You will notice on the lower right a
thumb tack which represents a placemark. You will also notice the longitude and latitude
of the coordinate of the placemark. After selecting your point, you can enter in a
description and name for the placemark.




After entering in a description, you are ready to save your placemark.
Right click on the placemark (upper left) and save it to any location. You will see the file
with the Google Earth logo.
Use note pad and open the file. You should see something like this.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Placemark>
 <name>Safeco</name>
 <description>Safco Field</description>
 <LookAt>
  <longitude>-122.3323305602353</longitude>
  <latitude>47.59132456856907</latitude>
  <range>205.4492368291692</range>
  <tilt>-7.083858945952525e-010</tilt>
  <heading>-16.45678690681833</heading>
 </LookAt>
 <styleUrl>root://styleMaps#default+nicon=0x307+hicon=0x317</styleUrl>
 <Point>
  <coordinates>-122.3327111012312,47.59122015947336,0</coordinates>
 </Point>
</Placemark>
</kml>

The only real important feature at this point is the <coordinates> tag. This shows the
GPS location in decimal degrees. By simply changing the coordinate you would have
made your first placemark at a different location.

Please visit the www.keyhole.com site to see the various tag definitions.

 <Point> Specifies the type of feature being displayed. In this case it is a point.


<coordinates>-122.3327111012312,47.59122015947336,0</coordinates>

The Longitude, Latitude, and the elevation are the shown above.
Lines

Using the same concept, simply change the words Point to LineString.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Placemark>
 <name>Safeco</name>
 <description>Safco Field</description>
 <LookAt>
  <longitude>-122.3323305602353</longitude>
  <latitude>47.59132456856907</latitude>
  <range>205.4492368291692</range>
  <tilt>-7.083858945952525e-010</tilt>
  <heading>-16.45678690681833</heading>
 </LookAt>
 <styleUrl>root://styleMaps#default+nicon=0x307+hicon=0x317</styleUrl>
 <LineString>
  <coordinates>
-122.3327111012312,47.59122015947336,0
-123.3327111012312,47.59122015947336,0
</coordinates>
</LineString>
</Placemark>
</kml>

This will draw a line (west) from our original point to an alternate location.
Polygons

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Placemark>
 <name>The Pentagon</name>
 <LookAt>
  <longitude>-77.05580139178142</longitude>
  <latitude>38.870832443487</latitude>
  <range>742.0552506670548</range>
  <tilt>48.09646074797388</tilt>
  <heading>59.88865561738225</heading>
 </LookAt>
 <Polygon>
  <extrude>1</extrude>
  <altitudeMode>relativeToGround</altitudeMode>
  <outerBoundaryIs>
   <LinearRing>
     <coordinates>
        -77.05788457660967,38.87253259892824,100
        -77.05465973756702,38.87291016281703,100
        -77.05315536854791,38.87053267794386,100
        -77.05552622493516,38.868757801256,100
        -77.05844056290393,38.86996206506943,100
        -77.05788457660967,38.87253259892824,100
     </coordinates>
   </LinearRing>
  </outerBoundaryIs>
 </Polygon>
</Placemark>
</KML>
The next step would be to script the code to parse in a number of different points and
polygons. Python and or php could be utilized in a browser for storing data within a
database. All it is a parsing and understanding the tags.

Example below

<?php
include('header.php');
# DATABASE WORK
$db=mysql_pconnect('localhost','xx','xx');
if (!$db)
{
print "ERROR CONNECTING";
exit;
}
mysql_select_db('xxx');

$query3="SELECT * from students";
$result3=mysql_query($query3);
while ($line3 = mysql_fetch_array($result3, MYSQL_ASSOC)) {
$holdcolor=$line3['color'];
$holdmess=$line3['message'];
$holdnumber=$line3['number'];
$code=$line3['cnty'];
print '<Placemark>';
print '<name>'.$holdmess.'</name>';
print '<desc>'.$holdmess.'</desc>';
print '<LookAt>';
print ' <longitude>-122.084120030116</longitude>';
print ' <latitude>48.42174011925477</latitude>';
print ' <range>4276.7870053764046</range>';
print ' <tilt>43.454348562403</tilt>';
print ' <heading>-34.82469740081282</heading>';
print ' </LookAt>';
print ' <styleUrl>root://styles#default+icon=0x307</styleUrl>';
print ' <Point>';
$query2="SELECT * from latlong where cnty = ".$code." LIMIT 1";
print $query2;
$result2=mysql_query($query2);
while ($line2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
print "<coordinates>".$line2['long'].",".$line2['lat'].", ".$holdnumber." "."</coordinates>";


}
print ' </Point>';
print "</Placemark>";
}

print "</Folder>";
Sites to visit

http://merlin.cs.wwu.edu/kml Resource


http://www.keyhole.com/kml/docs/webhelp/index.htm Tag Index

http://www.keyhole.com/kml/kml_tut.html Google Earth Tutorial
al

				
DOCUMENT INFO