在 Google 地图图块上绘制 shapefile

发布于 2024-08-18 06:31:06 字数 1435 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2024-08-25 06:31:06

从开发者的角度来看,它似乎运行良好。

  shpFile <- system.file("shapes/sids.shp", package="maptools");  
  shp<-importShapefile(shpFile,projection="LL");  
  bb <- qbbox(lat = shp[,"Y"], lon = shp[,"X"]);  
  MyMap <- GetMap.bbox(bb$lonR, bb$latR, destfile = "SIDS.jpg");  
  #compute regularized SID rate  
  sid <- 100*attr(shp, "PolyData")$SID74/(attr(shp, "PolyData")$BIR74+500)  
  b <- as.integer(cut(sid, quantile(sid, seq(0,1,length=8)) ));  
  b[is.na(b)] <- 1;  
  opal <- col2rgb(grey.colors(7), alpha=TRUE)/255;  opal["alpha",] <- 0.2;  
  shp[,"col"] <- rgb(0.1,0.1,0.1,0.2);  
  for (i in 1:length(b)) shp[shp[,"PID"] == i,"col"] <- rgb(opal[1,b[i]],opal[2,b[i]],opal[3,b[i]],opal[4,b[i]]);  
  PlotPolysOnStaticMap(MyMap, shp, lwd=.5, col = shp[,"col"], add = F);

From the developer, it seems to work nicely.

  shpFile <- system.file("shapes/sids.shp", package="maptools");  
  shp<-importShapefile(shpFile,projection="LL");  
  bb <- qbbox(lat = shp[,"Y"], lon = shp[,"X"]);  
  MyMap <- GetMap.bbox(bb$lonR, bb$latR, destfile = "SIDS.jpg");  
  #compute regularized SID rate  
  sid <- 100*attr(shp, "PolyData")$SID74/(attr(shp, "PolyData")$BIR74+500)  
  b <- as.integer(cut(sid, quantile(sid, seq(0,1,length=8)) ));  
  b[is.na(b)] <- 1;  
  opal <- col2rgb(grey.colors(7), alpha=TRUE)/255;  opal["alpha",] <- 0.2;  
  shp[,"col"] <- rgb(0.1,0.1,0.1,0.2);  
  for (i in 1:length(b)) shp[shp[,"PID"] == i,"col"] <- rgb(opal[1,b[i]],opal[2,b[i]],opal[3,b[i]],opal[4,b[i]]);  
  PlotPolysOnStaticMap(MyMap, shp, lwd=.5, col = shp[,"col"], add = F);
笔落惊风雨 2024-08-25 06:31:06

数据来自data.gov.sg。加载绘制地图所需的包

library(rgdal)  #for shapefiles
library(ggmap)  #plotting maps
library(ggplot2) #general plots

使用 readOGR 读取 shapefile

 shpfile <- readOGR(dsn = 'data/street-and-places/','StreetsandPlaces',verbose = FALSE)
head(coordinates(shpfile),5)

     coords.x1 coords.x2
[1,]  28640.40  29320.77
[2,]  29429.83  28548.69
[3,]  29224.01  28360.38
[4,]  29827.20  29664.26
[5,]  28451.00  29451.78

我们观察到坐标位于不同的投影系统中。所以我们必须将其转换为网络墨卡托投影系统。我们使用 spTransform 转换 shapefile。

crsobj <- CRS("+proj=longlat +datum=WGS84")   #Web Mercator projection system
shpfile_t <- spTransform(shpfile,crsobj)      #Applying projection transformation
df <- as.data.frame(coordinates(shpfile_t))   #Converting to a data frame
head(df,5)

coords.x1<dbl>coords.x2<dbl>
1   103.8391    1.281441        
2   103.8462    1.274459        
3   103.8443    1.272756        
4   103.8497    1.284547        
5   103.8374    1.282626    

我们注意到变换后的投影系统。让我们将其绘制在底图之上。

sgmap <- get_map(location="Singapore", zoom=11,   
             maptype="roadmap", source="google") #Using Osm base map of Singapore
p <- ggmap(sgmap) +
    geom_point(data = df,
    aes(x = coords.x1, y = coords.x2),
    color = 'orange',size = 1)
p

The data is from data.gov.sg. Loading the packages required to plot the maps

library(rgdal)  #for shapefiles
library(ggmap)  #plotting maps
library(ggplot2) #general plots

Reading the shapefile using readOGR

 shpfile <- readOGR(dsn = 'data/street-and-places/','StreetsandPlaces',verbose = FALSE)
head(coordinates(shpfile),5)

     coords.x1 coords.x2
[1,]  28640.40  29320.77
[2,]  29429.83  28548.69
[3,]  29224.01  28360.38
[4,]  29827.20  29664.26
[5,]  28451.00  29451.78

We observe that the coordinates are in a different projection system. So we have to convert this to web mercator projection system. We transform the shapefile using spTransform.

crsobj <- CRS("+proj=longlat +datum=WGS84")   #Web Mercator projection system
shpfile_t <- spTransform(shpfile,crsobj)      #Applying projection transformation
df <- as.data.frame(coordinates(shpfile_t))   #Converting to a data frame
head(df,5)

coords.x1<dbl>coords.x2<dbl>
1   103.8391    1.281441        
2   103.8462    1.274459        
3   103.8443    1.272756        
4   103.8497    1.284547        
5   103.8374    1.282626    

We notice the transformed projection system. Let us plot it on top of a base map.

sgmap <- get_map(location="Singapore", zoom=11,   
             maptype="roadmap", source="google") #Using Osm base map of Singapore
p <- ggmap(sgmap) +
    geom_point(data = df,
    aes(x = coords.x1, y = coords.x2),
    color = 'orange',size = 1)
p
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文