Skip to content

Commit

Permalink
Merge pull request #26 from weiyaw/master
Browse files Browse the repository at this point in the history
Use withr::local_par to modify graphical parameters
  • Loading branch information
filipematias23 authored Jul 30, 2021
2 parents d3ce597 + d5edb3a commit af55532
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 24 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Imports:
plyr,
processx,
grDevices,
spatialEco
spatialEco,
withr
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
Expand Down
2 changes: 1 addition & 1 deletion R/fct_fieldArea.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fieldArea <- function(mosaic, areaValue = 0, fieldShape = NULL, buffer = NULL, n
Out<-list(areaPorcent=porarea)
}
if(plot){
par(mfrow=c(1,1))
withr::local_par(mfrow = c(1, 1))
raster::plot(mosaic, col=grey(1:100/100), axes=FALSE, box=FALSE)}
if(!is.null(fieldShape)){
print("Evaluating the object area percetage per plot...")
Expand Down
2 changes: 1 addition & 1 deletion R/fct_fieldCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fieldCount <- function(mosaic, fieldShape, value = 0, minSize = 0.01, n.core = N
mosaic <- crop(x = mosaic, y = fieldShape)
print("Identifying objects... ")
print(paste("You can speed up this step using n.core=", detectCores(), " or less.", sep = ""))
par(mfrow=c(1,1))
withr::local_par(mfrow = c(1, 1))
raster::plot(mosaic, col=grey(1:100/100), axes=FALSE, box=FALSE, legend=FALSE)
sp::plot(fieldShape, add=T)
names(mosaic)<-"mask"
Expand Down
3 changes: 1 addition & 2 deletions R/fct_fieldCrop.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fieldCrop <- function(mosaic, fieldShape = NULL, nPoint = 4, plot = TRUE, remove
num.band <- length(mosaic@layers)
print(paste(num.band," layers available", sep = ""))
if(nPoint<4|nPoint>50){stop("nPoint must be >= 4 and <= 50")}
par(mfrow=c(1,2))
withr::local_par(mfrow = c(1, 2))
if(is.null(fieldShape)|plot){
if(fast.plot){
raster::plot(mosaic[[1]], col=grey(1:100/100), axes=FALSE, box=FALSE, legend=FALSE)}
Expand Down Expand Up @@ -69,6 +69,5 @@ fieldCrop <- function(mosaic, fieldShape = NULL, nPoint = 4, plot = TRUE, remove
if(!fast.plot){
if(num.band>2){plotRGB(RGB.rescale(r,num.band=3), r = 1, g = 2, b = 3)}
if(num.band<3){raster::plot(r, axes=FALSE, box=FALSE)}}}
par(mfrow=c(1,1))
return(r)
}
7 changes: 4 additions & 3 deletions R/fct_fieldDraw.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fieldDraw <- function(mosaic,line = TRUE, ndraw = 1, dist = FALSE, distSel = 0.5
mosaic <- stack(mosaic)
num.band <- length(mosaic@layers)
print(paste(num.band, " layer available", sep = ""))
par(mfrow=c(1,1))

if(dist){
if(!line){
stop("For dist=T only line=TRUE can be used to evaluate distances")
Expand All @@ -55,7 +55,9 @@ fieldDraw <- function(mosaic,line = TRUE, ndraw = 1, dist = FALSE, distSel = 0.5
if (distSel<=0|distSel>1) {
stop("distSel must be a vlaue between 1 or 0 ")
}
par(mfrow=c(1,2), mai = c(1, 1, 1, 1))
withr::local_par(mfrow = c(1, 2), mai = c(1, 1, 1, 1))
} else {
withr::local_par(mfrow = c(1, 1))
}
if (num.band > 2) {
plotRGB(RGB.rescale(mosaic, num.band = 3), r = 1,
Expand Down Expand Up @@ -116,6 +118,5 @@ fieldDraw <- function(mosaic,line = TRUE, ndraw = 1, dist = FALSE, distSel = 0.5
}
names(Out2)<-paste("Draw",c(1:ndraw),sep="")
if(ndraw==1){Out2<-Out1}
par(mfrow=c(1,1))
return(Out2)
}
13 changes: 8 additions & 5 deletions R/fct_fieldMask.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ fieldMask <- function(mosaic, Red = 1, Green = 2, Blue = 3, RedEdge = NULL, NIR
m<-mr<cropValue
}

par(mfrow=c(1,3))
if(!is.null(DSMmosaic)){
par(mfrow=c(1,4))
if(projection(DSMmosaic)!=projection(mosaic)){stop("DSMmosaic and RGBmosaic must have the same projection CRS, use fieldRotate() in both files.")}}
if (!is.null(DSMmosaic)) {
withr::local_par(mfrow = c(1, 4))
if (projection(DSMmosaic) != projection(mosaic)) {
stop("DSMmosaic and RGBmosaic must have the same projection CRS, use fieldRotate() in both files.")
}
} else {
withr::local_par(mfrow = c(1, 3))
}

if(plot){if(num.band>2){plotRGB(RGB.rescale(mosaic,num.band=3), r = 1, g = 2, b = 3)}
if(num.band<3){raster::plot(mosaic, axes=FALSE, box=FALSE)}
Expand Down Expand Up @@ -123,6 +127,5 @@ fieldMask <- function(mosaic, Red = 1, Green = 2, Blue = 3, RedEdge = NULL, NIR
DSMmosaic <- stack(DSMmosaic)
Out<-list(newMosaic=mosaic,mask=m,DSM=DSMmosaic)
}
par(mfrow=c(1,1))
return(Out)
}
4 changes: 2 additions & 2 deletions R/fct_fieldObject.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fieldObject <- function(mosaic, fieldShape = NULL, minArea = 0.01, areaValue = 0
fieldShape <- SpatialPolygonsDataFrame(fieldShape, data.frame(z = 1))
raster::projection(fieldShape) <- raster::projection(r)}
if (plot) {
par(mfrow=c(1,1))
withr::local_par(mfrow = c(1, 1))
raster::plot(mosaic, col = grey(1:100/100), axes = FALSE,
box = FALSE, legend=FALSE)
sp::plot(fieldShape, add = T)
Expand All @@ -70,7 +70,7 @@ fieldObject <- function(mosaic, fieldShape = NULL, minArea = 0.01, areaValue = 0
mosaic$watershed <- watershed(dd)
n.obj<-unique(values(mosaic$watershed))[-1]
if (plot) {
par(mfrow=c(1,1))
withr::local_par(mfrow = c(1, 1))
raster::plot(mosaic$watershed, col = grey((length(n.obj)+10):1/(length(n.obj)+10)), axes = FALSE,
box = FALSE, add=T, legend=FALSE)
sp::plot(fieldShape, add = T)
Expand Down
3 changes: 1 addition & 2 deletions R/fct_fieldPolygon.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fieldPolygon <- function(mosaic, nPolygon = 1, nPoint = 4, polygonID = NULL, pol
if (nPoint < 4 | nPoint > 50) {
stop("nPoint must be >= 4 and <= 50")
}
par(mfrow = c(1, 2))
withr::local_par(mfrow = c(1, 2))
if (fast.plot) {
raster::plot(mosaic[[1]], col = grey(1:100/100), axes = FALSE,
box = FALSE, legend = FALSE)
Expand Down Expand Up @@ -136,6 +136,5 @@ fieldPolygon <- function(mosaic, nPolygon = 1, nPoint = 4, polygonID = NULL, pol
}
}
}
par(mfrow = c(1, 1))
return(Out)
}
13 changes: 8 additions & 5 deletions R/fct_fieldRotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ fieldRotate <- function(mosaic, theta = NULL, clockwise = TRUE, h = FALSE, n.cor
mosaic <- raster::stack(mosaic)
num.band<-length(mosaic@layers)
print(paste(num.band," layers available", sep = ""))
par(mfrow=c(1,2))
if(!is.null(DSMmosaic)){
par(mfrow=c(1,3))
if(raster::projection(DSMmosaic)!=raster::projection(mosaic)){stop("DSMmosaic and RGBmosaic must have the same projection CRS")}}
if (!is.null(DSMmosaic)) {
withr::local_par(mfrow = c(1, 3))
if (raster::projection(DSMmosaic) != raster::projection(mosaic)) {
stop("DSMmosaic and RGBmosaic must have the same projection CRS")
}
} else {
withr::local_par(mfrow = c(1, 2))
}
if(plot|is.null(theta)){
if(fast.plot){
raster::plot(mosaic[[1]], col=grey(1:100/100), axes=FALSE, box=FALSE, legend=FALSE)}
Expand Down Expand Up @@ -110,6 +114,5 @@ fieldRotate <- function(mosaic, theta = NULL, clockwise = TRUE, h = FALSE, n.cor
}
Out<-list(rotatedMosaic=r,rotatedDSM=DSMmosaic)
}
par(mfrow=c(1,1))
return(Out)
}
3 changes: 1 addition & 2 deletions R/fct_fieldShape.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fieldShape <- function(mosaic, ncols = 10, nrows = 10, nPoint = 4, fieldMap = NU
num.band<-length(mosaic@layers)
print(paste(num.band," layers available", sep = ""))
if(nPoint<4|nPoint>50){stop("nPoint must be >= 4 and <= 50")}
par(mfrow=c(1,2))
withr::local_par(mfrow = c(1, 2))
if(fast.plot){
raster::plot(mosaic[[1]], col=grey(1:100/100), axes=FALSE, box=FALSE, legend=FALSE)}
if(!fast.plot){
Expand Down Expand Up @@ -91,6 +91,5 @@ fieldShape <- function(mosaic, ncols = 10, nrows = 10, nPoint = 4, fieldMap = NU
raster::crs(fieldShape1) <- raster::crs(mosaic)
Out<-list(fieldShape=fieldShape,fieldShapeGIS=fieldShape1,cropField=r)
}
par(mfrow=c(1,1))
return(Out)
}

0 comments on commit af55532

Please sign in to comment.