Skip to content

Commit

Permalink
Fix/final ant 714 (#10)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vargastat authored Aug 31, 2023
1 parent 1c05889 commit 557eaa7
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 181 deletions.
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", role = c("aut", "cre")),
person("Jalal-Edine", "ZAWAM", role = "aut"),
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
10 changes: 6 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- badges: start -->
[![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)
<!-- badges: end -->


Expand Down
224 changes: 77 additions & 147 deletions inst/dataset/build_save_data.R
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)


#------------------------------------------------------------------------------------------#
Expand All @@ -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")
#
plotMapLayout(ml)
Loading

0 comments on commit 557eaa7

Please sign in to comment.