将纬度和经度转换为空间点,并将它们投影到R中的另一张地图上

将纬度和经度转换为空间点,并将它们投影到R中的另一张地图上,r,rgdal,R,Rgdal,我从一个官方政府网站下载了一张墨西哥所有州的SHP地图。我还有一个包含300个气象站及其坐标的数据集我想将这300个桩号添加到SHP对象。但是,我的坐标似乎不在同一地理空间区域上 我阅读并绘制了墨西哥地图。然后,我将所有300个站点的纬度和经度转换为空间点。不幸的是,当我把这些站点添加到地图上时,我无法找到它们 #sample stations stations <- data.frame( station_number = c(10003),

我从一个官方政府网站下载了一张墨西哥所有州的SHP地图。我还有一个包含300个气象站及其坐标的数据集我想将这300个桩号添加到SHP对象。但是,我的坐标似乎不在同一地理空间区域上

我阅读并绘制了墨西哥地图。然后,我将所有300个站点的纬度和经度转换为空间点。不幸的是,当我把这些站点添加到地图上时,我无法找到它们

#sample stations
stations <- data.frame( station_number = c(10003),
                        station_alt = c(1754),
                        station_long = c(-106.567),
                        station_lat = c(26)
)

#make a sf-object out of them
stations <- st_as_sf( stations, coords = c( "station_long", "station_lat" ) )

#and plot
mapview::mapview( list( sh, stations ) )
这是我的密码:

# Read official SHP map
Mex <- readOGR(dsn = "/Desktop",
               layer = "areas_geoestadisticas_estatales")
# Turn `stations` into Spatial Points and project them onto `Mex`
Geo_stations <- SpatialPoints(stations[, c("stations_lat", "stations_long")],
                              proj4string = CRS(proj4string(mex)))
# Plot `Mex`, then add `Geo_stations`
plot(Mex) 
plot(Geo_stations, col = "red", add = TRUE) # Nowhere to be found
在比较了
坐标(Mex)
坐标(Geo_站)
的结果后,我意识到Mex的“坐标”是巨大的数字,而来自站的坐标看起来像实际的地理空间参考。我想我没有把它们正确地投射到Mex上。我希望
plot(Geo_stations,add=TRUE)
在全国范围内添加一层站点。

library(sf)
library(sf)
library(mapview)

#load the shapefile
sh = st_read( "./702825217341_s/conjunto_de_datos/areas_geoestadisticas_estatales.shp",
              stringsAsFactors = FALSE )

#set encoding of second column, because of the special characters used
Encoding(sh$NOM_ENT) <- "UTF-16"
#set crs of the current map
st_set_crs( sh, 5332 )
#transform to WGS84-coordinates
st_transform( sh, 4326 )

#let's see what we have so far
mapview::mapview(sh)
图书馆(地图视图) #加载shapefile sh=st_read(“/702825217341_s/convento_de_datos/areas_geostaticas_estatales.shp”, stringsAsFactors=FALSE) #设置第二列的编码,因为使用了特殊字符
编码(sh$NOM)您的采样站似乎缺少纬度坐标?另外:最好使用
dput()
提供数据,这样用户就不必重新键入/手动将数据插入R.@Wimpel Yes。我的错!我更新了问题。我不知道dput的功能。站点现在是作为一个新图层嵌入到地图中,还是它们仍然是两个独立的对象?另外,站点是否因为CRS不同而没有绘制在我的地图上?我这样问是因为我不理解脚本中的
st_set_crs
st_transform
行。你怎么知道该设置什么CRS的?@ArturoSbr在你地图的下载页面上写着:
datum:ITRF08
,一个快速的谷歌搜索会显示该数据的CRS代码。。。。使用
st_set_crs
为加载的地图填充crs。。。所以sf知道从什么转变过来。使用
st_transform
,您可以告诉sf转换到哪里。
#sample stations
stations <- data.frame( station_number = c(10003),
                        station_alt = c(1754),
                        station_long = c(-106.567),
                        station_lat = c(26)
)

#make a sf-object out of them
stations <- st_as_sf( stations, coords = c( "station_long", "station_lat" ) )

#and plot
mapview::mapview( list( sh, stations ) )