Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Perl by vvvv ( 17 years ago )
## An array of 5 colors.  In KML the last 6 hex characters specify the color.  The first 2 specify the transparency level.  00 is no transparency — ff is 100% transparent.  Here I use 7f which is 50% transparent.

$colors[0] = “7f0000ff”;
$colors[1] = “7f00ff00″;
$colors[2] = “7fff0000″;
$colors[3] = “7f00ffff”;
$colors[4] = “7f008cff”;

# Get the geometry from the database.  The asKML function takes a geometry column and an integer which specifies the significant digits to return.  Keeping this to for provides enough accuracy while reducing the output file size.

$select = “select asKML(the_geom,4) as points,name from shapes.states”;
$sth = $dbh->prepare($select);
$sth->execute();

# Write kml

# Standard headers.  I called the document States

print “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n”;
print “<kml >\n”;
print “<Document><name>States</name>\n”;

# Write a style element for each of the 5 colors

for($i=0; $i<5; $i++){

print “<Style id=\”zn$i\”><LineStyle><color>FFff9933</color></LineStyle>
<PolyStyle><color>$colors[$i]</color></PolyStyle></Style>”;
}

# For each record in the shapes.states table write the kml

while (($kml,$name) = $sth->fetchrow_array()){

# get a random number to select the color

$number = int(rand(5));

# A Placemark is basically an element in the map.  Here one for each state.  I plug in the state name, the style element to use for coloring, and finally the kml for the geometry output by the database.
print “<Placemark>
<name>$name</name>\n
<styleUrl>#zn$number</styleUrl>
$kml\n
</Placemark>\n”;
}

#Finish the document
print “</Document></kml>\n”;

 

Revise this Paste

Your Name: Code Language: