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

Python API Create a new RasterCoverage

Johannes Kolbe edited this page Jul 29, 2014 · 1 revision

In a lot of cases you may want to create a completely new RasterCoverage and fill it with data. To achieve this, you have to consider some characteristics of RasterCoverages. Which are espescially, that they always need to have a valid GeoReference and a valid DataDefinition attached.

The simplest way to get them is from an existing RasterCoverage, which has in general the same properties as your new one will have, e.g. if you try to get values from a Raster, process them and them write them in the new Raster. In this case it is quite easy:

#existing rasterf
rc = new RasterCoverage("landuse.mpr")

#new raster
rcNew = RasterCoverage()
rcNew.setGeoReference(rc.geoReference())
rcNew.setDatadef(rc.datadef())

If you don't have these values yet, you can create a new DataDefiniton and GeoReference. In the wiki you can find instructions on how to create new DataDefinitons.

So if you now have your new and empty RasterCoverage it is time to fill it with some data. If your file will only have one band this is a really simple procedure and only slightly more difficult for multi bands. The first step in any case is to get a PixelIterator from the raster you want to fill. We simply use the iter() function for this. You can then loop over all values and assign them directly to the PixelIterator, this will automatically set the values in the RasterCoverage the iterator is connected to.

# create e.g. a numpy array and fill it with data from the original raster
it = iter(rc)
a = np.fromiter(it, np.float, it.box().size().linearSize())

#create a new PixelIterator from the empty RaserCoverage
pixIt = iter(rcNew)
#loop over the values and assign them to the iterator
for y in range(rc.size().linearSize()):
    pixIt[y] = a[y]

Now you have a new RasterCoverage with the values you provided. Now we're going to extend this example for multiple bands. We will just build on top of the first example, so we already have the first band and will add some more. For this we need another raster, which will be our temproary storage for the data we're reading before we store it in the rcNew.

We will create an iterator for it by using the .begin() method, which gives us an iterator, that is set right to the beginning of the raster. Than we add a band using the addBand() method. As parameters you will need to pass the number the new band will have and a PixelIterator from which it will read the values.

#create a new RasterCovera as temporary storage location (must have the same properties as the RasterCoverage it gets stored to in the end)
rcTemp = RasterCoverage()
rcTemp.setGeoReference(rc.geoReference())
rcTemp.setDatadef(rc.datadef())

# you can loop this part over all the data you have
pixIt = rcTemp.begin()
#...
# do some calculations, assign values to pixIt, like for the first band
#...
# add the new band with addBand(index, PixelIterator)
#(the already existing first band counts as 0, so this one will have the index 1)
rcNew.addBand(1, rcTemp.begin())

This way you can get a totally new RasterCoverage with values provided by you or other programs.

Clone this wiki locally