You are on page 1of 13

Using Python, GDAL and NumPy for spatial analysis and modeling

  

Overview of GDAL and NumPy Where to use these instead of ArcGIS, Imagine, etc. Description of a real-world application Step through two examples Mess around with NumPy functionality How to install the environment using Python 2.4

JPEG. Can handle many image formats for read and slightly fewer for write: AI Grid. etc. GeoTiff. PNG. Google Earth. UMN MapServer.2. ArcGIS 9.GDAL (Geospatial Data Abstraction Library)     GDAL is a “translator library for raster geospatial data formats” Open source Used in many applications: GRASS. Imagine. NetCDF. . etc.

VB. Java … Early developers have chosen Python as their scripting language and documentation is relatively good for this. R. C#. Perl. .GDAL (Geospatial Data Abstraction Library)    Presents an “abstract data model” for processing spatial data Can be used directly from C/C++ and can be “wrapped” for use with Python.

type conversions.e. etc. mathematical.NumPy (Numerical Python)     An array/matrix package for Python Well suited for image processing – i. one function can operate on the entire array Slicing by dimensions and applying functions to these slices is concise and straightforward Nearly 400 methods defined for use with NumPy arrays (e. logical.g.) .

GDAL and NumPy   Since GDAL 1. GDAL has implemented NG (New Generation) Python bindings which includes NumPy Process: Get raster band(s) Convert the raster band(s) to a NumPy array using ReadAsArray() Process the raster band(s) using NumPy functionality Open GDALDataset Write out GDALDataset Convert the NumPy array to GDAL raster bands using WriteAsArray() .3(?).

etc.Why use GDAL/NumPy instead of canned GIS software?    Not advisable if what you want to do is easily handled within ArcGIS/Imagine/etc.) . – there is a lot of programming overhead Well suited for process model applications where the logic at a cell based is too complex Example:   Grid algebra : grid1 + grid2 (probably use GIS) Finding NN in multidimensional space (maybe use GDAL/Numpy)  Also useful if your spatial data is NOT standard GIS formats (JPEG. PNG.

PNG and is easiest to ingest as unsigned 8.A real-world application   GNNViz – Translate the Gradient Nearest Neighbor (GNN) model into a rendered 3-D environment Game uses standard imagery such as JPEG.or 16-bit images .

projection.or 16-bit image where: No data : 0  Clipped window minimum : 1  Clipped window maximum : 255   Also needed to maintain metadata about spatial location. content . original Z values.A real-world application  Problem: Clip and convert spatial data (in any format) to an unsigned 8.

A real-world application (design)  Class design: Superclasses class Header Subclass class Viz8BitImage class SpatialReference class VizImage class Viz16BitImage class ValueReference .

“python extractor.bat Run the GnnViz example: Cd to gnnviz (i. “cd gnnviz”)  Run the example (i.py”)  .Step through      Copy the folder “geoprogramming” from T:\commons\matt.gregory to C:\temp Open a CMD shell (Start->Run->Type “cmd”) CD to C:\temp\geoprogramming Run setenv.e.e.

b.reshape(2.min().shape(b) c = numpy. numpy.3) print c. a-b. b.3]) b = numpy.NumPy examples import numpy a = numpy. a.array([4.shape(a).append(a.6]) print a+b.array([1.max().b) d = c. a*b.min().5. a/b print a.2.max() print numpy. d Go nuts … .

org and http://trac.scipy.org/gdal/ Listserve: gdal-dev@lists.osgeo.scipy.org/Tentative_NumPy_Tutorial Numpy Example List: http://www.scipy.org/Download Numpy tutorial (not complete): http://www.org Numpy homepage: http://numpy.scipy.org/ Numpy download: http://www.maptools.org/Numpy_Example_List_With_Doc  NumPy     .Resources  GDAL:   www.gdal.

edu/gregorym/mediawiki/index. Incomplete docs at: Easier to install Numpy and GDAL binaries into your site-packages Python folder    http://sandbox.fsl.4 Copy T:\commons\matt.orst.gregory\install into C:\Python24\Lib\site-packages Set environmental variables:   set PATH=%PATH%:C:\Python24\Lib\site-packages\gdal set PYTHONPATH=%PYTHONPATH%:C:\Python24\Lib\sitepackages\gdal .Building the Environment  Somewhat painful.php/Installing_GDAL-Python24-Numpy  Install Numpy for Python 2.