r - Derived spatial lines pointing to incorrect polygons -


i'm trying make flow map in leaflet r map movement of 1 area (polygon) another.

library(sp) library(rgdal) library(tidyverse) library(leaflet) 

i'm using this shapefile of local authorities (las) in england polygons, , i'm deriving lines coordinates those.

map <- readogr(dsn = "map", layer = "lea_boundaries") map <- map[!is.na(map$lad16cd),] 

i'm wanting create lines centre of 1 polygon used origin , destination polygons latitude , longitude(s) derive beginning , end point of line. did through sampling la names map's data slot, sampling them again create starting , ending las, merging coordinates starting la on, followed coordinates ending la, follows:

startingla <- sample(map@data$lad16nm, 100000, replace = true) nextla <- sample(startingla, 100000, replace = true) movement <- data.frame(startingla, nextla) ld <- merge(movement, map@data[,c("lad16nm", "long", "lat")], by.x = "startingla", by.y = "lad16nm") %>%       merge(map@data[,c("lad16nm", "long", "lat")], by.x = "nextla", by.y = "lad16nm", suffixes = c("",".y")) 

after have tried 2 separate ways create spatial lines, 1 custom function add bezier line, , other gcintermediate(), i'll use here it's more accessible , rules out programming errors in function.

flow <- gcintermediate(ld[,c("long", "lat")], ld[,c("long.y", "lat.y")], sp = true, addstartend = true) 

and map itself; we'll add popup labels polygons can see las which.

leaflet() %>%   addprovidertiles(providers$cartodb.positronnolabels) %>%   addpolygons(data = map, weight = 1, col = "#000000", fillopacity = 0, popup = map@data$lad16nm) %>%   addpolylines(data = flow) 

and we'll take @ data used create line.

ld 

on map have blue line joining 1 polygon next. startingla in ld, have 'east sussex', , nextla 'hillingdon', line points to/from birmingham , wandsworth.

this true using custom function fit bezier lines, makes me think happened before, data correct right until lines made spatiallines, , after goes haywire.

does have insight i'm going wrong here?

thank you.

the co-ordinates in map@data object not appropriate use case.

greenwich, instance, has co-ordinates -1.108940, 53.52697, firmly located in doncaster. perhaps @ earlier processing step co-ordinates , ids got unmatched somehow?

in case, it's quickest/easiest re-calculate co-ordinates using using rgeos::gcentroid on map object , replace offending columns in data file:

rgeos::gcentroid(map,byid = t) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -