parks/generate.php

Unless stated otherwise, code available for viewing through this tool is dedicated to the public domain. If you have any questions, drop me a line.

<?php

die();

$url_index = 'http://ontarioparks.com/english/markers.xml';
$url_base = 'http://www.ontarioparks.com/english/';

$kml_file = 'parks.kml';


require('../curl.php');
$curl = new CURL();
$curl->enableCache();

$text_response = strstr($curl->get($url_index), '<markers>'); // strip header
$xml = new SimpleXMLElement($text_response); 

$marker_data = array();

foreach($xml->marker as $marker) {

	// put the all-uppercase names into title case
	$name = (string)$marker['park'];
	if (strtoupper($name) == $name)
		$name = ucwords(strtolower($marker['park']));

	// fetch description, if available
	$page = (string)$marker['url'];
	if ('sorry.html' == $page) {
		continue; // bail if no description
	} else {
		$page_data = $curl->get($url_base . $page);
		$paragraph = trim(strip_tags(scrape('#<td width="56%" valign="top">.*?<p>(.*?)</p>.*?</td>#is', $page_data)));
		if ('' == $paragraph) continue; // bail if regex fails

		$facilities_text = scrape('#Activities and Facilities(.*?)<table#is', $page_data);
		preg_match_all('#<img src="(.*?\.gif)" .*?/></td>.*?<td[^>]*><a href="(.*?)" .*?>(.*?)</a></td>#is', $facilities_text, $facilities_match);
		$icons = $facilities_match[1];
		$links = $facilities_match[2];
		$titles = $facilities_match[3];
		$icon_blob = '';
		foreach($icons as $id => $icon) {
			$icon_blob .= '<a href="'.$url_base.$links[$id].'" title="'.$titles[$id].'"><img src="'.$url_base.$icon.'" alt="'.$titles[$id].'" /></a> ';
		}
	}

	$marker_data[] = array(
		'name' => $name,
		'desc' => "<p>$paragraph</p><p>$icon_blob</p><p><a href=\"$url_base$page\">$name</a> | <a href=\"http://ontarioparks.com/english/\">Ontario Parks</a></p>",
		'll' => $marker['lng'] . ',' . $marker['lat']
	);
}


function scrape($regex, $target) {
	if (preg_match($regex, $target, $match)) {
		return $match[1];
		}
	else
		return null;
}



ob_start();  // grab output to write file


echo '<?xml version="1.0" encoding="UTF-8"?>';

?>
<kml xmlns="http://www.google.com/earth/kml/2">
<Document>
	<name>Ontario Parks</name>
<? 	foreach($marker_data as $this_point): extract($this_point); ?>
	<Placemark>
		<name><?= $name ?></name>
		<description><![CDATA[<?= $desc ?>]]></description>
		<Point>
			<coordinates><?= $ll ?></coordinates>
		</Point>
	</Placemark>
<?	endforeach; ?>
</Document>
</kml>

<?php
  $fp = @fopen($kml_file, 'w');

  // save contents of output buffer
  @fwrite($fp, ob_get_contents());
  @fclose($fp); 

?>