Description Usage Arguments Details Value Examples
View source: R/extractFields.R
Extracts and vectorizes clumps of pixels with equal value within a raster object.
1 | extractFields(x, method = "simple", smooth.x = FALSE)
|
x |
Object of class RasterLayer. |
method |
One of "simple" or "complex". |
smooth.x |
A logical argument. |
Assuming x is a classified or segmented image, this function segments it using ccLabel and, draws polygons for each group of connected pixels. The way polygons are drawn depends on the method keyword. The function will accept one of the following options:
simple - Extracts the center pixel coordinates and builds a polygon based on their minimum convex hull.
complex - Extracts the center pixel coordinates and builds a polygon based on their minimum concave hull.
The "simple" approach is a faster but it can lead to poor results when dealing with very complex shapes. For example, crop fields can be rectangular in which case the "simple" method is sufficient. On the other hand, forest belts can have irregular shapes, in which case the "complex" method is more appropriate. By default, the function will use all pixels associated to a region. However, if smooth.x is set to TRUE, for each region, the function will remove samples with less than 5 neighbors efectively removing isolated pixels along the edge of a region. The final output is a SpatialPolygonsDataFrame reporting on:
region.id - Unique polygon identifier corresponding to the original pixel region.
area - Polygon Area (in square meters).
perimeter - Polygon perimeter (in meters).
cover.ratio - Ration between the polygon area and the area of the corresponding pixels.
A SpatialPolygonsDataFrame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | {
require(raster)
# read raster data
r <- brick(system.file("extdata", "ndvi.tif", package="fieldRS"))
# spatial change labeling
or <- ccLabel(r, method="temporal", change.threshold=-50)$regions
# convert to polygons and plot (simple)
ef <- extractFields(or[10:30,10:30, drop=FALSE])
plot(ef)
# convert to polygons and plot (complex)
ef <- extractFields(or[10:30,1:30, drop=FALSE], method="complex")
plot(ef, border="red", add=TRUE)
# convert to polygons and plot (complex and smoothed)
ef <- extractFields(or[10:30,1:30, drop=FALSE], method="complex", smooth.x=TRUE)
plot(ef, border="blue", add=TRUE)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.