<?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>Michalis Avraam &#187; gdal/ogr</title>
	<atom:link href="http://michalisavraam.org/tag/gdalogr/feed/" rel="self" type="application/rss+xml" />
	<link>http://michalisavraam.org</link>
	<description>intersecting space and time through gis endeavors</description>
	<lastBuildDate>Wed, 08 Sep 2010 20:03:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>The Essential Python Modules for GIS</title>
		<link>http://michalisavraam.org/2010/04/the-essential-python-modules-for-gis/</link>
		<comments>http://michalisavraam.org/2010/04/the-essential-python-modules-for-gis/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 00:41:45 +0000</pubDate>
		<dc:creator>Michalis Avraam</dc:creator>
				<category><![CDATA[GIS* Points]]></category>
		<category><![CDATA[Python Points]]></category>
		<category><![CDATA[gdal]]></category>
		<category><![CDATA[gdal/ogr]]></category>
		<category><![CDATA[geoprocessing]]></category>
		<category><![CDATA[networkx]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[ogr]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python gis]]></category>
		<category><![CDATA[python modules]]></category>
		<category><![CDATA[xlrd]]></category>
		<category><![CDATA[xlwt]]></category>

		<guid isPermaLink="false">http://michalisavraam.org/?p=304</guid>
		<description><![CDATA[With ESRI&#8217;s use of Python as their scripting language and the proliferation of open source GIS, Python became one of the required languages for GIS developers and hobbyists alike. What makes Python powerful is well documented throughout the web, but I want to highlight one very important aspects of Python today: Python Modules. Python Modules <a href='http://michalisavraam.org/2010/04/the-essential-python-modules-for-gis/'>[...]</a>


Related posts:<ol><li><a href='http://michalisavraam.org/2009/06/manipulating-excel-files-using-python-part-1-reading-files/' rel='bookmark' title='Permanent Link: Manipulating Excel files using Python part 1: Reading Excel Files'>Manipulating Excel files using Python part 1: Reading Excel Files</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>With ESRI&#8217;s use of Python as their scripting language and the proliferation of open source GIS, Python became one of the required languages for GIS developers and hobbyists alike. What makes Python powerful is <a href="http://www.stanford.edu/~pgbovine/python-teaching.htm">well documented</a> <a href="http://www.python.org/about/quotes/">throughout</a> <a href="http://www.vni.com/company/whitepapers/html/AnalyticModelinginPython.php">the</a> <a href="http://webhelp.esri.com/arcgisdesktop/9.1/body.cfm?tocVisable=1&amp;ID=1925&amp;TopicName=An%20overview%20of%20writing%20geoprocessing%20scripts">web</a>, but I want to highlight one very important aspects of Python today: Python Modules.</p>
<p>Python Modules are code someone else has written and distributed, in order to make life easier for the rest of us. You may be familiar with the <a href="http://docs.python.org/modindex.html">standard modules that come with Python</a>, like math or datetime, but there are numerous more resources out there for the GIS minded developers. I will be discussing some of the modules I find essential in my work apart from the famous ArcGISScripting module by ESRI: GDAL, numpy, NetworkX, xlrd and xlwt. Let&#8217;s dive in!<span id="more-304"></span></p>
<h3>GDAL &#8211; Geospatial Data Abstraction Layer</h3>
<p>It will come a time in every GIS Professional&#8217;s career when they will need to quickly access information from a random shapefile they have, but do not have access to any GIS software or geoprocessing functionality (think of a laptop on the road, a remote machine not running Windows, etc). GDAL comes to the rescue, providing us such functionalities.</p>
<p>GDAL is a translator library with Python bindings that allows access to raster data using a unified abstract layer. Bundled with it is OGR, which provides similar functionality for vector data. <a href="http://trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries">Download it here</a>.</p>
<p>A quick example of using GDAL:</p>
<pre class="brush: python; title: ;">
import gdal
from gdalconst import *
# Open the raster dataset
dataset = gdal.Open(filename, GA_ReadOnly)
# Print the projection of the data
print dataset.GetProjection()
</pre>
<p>Using OGR:</p>
<pre class="brush: python; title: ;">
import ogr
# Get the driver
driver = ogr.GetDriverByName('ESRI Shapefile')
# Open a shapefile
dataset = driver.Open(shapefileName, 0)
</pre>
<h3>numpy &#8211; Numerical Python</h3>
<p>I cannot think of any GIS practitioner that did not have to manipulate raster data in a peculiar way, only finding that the software at hand doesn&#8217;t allow you to perform many customized functions. If one can interpret raster data (which GDAL above helps us with), then one can use them in Python as a matrix (algebraic matrix). numpy is the best Python package out there for this sort of situation.</p>
<p>numpy is a package that enables n-dimensional array manipulation in Python, as essential part of any scientific endeavor. It also provides linear algebra functionalities, Fourier transforms and random number generators. <a href="http://www.scipy.org/Download">Get it here</a>.</p>
<p>An example of the use of numpy:</p>
<pre class="brush: python; title: ;">
from numpy import *
# Sample IO Table data
ioSample = [[1,2], [3,4]]
# Turn into a numpy array
ioMatrix = array(ioSample)
# Find the inverse of ioMatrix
ioMatrixInv = linalg.inv(ioMatrix)
</pre>
<h3>NetworkX &#8211; Complex Networks Analysis</h3>
<p>While most GIS software out there provides the ability to build networks, sometimes it is easier to build networks quickly and dirty, without having to involve complex GIS software. An analysis of participation by space in an experiment can easily be achieved using the simple, yet powerful NetworkX module.</p>
<p>NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. It is hosted by the Los Alamos National Laboratory, and sees active development (presumably sponsored somehow by Los Alamos). <a href="http://networkx.lanl.gov/download.html">Download it here</a>.</p>
<p>A quick sample is shown below:</p>
<pre class="brush: python; title: ;">
import networkx as nx
# Create a graph
g = nx.Graph()
# Populate the graph
g.add_node(1)
g.add_node(2)
g.add_node(3)
# Create edges
g.add_edge(1,2)
g.add_edge(1,3)
# Print the neighbors of node 1 (returns 2)
print g.neighbors(1)
</pre>
<h3>xlrd &#8211; Excel™ File Reader</h3>
<p>All GIS practitioners have been sent &#8220;GIS data&#8221; in an Excel file, either a geocoding result or GPS waypoints, or anything similar. While ideally whoever sent the data would be educated on why it is a bad idea, most often we have to deal with the data without any additional help. xlrd comes into play, allowing you to read the said Excel formatted data into Python with little effort.</p>
<p>xlrd is a Python module that allows one to read Excel files without the need of Microsoft Excel or Windows. It provides access to XLS files for Microsoft Office 2003 or earlier. <a href="http://www.lexicon.net/sjmachin/xlrd.htm">Download it here</a>.</p>
<p>A quick example that will read an XLS file and print it to screen:</p>
<pre class="brush: python; title: ;">
import xlrd
# Open the Excel file
book = xlrd.open_workbook(&quot;excelFile.xls&quot;)
# Read the first sheet in the Excel workbook
sheet = book.sheet_by_index(0)
# Read the first row from column A to E
rowValues = sheet.row_values(0, start_colx=0, end_colx=4)
# Print the row values
for value in rowValues:
    print value
</pre>
<h3>xlwt &#8211; Excel™ file writer</h3>
<p>Business requirements often want results in an Excel file, so some other person, in another department, can run some sort of analysis on the data. Building a distance matrix is fine, but Joe from accounting is using complicated Excel spreadsheets and does not want to bother with database connections or DBF files. This is a situation in which xlwt excels, writing data to an Excel spreadsheet without the need of Excel or manipulation by the mouse.</p>
<p>xlwt is a Python module that, similarly to xlrd mention above, allows for cross platform Excel file creation without the need of Microsoft Office. <a href="https://secure.simplistix.co.uk/svn/xlwt/trunk/">Download it here</a>.</p>
<p>An example follows:</p>
<pre class="brush: python; title: ;">
import xlwt
# Create a new workbook
book = xlwt.Workbook()
# Add a new sheet
sheet = book.add_sheet(&quot;My Sheet&quot;)
# Write the number 5 in the first row, first column
sheet.write(0, 0, 5)
# Save the file
book.save(&quot;myExcelFile.xls&quot;)
</pre>
</pre>
<p>These are my essential Python modules for GIS development. Notice I left quite a few behind, especially those relating to web-based GIS developments. Those will appear in another post in the future. But please, do share your thoughts on what Python modules are essential for your GIS work.</p>
Note: There is a rating embedded within this post, please visit this post to rate it.


<p>Related posts:<ol><li><a href='http://michalisavraam.org/2009/06/manipulating-excel-files-using-python-part-1-reading-files/' rel='bookmark' title='Permanent Link: Manipulating Excel files using Python part 1: Reading Excel Files'>Manipulating Excel files using Python part 1: Reading Excel Files</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://michalisavraam.org/2010/04/the-essential-python-modules-for-gis/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

