Bryan Swan
4-14-08, 11:53 PM
Anyone here have experience with dynamic generation of a KML or KMZ file? I've got a functioning script running just fine on a when-called basis that generates a file with 10 placemarks as needed. I can easily expand it to the capacity I need, but I'm concerned it may generate too much of a strain on the server and I'm wondering if anyone can give me some feedback as to whether this will work or not on the fly as a user initiated process, or whether I should run monthly updates via a cron job or something to that nature.
Here's my waypoint generation file:
<?php
$getId = $_REQUEST['num'];
mysql_select_db($database_nwsConn1, $nwsConn1);
$query_kmlRecord = "SELECT * FROM nws_Falls WHERE id = '$getId'";
$kmlRecord = mysql_query($query_kmlRecord, $nwsConn1) or die(mysql_error());
$row_kmlRecord = mysql_fetch_assoc($kmlRecord);
$totalRows_kmlRecord = mysql_num_rows($kmlRecord);
$varLatitude = $row_kmlRecord['latitude'];
$varLongitude = $row_kmlRecord['longitude'];
mysql_select_db($database_nwsConn1, $nwsConn1);
$query_nearbyFalls = "SELECT *, SQRT(POW((69.1*(longitude - $varLongitude)*cos($varLatitude/57.3)),2) + POW((69.1*(latitude - $varLatitude)),2)) AS distance FROM nws_Falls WHERE (SQRT(POW((69.1*(longitude - $varLongitude)*cos($varLatitude/57.3)),2) + POW((69.1*(latitude - $varLatitude)),2))) <= 5 AND (SQRT(POW((69.1*(longitude - $varLongitude)*cos($varLatitude/57.3)),2) + POW((69.1*(latitude - $varLatitude)),2))) > 0 ORDER BY distance LIMIT 10";
$nearbyFalls = mysql_query($query_nearbyFalls, $nwsConn1) or die(mysql_error());
$row_nearbyFalls = mysql_fetch_assoc($nearbyFalls);
$totalRows_nearbyFalls = mysql_num_rows($nearbyFalls);
?>
<?php
header('Content-Type: application/vnd.google-earth.kml+xml');
header('Content-Disposition: attachment; filename="nws.kml"');
echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<Style id="mainPlacemark_<?php echo $row_kmlRecord['id']; ?>">
<IconStyle>
<Icon>
<href>http://www.waterfallsnorthwest.com/nws/graphics/gmaps_icon.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="nearbyPlacemark_<?php echo $row_kmlRecord['id']; ?>">
<IconStyle>
<Icon>
<href>http://www.waterfallsnorthwest.com/nws/graphics/gmaps_icon2.png</href>
</Icon>
</IconStyle>
</Style>
<Placemark>
<name><?php echo $row_kmlRecord['name']; ?>, <? echo $row_kmlRecord['state']; ?></name>
<styleUrl>#mainPlacemark_<?php echo $row_kmlRecord['id']; ?></styleUrl>
<description>
<![CDATA[
<ul>
<li>Height - <?php echo $row_kmlRecord['height']; ?> feet</li>
<li>Watercourse - <?php echo $row_kmlRecord['stream']; ?></li>
<li>Form - <?php echo $row_kmlRecord['form']; ?></li>
<li>USGS Quad - <?php echo $row_kmlRecord['usgsQuad']; ?></li>
<li><a href="http://www.waterfallsnorthwest.com/nws/waterfall.php?num=<?php echo $row_kmlRecord['id']; ?>">More Information</a></li>
</ul>
]]>
</description>
<Point>
<coordinates>-<?php echo $row_kmlRecord['longitude']; ?>,<?php echo $row_kmlRecord['latitude']; ?></coordinates>
</Point>
</Placemark>
<?
if ($totalRows_nearbyFalls >= 1) {
do { ?>
<Placemark>
<name><?php echo $row_nearbyFalls['name']; ?>, <? echo $row_nearbyFalls['state']; ?></name>
<styleUrl>#nearbyPlacemark_<?php echo $row_kmlRecord['id']; ?></styleUrl>
<description>
<![CDATA[
<ul>
<li>Height - <?php echo $row_nearbyFalls['height']; ?> feet</li>
<li>Watercourse - <?php echo $row_nearbyFalls['stream']; ?></li>
<li>Form - <?php echo $row_nearbyFalls['form']; ?></li>
<li>USGS Quad - <?php echo $row_nearbyFalls['usgsQuad']; ?></li>
<li><a href="http://www.waterfallsnorthwest.com/nws/waterfall.php?num=<?php echo $row_nearbyFalls['id']; ?>">More Information</a></li>
</ul>
]]>
</description>
<Point>
<coordinates>-<?php echo $row_nearbyFalls['longitude']; ?>,<?php echo $row_nearbyFalls['latitude']; ?></coordinates>
</Point>
</Placemark>
<? } while ($row_nearbyFalls = mysql_fetch_assoc($nearbyFalls)); } ?>
<LookAt>
<longitude>-<?php echo $row_kmlRecord['longitude']; ?></longitude>
<latitude><?php echo $row_kmlRecord['latitude']; ?></latitude>
<altitude>0</altitude>
<range>500</range>
<tilt>35</tilt>
<heading>0</heading>
<altitudeMode>relativeToGround</altitudeMode>
</LookAt>
</Document>
</kml>
<?php
mysql_free_result($kmlRecord);
?>
Ideally, if I were to set this up to run as a user initiated system, it would generate anywhere from 10 up to as many as 2000 waypoints for a single file (everything in red would be within the repeat region). Any feedback I can get on this would be much appreciated. Thanks.
Here's my waypoint generation file:
<?php
$getId = $_REQUEST['num'];
mysql_select_db($database_nwsConn1, $nwsConn1);
$query_kmlRecord = "SELECT * FROM nws_Falls WHERE id = '$getId'";
$kmlRecord = mysql_query($query_kmlRecord, $nwsConn1) or die(mysql_error());
$row_kmlRecord = mysql_fetch_assoc($kmlRecord);
$totalRows_kmlRecord = mysql_num_rows($kmlRecord);
$varLatitude = $row_kmlRecord['latitude'];
$varLongitude = $row_kmlRecord['longitude'];
mysql_select_db($database_nwsConn1, $nwsConn1);
$query_nearbyFalls = "SELECT *, SQRT(POW((69.1*(longitude - $varLongitude)*cos($varLatitude/57.3)),2) + POW((69.1*(latitude - $varLatitude)),2)) AS distance FROM nws_Falls WHERE (SQRT(POW((69.1*(longitude - $varLongitude)*cos($varLatitude/57.3)),2) + POW((69.1*(latitude - $varLatitude)),2))) <= 5 AND (SQRT(POW((69.1*(longitude - $varLongitude)*cos($varLatitude/57.3)),2) + POW((69.1*(latitude - $varLatitude)),2))) > 0 ORDER BY distance LIMIT 10";
$nearbyFalls = mysql_query($query_nearbyFalls, $nwsConn1) or die(mysql_error());
$row_nearbyFalls = mysql_fetch_assoc($nearbyFalls);
$totalRows_nearbyFalls = mysql_num_rows($nearbyFalls);
?>
<?php
header('Content-Type: application/vnd.google-earth.kml+xml');
header('Content-Disposition: attachment; filename="nws.kml"');
echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Document>
<Style id="mainPlacemark_<?php echo $row_kmlRecord['id']; ?>">
<IconStyle>
<Icon>
<href>http://www.waterfallsnorthwest.com/nws/graphics/gmaps_icon.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="nearbyPlacemark_<?php echo $row_kmlRecord['id']; ?>">
<IconStyle>
<Icon>
<href>http://www.waterfallsnorthwest.com/nws/graphics/gmaps_icon2.png</href>
</Icon>
</IconStyle>
</Style>
<Placemark>
<name><?php echo $row_kmlRecord['name']; ?>, <? echo $row_kmlRecord['state']; ?></name>
<styleUrl>#mainPlacemark_<?php echo $row_kmlRecord['id']; ?></styleUrl>
<description>
<![CDATA[
<ul>
<li>Height - <?php echo $row_kmlRecord['height']; ?> feet</li>
<li>Watercourse - <?php echo $row_kmlRecord['stream']; ?></li>
<li>Form - <?php echo $row_kmlRecord['form']; ?></li>
<li>USGS Quad - <?php echo $row_kmlRecord['usgsQuad']; ?></li>
<li><a href="http://www.waterfallsnorthwest.com/nws/waterfall.php?num=<?php echo $row_kmlRecord['id']; ?>">More Information</a></li>
</ul>
]]>
</description>
<Point>
<coordinates>-<?php echo $row_kmlRecord['longitude']; ?>,<?php echo $row_kmlRecord['latitude']; ?></coordinates>
</Point>
</Placemark>
<?
if ($totalRows_nearbyFalls >= 1) {
do { ?>
<Placemark>
<name><?php echo $row_nearbyFalls['name']; ?>, <? echo $row_nearbyFalls['state']; ?></name>
<styleUrl>#nearbyPlacemark_<?php echo $row_kmlRecord['id']; ?></styleUrl>
<description>
<![CDATA[
<ul>
<li>Height - <?php echo $row_nearbyFalls['height']; ?> feet</li>
<li>Watercourse - <?php echo $row_nearbyFalls['stream']; ?></li>
<li>Form - <?php echo $row_nearbyFalls['form']; ?></li>
<li>USGS Quad - <?php echo $row_nearbyFalls['usgsQuad']; ?></li>
<li><a href="http://www.waterfallsnorthwest.com/nws/waterfall.php?num=<?php echo $row_nearbyFalls['id']; ?>">More Information</a></li>
</ul>
]]>
</description>
<Point>
<coordinates>-<?php echo $row_nearbyFalls['longitude']; ?>,<?php echo $row_nearbyFalls['latitude']; ?></coordinates>
</Point>
</Placemark>
<? } while ($row_nearbyFalls = mysql_fetch_assoc($nearbyFalls)); } ?>
<LookAt>
<longitude>-<?php echo $row_kmlRecord['longitude']; ?></longitude>
<latitude><?php echo $row_kmlRecord['latitude']; ?></latitude>
<altitude>0</altitude>
<range>500</range>
<tilt>35</tilt>
<heading>0</heading>
<altitudeMode>relativeToGround</altitudeMode>
</LookAt>
</Document>
</kml>
<?php
mysql_free_result($kmlRecord);
?>
Ideally, if I were to set this up to run as a user initiated system, it would generate anywhere from 10 up to as many as 2000 waypoints for a single file (everything in red would be within the repeat region). Any feedback I can get on this would be much appreciated. Thanks.