Skip to content

Commit 557eaa7

Browse files
authored
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
1 parent 1c05889 commit 557eaa7

File tree

9 files changed

+124
-181
lines changed

9 files changed

+124
-181
lines changed

DESCRIPTION

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: spMaps
22
Type: Package
33
Title: Europe SpatialPolygonsDataFrame Builder
4-
Version: 0.4.1
4+
Version: 0.5.0
55
Authors@R: c(
66
person("Tatiana", "Vargas", email = "tatiana.vargas@rte-france.com", role = c("aut", "cre")),
77
person("Jalal-Edine", "ZAWAM", role = "aut"),
@@ -16,10 +16,11 @@ URL: https://github.com/rte-antares-rpackage/spMaps
1616
BugReports: https://github.com/rte-antares-rpackage/spMaps/issues
1717
License: GPL (>= 2) | file LICENSE
1818
Encoding: UTF-8
19-
Depends: R (>= 2.10),
20-
sp,
21-
raster,
22-
rgeos
19+
Depends:
20+
R (>= 2.10),
21+
sf,
22+
methods,
23+
sp (>= 2.0-0)
2324
RoxygenNote: 7.2.2
2425
Suggests: testthat,
2526
covr,

NAMESPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export(getEuropeCountries)
44
export(getEuropeReferenceTable)
55
export(getEuropeStates)
66
export(getSpMaps)
7-
import(rgeos)
87
import(sp)
9-
importFrom(raster,aggregate)
8+
importFrom(methods,as)
9+
importFrom(sf,st_cast)
1010
importFrom(utils,data)

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
# changes in version 0.5.0 (2023-08-31)
2+
3+
* rgeos and raster replaced by sf
4+
* add necessary minimal version sp (>= 2.0-0)
5+
6+
## FIX DEPENDENCIES :
7+
* fixing issue #6 (dependencies on **rgdal**, **rgeos**, and/or **maptools**)
8+
19
# changes in version 0.4.1 (2023-08-02)
10+
211
* fix file "LICENCE" format to UTF-8
312
* fix note on 0.4.0 (lazy data)
413

R/utils.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ getEuropeReferenceTable <- function(){
1212
#' @rdname spMaps
1313
#'
1414
#' @export
15-
#'
16-
#' @import rgeos
17-
#' @importFrom raster aggregate
15+
#'
16+
#' @importFrom sf st_cast
17+
#' @importFrom methods as
1818
getEuropeCountries <- function(mergeCountry = TRUE){
1919
europe_countries_10m
2020
if(mergeCountry){
21-
europe_countries <- raster::aggregate(europe_countries_10m, by = c("code", "admin"))
21+
europe_sf <- sf::st_as_sf(europe_countries_10m)
22+
aggregated_sf <- sf::st_cast(europe_sf, "MULTIPOLYGON", group = c("code", "admin"))
23+
europe_countries <- as(aggregated_sf, "Spatial")
2224
europe_countries$name <- europe_countries$admin
2325
return(europe_countries)
2426
} else {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<!-- badges: start -->
66
[![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)
77
[![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)
8+
[![CRAN status](https://www.r-pkg.org/badges/version/spMaps)](https://CRAN.R-project.org/package=spMaps)
89
<!-- badges: end -->
910

1011

inst/dataset/build_save_data.R

Lines changed: 77 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,100 @@
1-
require(rgdal)
1+
require(sf)
22
require(sp)
33

4-
# http://www.naturalearthdata.com/downloads/
5-
6-
#----------------
7-
# countries - 50m
8-
#----------------
9-
10-
# country_50m <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\50m_cultural",
11-
# layer = "ne_50m_admin_0_countries")
12-
#
13-
# summary(country_50m)
14-
#
15-
# # keep only Europe
16-
# country_50m <- country_50m[country_50m$continent%in% "Europe", ]
17-
#
18-
# # subset on some columns
19-
# europe_countries_50m <- country_50m[, c("name", "name_long", "admin", "adm0_a3",
20-
# "adm0_a3_is","adm0_a3_us", "type", "subunit",
21-
# "su_a3", "pop_est", "pop_year", "continent", "region_un",
22-
# "subregion", "sovereignt")]
23-
#
24-
# summary(europe_countries_50m)
25-
#
26-
# # save as rda
27-
# devtools::use_data(europe_countries_50m, overwrite = T)
28-
29-
# saveRDS(country_50m, file = "inst/dataset/maps/europe_countries_50m.RDS", overwrite = T)
30-
314
#----------------
325
# countries - 10m
336
#----------------
347

35-
country_10m_map <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural",
36-
layer = "ne_10m_admin_0_map_units")
8+
9+
country_10m_map <- sf::st_read(dsn = "D:\\Users\\mahoudiabd\\Downloads\\Datastorm",
10+
layer = "ne_10m_ADMIN_0_map_units")
3711
#
3812
# country_10m_ref <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural",
39-
# layer = "ne_10m_admin_0_countries")
13+
# layer = "ne_10m_ADMIN_0_countries")
4014

41-
country_10m_map <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\50m_cultural",
42-
layer = "ne_50m_admin_0_map_units")
15+
country_10m_map <- sf::st_read(dsn = "D:\\Users\\mahoudiabd\\Downloads\\Datastorm",
16+
layer = "ne_50m_ADMIN_0_map_units")
4317

44-
# country_10m_ref <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\50m_cultural",
45-
# layer = "ne_50m_admin_0_countries")
18+
# country_10m_ref <- st_read(dsn = "C:\\Users\\Datastorm\\Downloads\\50m_cultural",
19+
# layer = "ne_50m_ADMIN_0_countries")
4620
# keep only Europe + Turquie
47-
country_10m <- country_10m_map[country_10m_map$continent%in% "Europe" |
48-
country_10m_map$name_long %in% c("Turkey", "Cyprus"), ]
21+
country_10m <- country_10m_map[country_10m_map$CONTINENT %in% "Europe" |
22+
country_10m_map$NAME_LONG %in% c("Turkey", "Cyprus"), ]
4923
summary(country_10m)
5024

5125
plot(country_10m)
52-
plot(country_10m[country_10m$name_long %in% "Cyprus",])
26+
plot(country_10m[country_10m$NAME_LONG %in% "Cyprus",])
5327

5428
# chypre : fusion avec la chypre du nors
55-
country_10m_cyprus <- country_10m_map[country_10m_map$name_long %in% c("Cyprus", "Northern Cyprus"), ]
56-
country_10m_cyprus <- raster::aggregate(country_10m_cyprus, by = c("adm0_a3_is"))
57-
plot(country_10m_cyprus)
58-
59-
slot(country_10m, "polygons")[[which(country_10m$name_long %in% "Cyprus")]] <- slot(country_10m_cyprus, "polygons")[[1]]
60-
plot(country_10m[country_10m$name_long %in% "Cyprus",])
61-
62-
# remove canaries from spain
63-
# plot(country_10m[country_10m$adm0_a3 %in% "ESP",])
64-
65-
pols_esp <- slot(country_10m, "polygons")[[which(country_10m$adm0_a3 %in% "ESP")]]
29+
########
6630

67-
sum_area <- 0
68-
# min_lat <- NA
69-
# max_lat <- NA
70-
# min_lon <- NA
71-
# max_lon <- NA
72-
keep_polygons <- sapply(country_10m[country_10m$adm0_a3 %in% "ESP", ]@polygons[[1]]@Polygons, function(x){
73-
# canaries : lattitude < 30
74-
if(x@labpt[2] > 30){
75-
sum_area <<- sum_area + x@area
76-
# min_lon <<- min(min_lon, x@coords[, 1], na.rm = T)
77-
# min_lat <<- min(min_lat, x@coords[, 2], na.rm = T)
78-
# max_lon <<- max(max_lon, x@coords[, 1], na.rm = T)
79-
# max_lat <<- max(max_lat, x@coords[, 2], na.rm = T)
80-
}
81-
x@labpt[2]>30
82-
})
31+
# Subset data
32+
country_cyprus <- country_10m_map[country_10m_map$NAME_LONG %in% c("Cyprus", "Northern Cyprus"), ]# Perform a union of geometries
33+
union_geometry <- st_union(country_cyprus$geometry)# Replace the geometry of the "Cyprus" feature in the original dataset
34+
country_10m[country_10m$NAME_LONG == "Cyprus", "geometry"] <- union_geometry
8335

84-
# new_bbox <- matrix(c(min_lon, max_lon, min_lat, max_lat), nrow = 2, ncol = 2, byrow = T,
85-
# dimnames = list(c("x", "y"), c("min", "max")))
36+
# Plot the updated dataset
37+
plot(country_10m[country_10m$NAME_LONG %in% "Cyprus", ])
8638

87-
new_order <- country_10m[country_10m$adm0_a3 %in% "ESP", ]@polygons[[1]]@plotOrder[which(keep_polygons)]
88-
new_order[order(new_order)] <- 1:length(new_order)
8939

90-
slot(pols_esp, "area") <- sum_area
91-
slot(pols_esp, "plotOrder") <- new_order
92-
slot(pols_esp, "Polygons") <- country_10m[country_10m$adm0_a3 %in% "ESP", ]@polygons[[1]]@Polygons[which(keep_polygons)]
40+
###########
9341

94-
# bug leaflet : have to reset comment...
95-
comment(pols_esp) <- rgeos::createPolygonsComment(pols_esp)
42+
#slot(country_10m, "polygons")[[which(country_10m$NAME_LONG %in% "Cyprus")]] <- slot(country_10m_cyprus, "polygons")[[1]]
9643

97-
slot(country_10m, "polygons")[[which(country_10m$adm0_a3 %in% "ESP")]] <- pols_esp
98-
99-
plot(country_10m[country_10m$adm0_a3 %in% "ESP",])
44+
# remove canaries from spain
45+
# plot(country_10m[country_10m$ADM0_A3 %in% "ESP",])
46+
47+
pols_esp <- country_10m[country_10m$ADM0_A3 == "ESP", ]
48+
49+
#########
50+
51+
# Assuming country_10m is your sf object
52+
spain <- country_10m[country_10m$ADM0_A3 %in% "ESP", ]
53+
# Extract the geometries and calculate sum area
54+
spain_polygons <- spain$geometry
55+
sum_area <- sum(st_area(spain_polygons[st_coordinates(st_centroid(spain_polygons))[ ,2] > 30]))
56+
# Calculate new order
57+
new_order <- seq_along(spain_polygons[st_coordinates(st_centroid(spain_polygons))[ ,2] > 30])
58+
# Update the order of the geometries
59+
spain_polygons[st_coordinates(st_centroid(spain_polygons))[ ,2] > 30] <- spain_polygons[new_order]
60+
# Update the area attribute
61+
spain$area <- sum_area
62+
# Update the geometry
63+
spain$geometry <- spain_polygons
64+
# Plot the modified polygons
65+
plot(spain)
66+
67+
#########
10068

10169
# subset on columns
102-
europe_countries_10m <- country_10m[, c("name", "admin", "adm0_a3",
103-
"adm0_a3_is","adm0_a3_us",
104-
"type", "subunit",
105-
"continent", "region_un",
106-
"subregion", "sovereignt")]
70+
europe_countries_10m <- country_10m[, c("NAME", "ADMIN", "ADM0_A3",
71+
"ADM0_A3_US",
72+
"TYPE", "SUBUNIT",
73+
"CONTINENT", "REGION_UN",
74+
"SUBREGION", "SOVEREIGNT")]
10775
summary(europe_countries_10m)
10876
plot(europe_countries_10m)
10977

110-
names(europe_countries_10m) <- gsub("^adm0_a3$", "code", names(europe_countries_10m))
78+
names(europe_countries_10m) <- gsub("^ADM0_A3$", "CODE", names(europe_countries_10m))
11179

11280
# ref table
113-
europe_countries_ref <- unique(data.frame(europe_countries_10m[, c("admin", "code")],
81+
europe_countries_ref <- unique(data.frame(europe_countries_10m[, c("ADMIN", "CODE")],
11482
stringsAsFactors = F))
115-
colnames(europe_countries_ref) <- c("name", "code")
83+
colnames(europe_countries_ref) <- c("NAME", "CODE")
11684

11785

11886
#----------------
11987
# states - 10m
12088
#----------------
12189
#
122-
# states_10m <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural",
123-
# layer = "ne_10m_admin_1_states_provinces_shp")
90+
# states_10m <- st_read(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural",
91+
# layer = "ne_10m_ADMIN_1_states_provinces_shp")
12492

125-
states_10m <- readOGR(dsn = "C:\\Users\\Datastorm\\Downloads\\10m_cultural\\10m_cultural",
126-
layer = "ne_10m_admin_1_states_provinces_lakes_shp")
93+
states_10m <- st_read(dsn = "D:\\Users\\mahoudiabd\\Downloads\\Datastorm",
94+
layer = "ne_10m_ADMIN_1_states_provinces_lakes")
12795
# subset on Europe
128-
states_10m_europe <- states_10m[states_10m$sr_adm0_a3%in% europe_countries_10m$code |
96+
97+
states_10m_europe <- states_10m[states_10m$adm0_a3%in% europe_countries_10m$CODE |
12998
states_10m$admin %in% c("Cyprus", "Northern Cyprus"), ]
13099
summary(states_10m_europe)
131100

@@ -139,45 +108,41 @@ summary(states_10m_europe[states_10m_europe$admin %in% c("Cyprus", "Northern Cyp
139108

140109
# chypre : fusion avec la chypre du nors
141110
levels(states_10m_europe$admin) <- gsub("Northern Cyprus", "Cyprus", levels(states_10m_europe$admin))
142-
levels(states_10m_europe$sr_adm0_a3) <- gsub("^CYN$", "CYP", levels(states_10m_europe$sr_adm0_a3))
111+
levels(states_10m_europe$adm0_a3) <- gsub("^CYN$", "CYP", levels(states_10m_europe$adm0_a3))
143112

144113
plot(states_10m_europe[states_10m_europe$admin %in% c("Cyprus"), ])
145114

146115
# remove islands from france
147-
plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "FRA", ])
148-
states_10m_europe <- states_10m_europe[!(states_10m_europe$sr_adm0_a3 %in% "FRA" & !states_10m_europe$type_en %in% "Region"), ]
149-
plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "FRA", ])
116+
plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "FRA", ])
117+
states_10m_europe <- states_10m_europe[!(states_10m_europe$adm0_a3 %in% "FRA" & !states_10m_europe$type_en %in% "Region"), ]
118+
plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "FRA", ])
150119

151120
# N0R
152-
plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "NOR", ])
153-
states_10m_europe <- states_10m_europe[!(states_10m_europe$sr_adm0_a3 %in% "NOR" & !states_10m_europe$type_en %in% "County"), ]
154-
plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "NOR", ])
121+
plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "NOR", ])
122+
states_10m_europe <- states_10m_europe[!(states_10m_europe$adm0_a3 %in% "NOR" & !states_10m_europe$type_en %in% "County"), ]
123+
plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "NOR", ])
155124

156125
# NLD
157-
plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "NLD", ])
158-
states_10m_europe <- states_10m_europe[!(states_10m_europe$sr_adm0_a3 %in% "NLD" & !states_10m_europe$type_en %in% "Province"), ]
159-
plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "NLD", ])
160-
161-
# ESP
162-
plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "ESP", ])
163-
states_10m_europe <- states_10m_europe[!(states_10m_europe$sr_adm0_a3 %in% "ESP" & states_10m_europe$name %in% "ESP-00 (Canary Is. aggregation)"), ]
164-
plot(states_10m_europe[states_10m_europe$sr_adm0_a3 %in% "ESP", ])
126+
plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "NLD", ])
127+
states_10m_europe <- states_10m_europe[!(states_10m_europe$adm0_a3 %in% "NLD" & !states_10m_europe$type_en %in% "Province"), ]
128+
plot(states_10m_europe[states_10m_europe$adm0_a3 %in% "NLD", ])
165129

166-
# for(co in europe_countries_ref$code){
130+
# for(co in europe_countries_ref$CODE){
167131
# print(co)
168132
# par(ask = T)
169-
# plot(states_10m_europe[!states_10m_europe$sr_adm0_a3 %in% co, ])
133+
# plot(states_10m_europe[!states_10m_europe$adm0_a3 %in% co, ])
170134
# }
171135

172136
# subset on columns
173-
europe_states_provinces_10m <- states_10m_europe[, c("admin", "sr_adm0_a3", "sr_sov_a3", "adm1_code",
137+
europe_states_provinces_10m <- states_10m_europe[, c("admin", "adm0_a3", "sov_a3", "adm1_code",
174138
"name", "type", "type_en", "region")]
175139
summary(europe_states_provinces_10m)
176140

177-
names(europe_states_provinces_10m) <- gsub("^sr_adm0_a3$", "code", names(europe_states_provinces_10m))
141+
names(europe_states_provinces_10m) <- gsub("^adm0_a3$", "code", names(europe_states_provinces_10m))
178142

179-
devtools::use_data(europe_countries_10m, europe_countries_ref,
180-
europe_states_provinces_10m, internal = TRUE, overwrite = T)
143+
144+
## Export des tables vers le dossier inst, ne pas lancer si on ne souhaite pas écraser les données.
145+
#devtools::use_data(europe_countries_10m, europe_countries_ref, europe_states_provinces_10m, internal = TRUE, overwrite = T)
181146

182147

183148
#------------------------------------------------------------------------------------------#
@@ -193,39 +158,4 @@ myData1<-readAntares(areas = "all", links = "all")
193158
ml<-mapLayout(readLayout())
194159
plotMap(myData1, ml)
195160

196-
plotMapLayout(ml)
197-
198-
#-------------------
199-
# Identify leaflet bug with Spain
200-
#-------------------
201-
202-
# require(leaflet)
203-
#
204-
# map=getSpMaps(countries = c("ESP"))
205-
#
206-
# str(map[map$name %in% 'France',])
207-
# str(map[map$name %in% 'Spain',])
208-
#
209-
# s <- sp::polygons(map)
210-
#
211-
# plot(s)
212-
# leaflet(map) %>% addPolygons()
213-
#
214-
# leaflet:::derivePolygons
215-
# leaflet:::polygonData.SpatialPolygons
216-
# leaflet:::polygonData(s)
217-
#
218-
# leaflet:::sp_bbox(s)
219-
# leaflet:::to_multipolygon_list.SpatialPolygons
220-
#
221-
# pgons <- s@polygons[[1]]
222-
# comment(pgons)
223-
# rgeos::createPolygonsComment(pgons)
224-
# leaflet:::to_multipolygon.Polygons
225-
# lapply(pgons@polygons, to_multipolygon)
226-
#
227-
# str(s)
228-
# s@polygons
229-
# pgons = leaflet:::derivePolygons(map, lng = NULL, lat = NULL, T, T,
230-
# "addPolygons")
231-
#
161+
plotMapLayout(ml)

0 commit comments

Comments
 (0)