<?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>State Of Flux &#187; centroid</title>
	<atom:link href="http://stateofflux.com/tag/centroid/feed/" rel="self" type="application/rss+xml" />
	<link>http://stateofflux.com</link>
	<description>always changing</description>
	<lastBuildDate>Fri, 29 Jan 2010 03:29:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Australian GeoSpatial Data &#8211; Free</title>
		<link>http://stateofflux.com/2008/10/19/australian-geospatial-data-free/</link>
		<comments>http://stateofflux.com/2008/10/19/australian-geospatial-data-free/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 09:44:00 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[australia]]></category>
		<category><![CDATA[centroid]]></category>
		<category><![CDATA[esri]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[geospatial]]></category>
		<category><![CDATA[lambertconformalconic]]></category>
		<category><![CDATA[postgis]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[projection]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[suburbs]]></category>
		<category><![CDATA[transform]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://markmansour.wordpress.com//2008/10/19/australian-geospatial-data-free</guid>
		<description><![CDATA[Edit: There are notes in the comments from Tim that explain the changes for PostgreSql 8.4.  Thanks Tim! I’ve built a couple of sites that needed geospatial data. One was a social networking site that needed a way to list people who were near other people, the other was a art web site that allowed [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;">Edit: There are notes in the comments from Tim that explain the changes for PostgreSql 8.4.  Thanks Tim!</span></p>
<p>I’ve built a couple of sites that needed geospatial data.  One was a social networking site that needed a way to list people who were near other people, the other was a art web site that allowed users to upload steet art and show it on a map.  I thought it would be interesting to get the basics of an Australian suburb dataset up and running in a geospatial database and do some simple queries.</p>
<h3>Install PostgreSql and PostGIS</h3>
<p>First thing to do is setup PostgreSql and PostGIS.  I’m sure you can do this in MySQL but I haven’t done it, so leave a note in the comments if you get that up and running <img src='http://stateofflux.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .   There are a few article on how to do this and it is platform specific so go and do that.</p>
<h3>Get some Suburb data</h3>
<p>Now we need some data.  The <span class="caps">ABS</span> is kind enough to provide Australia broken down into suburbs and postcodes on their site.  I’m going to deal with suburbs so go ahead and download the <a href="http://www.abs.gov.au/AUSSTATS/abs@.nsf/DetailsPage/2923.0.30.0012006?OpenDocument">State Suburbs (SSC) 2006 Digital Boundaries in <span class="caps">ESRI</span> Shapefile format</a> data cube.  This data cube has every suburb in Australia defined as a Polygon (or a multipolygon) with each node defined as a latitude and longitude.</p>
<h3>Converting it to <span class="caps">SQL</span></h3>
<p>Unzip the downloaded shapefile and you’ll get 8 files but we are only concerned with the <code>SSC06aAUST_region.*</code> ones.  We are going to load the POA06aAUST_region data into the database but firstly we need to convert it into <span class="caps">SQL</span>.</p>
<pre lang="bash">shp2pgsql SSC06aAUST_region.shp suburbs -s 4283 -I -d &gt; suburbs.sql</pre>
<p>shp2pgsql converts the <span class="caps">ESRI</span> Shapefile into <span class="caps">SQL</span>.  -I adds an index (which is very important for speed) and the -d Drop and recreates the table.  The -s 4283 make sure the suburb data is defined in with the correct projection.  The earth isn’t a sphere and different parts of the earth are curved slighly differently so the geo-bods came up with a whole bunch of projections.  4283 is the standardized number for the <span class="caps">GDA 1994</span> projection which is the projection the suburb data comes in (you can just take a peek inside the POA06aAUST_region.prj file to see what the project is).</p>
<h3>Create a Geo-enabled DB and load the data</h3>
<pre lang="bash">createdb australia
createlang plpgsql australia
psql -f /opt/local/share/postgis/lwpostgis.sql -d australia
psql -f /opt/local/share/postgis/spatial_ref_sys.sql -d australia
psql australia &lt; suburbs.sql</pre>
<p>Note: The directories for the lwpostgis.sql and spatial_ref_sys will vary from system to system so you’ll have to find them on your own machine.</p>
<p>You will also want to create a reference table for the Australian States</p>
<pre lang="sql">create table aust_states (id integer primary key, state_name varchar, state_abbrev varchar);
insert into aust_states (id, state_name, state_abbrev) values (1, 'New South Wales', 'NSW');
insert into aust_states (id, state_name, state_abbrev) values (2, 'Victoria', 'VIC');
insert into aust_states (id, state_name, state_abbrev) values (3, 'Queensland', 'QLD');
insert into aust_states (id, state_name, state_abbrev) values (4, 'South Australia', 'SA');
insert into aust_states (id, state_name, state_abbrev) values (5, 'Western Australia', 'WA');
insert into aust_states (id, state_name, state_abbrev) values (6, 'Tasmania', 'TAS');
insert into aust_states (id, state_name, state_abbrev) values (7, 'Northern Territory', 'NT');
insert into aust_states (id, state_name, state_abbrev) values (8, 'Australian Captial Territory', 'ACT');
insert into aust_states (id, state_name, state_abbrev) values (9, 'Other Territories', 'OT');</pre>
<h3>Get some awesome answers!</h3>
<h4>Show me the polygon of Port Melbourne</h4>
<pre lang="sql">select name_2006, astext(the_geom)  from suburbs where name_2006 = 'Port Melbourne';</pre>
<p>This returns a whole bunch of lat and longs.  Pretty useless really.  Maybe having the center of a suburb would be more useful.</p>
<h4>Show me the center of Port Melbourne</h4>
<pre lang="sql">select name_2006, astext(centroid(the_geom))  from suburbs where name_2006 = 'Port Melbourne';

   name_2006    |                  astext
----------------+-------------------------------------------
 Port Melbourne | POINT(144.921987367191 -37.8328692507562)
(1 row)</pre>
<p>Much better!</p>
<h4>Show me the suburbs that surround Port Melbourne</h4>
<pre lang="sql">select surrounding.name_2006
    from suburbs source, suburbs surrounding
    where source.name_2006 = 'Port Melbourne'
        and touches(source.the_geom, surrounding.the_geom);

    name_2006
-----------------
 Albert Park
 Docklands
 South Melbourne
 Southbank
 Spotswood
 West Melbourne
 Yarraville
(7 rows)</pre>
<p>Here I select the suburb table twice, once to represent it as the source suburb, in this case Port Melbourne and as a destination or surrounding suburb.  I then restrict my matches to only show polygons that touch the source.</p>
<h4>Show me the suburbs that surround Port Melbourne with distances between suburbs</h4>
<pre lang="sql">select surrounding.name_2006,
       distance(transform(centroid(source.the_geom),3112),
                transform(centroid(surrounding.the_geom),3112))
    From suburbs source, suburbs surrounding
    where source.name_2006 = 'Port Melbourne'
        and touches(source.the_geom, surrounding.the_geom);

    name_2006    |     distance
-----------------+------------------
 Albert Park     | 3908.06472236311
 Docklands       | 2316.21021732757
 South Melbourne | 3106.68573231296
 Southbank       | 3492.93829708397
 Spotswood       |  3035.6283677131
 West Melbourne  | 2682.84381789969
 Yarraville      | 3914.04324956383
(7 rows)</pre>
<p>The interesting part here is getting the distance between suburbs.  The distance() method gets the distance between two points, which for us is the 2 center points of our suburbs.  Unfortunately if you measure the distance you’ll get an answer in degrees which isn’t that useful.  So you need to transform the projection from a degree (lat and long are in degrees) to a <a href="http://postgis.refractions.net/pipermail/postgis-users/2008-June/020182.html">meter based projection</a> .  Australia happens to have one called the Lambert Conformal Conic projection known as number 3112.  Hence:</p>
<pre lang="sql">distance(transform(centroid(source.the_geom),3112),
                transform(centroid(surrounding.the_geom),3112))</pre>
<p>will get the distance, in meters, betwwen two suburbs.</p>
<h4>Show me all the suburbs named Richmond</h4>
<pre lang="sql">select name_2006,
       state_name
    from suburbs
    inner join aust_states on suburbs.state_2006 = aust_states.id
    where name_2006 = 'Richmond';

 name_2006 |   state_name
-----------+-----------------
 Richmond  | Victoria
 Richmond  | South Australia
 Richmond  | Tasmania
(3 rows)</pre>
<h3>What’s next?</h3>
<p>This is all very nice, but when you start geocoding data and getting lat/longs of items you can store in the db then you can do some really fun stuff.  If this article generates enough interest I’ll follow up with some Ruby code and Google Maps integration.</p>
]]></content:encoded>
			<wfw:commentRss>http://stateofflux.com/2008/10/19/australian-geospatial-data-free/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

