Cropping maps: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
(+hint)
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Cropping maps means taking the contents of one map (the ''source'') and eliminating all data outside of the area you are studying (the ''boundary'') to yield a smaller new map (the ''cropped map''). Techniques differ depending on whether your source is raster or vector and whether your boundary is defined by another map or by coordinates. You especially might want to do this with vectors because GRASS operations always operate on the whole vector map, even features outside of the current region.
Cropping maps means taking the contents of one map (the ''source'') and eliminating all data outside of the area you are studying (the ''boundary'') to yield a smaller new map (the ''cropped map''). Techniques differ depending on whether your source is raster or vector and whether your boundary is defined by another map or by coordinates. You especially might want to do this with vectors because GRASS operations always operate on the whole vector map, even features outside of the [[current region]].


'''Vector source map''' and '''vector map as boundary'''. Use <code>{{cmd|v.overlay}} operator=and</code>. Example: you have a map of roads of the USA and you want a map of only roads in California. Formerly the module <code>{{cmd|v.cutter}}</code> did this task; it has however been replaced by <code>{{cmd|v.overlay|version=6}}</code> in GRASS release 6.
'''Vector source map''' and '''vector map as boundary'''. Use
{{cmd|v.overlay}} operator=and


'''Vector source map''' and '''coordinates as boundary'''. Set region to desired coordinates (<code>{{cmd|g.region}}</code>), create new vector from region extent to use as boundary using <code>{{cmd|v.in.region}}</code>, then <code>{{cmd|v.overlay}}</code> as above.
Example: you have a map of roads of the USA and you want a map of only roads in California. Formerly the module <tt>v.cutter</tt> did this task; it has however been replaced by {{cmd|v.overlay}} in GRASS release 6.
 
'''Vector source map''' and '''coordinates as boundary'''. Set region to desired coordinates ({{cmd|g.region}}), create new vector from region extent to use as boundary using {{cmd|v.in.region}}, then {{cmd|v.overlay}} as above.


Otherwise, consider to [[Crop a shapefile during import]].
Otherwise, consider to [[Crop a shapefile during import]].


'''Raster source map''' and '''raster map as boundary'''. Make boundary into mask using <code>{{cmd|r.mask}}</code> then copy raster source to new map (<code>{{cmd|g.copy}}</code>). All cells outside of the mask will be omitted from the new copy. Alternately, use <code>{{cmd|r.mapcalc}}</code>:
'''Raster source map''' and '''vector map as boundary'''. Make boundary into mask using {{cmd|r.mask}} then use {{cmd|r.mapcalc}}:
 
{{cmd|r.mask}} vector=boundary
{{cmd|r.mapcalc}} "cropped = raster_source"
{{cmd|r.colors}} map=cropped rast=source # may be required to transfer the color table
 
The same procedure can be followed for a raster map as boundary. However, with a raster map one can easily just use r.mapcalc in one single go, without using a mask:
 
{{cmd|r.mapcalc}} "cropped = if(boundary, raster_source)"
 
The size of the resulting cropped raster map will be the same as that of raster_source as this is defined by the computational region, but all cells outside the mask will be set to NULL. If you want to reduce the size of the raster map you can use {{cmd|r.mask}}, {{cmd|g.region}} with the zoom parameter, and then r.mapcalc. Here an example with the North Carolina Demo dataset and a vector boundary:
 
{{cmd|g.region}} rast=elev_state_500m -p
{{cmd|r.mask}} vector=census_wake2000
{{cmd|g.region}} zoom=elev_state_500m -p # will reduce the region to the non-null part of elev_state_500m, taking the mask into account
{{cmd|r.mapcalc}} "cropped = elev_state_500m"
{{cmd|r.mask}} -r


:{{cmd|r.mapcalc}} "cropped = if(boundary,raster_source)"
:{{cmd|r.colors}} map=cropped rast=source # may be required to transfer the color table


'''Raster source map''' and '''vector boundary'''. Convert the vector to a raster mask (1 where inside, null elsewhere):
'''Raster source map''' and '''vector boundary'''. Convert the vector to a raster mask (1 where inside, null elsewhere):


:{{cmd|v.to.rast}} in=vector_boundary out=raster_boundary use=val value=1
{{cmd|v.to.rast}} in=vector_boundary out=raster_boundary use=val value=1


Then proceed as above for two rasters.
Then proceed as above for two rasters.
=== Update for GRASS GIS 7 ===
* Simply use {{cmd|r.mask}} which also supports vector maps nowadays.


[[Category:FAQ]]
[[Category:FAQ]]

Latest revision as of 14:35, 26 September 2017

Cropping maps means taking the contents of one map (the source) and eliminating all data outside of the area you are studying (the boundary) to yield a smaller new map (the cropped map). Techniques differ depending on whether your source is raster or vector and whether your boundary is defined by another map or by coordinates. You especially might want to do this with vectors because GRASS operations always operate on the whole vector map, even features outside of the current region.

Vector source map and vector map as boundary. Use

v.overlay operator=and

Example: you have a map of roads of the USA and you want a map of only roads in California. Formerly the module v.cutter did this task; it has however been replaced by v.overlay in GRASS release 6.

Vector source map and coordinates as boundary. Set region to desired coordinates (g.region), create new vector from region extent to use as boundary using v.in.region, then v.overlay as above.

Otherwise, consider to Crop a shapefile during import.

Raster source map and vector map as boundary. Make boundary into mask using r.mask then use r.mapcalc:

r.mask vector=boundary
r.mapcalc "cropped = raster_source"
r.colors map=cropped rast=source # may be required to transfer the color table

The same procedure can be followed for a raster map as boundary. However, with a raster map one can easily just use r.mapcalc in one single go, without using a mask:

r.mapcalc "cropped = if(boundary, raster_source)"

The size of the resulting cropped raster map will be the same as that of raster_source as this is defined by the computational region, but all cells outside the mask will be set to NULL. If you want to reduce the size of the raster map you can use r.mask, g.region with the zoom parameter, and then r.mapcalc. Here an example with the North Carolina Demo dataset and a vector boundary:

g.region rast=elev_state_500m -p
r.mask vector=census_wake2000
g.region zoom=elev_state_500m -p # will reduce the region to the non-null part of elev_state_500m, taking the mask into account
r.mapcalc "cropped = elev_state_500m"
r.mask -r


Raster source map and vector boundary. Convert the vector to a raster mask (1 where inside, null elsewhere):

v.to.rast in=vector_boundary out=raster_boundary use=val value=1

Then proceed as above for two rasters.

Update for GRASS GIS 7

  • Simply use r.mask which also supports vector maps nowadays.