@@ -15,9 +15,8 @@ latlong_to_tilexy <- function(lon_deg, lat_deg, zoom){
15
15
# ' function to get a data.frame of all xyz tiles to download
16
16
# ' @keywords internal
17
17
get_tilexy <- function (bbx ,z ){
18
-
19
- min_tile <- latlong_to_tilexy(bbx [1 ,1 ],bbx [2 ,1 ],z )
20
- max_tile <- latlong_to_tilexy(bbx [1 ,2 ],bbx [2 ,2 ],z )
18
+ min_tile <- latlong_to_tilexy(bbx $ xmin ,bbx $ ymin ,z )
19
+ max_tile <- latlong_to_tilexy(bbx $ xmax ,bbx $ ymax ,z )
21
20
x_all <- seq(from = floor(min_tile [1 ]), to = floor(max_tile [1 ]))
22
21
y_all <- seq(from = floor(min_tile [2 ]), to = floor(max_tile [2 ]))
23
22
@@ -58,21 +57,28 @@ loc_check <- function(locations, prj = NULL){
58
57
} else {
59
58
nfeature <- nrow(locations )
60
59
}
61
-
62
- if (any(class(locations )== " data.frame" )){
60
+ if (all(class(locations )== " data.frame" )){
63
61
if (is.null(prj ) & ! any(class(locations ) %in% c(" sf" , " sfc" , " sfg" ))){
64
62
stop(" Please supply a valid sf crs via locations or prj." )
65
63
}
66
64
67
65
locations <- sf :: st_as_sf(x = locations , coords = c(" x" , " y" ), crs = prj )
68
66
locations $ elevation <- rep(0 , nfeature )
69
67
68
+ } else if (any(class(locations ) %in% c(" sf" , " sfc" , " sfg" ))){
69
+
70
+ sf_crs <- sf :: st_crs(locations )
71
+
72
+ if ((is.null(sf_crs ) | is.na(sf_crs )) & is.null(prj )){
73
+ stop(" Please supply an sf object with a valid crs." )
74
+ }
75
+
70
76
} else if (attributes(class(locations )) %in% c(" raster" )){
71
77
72
78
raster_crs <- raster :: crs(locations )
73
79
74
- if ((is.null(raster_crs ) | is.na(raster_crs )) & is.null( prj ) ){
75
- stop(" Please supply a valid crs via locations or prj." )
80
+ if ((is.null(raster_crs ) | is.na(raster_crs ))){
81
+ stop(" Please supply a valid sf crs via locations or prj." )
76
82
}
77
83
78
84
if (is.null(raster_crs ) | is.na(raster_crs )){
@@ -89,21 +95,25 @@ loc_check <- function(locations, prj = NULL){
89
95
}
90
96
} else if (attributes(class(locations )) %in% c(" raster" )){
91
97
92
- locations <- unique(data.frame (raster :: rasterToPoints(locations )))
93
- locations $ elevation <- vector(" numeric" , nrow(locations ))
94
- locations <- sf :: st_as_sf(x = locations , coords = c(" x" , " y" ),
95
- crs = raster_crs )
98
+ if (sum(! is.na(raster :: getValues(locations ))) == 0 ){
99
+ stop(" No distinct points, all values NA." )
100
+ } else {
101
+ locations <- unique(data.frame (raster :: rasterToPoints(locations )))
102
+ locations $ elevation <- vector(" numeric" , nrow(locations ))
103
+ locations <- sf :: st_as_sf(x = locations , coords = c(" x" , " y" ),
104
+ crs = raster_crs )
105
+ }
96
106
}
97
107
}
98
108
99
- # check for long> 180
100
- lll <- any(grepl(" GEOGCRS" ,sf :: st_crs(prj )) |
101
- grepl(" GEODCRS" , sf :: st_crs(prj )) |
102
- grepl(" GEODETICCRS" , sf :: st_crs(prj )) |
103
- grepl(" GEOGRAPHICCRS" , sf :: st_crs(prj )) |
104
- grepl(" longlat" , sf :: st_crs(prj )) |
105
- grepl(" latlong" , sf :: st_crs(prj )) |
106
- grepl(" 4326" , sf :: st_crs(prj )))
109
+ # check for long > 180
110
+ lll <- any(grepl(" ^ GEOGCRS$ " ,sf :: st_crs(prj )$ wkt ) |
111
+ grepl(" ^ GEODCRS$ " , sf :: st_crs(prj )$ wkt ) |
112
+ grepl(" ^ GEODETICCRS$ " , sf :: st_crs(prj )$ wkt ) |
113
+ grepl(" ^ GEOGRAPHICCRS$ " , sf :: st_crs(prj )$ wkt ) |
114
+ grepl(" ^ longlat$ " , sf :: st_crs(prj )$ wkt ) |
115
+ grepl(" ^ latlong$ " , sf :: st_crs(prj )$ wkt ) |
116
+ grepl(" ^ 4326$ " , sf :: st_crs(prj )$ wkt ))
107
117
if (lll ){
108
118
if (any(sf :: st_coordinates(locations )[,1 ]> 180 )){
109
119
stop(" The elevatr package requires longitude in a range from -180 to 180." )
@@ -133,7 +143,7 @@ proj_expand <- function(locations,prj,expand){
133
143
nfeature <- nrow(locations )
134
144
}
135
145
136
- if (any(sp :: bbox (locations )[2 , ] == 0 ) & lll & is.null(expand )){
146
+ if (any(sf :: st_bbox (locations )[c( " ymin " , " ymax " ) ] == 0 ) & lll & is.null(expand )){
137
147
# Edge case for lat exactly at the equator - was returning NA
138
148
expand <- 0.01
139
149
} else if (nfeature == 1 & lll & is.null(expand )){
@@ -147,17 +157,16 @@ proj_expand <- function(locations,prj,expand){
147
157
mode = " standard" )
148
158
expand <- as.numeric(expand )
149
159
}
150
-
151
- #
160
+
152
161
153
162
if (! is.null(expand )){
154
- # bbx <- methods::as(sf::st_buffer(sf::st_as_sf(bbx), expand), "Spatial")
155
- bbx <- sp :: bbox (locations ) + c(- expand , - expand , expand , expand )
163
+
164
+ bbx <- sf :: st_bbox (locations ) + c(- expand , - expand , expand , expand )
156
165
} else {
157
- bbx <- sp :: bbox (locations )
166
+ bbx <- sf :: st_bbox (locations )
158
167
}
159
- bbx <- bbox_to_sp (bbx , prj = prj )
160
- bbx <- sp :: bbox( sp :: spTransform (bbx , sp :: CRS( ll_geo ) ))
168
+ bbx <- bbox_to_sf (bbx , prj = prj )
169
+ bbx <- sf :: st_bbox( sf :: st_transform (bbx , crs = ll_geo ))
161
170
bbx
162
171
163
172
# sf expand - save for later
@@ -173,37 +182,48 @@ proj_expand <- function(locations,prj,expand){
173
182
# ' @keywords internal
174
183
clip_it <- function (rast , loc , expand , clip ){
175
184
176
- loc_wm <- sp :: spTransform (loc , raster :: crs(rast ))
177
- if (clip == " locations" & ! grepl(" Points " , class(loc_wm ))){
185
+ loc_wm <- sf :: st_transform (loc , crs = raster :: crs(rast ))
186
+ if (clip == " locations" & ! grepl(" sfc_POINT " , class(st_geometry( loc_wm ))[ 1 ] )){
178
187
dem <- raster :: mask(raster :: crop(rast ,loc_wm ), loc_wm )
179
- } else if (clip == " bbox" | grepl(" Points " , class(loc_wm ))){
188
+ } else if (clip == " bbox" | grepl(" sfc_POINT " , class(st_geometry( loc_wm ))[ 1 ] )){
180
189
bbx <- proj_expand(loc_wm , as.character(raster :: crs(rast )), expand )
181
- bbx_sp <- sp :: spTransform(bbox_to_sp (bbx ), raster :: crs(rast ))
182
- dem <- raster :: mask(raster :: crop(rast ,bbx_sp ), bbx_sp )
190
+ bbx_sf <- sf :: st_transform(bbox_to_sf (bbx ), crs = raster :: crs(rast ))
191
+ dem <- raster :: mask(raster :: crop(rast ,bbx_sf ), bbx_sf )
183
192
}
184
193
dem
185
194
}
186
195
187
- # ' Edited from https://github.com/jhollist/quickmapr/blob/master/R/internals.R
196
+ # Edited from https://github.com/jhollist/quickmapr/blob/master/R/internals.R
197
+ # Assumes geographic projection
198
+ # sp bbox to poly
199
+ # @param bbx an sp bbox object
200
+ # @param prj defaults to "EPSG:4326"
201
+ # @keywords internal
202
+ # @importFrom sp wkt
203
+ # bbox_to_sp <- function(bbox, prj = "EPSG:4326") {
204
+ # x <- c(bbox[1, 1], bbox[1, 1], bbox[1, 2], bbox[1, 2], bbox[1, 1])
205
+ # y <- c(bbox[2, 1], bbox[2, 2], bbox[2, 2], bbox[2, 1], bbox[2, 1])
206
+ # p <- sp::Polygon(cbind(x, y))
207
+ # ps <- sp::Polygons(list(p), "p1")
208
+ # if(grepl("+proj", prj)){
209
+ # sp_bbx <- sp::SpatialPolygons(list(ps), 1L,
210
+ # proj4string = sp::CRS(prj))
211
+ # } else {
212
+ # sp_bbx <- sp::SpatialPolygons(list(ps), 1L,
213
+ # proj4string = sp::CRS(SRS_string = prj))
214
+ # }
215
+ # sp_bbx
216
+ # }
217
+
188
218
# ' Assumes geographic projection
189
- # ' sp bbox to poly
190
- # ' @param bbx an sp bbox object
219
+ # ' sf bbox to poly
220
+ # ' @param bbx an sf bbox object
191
221
# ' @param prj defaults to "EPSG:4326"
192
222
# ' @keywords internal
193
- # ' @importFrom sp wkt
194
- bbox_to_sp <- function (bbox , prj = " EPSG:4326" ) {
195
- x <- c(bbox [1 , 1 ], bbox [1 , 1 ], bbox [1 , 2 ], bbox [1 , 2 ], bbox [1 , 1 ])
196
- y <- c(bbox [2 , 1 ], bbox [2 , 2 ], bbox [2 , 2 ], bbox [2 , 1 ], bbox [2 , 1 ])
197
- p <- sp :: Polygon(cbind(x , y ))
198
- ps <- sp :: Polygons(list (p ), " p1" )
199
- if (grepl(" +proj" , prj )){
200
- sp_bbx <- sp :: SpatialPolygons(list (ps ), 1L ,
201
- proj4string = sp :: CRS(prj ))
202
- } else {
203
- sp_bbx <- sp :: SpatialPolygons(list (ps ), 1L ,
204
- proj4string = sp :: CRS(SRS_string = prj ))
205
- }
206
- sp_bbx
223
+ bbox_to_sf <- function (bbox , prj = 4326 ) {
224
+ sf_bbx <- sf :: st_as_sf(sf :: st_as_sfc(bbox ))
225
+ sf_bbx <- sf :: st_transform(sf_bbx , crs = prj )
226
+ sf_bbx
207
227
}
208
228
209
229
# ' Estimate download size of DEMs
@@ -212,13 +232,12 @@ bbox_to_sp <- function(bbox, prj = "EPSG:4326") {
212
232
# ' @param src the src
213
233
# ' @param z zoom level if source is aws
214
234
# ' @keywords internal
215
- # ' @importFrom sp wkt
216
235
estimate_raster_size <- function (locations , prj , src , z = NULL ){
217
236
218
- locations <- bbox_to_sp( sp :: bbox (locations ),
237
+ locations <- bbox_to_sf( sf :: st_bbox (locations ),
219
238
prj = prj )
220
239
221
- locations <- sp :: spTransform (locations , sp :: CRS( SRS_string = " EPSG: 4326" ) )
240
+ locations <- sf :: st_transform (locations , crs = 4326 )
222
241
# Estimated cell size (at equator) from zoom level source
223
242
# https://github.com/tilezen/joerd/blob/master/docs/data-sources.md#sources-native-resolution
224
243
# Each degree at equator = 111319.9 meters
@@ -248,8 +267,8 @@ estimate_raster_size <- function(locations, prj, src, z = NULL){
248
267
alos = 0.0002778 ,
249
268
srtm15plus = 0.004165 )
250
269
}
251
- num_rows <- (sp :: bbox (locations )[ 1 , " max " ] - sp :: bbox (locations )[ 1 , " min " ] )/ res
252
- num_cols <- (sp :: bbox (locations )[ 2 , " max " ] - sp :: bbox (locations )[ 2 , " min " ] )/ res
270
+ num_rows <- (sf :: st_bbox (locations )$ xmax - sf :: st_bbox (locations )$ xmin )/ res
271
+ num_cols <- (sf :: st_bbox (locations )$ ymax - sf :: st_bbox (locations )$ ymin )/ res
253
272
254
273
num_megabytes <- (num_rows * num_cols * bits )/ 8388608
255
274
num_megabytes
0 commit comments