Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Python API GeoReference

Johannes Kolbe edited this page Jun 18, 2014 · 3 revisions

Creating GeoReference Objects

IlwisObjects lets you easily excerpt the GeoReference data from RasterCoverage maps. All you have to do is to call the method .geoReference() on an existing RasterCoverage and assign it to a variable.

rc = RasterCoverage("subkenya.mpr")
georef= rc.geoReference()

If you have an existing .grf which you want to load you can easily do this by setting the working catalog to the correct folder and passing the name of zour file to the constructor GeoReference()

Engine.setWorkingCatalog("file:///D:/<working>/<directory>/") 
georef = GeoReference("n000302.grf")

Using GeoReference Data

There are many useful ways in which you can use the data accesible through the GeoReference. One possible way is to create a box of a certain size and turning it into an envelope with the help of the coordinate system given by the GeoReference, using the box2Envelope() method. You can also go the other way around and turn an envelope into a box, using the envelope2Box() method.

rc = RasterCoverage("n000302.mpr")
georef = rc.geoReference();

box = Box(georef.size())
# the WKT representation of box is now "POLYGON(0 0 0,1151 1151 0)"
# the box can also be initalized manually by passing the minimum and then the maximum value to the Box() constructor as coordinates

env = georef.box2Envelope(box)
# the WKT representation of env is now "POLYGON(-4.60799e+06 -4.59997e+06,4.60001e+06 4.60803e+06)"

subenv = Envelope(Coordinate(-1e+06, -1e+06), Coordinate(1e+06, 1e+06))
# creating a new envelope with min and max coordinates
# WKT "POLYGON(-1e+06 -1e+06,1e+06 1e+06)" (obviously)

subbox = gr.envelope2Box(subenv)
# WKT "POLYGON(451 451,701 701)"

The main thing to remember here is, that Box coordinates are always referencing the grid, while the envelope coordinates are always refer to the coordinate system.

Another way to work with the data from the GeoReference is e.g. to get a the number of the pixel at a certain coordinate. Like in the above example all you need for it is one simple method, called coord2Pixel(). And of course you can also get the coordinate of a specific pixel, by calling pixel2Coord().

rc = RasterCoverage("n000302.mpr")
georef = rc.geoReference();

coord = Coordinate(-319195.47, 784540.64)
pix = georef.coord2Pixel(coord)
# pix has now the values 536.599,478.436

pixI = Pixel(536, 478)
pixD = PixelD(536.599, 478.436)
# if you want to use doubles as pixel values you have to use the PixelD() constructor
coord2 = georef.pixel2Coord(pixI)
coord3 = georef.pixel2Coord(pixD)
# because the pixel values are just slightly different, the coordinates 2 and 3 have the same values: -315198.248000,780544.506500

Clone this wiki locally