12
Raster processing with scipy.ndimage Henry Walshaw [email protected] @om_henners N g g g g g g g g g g g g g i i i i i i i i s s s . . s s t t t t t t a a c c c c c c c c c c c c c k k k k k k k k k k k k k e e e x x x x x c c c c c c c c c c c h h h h a a a a a a a n n n n n g g g g g g g g g g g g g g g g g e e e e . . c c c c o o o o m m m m m m m m m m m m m m m m m m c c c c c c c c c c c h h h h h h h h h h h x x x x x x x x x x x x x a a a a a a a a e e e e e n n n n n n n n n n k k k k k k k k k k k k k k k k k k k g g g g g g g g g g g g g g g g g g g g g g g g g g c c c c c c c c c c c c c c c c c c c c a a a a a a a a e e e e e e e t t t t t t t t t t t t . . . . s s s s s c c c c c c c c c . . . . o o o o o o o o o s s s s s s s i i i i i i i i i i i m m m m m m m m m m m m m m m m i i i g g g g g g g g g

Raster Processing with Scipy.ndimage (Dev Meet Up II)

Embed Size (px)

Citation preview

Page 1: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Raster processing with scipy.ndimageHenry [email protected] @om_hennersN

ggggggggggggggiiiiiiiiisss....ssttttttaaccccccccccccccckkkkkkkkkkkkkkeeexxxxxcccccccccccchhhhaaaaaaannnnngggggggggggggggggg

eeee..ccccoooo

mmmmmmmmmmmmmmmmmm

cccccccccccchhhhhhhhhhhhxxxxxxxxxxxxxx aaaaaaaaaaeeeeeee nnnnnnnnnnnkkkkkkkkkkkkkkkkkkkkkk gggggggggggggggggggggggggggggggggccccccccccccccccccccccccaaaaaaaaa eeeeeeettttttttttttt ....sssssss cccccccccc.... oooooooooossssssssiiiiiiiiiiiii mmmmmmmmmmmmmmmmmiiiiigggggggggggggg

Page 2: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Getting set up

arcpy for ArcGIS 10 requires numpy 1.3.0

This means we’re restricted to scipy 0.7.1, matplotlib 1.0.1 and PIL 1.1.7 (which is still the latest version)

If you’re not using ArcGIS, or you’re using virtualenv get the latest versions!

All code for this talk can be downloaded from github:https://github.com/om-henners/ndimage_talk.git

Page 3: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Why process with scipy?

Open Source scientific algorithms

Easy to set up for large concurrent processing on local PCs and in the cloud (see PiCloud)

It’s free!*

*Well, aside from development cost

Page 4: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Getting data in and out

We’ll be using the arcpy.RasterToNumPyArray and arcpy.NumPyArrayToRaster functions

Alternatives include GDAL Python bindings, scipy image read functions, and many others

See getting_data_in.py and getting_data_out.py

Page 5: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Calculating median filter

Once you’ve got an array it’s one line of code to perform a basic filter function

scipy.ndimage.filters.median_filter(a, size=9)

Page 6: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Getting more complex

n-dimensional processing

We can supply a size or filter shape, but now we have to be even more aware of edge effects

Still the same one line of code

Page 7: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Generic filters in scipy

Most Spatial Analyst operations are in ndimage

For everything else there’s generic_filter

Flattens the target region and passes through to a callback function

Can be used to handle null data as ndimage can’t handle masked arrays (yet)

Page 8: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Calculating slope

We can implement a simple slope calculation using generic filter over a 3x3 footprint

We can use the form as described by the ESRI documentation:

slope = e is atan ( sqrt( [dz/dx]**2 + [dz/dy]**2 ) ) where[dz/dx] = ((c + 2f + i) - (a + 2d + g) / (8 * x_cellsize)[dz/dy] = ((g + 2h + i) - (a + 2b + c)) / (8 * y_cellsize))

Slower than a standard ndimage filter, but faster thanarcpy!

Page 9: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Calculating a variable focal maximum

Calculate the focal maximum for every point in an array, with the focal annulus defined by a radius from another raster

Don’t use a generic filter (there’s no need)

Don’t use ArcGIS Spatial analyst (it’s really slow)

Method 100x100 random raster

generic filter 4.7 secs

arcpy Spatial Analyst 6.7 secs

Page 10: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Do use inbuilt functions

It’s faster to calculate the focal sum using ndimage.maximum_filter for every possible buffer value than to roll your own function

100x100 array? 0.009 seconds

Page 11: Raster Processing with Scipy.ndimage (Dev Meet Up II)

Last words on scipy.ndimage

It’s awesome, and free and threadsafe

It ties into any number of other packages (scikits-learn, scikits-image)

You can do just about anything in ndimage that you can in Spatial Analyst

If you have time to code it

And code it right

Page 12: Raster Processing with Scipy.ndimage (Dev Meet Up II)

John Hunter (1968 - 2012)

http://numfocus.org/johnhunter/