-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathprojection.R
176 lines (145 loc) · 5.43 KB
/
projection.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
## the two crs we use
wmcrs <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"
llcrs <- "+proj=longlat +datum=WGS84 +no_defs"
non_proj_warning <-
"supplied layer has no projection information and is shown without background map"
wrong_proj_warning <-
paste0("projection of supplied layer is not leaflet conform.", "\n",
" projecting to '", llcrs, "'")
# Check and potentially adjust projection of objects to be rendered =======
checkAdjustProjection <- function(x, method = "bilinear") {
x <- switch(class(x)[1],
"RasterLayer" = rasterCheckAdjustProjection(x, method),
"RasterStack" = rasterCheckAdjustProjection(x, method),
"RasterBrick" = rasterCheckAdjustProjection(x, method),
"SpatialPointsDataFrame" = spCheckAdjustProjection(x),
"SpatialPolygonsDataFrame" = spCheckAdjustProjection(x),
"SpatialLinesDataFrame" = spCheckAdjustProjection(x),
"SpatialPoints" = spCheckAdjustProjection(x),
"SpatialPolygons" = spCheckAdjustProjection(x),
"SpatialLines" = spCheckAdjustProjection(x),
"sf" = sfCheckAdjustProjection(x),
"XY" = sfCheckAdjustProjection(x),
"sfc_POINT" = sfCheckAdjustProjection(x),
"sfc_MULTIPOINT" = sfCheckAdjustProjection(x),
"sfc_LINESTRING" = sfCheckAdjustProjection(x),
"sfc_MULTILINESTRING" = sfCheckAdjustProjection(x),
"sfc_POLYGON" = sfCheckAdjustProjection(x),
"sfc_MULTIPOLYGON" = sfCheckAdjustProjection(x),
"sfc_GEOMETRY" = sfCheckAdjustProjection(x),
"sfc_GEOMETRYCOLLECTION" = sfCheckAdjustProjection(x))
return(x)
}
#
# if (class(x)[1] %in% c("RasterLayer", "RasterStack", "RasterBrick")) {
# x <- rasterCheckAdjustProjection(x)
# } else if (class(x)[1] %in% c("SpatialPointsDataFrame",
# "SpatialPolygonsDataFrame",
# "SpatialLinesDataFrame",
# "SpatialPoints",
# "SpatialPolygons",
# "SpatialLines")) {
# x <- spCheckAdjustProjection(x)
# }
#
# return(x)
# }
# Project Raster* objects for mapView =====================================
rasterCheckAdjustProjection <- function(x, method) {
is.fact <- raster::is.factor(x)[1]
if (is.na(raster::projection(x))) {
warning(non_proj_warning)
raster::extent(x) <- scaleExtent(x)
raster::projection(x) <- llcrs
} else if (is.fact) {
x <- raster::projectRaster(
x, raster::projectExtent(x, crs = sp::CRS(wmcrs)),
method = "ngb")
x <- raster::as.factor(x)
} else {
x <- raster::projectRaster(
x, raster::projectExtent(x, crs = sp::CRS(wmcrs)),
method = method)
}
return(x)
}
# Project stars* objects for mapView =====================================
starsCheckAdjustProjection <- function(x, method) {
# is.fact <- raster::is.factor(x)[1]
# if (is.na(raster::projection(x))) {
# warning(non_proj_warning)
# raster::extent(x) <- scaleExtent(x)
# raster::projection(x) <- llcrs
# } else if (is.fact) {
# x <- raster::projectRaster(
# x, raster::projectExtent(x, crs = sp::CRS(wmcrs)),
# method = "ngb")
# x <- raster::as.factor(x)
# } else {
x <- sf::st_transform(
x,
crs = llcrs
)
# }
return(x)
}
# Check and potentially adjust projection of sf objects ===================
sfCheckAdjustProjection <- function(x) {
if (is.na(sf::st_crs(x))) {
return(x) # warning(non_proj_warning)
} else { #if (!validLongLat(sf::st_crs(x)$proj4string)) {
x <- sf::st_transform(x, llcrs)
}
return(x)
}
# Check and potentially adjust projection of Spatial* objects =============
spCheckAdjustProjection <- function(x) {
if (is.na(raster::projection(x))) {
warning(non_proj_warning)
if (class(x)[1] %in% c("SpatialPointsDataFrame", "SpatialPoints")) {
methods::slot(x, "coords") <- scaleCoordinates(coordinates(x)[, 1],
coordinates(x)[, 2])
} else if (class(x)[1] %in% c("SpatialPolygonsDataFrame",
"SpatialPolygons")) {
x <- scalePolygonsCoordinates(x)
} else if (class(x)[1] %in% c("SpatialLinesDataFrame",
"SpatialLines")) {
x <- scaleLinesCoordinates(x)
}
raster::projection(x) <- llcrs
} else if (!identical(raster::projection(x), llcrs)) {
x <- sp::spTransform(x, CRSobj = llcrs)
}
return(x)
}
# Check projection of objects according to their keywords =================
# validLongLat <- function (p4s) {
# proj <- datum <- nodefs <- FALSE
# allWGS84 <- c("+init=epsg:4326", "+proj=longlat", "+datum=WGS84",
# "+no_defs", "+ellps=WGS84", "+towgs84=0,0,0")
#
# p4s_splt = strsplit(p4s, " ")[[1]]
#
# for (comp in allWGS84) {
# if (comp %in% p4s_splt) {
# if (comp == "+init=epsg:4326") {
# proj <- datum <- nodefs <- TRUE
# }
# if (comp == "+proj=longlat") {
# proj <- TRUE
# }
# if (comp == "+no_defs") {
# nodefs <- TRUE
# }
# if (comp == "+datum=WGS84") {
# datum <- TRUE
# }
# }
# }
# if (proj & datum & nodefs) {
# return(TRUE)
# } else {
# warning(wrong_proj_warning)
# return(FALSE)
# }
# }