我正在尝试使用mapView在太平洋中的经度为180度的网格中使用mapView绘制数据。原始crs是“ + proj = merc + lon_0 = 150 + lat_ts = 0 + x_0 = 0 + y_0 = 0 +数据= WGS84 +单位= m + no_defs + ellps = WGS84 + towgs84 = 0,0,0”。 等效数据的示例是:
FBNavResponseEnd:1575657509364
FBNavDomContentLoaded:1575657510483
FBNavAmpDetect:false
FBNavResponseEnd:1575657509364
FBNavDomContentLoaded:1575657510483
FBNavAmpDetect:false
FBNavResponseEnd:1575657509364
FBNavDomContentLoaded:1575657510483
FBNavAmpDetect:false
FBNavResponseEnd:1575657509364
FBNavDomContentLoaded:1575657510483
FBNavLoadEventEnd:1575657511192
FBNavAmpDetect:false
使用绘图功能时,点居中,而使用mapView时,点在地图的两侧(链接图像)之间分开。可以使用mapView的视图以不同的经度为中心吗?
为此地图使用本机crs没有帮助。我收到错误消息。
test.coords<-as.data.frame(cbind(c(runif(15,-180,-130),runif(5,160,180)),runif(20,40,60)))
test.sp <- SpatialPointsDataFrame(coords = cbind(test.coords$V1,test.coords$V2), data = test.coords,
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
test.sp <- spTransform(test.sp,
"+proj=merc +lon_0=150 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
plot(test.sp)
mapview(test.sp)
警告信息: sf层不是长期数据
谢谢
答案 0 :(得分:1)
好的,出于以下几个原因,这是您问题的半最佳解决方案:
对于一组线或多边形,我们将需要另一种方法,但是我目前尚不知道针对对象的往返坐标的sp / sf本机解决方案。
library(sp)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(mapview)
test.coords<-as.data.frame(cbind(c(runif(15,-180,-130),runif(5,160,180)),runif(20,40,60)))
test.sp <- SpatialPointsDataFrame(coords = cbind(test.coords$V1,test.coords$V2), data = test.coords,
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
test_sf = st_as_sf(test.sp)
shift = function(x) {
geom = st_geometry(x)
st_geometry(x) = st_sfc(
lapply(seq_along(geom), function(i) {
geom[[i]][1] = ifelse(geom[[i]][1] < 0, geom[[i]][1] + 360, geom[[i]][1])
return(geom[[i]])
})
, crs = st_crs(geom)
)
return(x)
}
mapview(shift(test_sf)) +
mapview(test_sf, col.regions = "orange")
由reprex package(v0.3.0)于2019-12-11创建
为此,我选择使用sf而不是sp,因为 mapview 在内部使用sf,并希望找到可以解决该问题的通用方法。
答案 1 :(得分:1)
这是多边形的一种解决方法-会产生一些警告,但总体上是可以完成的。
# transform grid to standard latitude and longitude
# original grid in crs "+proj=merc +lon_0=150 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
grid<-spTransform(grid,"+init=epsg:4326")
summary(coordinates(grid))
# V1 V2
# Min. :-179.8 Min. :37.17
# 1st Qu.:-168.8 1st Qu.:51.70
# Median :-154.3 Median :55.02
# Mean :-118.0 Mean :54.65
# 3rd Qu.:-131.4 3rd Qu.:58.31
# Max. : 179.8 Max. :65.56
out <- lapply(1:length(grid), function(i) grid[i, ])
for (i in 1:length(grid)) {
cds <- slot(slot(slot(out[[i]], "polygons")[[1]], "Polygons")[[1]], "coords")
cds_polygon <- Polygons(list(Polygon(cds)), ID="out.x")
out.x <- SpatialPolygons(list(cds_polygon))
proj4string(out.x) <- CRS("+init=epsg:4326")
if (cds[1,1]>0) { # depending to which side one want to move polygons that are on both sides of International Date Line
out.y <- elide(out.x, shift=c(-360,0))
} else {
out.y<-out.x}
if(i==1){grid.shifted<-out.y} else {
grid.shifted<-raster::union(grid.shifted,out.y)
}
}
# add data in the original grid
grid.shifted <- SpatialPolygonsDataFrame(grid.shifted, grid@data, match.ID = F)
mapview(grid.shifted)