05 Geographic scripting in uDig - halfway between user and developer

1,927 views
1,794 views

Published on

Part 05 - Geographic scripting in uDig - halfway
between user and developer

Course I gave at the University of Potsdam about Geoscripting in uDig.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,927
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
19
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

05 Geographic scripting in uDig - halfway between user and developer

  1. 1. Open Source GISGeographic scripting in uDig - halfway between user and developer Geoinformation Research Group, Department of Geography University of Potsdam March 2013 JGrasstools & Geoscript Tutor: Andrea Antonello ydroloGIS nvironmental ngineering HydroloGIS S.r.l. - Via Siemens, 19 - 39100 Bolzano www.hydrologis.com
  2. 2. Raster in Geoscript - the JGrasstools projectAt the time of writing of this tutorial Geoscript didnt support much of rasterdata handling.uDig can do a lot of raster processing through the Spatial Toolbox and theJGrasstools libraries.That means that we can access from Geoscript the exact same processingmodules and exploit their raster processing power.The JGrasstools project is a more then a decade old project that specializeson geomorphology and hydrology.Since in those fields most of the processing is done on raster data, a lot ofutilities the handle rasters are available in the library.
  3. 3. The main class that we will use is the Raster class, which wraps the dataand allows us to access the data and main properties.The latest libraries can be downloaded from the projects download area.The modules needed to work with geoscripts are: • jgt-jgrassgears-XXX.jar • jgt-hortonmachine-XXX.jar • jgt-modules-XXX.jarOnce donwnloaded, they need to be copied inside a folder namedspatialtoolbox in the installation folder of uDig.
  4. 4. An example datasetTo do some tests we can download the Spearfish raster dataset.Once unzipped and loaded in uDig and styled it should look like:
  5. 5. Reading the properties of a rasterTo access raster data and use them inside scripts, the Raster class is yourfriend. path = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elevation.asc" elev = Raster.read(path) println """ Spearfish properties -------------------------------- north = ${elev.north} south = ${elev.south} west = ${elev.west} east = ${elev.east} cols = ${elev.cols} rows = ${elev.rows} cellsize = ${elev.res} """
  6. 6. Reading more properties of a rasterThrough the Raster object we can loop over the map and calculate somebasic statistics, like average, max and min elevation: path = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elevation.asc" elev = Raster.read(path) count = 0 sum = 0 min = 10000000 // initialize to something high max = 0 // initialize to something low for ( col in 0..(elev.cols-1) ){ for ( row in 0..(elev.rows-1) ){ value = elev.valueAt( col, row ) if(!elev.isNoValue( value )){ count++ sum = sum + value if(value > max ) max = value if(value < min ) min = value } } } println "Max elevation: ${max}" println "Min elevation: ${min}" println "Average elevation: ${sum/count}" println "Valid cells: ${count} of ${elev.cols*elev.rows}"
  7. 7. Putting the raster max and min elevation in a shapefilepath = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elevation.asc"dir = new Directory("/home/moovida/giscourse/data_1_3/spearfish_elevation/")// create the vector layer for the max and minlayer = dir.create(spearfish_max_min,[[geom,Point,epsg:26713], [type,string],[value,double]])// get max and min from rasterelev = Raster.read(path)count = 0min = 10000000; minCol = 0; minRow = 0 // can be put on one line using semicolonmax = 0; maxCol = 0; maxRow = 0for ( col in 0..(elev.cols-1) ){ for ( row in 0..(elev.rows-1) ){ value = elev.valueAt( col, row ) if(!elev.isNoValue( value )){ if(value > max ) { max = value maxCol = col // important, keep also the maxRow = row // positions of the max and min } if(value < min ) { min = value minCol = col minRow = row } } }}// get the world position from the raster grid positionminXY = elev.positionAt(minCol, minRow)maxXY = elev.positionAt(maxCol, maxRow)// add the featureslayer.add([new Point(minXY[0], minXY[1]),min, min])layer.add([new Point(maxXY[0], maxXY[1]),max, max])
  8. 8. The result should look like:
  9. 9. Create a new rasterA new raster can be created from an existing one by using it as a template.To see how a new raster is created, we can extarct all those parts from theraster map, in which the elevation lies within the elevation of 1400 and 1600meters: path = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elevation.asc" outPath = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elev_1400_1600.asc" elev = Raster.read(path) // create a new raster using elev as template newElev = new Raster(elev) // loop over the elevation and pick only values between 1440 and 1600 for ( col in 0..(elev.cols-1) ){ for ( row in 0..(elev.rows-1) ){ value = elev.valueAt( col, row ) if(value < 1600.0 && value > 1400.0 ) { // set the elevation in the new raster newElev.setValueAt( col, row, value ) } } } // write the raster to file newElev.write(outPath)
  10. 10. The result should look like, if overlayed in greyscale style:
  11. 11. Neighbour operationsWith the raster object it is possible to access the neighbour cell values. Inthis example we will extract pits: path = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elevation.asc" dir = new Directory("/home/moovida/giscourse/data_1_3/spearfish_elevation/") // create the vector layer for the pits layer = dir.create(spearfish_pits,[[geom,Point,epsg:26713], [value,double]]) elev = Raster.read(path) for ( col in 0..(elev.cols-1) ){ for ( row in 0..(elev.rows-1) ){ value = elev.valueAt( col, row ) if(!elev.isNoValue( value )){ // get all neighbour values surr = elev.surrounding(col, row) isPit = true surr.each(){ neighValue -> // check if one is smaller if(neighValue <= value ) isPit = false } if(isPit){ // add pits to the vector layer xy = elev.positionAt(col, row) layer.add([new Point(xy[0], xy[1]), value]) } } } }
  12. 12. Which should look like:
  13. 13. Geomorphologic analysesThe JGrasstools project supplies a whole pile of modules. Two methods helpto get started.The help method shows the help for the module in the console: new Aspect().help()
  14. 14. The template method: new Aspect().template()on each module provides a syntax blueprint which can be used as startpoint
  15. 15. AspectUsing the template as starting point, lets calculate the map of aspect from thelevation model: // set in and out paths path = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elevation.asc" outPath = "/home/moovida/giscourse/data_1_3/spearfish_elevation/aspect.asc" // run the aspect module Aspect aspect = new Aspect(); aspect.inElev = path; aspect.outAspect = outPath; aspect.process();
  16. 16. Which should look like:
  17. 17. Contours extractionThe ContourExtractor module can be used to extract contour lines: // set in and out paths path = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elevation.asc" outPath = "/home/moovida/giscourse/data_1_3/spearfish_elevation/contours.shp" // run the module defining start, end and contours interval ContourExtractor contourextractor = new ContourExtractor(); contourextractor.inCoverage = path; contourextractor.pMin = 1000; contourextractor.pMax = 2000; contourextractor.pInterval = 20; contourextractor.outGeodata = outPath; contourextractor.process();
  18. 18. Which should look like:
  19. 19. Network extractionA network extraction can be done in 3 steps: flowdirections + tca, raster net,vector net: // set all in and out paths path = "/home/moovida/giscourse/data_1_3/spearfish_elevation/elevation.asc" flowpath = "/home/moovida/giscourse/data_1_3/spearfish_elevation/flow.asc" tcapath = "/home/moovida/giscourse/data_1_3/spearfish_elevation/tca.asc" netpath = "/home/moovida/giscourse/data_1_3/spearfish_elevation/net.asc" netshppath = "/home/moovida/giscourse/data_1_3/spearfish_elevation/net.shp" // calculate flowdirections and tca LeastCostFlowDirections leastcostflowdirections = new LeastCostFlowDirections(); leastcostflowdirections.inElev = path; leastcostflowdirections.outFlow = flowpath; leastcostflowdirections.outTca = tcapath; leastcostflowdirections.process(); // extract the network raster ExtractNetwork extractnetwork = new ExtractNetwork(); extractnetwork.inTca = tcapath; extractnetwork.inFlow = flowpath; extractnetwork.pThres = 100; extractnetwork.outNet = netpath; extractnetwork.process(); // convert the network to shapefile NetworkAttributesBuilder networkattributesbuilder = new NetworkAttributesBuilder(); networkattributesbuilder.inNet = netpath; networkattributesbuilder.inFlow = flowpath; networkattributesbuilder.inTca = tcapath; networkattributesbuilder.outNet = netshppath; networkattributesbuilder.process();
  20. 20. The first maps extracted are the ones of flowdirections and total contributingarea. The map of TCA already suggests the network paths:
  21. 21. In the second step the raster network was extracted:
  22. 22. In the third step the raster network can be transformed to shapefile:

×