R地图包 - 如何在国内城市之间划线?

时间:2014-03-09 13:20:55

标签: r

我有一个Esri形状文件(多字形),我县的所有城市都在我自己的国家坐标系(srid:23700)。我想在城市之间画线?我怎么能在R?

中做到这一点

我尝试复制此模式,我将国家/地区名称更改为城市名称:

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')

但这不起作用

我收到了这个错误:

Error in .pointsToMatrix(p1) : 
  points should be vectors of length 2, matrices with 2 columns, or inheriting from a SpatialPoints* object

我以这种方式导入数据:

cities <-readShapePoly("cities.shp")

我尝试了这个(不工作),我如何指定我自己的投影???:

readShapeSpatial("cities.shp", proj4string=CRS("+proj=longlat"))

所以我想在城市之间划线。

由于

1 个答案:

答案 0 :(得分:5)

它对我来说很好。

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)

加载数据

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), n=10,
                        addStartEnd=T)

绘制

plot(wrld_simpl, col = 'grey', border = NA, axes = T)
lines(points, col='red')
text(x=0, y=65, "Hello!", pos = 1)

general plot

bbox <- ggmap::make_bbox(lon, lat, points, f = 1.1)
> bbox
       left      bottom       right         top 
-223.869600    7.676652  140.533600  100.608574 

我不喜欢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))

ggmap

对于您的数据集,您可以尝试这样的方式:

阅读国家/地区数据

hun <- readOGR(dsn='D:/Temporarios/r_project', layer = 'telepulesek')

是否预计?如果是这样,请将其作为未投影的图层

if(is.projected(hun)) hun <- spTransform(hun, CRS('+init=epsg:4326'))

获取每个行政区划的属性和属性数据。

ctd <- data.frame(nev = hun@data$Nev, azon = hun@data$Azon, coordinates(hun))
head(ctd)

具有无法识别的匈牙利字符的属性数据

> 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

将大圆线作为spatialLines(sp = T)对象获取。我使用1st adm作为我的'from'和一些随机的'to'。您可以根据需要使用“Nev”或“Azon”属性进行更改。

对于特定的Azon代码

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)

plot azon

你可以添加代码到azonp2,只需用代码c(1513,1514,...)构建一个向量

如果您不希望gcIntermediate设置sp = F的空间对象并运行类似

的内容
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        
>