Using Python GDAL and Num Py for spatial

  • Slides: 13
Download presentation
Using Python, GDAL and Num. Py for spatial analysis and modeling Overview of GDAL

Using Python, GDAL and Num. Py for spatial analysis and modeling Overview of GDAL and Num. Py n Where to use these instead of Arc. GIS, Imagine, etc. n Description of a real-world application n Step through two examples n Mess around with Num. Py functionality n How to install the environment using Python 2. 4 n

GDAL (Geospatial Data Abstraction Library) GDAL is a “translator library for raster geospatial data

GDAL (Geospatial Data Abstraction Library) GDAL is a “translator library for raster geospatial data formats” n Open source n Used in many applications: GRASS, UMN Map. Server, Google Earth, Arc. GIS 9. 2, etc. n Can handle many image formats for read and slightly fewer for write: AI Grid, Imagine, Geo. Tiff, JPEG, PNG, Net. CDF, etc. n

GDAL (Geospatial Data Abstraction Library) Presents an “abstract data model” for processing spatial data

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

Num. Py (Numerical Python) An array/matrix package for Python n Well suited for image

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

GDAL and Num. Py Since GDAL 1. 3(? ), GDAL has implemented NG (New

GDAL and Num. Py Since GDAL 1. 3(? ), GDAL has implemented NG (New Generation) Python bindings which includes Num. Py n Process: n Get raster band(s) Open GDALDataset Write out GDALDataset Convert the Num. Py array to GDAL raster bands using Write. As. Array() Convert the raster band(s) to a Num. Py array using Read. As. Array() Process the raster band(s) using Num. Py functionality

Why use GDAL/Num. Py instead of canned GIS software? n n n Not advisable

Why use GDAL/Num. Py instead of canned GIS software? n n n Not advisable if what you want to do is easily handled within Arc. GIS/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: n n n Grid algebra : grid 1 + grid 2 (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

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

A real-world application n Problem: Clip and convert spatial data (in any format) to

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

A real-world application (design) n Class design: Superclasses class Header Subclass Spatial. Reference class

A real-world application (design) n Class design: Superclasses class Header Subclass Spatial. Reference class Viz 8 Bit. Image class Viz 16 Bit. Image class Value. Reference

Step through Copy the folder “geoprogramming” from T: commonsmatt. gregory to C: temp n

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

Num. Py examples import numpy a = numpy. array([1, 2, 3]) b = numpy.

Num. Py 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 n GDAL: n n n www. gdal. org and http: //trac. osgeo. org/gdal/

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

Building the Environment n Somewhat painful. Incomplete docs at: http: //sandbox. fsl. orst. edu/gregorym/mediawiki/index.

Building the Environment n Somewhat painful. Incomplete docs at: http: //sandbox. fsl. orst. edu/gregorym/mediawiki/index. php/Installing_GDAL-Python 24 -Numpy n Easier to install Numpy and GDAL binaries into your site-packages Python folder n n n Install Numpy for Python 2. 4 Copy T: commonsmatt. gregoryinstall into C: Python 24Libsite-packages Set environmental variables: n n set PATH=%PATH%: C: Python 24Libsite-packagesgdal set PYTHONPATH=%PYTHONPATH%: C: Python 24Libsitepackagesgdal