[GRASS-user] cutting rasters with a vector

Hi everybody,

probably an old and known question:
is there a way in Grass to cut rasters by a polygon (i.e. vector)?

This is possible in Qgis, but not for multiple images (as Landsat bands for example),
while in Grass I could do it on a series of images with r.mapcalc, masking one raster by another one.
It would be comfortable, anyway, to use a polygon sometimes, especially in case of not-rectangular shapes.

Does anyone have a hint?

ciao!

Anna

anna zanchetta wrote:

Hi everybody,

Hi Anna :slight_smile:

probably an old and known question: is there a way in Grass to cut rasters
by a polygon (i.e. vector)?

This is possible in Qgis, but not for multiple images (as Landsat bands for
example), while in Grass I could do it on a series of images with r.mapcalc,
masking one raster by another one. It would be comfortable, anyway, to use
a polygon sometimes, especially in case of not-rectangular shapes.

Does anyone have a hint?

Is it about trimming Landsat borders?

Nikos

Just a shot: rasterize a vector polygon and use the rasterized area (or areas) as mask?
Cheers,
Francesco

···

On Sat, Nov 30, 2013 at 10:21 AM, anna zanchetta <ciupava@gmail.com> wrote:

Hi everybody,

probably an old and known question:
is there a way in Grass to cut rasters by a polygon (i.e. vector)?

This is possible in Qgis, but not for multiple images (as Landsat bands for example),
while in Grass I could do it on a series of images with r.mapcalc, masking one raster by another one.
It would be comfortable, anyway, to use a polygon sometimes, especially in case of not-rectangular shapes.

Does anyone have a hint?

ciao!

Anna


grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

FRANCESCO PIROTTI

CIRGEO Interdepartmental Research Center in Geomatics
TESAF Department of Land, Environment, Agriculture and Forestry

Università di Padova
Via dell’Università, 16
35020 – Legnaro (PD),Italy

Tel: +39 049 827 2710
Cell.: +39 392 395 2067
Email: francesco.pirotti@unipd.it
Skype: francesco197576
Web: http://www.cirgeo.unipd.it/fp

No, it’s about selecting an area inside the tile, which I need to work on.

anyway, since you mentioned it,
the issue of cutting away borders is also interesting …

ciao!

···

On Sat, Nov 30, 2013 at 10:29 AM, Nikos Alexandris <nik@nikosalexandris.net> wrote:

Is it about trimming Landsat borders?

Nikos

Francesco Pirotti UNIPD wrote:

Just a shot: rasterize a vector polygon and use the rasterized area (or
areas) as mask?

I wouldn't think otherwise too. In GRASS GIS 6.4.x you have to go this path
along (v.to.rast, r.mask, r.mapcalc). In GRASS GIS 7, however, you can mask
from a vector directly (i.e. r.mask vector=SomeVectorMap).

Ensure to have set the resolution of the region to the one you desire as an
output. Most of the times it should match the resolution of the raster map(s)
to be masked. Otherwise, a resampling (based on Nearest Neighbour) will take
place.

Nikos

Nikos A:

> Is it about trimming Landsat borders?

Anna Z:

No, it's about selecting an area inside the tile, which I need to work on.

anyway, since you mentioned it, the issue of cutting away borders is also
interesting ...

In short

Attached a custom python script (for GRASS GIS 7 I think) which needs a
Landsat scene to be imported based on a GRASS-Wiki Landsat import script [1]
and the corresponding vector tile to be imported in GRASS as well. If you get
into this I'll try to make it more clear how I used it.

Attached as well, a slightly modified import script, based on the GRASS-Wiki
one, to handle, hopefully all: L5, L7 and L8.

Long story

There is this question: <http://gis.stackexchange.com/q/34637/5256&gt; and a
GRASSy answer: <http://gis.stackexchange.com/a/45268/5256&gt;\. So there is/was
already a grass-addon named i.landsat.trim in
<http://grasswiki.osgeo.org/wiki/AddOns/GRASS_6#i.landsat.trim&gt;\. However, it
is somewhat experimental and not using the exact WRS2 tiles.

Back in May I have tried to give a generic answer to this task by writing a
Python script called "i.landsat.wrs2trim" :-). I imagined that a script could
simply be instructed which tile(s) and row(s) from the official WRS2 tiles [0]
and it should trim all Landsat bands that pertain to an aquisition over the
tile(s) in question.

I ended up, once again, with something kinda hardcoded that would require to
have prepared and have beforehand the vector tile of interest. It is attached.
It works only for "LE7" scenes and only if the bands are imported using the
python script given in GRASS-Wiki [1] (thanks to Martin for this), i.e. they
are named after B1, B2, B3, B4, B5, B61, B62, B8.

Of course (!), one needs to prepare the vector tile for the specific to-be-
trimmed Landsat bands. As far as I remember, this involves getting the WRS2
Shapefile, selecting and saving the tile of interest (in QGIS quicker I think,
load > select > save as...), then importing in GRASS.

The logic is to have Landsat scenes imported in independent Mapsets, that is
all bands of a scene in one Mapset, and all bands of another scene in another
Mapset, et.c. Each Mapset named after the Landsat scenes' ID.

I didn't know how to have available the WRS2 tiles. Let the user download
them and set the path to it, and let the script do its work? Or, make the
script do all this? Packing the WRS2 tiles along with the script somehow?
Bah... Not a programmer here :-/

Oh, thanks to Alexander Muriy for his time at some point for checking the
python script and giving some hints. I just have had no time to work on this
again. Nonetheless, I consider such a tool as a must-have along the other
landsat tools in GRASS.

Nikos
--

[0] <http://landsat.usgs.gov/documents/wrs2_descending.zip&gt;
[1] <http://grasswiki.osgeo.org/wiki/LANDSAT#Automated_data_import&gt;

(attachments)

import_landsat_custom.py (5.49 KB)
trim_landsat_scenes.py (13.6 KB)

The gdal uitlity “gdalwarp” has a -cutline option to clip a raster by a polygon. Maybe that will help. Not directly in GRASS, but it’s easy to script and use within a GRASS session.

···

On 11/30/2013 11:21 AM, anna zanchetta wrote:

Hi everybody,

probably an old and known question:
is there a way in Grass to cut rasters by a polygon (i.e. vector)?

This is possible in Qgis, but not for multiple images (as Landsat bands for example),
while in Grass I could do it on a series of images with r.mapcalc, masking one raster by another one.
It would be comfortable, anyway, to use a polygon sometimes, especially in case of not-rectangular shapes.

Does anyone have a hint?

ciao!

Anna

This mail was received via Mail-SeCure System.

_______________________________________________
grass-user mailing list
[grass-user@lists.osgeo.org](mailto:grass-user@lists.osgeo.org)
[http://lists.osgeo.org/mailman/listinfo/grass-user](http://lists.osgeo.org/mailman/listinfo/grass-user)
This mail was received via Mail-SeCure System.

-- 
Micha Silver
GIS Consultant, Arava Development Co.
[http://www.surfaces.co.il](http://www.surfaces.co.il)

thanks a lot to all of you.as i said, i’ve already performed a serial rasterization in 6.4.x through the path Nikos suggested,
good to know that something for vectors exists in 7.

···

i’ll also have a look at the trimming-borders hints and keep you updated.

Anna

On Sat, Nov 30, 2013 at 11:07 AM, Nikos Alexandris <nik@nikosalexandris.net> wrote:

Francesco Pirotti UNIPD wrote:

Just a shot: rasterize a vector polygon and use the rasterized area (or
areas) as mask?

I wouldn’t think otherwise too. In GRASS GIS 6.4.x you have to go this path
along (v.to.rast, r.mask, r.mapcalc). In GRASS GIS 7, however, you can mask
from a vector directly (i.e. r.mask vector=SomeVectorMap).

Ensure to have set the resolution of the region to the one you desire as an
output. Most of the times it should match the resolution of the raster map(s)
to be masked. Otherwise, a resampling (based on Nearest Neighbour) will take
place.

Nikos