Is there a way in R to create lines (black lines in the figure below) along the edge between two raster cells that have different values ?
Update
I'm trying the functions clump()
and rasterToPolygons()
as in the post [https://stackoverflow.com/questions/28859181/how-to-get-contour-lines-around-the-grids-in-r-raster].
First, I reclassified my raster "r" to have cells with two different values:
new.values <- cbind(c(0, 1, 2, 3, 4, 5, 6, 7, 8), c(NA, NA, 1, NA, 2, NA, NA, NA, NA))
new.r <- reclassify(r, rcl=new.values)
Then, I used the functions rasterToPolygons
and clump
:
test.edge <- rasterToPolygons(clump(new.r), dissolve=TRUE)
plot(test.edge, add=TRUE)
And I obtained:
How can I extract the edges that have not contour lines (for example, edges near red arrows) because they are located between two different values?
Best Answer
I think it is not wise to mix the
clump(..)
functionality fromigraph
with thedissolve=TRUE
parameter from therasterToPolygon
routine. They both do something with to aggregate the fields together but in a different way. At least we want to do 3 things:In the
clump
code raster, the type of raster and the definition ofNA
seems to be important to steer clumping the process. I made some test but with bad results. I followed your sketch and here is a little analysis how to get things work:Is the raster field well constructed?
Here is the function prototype that selects the field in the interval
[1,2)
.The contour cannot be dissolved (
clump
) because of the float number nature of the raster field is distinct in the ID process (dissolve-=TRUE
).You see the right contour groups, but polygons are not joint.
So if we have a unique ID of each cell as Integer, the dissolve process should work.
Conclusion: I think (do know not exactly) what behind the
clump
stuff works a raster based region growing routine. The processdissolve=TRUE
in the rasterToPolygon (based on rgeos CRAN) seems to follow a vector approach. So I've to read the manuals ofigraph
andrgeos
carfully.REM: The selection of contours (float vs. int) differs, because of the nature of (ceiling, floor and round).