<?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>jduck.net &#187; database</title>
	<atom:link href="http://jduck.net/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://jduck.net</link>
	<description></description>
	<lastBuildDate>Fri, 02 Jul 2010 13:01:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Getting to know PostGIS</title>
		<link>http://jduck.net/2007/11/06/getting-to-know-postgis/</link>
		<comments>http://jduck.net/2007/11/06/getting-to-know-postgis/#comments</comments>
		<pubDate>Tue, 06 Nov 2007 14:45:13 +0000</pubDate>
		<dc:creator>Jonah</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[geospatial]]></category>
		<category><![CDATA[gis]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://jduck.net/2007/11/06/getting-to-know-postgis/</guid>
		<description><![CDATA[I am making the plunge to get more informed with how to use PostGIS. It is pretty impressive so far. Here is a quick tutorial which shows you how to get started and loading in a shapefile to play around with some fun SQL querying. I assume a basic understanding of the linux command line [...]]]></description>
			<content:encoded><![CDATA[<p>I am making the plunge to get more informed with how to use PostGIS.  It is pretty impressive so far.  Here is a quick tutorial which shows you how to get started and loading in a shapefile to play around with some fun SQL querying.  I assume a basic understanding of the linux command line and some basic SQL skills. </p>
<p>To get started in ubuntu 7.04 (should work in 7.10 and debian too):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> postgis postgresql-<span style="color: #000000;">8.1</span>-postgis</pre></div></div>

<p>Next we&#8217;ll start setting up the PostGIS environment.<br />
<span id="more-104"></span></p>
<p>Then we need to set up a new database, I called mine geodb:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ createdb geodb</pre></div></div>

<p>We then need to bind it to the PostGIS libs with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ createlang plpgsql geodb</pre></div></div>

<p>And finally add the PostGIS Tables:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ psql <span style="color: #660033;">-d</span> mytestdb <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>postgresql-<span style="color: #000000;">8.1</span>-postgis<span style="color: #000000; font-weight: bold;">/</span>lwpostgis.sql
$ psql <span style="color: #660033;">-d</span> mytestdb <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>postgresql-<span style="color: #000000;">8.1</span>-postgis<span style="color: #000000; font-weight: bold;">/</span>spatial_ref_sys.sql</pre></div></div>

<p>Phew&#8230;.that was a bit of work.</p>
<p>Now we&#8217;re ready to go.  We have a database called geodb and we&#8217;ve told PostgreSQL that it is a PostGIS database.</p>
<p>So now we can use the shp2pgsql command to import a shapefile into the db:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ shp2pgsql <span style="color: #660033;">-W</span> LATIN1 <span style="color: #660033;">-D</span> <span style="color: #660033;">-I</span> myshapefile.shp mytablename <span style="color: #000000; font-weight: bold;">|</span> psql geodb</pre></div></div>

<p>-W is for setting the encoding type, I had problems with many shapefiles as my locale is UTF8 and PostgreSQL seems to default to the locale.  For me LATIN1 has been working better.</p>
<p>-D sets the SQL output to be in dump format for insertion into the database.</p>
<p>-I flags it to compute the spatial indexing upon import.  For large complex vector features this can take awhile, so use with discretion.</p>
<p>All of this is piped to </p>
<p>This gives us a new table called &#8220;tablename&#8221;, which we can see by doing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ psql geodb
Welcome to psql 8.1.8, the PostgreSQL interactive terminal.
&nbsp;
Type:  \\copyright <span style="color: #000000; font-weight: bold;">for</span> distribution terms
       \\h <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">help</span> with SQL commands
       \\? <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">help</span> with psql commands
       \\g or terminate with semicolon to execute query
       \\q to quit
&nbsp;
<span style="color: #007800;">geodb</span>=<span style="color: #666666; font-style: italic;">#</span></pre></div></div>

<p>At the PostgreSQL prompt you can enter various SQL commands including queries.  </p>
<pre>geodb=# \\d mytablename</pre>
<p>shows the columns and data types of the data in the table.</p>
<p>Notice the column called: the_geom</p>
<p>This is where the <a href="http://postgis.refractions.net/docs/ch04.html#RefObject">GIS Objects</a> are stored.</p>
<p>The sample query there would show all the data in the shapefile&#8217;s associated .dbf as well as a column called </p>
<p>Next time we&#8217;ll launch into some querying fun.</p>
<p><a href="http://jduck.net/2009/01/30/getting-to-know-postgis-part-ii/">Onward to Part II&#8230;</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jduck.net/2007/11/06/getting-to-know-postgis/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
