Anda di halaman 1dari 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

GDAL (Geospatial Data Abstraction Library)

GDAL is a translator library for raster geospatial data formats Open source Used in many applications: GRASS, UMN MapServer, Google Earth, ArcGIS 9.2, etc. Can handle many image formats for read and slightly fewer for write: AI Grid, Imagine, GeoTiff, JPEG, PNG, NetCDF, etc.

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, Perl, VB, C#, R, Java Early developers have chosen Python as their scripting language and documentation is relatively good for this.

NumPy (Numerical Python)

An array/matrix package for Python Well suited for image processing i.e. 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.g. type conversions, mathematical, logical, etc.)

GDAL and NumPy

Since GDAL 1.3(?), 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()

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, etc.)

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, PNG and is easiest to ingest as unsigned 8- or 16-bit images

A real-world application

Problem: Clip and convert spatial data (in any format) to an unsigned 8- or 16-bit image where:
No data : 0 Clipped window minimum : 1 Clipped window maximum : 255

Also needed to maintain metadata about spatial location, projection, original Z values, content

A real-world application (design)

Class design:
Superclasses class Header Subclass class Viz8BitImage

class SpatialReference

class VizImage class Viz16BitImage

class ValueReference

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.bat Run the GnnViz example:
Cd to gnnviz (i.e. cd gnnviz) Run the example (i.e. python extractor.py)

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

Resources

GDAL:

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

NumPy

Building the Environment

Somewhat painful. Incomplete docs at: Easier to install Numpy and GDAL binaries into your site-packages Python folder

http://sandbox.fsl.orst.edu/gregorym/mediawiki/index.php/Installing_GDAL-Python24-Numpy

Install Numpy for Python 2.4 Copy T:\commons\matt.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

Anda mungkin juga menyukai