From 557eaa7fb939d485880e9204692cb8f42fbba5dc Mon Sep 17 00:00:00 2001 From: vargastat <130074062+vargastat@users.noreply.github.com> Date: Thu, 31 Aug 2023 11:53:32 +0200 Subject: [PATCH] Fix/final ant 714 (#10) * rgeos and raster replaced by sf * change path data and maj on name attributes * replaced all deprecated functions, tested with data (inst/). * up version 0.5.0 fix strong deprecated dependencies --- DESCRIPTION | 11 +- NAMESPACE | 4 +- NEWS.md | 9 ++ R/utils.R | 10 +- README.md | 1 + inst/dataset/build_save_data.R | 224 ++++++++++------------------ spMaps.Rproj | 42 +++--- tests/testthat/test-dataset.R | 2 +- tests/testthat/test-getAntaresMap.R | 2 +- 9 files changed, 124 insertions(+), 181 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 662f0ca..9f82983 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: spMaps Type: Package Title: Europe SpatialPolygonsDataFrame Builder -Version: 0.4.1 +Version: 0.5.0 Authors@R: c( person("Tatiana", "Vargas", email = "tatiana.vargas@rte-france.com", role = c("aut", "cre")), person("Jalal-Edine", "ZAWAM", role = "aut"), @@ -16,10 +16,11 @@ URL: https://github.com/rte-antares-rpackage/spMaps BugReports: https://github.com/rte-antares-rpackage/spMaps/issues License: GPL (>= 2) | file LICENSE Encoding: UTF-8 -Depends: R (>= 2.10), - sp, - raster, - rgeos +Depends: + R (>= 2.10), + sf, + methods, + sp (>= 2.0-0) RoxygenNote: 7.2.2 Suggests: testthat, covr, diff --git a/NAMESPACE b/NAMESPACE index be5d29a..04f9800 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,7 +4,7 @@ export(getEuropeCountries) export(getEuropeReferenceTable) export(getEuropeStates) export(getSpMaps) -import(rgeos) import(sp) -importFrom(raster,aggregate) +importFrom(methods,as) +importFrom(sf,st_cast) importFrom(utils,data) diff --git a/NEWS.md b/NEWS.md index 2c2f25a..963830b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,13 @@ +# changes in version 0.5.0 (2023-08-31) + +* rgeos and raster replaced by sf +* add necessary minimal version sp (>= 2.0-0) + +## FIX DEPENDENCIES : +* fixing issue #6 (dependencies on **rgdal**, **rgeos**, and/or **maptools**) + # changes in version 0.4.1 (2023-08-02) + * fix file "LICENCE" format to UTF-8 * fix note on 0.4.0 (lazy data) diff --git a/R/utils.R b/R/utils.R index 09faef3..7cb5087 100644 --- a/R/utils.R +++ b/R/utils.R @@ -12,13 +12,15 @@ getEuropeReferenceTable <- function(){ #' @rdname spMaps #' #' @export -#' -#' @import rgeos -#' @importFrom raster aggregate +#' +#' @importFrom sf st_cast +#' @importFrom methods as getEuropeCountries <- function(mergeCountry = TRUE){ europe_countries_10m if(mergeCountry){ - europe_countries <- raster::aggregate(europe_countries_10m, by = c("code", "admin")) + europe_sf <- sf::st_as_sf(europe_countries_10m) + aggregated_sf <- sf::st_cast(europe_sf, "MULTIPOLYGON", group = c("code", "admin")) + europe_countries <- as(aggregated_sf, "Spatial") europe_countries$name <- europe_countries$admin return(europe_countries) } else { diff --git a/README.md b/README.md index 67ff440..610c177 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [![R-CMD-check](https://github.com/rte-antares-rpackage/spMaps/actions/workflows/check-standard.yaml/badge.svg)](https://github.com/rte-antares-rpackage/spMaps/actions/workflows/check-standard.yaml) [![Codecov test coverage](https://codecov.io/gh/rte-antares-rpackage/spMaps/branch/master/graph/badge.svg)](https://app.codecov.io/gh/rte-antares-rpackage/spMaps?branch=master) +[![CRAN status](https://www.r-pkg.org/badges/version/spMaps)](https://CRAN.R-project.org/package=spMaps) diff --git a/inst/dataset/build_save_data.R b/inst/dataset/build_save_data.R index 207a2b1..1eef729 100644 --- a/inst/dataset/build_save_data.R +++ b/inst/dataset/build_save_data.R @@ -1,131 +1,100 @@ -require(rgdal) +require(sf) require(sp) -# http://www.naturalearthdata.com/downloads/ - -#---------------- -# countries - 50m -#---------------- - -# country_50m <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\50m_cultural", -# layer = "ne_50m_admin_0_countries") -# -# summary(country_50m) -# -# # keep only Europe -# country_50m <- country_50m[country_50m$continent%in% "Europe", ] -# -# # subset on some columns -# europe_countries_50m <- country_50m[, c("name", "name_long", "admin", "adm0_a3", -# "adm0_a3_is","adm0_a3_us", "type", "subunit", -# "su_a3", "pop_est", "pop_year", "continent", "region_un", -# "subregion", "sovereignt")] -# -# summary(europe_countries_50m) -# -# # save as rda -# devtools::use_data(europe_countries_50m, overwrite = T) - -# saveRDS(country_50m, file = "inst/dataset/maps/europe_countries_50m.RDS", overwrite = T) - #---------------- # countries - 10m #---------------- -country_10m_map <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural", - layer = "ne_10m_admin_0_map_units") + +country_10m_map <- sf::st_read(dsn = "D:\\Users\\mahoudiabd\\Downloads\\Datastorm", + layer = "ne_10m_ADMIN_0_map_units") # # country_10m_ref <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural", -# layer = "ne_10m_admin_0_countries") +# layer = "ne_10m_ADMIN_0_countries") -country_10m_map <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\50m_cultural", - layer = "ne_50m_admin_0_map_units") +country_10m_map <- sf::st_read(dsn = "D:\\Users\\mahoudiabd\\Downloads\\Datastorm", + layer = "ne_50m_ADMIN_0_map_units") -# country_10m_ref <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\50m_cultural", -# layer = "ne_50m_admin_0_countries") +# country_10m_ref <- st_read(dsn = "C:\\Users\\Datastorm\\Downloads\\50m_cultural", +# layer = "ne_50m_ADMIN_0_countries") # keep only Europe + Turquie -country_10m <- country_10m_map[country_10m_map$continent%in% "Europe" | - country_10m_map$name_long %in% c("Turkey", "Cyprus"), ] +country_10m <- country_10m_map[country_10m_map$CONTINENT %in% "Europe" | + country_10m_map$NAME_LONG %in% c("Turkey", "Cyprus"), ] summary(country_10m) plot(country_10m) -plot(country_10m[country_10m$name_long %in% "Cyprus",]) +plot(country_10m[country_10m$NAME_LONG %in% "Cyprus",]) # chypre : fusion avec la chypre du nors -country_10m_cyprus <- country_10m_map[country_10m_map$name_long %in% c("Cyprus", "Northern Cyprus"), ] -country_10m_cyprus <- raster::aggregate(country_10m_cyprus, by = c("adm0_a3_is")) -plot(country_10m_cyprus) - -slot(country_10m, "polygons")[[which(country_10m$name_long %in% "Cyprus")]] <- slot(country_10m_cyprus, "polygons")[[1]] -plot(country_10m[country_10m$name_long %in% "Cyprus",]) - -# remove canaries from spain -# plot(country_10m[country_10m$adm0_a3 %in% "ESP",]) - -pols_esp <- slot(country_10m, "polygons")[[which(country_10m$adm0_a3 %in% "ESP")]] +######## -sum_area <- 0 -# min_lat <- NA -# max_lat <- NA -# min_lon <- NA -# max_lon <- NA -keep_polygons <- sapply(country_10m[country_10m$adm0_a3 %in% "ESP", ]@polygons[[1]]@Polygons, function(x){ - # canaries : lattitude < 30 - if(x@labpt[2] > 30){ - sum_area <<- sum_area + x@area - # min_lon <<- min(min_lon, x@coords[, 1], na.rm = T) - # min_lat <<- min(min_lat, x@coords[, 2], na.rm = T) - # max_lon <<- max(max_lon, x@coords[, 1], na.rm = T) - # max_lat <<- max(max_lat, x@coords[, 2], na.rm = T) - } - x@labpt[2]>30 -}) +# Subset data +country_cyprus <- country_10m_map[country_10m_map$NAME_LONG %in% c("Cyprus", "Northern Cyprus"), ]# Perform a union of geometries +union_geometry <- st_union(country_cyprus$geometry)# Replace the geometry of the "Cyprus" feature in the original dataset +country_10m[country_10m$NAME_LONG == "Cyprus", "geometry"] <- union_geometry -# new_bbox <- matrix(c(min_lon, max_lon, min_lat, max_lat), nrow = 2, ncol = 2, byrow = T, -# dimnames = list(c("x", "y"), c("min", "max"))) +# Plot the updated dataset +plot(country_10m[country_10m$NAME_LONG %in% "Cyprus", ]) -new_order <- country_10m[country_10m$adm0_a3 %in% "ESP", ]@polygons[[1]]@plotOrder[which(keep_polygons)] -new_order[order(new_order)] <- 1:length(new_order) -slot(pols_esp, "area") <- sum_area -slot(pols_esp, "plotOrder") <- new_order -slot(pols_esp, "Polygons") <- country_10m[country_10m$adm0_a3 %in% "ESP", ]@polygons[[1]]@Polygons[which(keep_polygons)] +########### -# bug leaflet : have to reset comment... -comment(pols_esp) <- rgeos::createPolygonsComment(pols_esp) +#slot(country_10m, "polygons")[[which(country_10m$NAME_LONG %in% "Cyprus")]] <- slot(country_10m_cyprus, "polygons")[[1]] -slot(country_10m, "polygons")[[which(country_10m$adm0_a3 %in% "ESP")]] <- pols_esp - -plot(country_10m[country_10m$adm0_a3 %in% "ESP",]) +# remove canaries from spain +# plot(country_10m[country_10m$ADM0_A3 %in% "ESP",]) + +pols_esp <- country_10m[country_10m$ADM0_A3 == "ESP", ] + +######### + +# Assuming country_10m is your sf object +spain <- country_10m[country_10m$ADM0_A3 %in% "ESP", ] +# Extract the geometries and calculate sum area +spain_polygons <- spain$geometry +sum_area <- sum(st_area(spain_polygons[st_coordinates(st_centroid(spain_polygons))[ ,2] > 30])) +# Calculate new order +new_order <- seq_along(spain_polygons[st_coordinates(st_centroid(spain_polygons))[ ,2] > 30]) +# Update the order of the geometries +spain_polygons[st_coordinates(st_centroid(spain_polygons))[ ,2] > 30] <- spain_polygons[new_order] +# Update the area attribute +spain$area <- sum_area +# Update the geometry +spain$geometry <- spain_polygons +# Plot the modified polygons +plot(spain) + +######### # subset on columns -europe_countries_10m <- country_10m[, c("name", "admin", "adm0_a3", - "adm0_a3_is","adm0_a3_us", - "type", "subunit", - "continent", "region_un", - "subregion", "sovereignt")] +europe_countries_10m <- country_10m[, c("NAME", "ADMIN", "ADM0_A3", + "ADM0_A3_US", + "TYPE", "SUBUNIT", + "CONTINENT", "REGION_UN", + "SUBREGION", "SOVEREIGNT")] summary(europe_countries_10m) plot(europe_countries_10m) -names(europe_countries_10m) <- gsub("^adm0_a3$", "code", names(europe_countries_10m)) +names(europe_countries_10m) <- gsub("^ADM0_A3$", "CODE", names(europe_countries_10m)) # ref table -europe_countries_ref <- unique(data.frame(europe_countries_10m[, c("admin", "code")], +europe_countries_ref <- unique(data.frame(europe_countries_10m[, c("ADMIN", "CODE")], stringsAsFactors = F)) -colnames(europe_countries_ref) <- c("name", "code") +colnames(europe_countries_ref) <- c("NAME", "CODE") #---------------- # states - 10m #---------------- # -# states_10m <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural", -# layer = "ne_10m_admin_1_states_provinces_shp") +# states_10m <- st_read(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural", +# layer = "ne_10m_ADMIN_1_states_provinces_shp") -states_10m <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural", - layer = "ne_10m_admin_1_states_provinces_lakes_shp") +states_10m <- st_read(dsn = "D:\\Users\\mahoudiabd\\Downloads\\Datastorm", + layer = "ne_10m_ADMIN_1_states_provinces_lakes") # subset on Europe -states_10m_europe <- states_10m[states_10m$sr_adm0_a3%in% europe_countries_10m$code | + +states_10m_europe <- states_10m[states_10m$adm0_a3%in% europe_countries_10m$CODE | states_10m$admin %in% c("Cyprus", "Northern Cyprus"), ] summary(states_10m_europe) @@ -139,45 +108,41 @@ summary(states_10m_europe[states_10m_europe$admin %in% c("Cyprus", "Northern Cyp # chypre : fusion avec la chypre du nors levels(states_10m_europe$admin) <- gsub("Northern Cyprus", "Cyprus", levels(states_10m_europe$admin)) -levels(states_10m_europe$sr_adm0_a3) <- gsub("^CYN$", "CYP", levels(states_10m_europe$sr_adm0_a3)) +levels(states_10m_europe$adm0_a3) <- gsub("^CYN$", "CYP", levels(states_10m_europe$adm0_a3)) plot(states_10m_europe[states_10m_europe$admin %in% c("Cyprus"), ]) # remove islands from france -plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "FRA", ]) -states_10m_europe <- states_10m_europe[!(states_10m_europe$sr_adm0_a3 %in% "FRA" & !states_10m_europe$type_en %in% "Region"), ] -plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "FRA", ]) +plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "FRA", ]) +states_10m_europe <- states_10m_europe[!(states_10m_europe$adm0_a3 %in% "FRA" & !states_10m_europe$type_en %in% "Region"), ] +plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "FRA", ]) # N0R -plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "NOR", ]) -states_10m_europe <- states_10m_europe[!(states_10m_europe$sr_adm0_a3 %in% "NOR" & !states_10m_europe$type_en %in% "County"), ] -plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "NOR", ]) +plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "NOR", ]) +states_10m_europe <- states_10m_europe[!(states_10m_europe$adm0_a3 %in% "NOR" & !states_10m_europe$type_en %in% "County"), ] +plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "NOR", ]) # NLD -plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "NLD", ]) -states_10m_europe <- states_10m_europe[!(states_10m_europe$sr_adm0_a3 %in% "NLD" & !states_10m_europe$type_en %in% "Province"), ] -plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "NLD", ]) - -# ESP -plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "ESP", ]) -states_10m_europe <- states_10m_europe[!(states_10m_europe$sr_adm0_a3 %in% "ESP" & states_10m_europe$name %in% "ESP-00 (Canary Is. aggregation)"), ] -plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "ESP", ]) +plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "NLD", ]) +states_10m_europe <- states_10m_europe[!(states_10m_europe$adm0_a3 %in% "NLD" & !states_10m_europe$type_en %in% "Province"), ] +plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "NLD", ]) -# for(co in europe_countries_ref$code){ +# for(co in europe_countries_ref$CODE){ # print(co) # par(ask = T) -# plot(states_10m_europe[!states_10m_europe$sr_adm0_a3 %in% co, ]) +# plot(states_10m_europe[!states_10m_europe$adm0_a3 %in% co, ]) # } # subset on columns -europe_states_provinces_10m <- states_10m_europe[, c("admin", "sr_adm0_a3", "sr_sov_a3", "adm1_code", +europe_states_provinces_10m <- states_10m_europe[, c("admin", "adm0_a3", "sov_a3", "adm1_code", "name", "type", "type_en", "region")] summary(europe_states_provinces_10m) -names(europe_states_provinces_10m) <- gsub("^sr_adm0_a3$", "code", names(europe_states_provinces_10m)) +names(europe_states_provinces_10m) <- gsub("^adm0_a3$", "code", names(europe_states_provinces_10m)) -devtools::use_data(europe_countries_10m, europe_countries_ref, - europe_states_provinces_10m, internal = TRUE, overwrite = T) + +## Export des tables vers le dossier inst, ne pas lancer si on ne souhaite pas écraser les données. +#devtools::use_data(europe_countries_10m, europe_countries_ref, europe_states_provinces_10m, internal = TRUE, overwrite = T) #------------------------------------------------------------------------------------------# @@ -193,39 +158,4 @@ myData1<-readAntares(areas = "all", links = "all") ml<-mapLayout(readLayout()) plotMap(myData1, ml) -plotMapLayout(ml) - -#------------------- -# Identify leaflet bug with Spain -#------------------- - -# require(leaflet) -# -# map=getSpMaps(countries = c("ESP")) -# -# str(map[map$name %in% 'France',]) -# str(map[map$name %in% 'Spain',]) -# -# s <- sp::polygons(map) -# -# plot(s) -# leaflet(map) %>% addPolygons() -# -# leaflet:::derivePolygons -# leaflet:::polygonData.SpatialPolygons -# leaflet:::polygonData(s) -# -# leaflet:::sp_bbox(s) -# leaflet:::to_multipolygon_list.SpatialPolygons -# -# pgons <- s@polygons[[1]] -# comment(pgons) -# rgeos::createPolygonsComment(pgons) -# leaflet:::to_multipolygon.Polygons -# lapply(pgons@polygons, to_multipolygon) -# -# str(s) -# s@polygons -# pgons = leaflet:::derivePolygons(map, lng = NULL, lat = NULL, T, T, -# "addPolygons") -# \ No newline at end of file +plotMapLayout(ml) \ No newline at end of file diff --git a/spMaps.Rproj b/spMaps.Rproj index 6d85658..aaf31b7 100644 --- a/spMaps.Rproj +++ b/spMaps.Rproj @@ -1,21 +1,21 @@ -Version: 1.0 - -RestoreWorkspace: Default -SaveWorkspace: Default -AlwaysSaveHistory: Default - -EnableCodeIndexing: Yes -UseSpacesForTab: Yes -NumSpacesForTab: 2 -Encoding: ISO8859-1 - -RnwWeave: Sweave -LaTeX: pdfLaTeX - -LineEndingConversion: Posix - -BuildType: Package -PackageUseDevtools: Yes -PackageInstallArgs: --no-multiarch --with-keep.source -PackageCheckArgs: --no-manual -PackageRoxygenize: rd,collate,namespace,vignette +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: ISO8859-1 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +LineEndingConversion: Posix + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageCheckArgs: --no-manual +PackageRoxygenize: rd,collate,namespace,vignette diff --git a/tests/testthat/test-dataset.R b/tests/testthat/test-dataset.R index 9bee484..25c0a31 100644 --- a/tests/testthat/test-dataset.R +++ b/tests/testthat/test-dataset.R @@ -1,4 +1,4 @@ -#Copyright © 2016 RTE Réseau de transport d’électricité +# COPYRIGHT : 2016 RTE Electricity transmission network context("Get dataset") test_that("getEuropeReferenceTable", { diff --git a/tests/testthat/test-getAntaresMap.R b/tests/testthat/test-getAntaresMap.R index a015ce7..bb2e7fe 100644 --- a/tests/testthat/test-getAntaresMap.R +++ b/tests/testthat/test-getAntaresMap.R @@ -1,4 +1,4 @@ -#Copyright © 2016 RTE Réseau de transport d’électricité +# COPYRIGHT : 2016 RTE Electricity transmission network context("Build map") test_that("Default map", {