Skip to content

Commit

Permalink
mapLayout : fix bug if one area and/or no link
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Thieurmel committed Sep 29, 2021
1 parent c666a78 commit 90e0a96
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions R/map_layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,12 @@ changeCoordsServer <- function(input, output, session,
coords <- copy(layout()$areas)
info <- coords$area
links <- copy(layout()$links)
if(is.null(links)) links <- data.table()
} else {
coords <- copy(layout()$districts)
info <- coords$district
links <- copy(layout()$districtLinks)
if(is.null(links)) links <- data.table()
}

links$x0 <- as.numeric(links$x0)
Expand Down Expand Up @@ -294,7 +296,7 @@ changeCoordsServer <- function(input, output, session,
cex[pt] <- 2
par (mar = rep(0.1, 4))
graphics::plot.default(points$oldLon, points$oldLat, bty = "n", xaxt = "n", yaxt = "n",
xlab = "", ylab = "", main = "", col = col, asp = 1, pch = 19, cex = cex)
xlab = "", ylab = "", main = "", col = col, asp = 1, pch = 19, cex = cex)
}
})
}
Expand Down Expand Up @@ -386,6 +388,10 @@ changeCoordsServer <- function(input, output, session,
coords <- sp::SpatialPoints(coords()[, c("lon", "lat")],
proj4string = sp::CRS("+proj=longlat +datum=WGS84"))

# special with only one area...
if(nrow(data()$coords) == 1){
coords <- coords[1, ]
}

map <- current_map()

Expand All @@ -396,8 +402,12 @@ changeCoordsServer <- function(input, output, session,
}

# Put coords in right order
ord <- order(c(data_points$pt1, data_points$pt2, (1:length(coords))[-c(data_points$pt1, data_points$pt2)]))
mapCoords <- coords[ord, ]
if(nrow(data()$coords) > 1){
ord <- order(c(data_points$pt1, data_points$pt2, (1:length(coords))[-c(data_points$pt1, data_points$pt2)]))
mapCoords <- coords[ord, ]
} else {
mapCoords <- coords
}

final_coords <- data()$coords
final_links <- data()$links
Expand All @@ -406,11 +416,15 @@ changeCoordsServer <- function(input, output, session,
final_coords$y <- sp::coordinates(mapCoords)[, 2]

if (what() == "areas") {
final_links[final_coords, `:=`(x0 = x, y0 = y), on = c(from = "area")]
final_links[final_coords, `:=`(x1 = x, y1 = y), on = c(to = "area")]
if(!is.null(final_links) && nrow(final_links) > 0){
final_links[final_coords, `:=`(x0 = x, y0 = y), on = c(from = "area")]
final_links[final_coords, `:=`(x1 = x, y1 = y), on = c(to = "area")]
}
} else {
final_links[final_coords, `:=`(x0 = x, y0 = y), on = c(fromDistrict = "district")]
final_links[final_coords, `:=`(x1 = x, y1 = y), on = c(toDistrict = "district")]
if(!is.null(final_links) && nrow(final_links) > 0){
final_links[final_coords, `:=`(x0 = x, y0 = y), on = c(fromDistrict = "district")]
final_links[final_coords, `:=`(x1 = x, y1 = y), on = c(toDistrict = "district")]
}
}

if (!is.null(map)) {
Expand Down

0 comments on commit 90e0a96

Please sign in to comment.