<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mark de Blois Blog &#187; PostGIS</title>
	<atom:link href="http://mark.deblois.eu/category/postgis/feed/" rel="self" type="application/rss+xml" />
	<link>http://mark.deblois.eu</link>
	<description>Geographical Information Systems, the Geoweb and cloud GIS</description>
	<lastBuildDate>Thu, 14 Jan 2010 12:16:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Google Map based on PostGIS data</title>
		<link>http://mark.deblois.eu/2007/10/17/googlemap-based-on-postgis-data/</link>
		<comments>http://mark.deblois.eu/2007/10/17/googlemap-based-on-postgis-data/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 22:09:54 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[FOSS GIS]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[PostGIS]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://mark.deblois.eu/2007/10/17/googlemap-based-on-postgis-data/</guid>
		<description><![CDATA[A spatial database does not store just plain records but is optimized to store and query data related to objects in space, including points, lines and polygons. Spatial databases have been around for a while now but are currently gaining popularity rapidly. Where the market was dominated by Oracle Spatial for a large number of [...]]]></description>
			<content:encoded><![CDATA[<p>A spatial database does not store just plain records but is optimized to store and query data related to objects in space, including points, lines and polygons. <a href="http://en.wikipedia.org/wiki/Spatial_database">Spatial databases</a> have been around for a while now but are currently gaining popularity rapidly. Where the market was dominated by Oracle Spatial for a large number of years, there are promising alternatives these days including <a href="http://www.postgresql.org/">PostgreSQL</a> in combination with <a href="http://postgis.refractions.net/">PostGIS </a>spatial extension, as well as <a href="http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html">MySQL with spatial extensions</a>. Microsoft&#8217;s SQL Server can also store spatial information.</p>
<p>So what is the advantage of having a spatial database? For one, having all your data in a central database including well defined privileges, is a great thing to have. Besides this it is possible to extend the more standard SQL queries with spatial queries. E.g. which towns lie within a zone of 100km around the epicenter of the earthquake location. But a spatial database can also be used to populate a Google Map (or other online mapping applications) as I have shown in the image here:<br />
<img src="http://mark.deblois.eu/wp-content/images/gmap.png" alt="Google Map based on PostGIS" /><br />
In the example below I connect to my PostgreSQL database by means of PHP.</p>
<p class="code"> $db_handle = pg_connect(&#8220;host=localhost port=5432 dbname=outbreak_locations user=mark password=whatever&#8221;);<br />
$query = &#8220;SELECT * FROM locations&#8221;;<br />
$result = pg_exec($db_handle, $query);</p>
<p>Once we have our connection it is time to generate the XML format which will be used to populate our Google Map.</p>
<p class="code"> header(&#8220;Content-type: text/xml&#8221;);<br />
// Start XML file, echo parent node<br />
echo &#8216;<markers>&#8216;;</markers><br />
for ($row = 0; $row &lt; pg_numrows($result); $row++) {<br />
// ADD TO XML DOCUMENT NODE<br />
echo &#8216;<marker><br />
echo &#8216;town=&#8221;&#8216; . parseToXML(pg_result($result, $row, &#8216;town&#8217;)) . &#8216;&#8221; &#8216;;<br />
echo &#8216;country=&#8221;&#8216; . parseToXML(pg_result($result, $row, &#8216;country&#8217;)) . &#8216;&#8221; &#8216;;<br />
echo &#8216;lat=&#8221;&#8216; . parseToXML(pg_result($result, $row, &#8216;latitude&#8217;)) . &#8216;&#8221; &#8216;;<br />
echo &#8216;lon=&#8221;&#8216; . parseToXML(pg_result($result, $row, &#8216;longitude&#8217;)) . &#8216;&#8221; &#8216;;<br />
echo &#8216;/&gt;&#8217;;<br />
}</marker><br />
// End XML file<br />
echo &#8221;;<br />
?&gt;</p>
<p>Make sure you include the parseToXML function included at the top of your php file:</p>
<p class="code"> function parseToXML($htmlStr)<br />
{<br />
$xmlStr=str_replace(&#8216;&lt;&#8217;,'&lt;&#8217;,$htmlStr);<br />
$xmlStr=str_replace(&#8216;&gt;&#8217;,'&gt;&#8217;,$xmlStr);<br />
$xmlStr=str_replace(&#8216;&#8221;&#8216;,&#8217;&#8221;&#8216;,$xmlStr);<br />
$xmlStr=str_replace(&#8220;&#8216;&#8221;,&#8221;&#8217;,$xmlStr);<br />
$xmlStr=str_replace(&#8220;&amp;&#8221;,&#8217;&amp;&#8217;,$xmlStr);<br />
return $xmlStr;<br />
}</p>
<p>The above code is the php used to generate the XML. This php file is referred to from the index.html file which loads the actual Google Map. Besides calling the Google Map API using your own key file and declaring your marker symbols, add two functions:</p>
<p class="code">function load() {<br />
if (GBrowserIsCompatible()) {<br />
var map = new GMap2(document.getElementById(&#8220;map&#8221;));<br />
map.addControl(new GSmallMapControl());<br />
map.addControl(new GMapTypeControl());<br />
map.setCenter(new GLatLng(0, 0), 1);<br />
GDownloadUrl(&#8220;generate_gmap_xml.php&#8221;, function(data) {<br />
var xml = GXml.parse(data);<br />
var markers = xml.documentElement.getElementsByTagName(&#8220;marker&#8221;);<br />
for (var i = 0; i &lt; markers.length; i++) {<br />
var town = markers[i].getAttribute(&#8220;town&#8221;);<br />
var country = markers[i].getAttribute(&#8220;country&#8221;);<br />
var type = markers[i].getAttribute(&#8220;type&#8221;);<br />
var point = new GLatLng(parseFloat(markers[i].getAttribute(&#8220;lat&#8221;)),<br />
parseFloat(markers[i].getAttribute(&#8220;lon&#8221;)));<br />
var marker = createMarker(point, country, town, type);<br />
map.addOverlay(marker);<br />
}<br />
});<br />
}<br />
}</p>
<p>and</p>
<p class="code">     function createMarker(point, name, country, town, type) {<br />
var marker = new GMarker(point, customIcons[type]);<br />
var html = &#8220;<strong>&#8221; + name + &#8220;</strong><br />
&#8221; + country + &#8221;<br />
&#8221; + town + &#8221;<br />
&#8221; + type;<br />
GEvent.addListener(marker, &#8216;click&#8217;, function() {<br />
marker.openInfoWindowHtml(html);<br />
});<br />
return marker;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://mark.deblois.eu/2007/10/17/googlemap-based-on-postgis-data/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
