Skip to content

Commit

Permalink
Refactor code to use esp_move_can
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Jan 15, 2024
1 parent 323c8fa commit 57d4c19
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 196 deletions.
47 changes: 9 additions & 38 deletions R/esp_get_can_box.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' Islands.
#'
#' @family political
#' @family canary
#'
#' @seealso [esp_move_can()]
#' @rdname esp_get_can_box
#'
Expand Down Expand Up @@ -154,25 +156,8 @@ esp_get_can_box <- function(style = "right",
lall <- sf::st_sfc(lall, crs = sf::st_crs(can))
}

moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1

if (moving) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

can <- sf::st_transform(lall, 3857) + offset
can <- sf::st_sfc(can, crs = 3857)
can <- sf::st_transform(can, sf::st_crs(lall))
lall <- can
}
# Move
lall <- esp_move_can(lall, moveCAN = moveCAN)

# Transform

Expand All @@ -188,6 +173,9 @@ esp_get_can_box <- function(style = "right",
#' * [esp_get_can_provinces()] is used to draw a separator line between the two
#' provinces of the Canary Islands.
#'
#'
#' See also [esp_move_can()] to align stand-alone \CRANpkg{sf} objects.
#'
#' @return `esp_get_can_provinces` returns a `LINESTRING` object.
#'
#' @source
Expand All @@ -212,25 +200,8 @@ esp_get_can_provinces <- function(moveCAN = TRUE,
lall <- sf::st_linestring(sf::st_coordinates(m))
lall <- sf::st_sfc(lall, crs = sf::st_crs(4326))

moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1

if (moving) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

can <- sf::st_transform(lall, 3857) + offset
can <- sf::st_sfc(can, crs = 3857)
can <- sf::st_transform(can, sf::st_crs(lall))
lall <- can
}
# Move
lall <- esp_move_can(lall, moveCAN = moveCAN)

# Transform

Expand Down
26 changes: 6 additions & 20 deletions R/esp_get_capimun.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,16 @@ esp_get_capimun <- function(year = Sys.Date(),

# Checks
moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1
moving <- any(isTRUE(moveCAN), length(moveCAN) > 1,
length(grep("05", data_sf$codauto)) > 0)


if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate

Check warning on line 170 in R/esp_get_capimun.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/esp_get_capimun.R,line=170,col=6,[indentation_linter] Indentation should be 4 spaces but is 6 spaces.
if (nrow(penin) > 0) {
Expand All @@ -187,7 +174,6 @@ esp_get_capimun <- function(year = Sys.Date(),
data_sf <- can
}
}
}

data_sf <- sf::st_transform(data_sf, as.double(init_epsg))
data_sf <- data_sf[order(data_sf$codauto, data_sf$cpro, data_sf$cmun), ]
Expand Down
19 changes: 3 additions & 16 deletions R/esp_get_ccaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,29 +244,16 @@ esp_get_ccaa_siane <- function(ccaa = NULL,

# Checks
moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1
moving <- any(isTRUE(moveCAN), length(moveCAN) > 1,
length(grep("05", data_sf$codauto)) > 0)

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand Down
21 changes: 3 additions & 18 deletions R/esp_get_comarca.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,16 @@ esp_get_comarca <- function(region = NULL,
# Checks

moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1
moving <- any(isTRUE(moveCAN), length(moveCAN) > 1,
length(grep("05", data_sf$codauto)) > 0)


if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(sf::st_drop_geometry(can),
geom = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand All @@ -166,7 +152,6 @@ esp_get_comarca <- function(region = NULL,
data_sf <- can
}
}
}

data_sf <- sf::st_transform(data_sf, as.double(init_epsg))
data_sf <- data_sf[order(data_sf$codauto, data_sf$cpro, data_sf$COMAGR), ]
Expand Down
45 changes: 7 additions & 38 deletions R/esp_get_munic.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,31 +268,17 @@ esp_get_munic <- function(year = "2019",
# Checks

moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1
moving <- any(isTRUE(moveCAN), length(moveCAN) > 1,
length(grep("05", data_sf$codauto)) > 0)

Check notice

Code scanning / lintr

Hanging indent should be 16 spaces but is 2 spaces. Note

Hanging indent should be 16 spaces but is 2 spaces.


if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(
sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)


# Regenerate
if (nrow(penin) > 0) {
Expand All @@ -301,7 +287,6 @@ esp_get_munic <- function(year = "2019",
data_sf <- can
}
}
}

data_sf <- sf::st_transform(data_sf, as.double(init_epsg))
data_sf <-
Expand Down Expand Up @@ -413,30 +398,15 @@ esp_get_munic_siane <- function(year = Sys.Date(),

# Checks
moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1
moving <- any(isTRUE(moveCAN), length(moveCAN) > 1,
length(grep("05", data_sf$codauto)) > 0)

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move CAN
can <- sf::st_sf(
sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand All @@ -445,7 +415,6 @@ esp_get_munic_siane <- function(year = Sys.Date(),
data_sf <- can
}
}
}

data_sf <- sf::st_transform(data_sf, as.double(init_epsg))
data_sf <-
Expand Down
16 changes: 1 addition & 15 deletions R/esp_get_nuts.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,26 +267,12 @@ esp_get_nuts <- function(year = "2016",

if (moving) {
if (length(grep("ES7", data_sf$NUTS_ID)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("ES7", data_sf$NUTS_ID), ]
can <- data_sf[grep("ES7", data_sf$NUTS_ID), ]

# Move can
can <- sf::st_sf(
sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand Down
22 changes: 3 additions & 19 deletions R/esp_get_prov.R
Original file line number Diff line number Diff line change
Expand Up @@ -342,30 +342,15 @@ esp_get_prov_siane <- function(prov = NULL,

# Checks
moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1
moving <- any(isTRUE(moveCAN), length(moveCAN) > 1,
length(grep("05", data_sf$codauto)) > 0)

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Move can
can <- sf::st_sf(
sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)
can <- esp_move_can(can, moveCAN = moveCAN)

# Regenerate
if (nrow(penin) > 0) {
Expand All @@ -374,7 +359,6 @@ esp_get_prov_siane <- function(prov = NULL,
data_sf <- can
}
}
}

data_sf <- sf::st_transform(data_sf, as.double(init_epsg))

Expand Down
31 changes: 4 additions & 27 deletions R/esp_get_roads.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,42 +86,19 @@ esp_get_roads <- function(year = Sys.Date(),
# Move can
# Checks
moving <- FALSE
moving <- isTRUE(moveCAN) | length(moveCAN) > 1
moving <- any(isTRUE(moveCAN), length(moveCAN) > 1,
length(grep("05", data_sf$codauto)) > 0)

if (moving) {
if (length(grep("05", data_sf$codauto)) > 0) {
offset <- c(550000, 920000)

if (length(moveCAN) > 1) {
coords <- sf::st_point(moveCAN)
coords <- sf::st_sfc(coords, crs = sf::st_crs(4326))
coords <- sf::st_transform(coords, 3857)
coords <- sf::st_coordinates(coords)
offset <- offset + as.double(coords)
}

data_sf <- sf::st_transform(data_sf, 3857)
penin <- data_sf[-grep("05", data_sf$codauto), ]
can <- data_sf[grep("05", data_sf$codauto), ]

# Change geometry name on penin
penin <- sf::st_sf(
sf::st_drop_geometry(penin),
geometry = sf::st_geometry(penin),
crs = sf::st_crs(penin)
)

# Move can
can <- sf::st_sf(
sf::st_drop_geometry(can),
geometry = sf::st_geometry(can) + offset,
crs = sf::st_crs(can)
)

can <- esp_move_can(can, moveCAN = moveCAN)

Check notice

Code scanning / lintr

Indentation should be 4 spaces but is 6 spaces. Note

Indentation should be 4 spaces but is 6 spaces.
# Regenerate
data_sf <- rbind(penin, can)
}
}


data_sf <- sf::st_transform(data_sf, as.double(init_epsg))

Expand Down
Loading

0 comments on commit 57d4c19

Please sign in to comment.