gpt4 book ai didi

r - 由于地理单位不同,我在向 ggmap 添加 shapefile 时遇到问题

转载 作者:行者123 更新时间:2023-12-04 23:54:59 25 4
gpt4 key购买 nike

我正在尝试将 ESRI shapefile (.shp) 添加到北卡罗来纳州的 ggmap 图中,该图具有以下代码:
x <- get_map(location="North Carolina", zoom=6, maptype="terrain")ggmap(x) +
geom_polygon(data=citylim83.df, aes(x=long, y=lat), fill="red", alpha=0.2)

我加载并强化的 shapefile citylim83.df does not show up .
这是用于将 shapefile 加载到 ggplot 的代码:

    epsgs <- make_EPSG()
citylim <- readOGR(dsn=".", layer="MunicipalBoundaries_polys")`

在 EPSG 中进行搜索后,市政边界的投影单位是 ft-US 对于状态平面系统。即使此 .shp 具有 NAD83 的地理坐标系,我也想将其投影到 NAD83 以摆脱状态平面系统(NAD83(UTM-17N)的 EPSG 代码为 26917):
    citylim83 <- spTransform(citylim, CRS("+init=epsg:26917"))
summary(citylim83)

Object of class SpatialPolygonsDataFrame
Is projected: TRUE
[+init=epsg:26917 +proj=utm +zone=17 +ellps=GRS80 +datum=NAD83 +units=m

citylim83.df <- fortify(citylim83)

然后在上面显示的 ggmap 代码中使用了这个数据框。

即使它现在肯定被投影到 NAD83 中,它仍然不会显示在基础 ggmap 上。我导入的 get_map 对象的基本投影是什么?是否有命令可以找出这一点,以便我可以将我的 map 与我想要在其上显示的 shapefile 进行匹配?我是否必须“取消投影”我的 citylim 对象?仅供引用,shapefile 是北卡罗来纳州每个自治市的城市边界,如果不清楚的话。对此的任何帮助将不胜感激,因为我对 ggplot2/ggmap 社区非常陌生。

最佳答案

在这里你有一个答案:

library(ggmap)
library(maptools)
shapefile <- readShapeSpatial('MunicipalBoundaries_polys.shp', proj4string = CRS("+proj=lcc +lat_1=34.33333333333334 +lat_2=36.16666666666666 +lat_0=33.75 +lon_0=-79 +x_0=609601.2199999997 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs"))
shp <- spTransform(shapefile, CRS("+proj=longlat +datum=WGS84"))
data <- fortify(shp)

nc <- get_map("North Carolina", zoom = 6, maptype = 'terrain')
ncmap <- ggmap(nc, extent = "device")
ncmap +
geom_polygon(aes(x = long, y = lat, group = group), data = data,
colour = 'grey', fill = 'black', alpha = .4, size = .1)

坐标需要转换为 longlat 投影并且它正在工作。

关于r - 由于地理单位不同,我在向 ggmap 添加 shapefile 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18084609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com