Skip to content

Commit a8e4e1e

Browse files
committed
georeference ifn3
1 parent 849d237 commit a8e4e1e

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

NEWS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
- It was returning metadata with the same name always. Now it will be different depending on the dataset that we download, and it will be checked if it exists before downloading it (according to #3).
5050

51-
- Some provinces in IFN4 were incorrectly georreferenced. Now they are correctly georreferenced based on the *Documentador.*
51+
- Some provinces in IFN4 were incorrectly georreferenced. Now they are correctly georreferenced based on the *Documentador.* IFN3 provinces are now also georreferenced
5252

5353
- `fd_landcover_copernicus()` and `fd_canopy_height()` now crop data regardless of the CRS.
5454

R/forest-inventory.R

+57-10
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ fd_inventory_spain <- function(province,
285285
if (!file.exists(metadata_file$filename)) {
286286
download.file(
287287
metadata_file$url,
288-
destfile = metadata_file$filename,
288+
destfile = paste0(path_metadata, "/", metadata_file$filename),
289289
mode = "wb",
290290
quiet = quiet
291291
)
@@ -319,6 +319,7 @@ fd_inventory_spain <- function(province,
319319
filename <- list.files(dir_unzip, full.names = TRUE, pattern = "\\.accdb$")
320320
## 4.2. Connect to DB
321321
conn <- RODBC::odbcConnectAccess2007(filename)
322+
on.exit(RODBC::odbcClose(conn))
322323
## 4.3. Table names
323324
tables_vec <- RODBC::sqlTables(conn) |>
324325
dplyr::filter(TABLE_TYPE != "SYSTEM TABLE") |>
@@ -337,21 +338,67 @@ fd_inventory_spain <- function(province,
337338
if (ifn == 4) {
338339
## get datum code based on province/ccaa
339340
datum <- dplyr::case_when(
340-
province %in% c("Navarra", "Lugo", "A Coruna", "Lugo", "Pontevedra",
341+
province_fix %in% c("Navarra", "Lugo", "A Coruna", "Lugo", "Pontevedra",
341342
"Ourense", "Asturias", "Cantabria", "Murcia", "Baleares",
342343
"Pais Vasco", "La Rioja", "Madrid", "Cataluna") ~ 230,
343-
province %in% c("Canarias") ~ 326,
344+
province_fix %in% c("Canarias") ~ 326,
344345
.default = 258
345346
)
347+
## convert to spatial
348+
data_lst$PCDatosMap_sf <- sf::st_as_sf(
349+
x = data_lst$PCDatosMap,
350+
coords = c("CoorX", "CoorY"),
351+
crs = paste0("EPSG:", datum, data_lst$PCDatosMap$Huso)
352+
)
353+
354+
## IFN 3 - SF column
346355
} else {
347-
datum <- 230
356+
data_lst$PCDatosMap <- data_lst$PCDatosMap |>
357+
dplyr::mutate(
358+
Huso = dplyr::case_when(
359+
## Huso 29
360+
province_fix %in% c("Lugo", "Ourense", "A Coruna", "Pontevedra") ~ 29,
361+
province_fix %in% c("Sevilla", "Badajoz", "Caceres", "Salamanca", "Cadiz",
362+
"Zamora", "Leon", "Asturias", "Huelva") & CoorX > 5e5 ~ 29,
363+
## Huso 28
364+
province_fix %in% c("Santa Cruz De Tenerife", "Las Palmas") ~ 28,
365+
## Huso 31
366+
province_fix %in% c("Islas Baleares", "Barcelona", "Girona", "Lleida", "Tarragona") ~ 31,
367+
province_fix %in% c("Castellon", "Huesca", "Zaragoza",
368+
"Teruel", "Alicante") & CoorX < 5e5 ~ 31,
369+
.default = 30
370+
)
371+
)
372+
## convert to spatial
373+
## some provinces have 2 different CRS (they use topographic map sheets)
374+
husos <- unique(data_lst$PCDatosMap$Huso)
375+
if (length(husos) > 1) {
376+
## they are always 2 different CRS
377+
data_huso_1 <- sf::st_as_sf(
378+
x = data_lst$PCDatosMap[data_lst$PCDatosMap$Huso == husos[1], ],
379+
coords = c("CoorX", "CoorY"),
380+
crs = paste0("EPSG:230", husos[1])
381+
)
382+
383+
data_huso_2 <- sf::st_as_sf(
384+
x = data_lst$PCDatosMap[data_lst$PCDatosMap$Huso == husos[2], ],
385+
coords = c("CoorX", "CoorY"),
386+
crs = paste0("EPSG:230", husos[2])
387+
) |> sf::st_transform(paste0("EPSG:230", husos[1]))
388+
389+
data_lst$PCDatosMap_sf <- rbind(data_huso_1, data_huso_2)
390+
391+
} else {
392+
## for only 1 CRS
393+
data_lst$PCDatosMap_sf <- sf::st_as_sf(
394+
x = data_lst$PCDatosMap,
395+
coords = c("CoorX", "CoorY"),
396+
crs = paste0("EPSG:", datum, data_lst$PCDatosMap$Huso[1])
397+
)
398+
}
399+
348400
}
349-
## convert to spatial
350-
data_lst$PCDatosMap_sf <- sf::st_as_sf(
351-
x = data_lst$PCDatosMap,
352-
coords = c("CoorX", "CoorY"),
353-
crs = paste0("EPSG:", datum, data_lst$PCDatosMap$Huso[1])
354-
)
401+
355402
}
356403
## 4.7. Return results
357404
return(data_lst)

0 commit comments

Comments
 (0)