I have an Esri shape file(a multipolygon), with all city of my county in my own country coordinate system (srid:23700
). I would like to draw lines between cities? How can I do it in R?
I tried to copy this pattern, I changed the country names to city names:
library(maptools)
library(geosphere)
data(wrld_simpl)
US_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'United States']
US_lon = wrld_simpl$LON[wrld_simpl$NAME == 'United States']
SWE_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'Sweden']
SWE_lon = wrld_simpl$LON[wrld_simpl$NAME == 'Sweden']
points = gcIntermediate(c(US_lon, US_lat), c(SWE_lon, SWE_lat), 100)
dev.new(width=6, height=4)
plot(wrld_simpl)
lines(points, col='red')
but that does not work
I got this error:
Error in .pointsToMatrix(p1) :
points should be vectors of length 2, matrices with 2 columns, or inheriting from a SpatialPoints* object
I imported the data this way:
cities <-readShapePoly("cities.shp")
I tried this one(not working), How can I specify my own projection???:
readShapeSpatial("cities.shp", proj4string=CRS("+proj=longlat"))
So I would like to make lines between cities.
Thanks
It is working just fine for me.
kpacks <- c("ggmap", 'sp','rgdal', 'maptools', 'geosphere')
new.packs <- kpacks[!(kpacks %in% installed.packages()[,"Package"])]
if(length(new.packs)) install.packages(new.packs)
lapply(kpacks, require, character.only=T)
remove(kpacks, new.packs)
load data
data(wrld_simpl)
US_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'United States']
US_lon = wrld_simpl$LON[wrld_simpl$NAME == 'United States']
SWE_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'Sweden']
SWE_lon = wrld_simpl$LON[wrld_simpl$NAME == 'Sweden']
Great circle points
points = gcIntermediate(c(US_lon, US_lat), c(SWE_lon, SWE_lat), n=10,
addStartEnd=T)
Plot it
plot(wrld_simpl, col = 'grey', border = NA, axes = T)
lines(points, col='red')
text(x=0, y=65, "Hello!", pos = 1)
![](https://www.manongdao.com/static/images/pcload.jpg)
or
bbox <- ggmap::make_bbox(lon, lat, points, f = 1.1)
> bbox
left bottom right top
-223.869600 7.676652 140.533600 100.608574
I don't like it as returned by ggmap.
bbox <- c('left' = -110, 'bottom' = 20, 'right' = 30, 'top' = 80)
map_loc <- get_map(location = bbox, source = 'google', maptype = 'roadmap')
map <- ggmap(map_loc, extent = 'panel', maprange=FALSE, darken = c(0.5, "white"))
map + geom_path(aes(x=lon, y=lat), data = data.frame(points))
![](https://www.manongdao.com/static/images/pcload.jpg)
For your dataset you could try someting like this:
Read country data
hun <- readOGR(dsn='D:/Temporarios/r_project', layer = 'telepulesek')
Is it projected? If so, get it as unprojected layer
if(is.projected(hun)) hun <- spTransform(hun, CRS('+init=epsg:4326'))
Get centroids of each administrative division and attribute data.
ctd <- data.frame(nev = hun@data$Nev, azon = hun@data$Azon, coordinates(hun))
head(ctd)
Attribute data with unrecognized Hungarian characters
> head(ctd)
nev azon X1 X2
0 FÜZÉR 1710 26.90856 44.25363
1 HIDVÉGARDÓ 2567 26.33393 44.28779
2 FÜZÉRKOMLÓS 1137 26.87719 44.23257
3 KÉKED 1526 26.82228 44.25519
4 HOLLÓHÃZA 3116 26.87083 44.24943
5 PUSZTAFALU 1704 26.94799 44.24740
Get Great circle lines as spatialLines (sp = T
) object. I'm using 1st adm division as my 'from' and a few random as 'to'. You can change it as required using 'Nev' or 'Azon' attributes.
For specific Azon codes
azonp1 <- 3336 # from
azonp2 <- 1513 # to
lines_hun = gcIntermediate(subset(ctd, azon %in% azonp1, select = c('X1', 'X2')),
subset(ctd, azon %in% azonp2, select = c('X1', 'X2')),
n = 10, addStartEnd=T, sp = T)
par(bg='white', cex = 0.6)
plot(hun, col = '#000040', border = '#19198c', axes = T)
plot(lines_hun, col = 'white', add = T)
![](https://www.manongdao.com/static/images/pcload.jpg)
You can add codes to azonp2 simply building a vector with codes c(1513, 1514, ...)
If you don't want a spatial object as result of gcIntermediate
set sp = F
and run something like
flines <- function(x){
x <- data.frame(x)
lines(x$lon, x$lat, col = 'white')
}
plot(hun, col = '#000040', border = '#19198c', axes = T)
mapply(flines, lines_hun)
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Portuguese_Portugal.1252 LC_CTYPE=Portuguese_Portugal.1252
[3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C
[5] LC_TIME=Portuguese_Portugal.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] mapproj_1.2-2 maps_2.3-6 geosphere_1.3-8 rgdal_0.8-16 ggmap_2.3 ggplot2_0.9.3.1
[7] maptools_0.8-29 sp_1.0-14
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 dichromat_2.0-0 digest_0.6.4 foreign_0.8-59 grid_3.0.2
[6] gtable_0.1.2 labeling_0.2 lattice_0.20-27 MASS_7.3-29 munsell_0.4.2
[11] plyr_1.8.1 png_0.1-7 proto_0.3-10 RColorBrewer_1.0-5 Rcpp_0.11.0
[16] reshape2_1.2.2 RgoogleMaps_1.2.0.5 rjson_0.2.13 RJSONIO_1.0-3 scales_0.2.3
[21] stringr_0.6.2 tools_3.0.2
>