我是地理空间数据的新手,只是通过查看网络上的几篇文章来设法以小块的方式进行绘制。
我正在尝试在我使用 rayshader 包构建的 3D 图上绘制多边形边界,但面临问题,并在顶部显示显示多边形边界。
使用的形状文件是
2011_Dist.shp
,可以从Shapefile Github链接下载
我尝试过的代码:
library(tidyverse)
library(sf)
library(elevatr)
library(raster)
library(rayshader)
library(osmdata)
# read districts shape file
ind_distirct_shp <- sf::st_read("local path/2011_Dist.shp")
ind_distirct_shp
# filter State
delhi_district_shp <- ind_distirct_shp %>%
sf::st_as_sf() %>%
filter(ST_NM %in% c("NCT of Delhi"))
# this shows the polygon boundaries that I need on top of map
plot(delhi_district_shp)
# download elevation data for State Delhi
delhi_raster <- elevatr::get_elev_raster(delhi_district_shp, z = 10, clip = "location")
# convert to matrix
delhi_mat <- raster_to_matrix(delhi_raster)
# 3D plot using Rayshader
delhi_mat %>%
height_shade(texture = grDevices::colorRampPalette(c("#9a133d","orange","red","purple"
))(256)) %>%
plot_3d(heightmap = delhi_mat,
windowsize = c(800,800), # c(800*wr,800*hr)
solid = FALSE,
zscale = 1,
phi = 90,
zoom = .6,
theta = 0,
shadowcolor = "grey50",
linewidth = 6,
background = "white",
solidlinecolor = "#013b39")
问题:现在,当我尝试修改此内容以添加
polygon lines
时,它不起作用。
polygon_layer = generate_polygon_overlay(delhi_district_shp, extent = extent(delhi_raster),
heightmap = delhi_mat) # , palette="grey30"
polygon_layer
delhi_mat %>%
height_shade(texture = grDevices::colorRampPalette(c("#9a133d","orange","red","purple"
))(256)) %>%
add_overlay(polygon_layer) %>%
plot_3d(heightmap = delhi_mat,
windowsize = c(800,800), # c(800*wr,800*hr)
solid = FALSE,
zscale = 1,
phi = 90,
zoom = .6,
theta = 0,
shadowcolor = "grey50",
linewidth = 6,
background = "white",
solidlinecolor = "#013b39")
它应该是橙色 3D 地图顶部的多边形线,但它没有按我的预期工作。
library(osmdata)
osm_bbox = c(extent(delhi_raster)[3],extent(delhi_raster)[1],extent(delhi_raster)[4],extent(delhi_raster)[2])
osm_bbox
raster_polygon_boundary <- osmdata::opq(osm_bbox) %>%
add_osm_feature("highway") %>% # "admin_level"
osmdata_sf()
raster_polygon_boundary
输出:
Object of class 'osmdata' with:
$bbox : 76.8425681832661,28.4030759258059,77.347719586084,28.8793200072187
$overpass_call : The call submitted to the overpass API
$meta : metadata including timestamp and version numbers
$osm_points : 'sf' Simple Features Collection with 0 points
$osm_lines : NULL
$osm_polygons : 'sf' Simple Features Collection with 0 polygons
$osm_multilines : NULL
$osm_multipolygons : NULL
我在上面得到0个多边形和线条,所以我无法在
3D图之上添加任何
polygon
。
我该如何解决这个问题。感谢任何帮助。
将
alphalayer
添加到 add_overlay(polygon_layer,alphalayer = .6)
有所帮助,尽管它有效,但透明度仍然无法完美发挥作用。它在情节中造成了一些whiteness
。如果其他人有更好的答案或改进方法,请随时分享。
delhi_mat %>%
height_shade(texture = grDevices::colorRampPalette(c("#9a133d","orange","red","purple"
))(256)) %>%
add_overlay(polygon_layer,alphalayer = .6) %>%
add_overlay(generate_label_overlay(delhi_district_shp, extent = extent(delhi_raster),
text_size = 5, point_size = 1,
halo_color = "white",halo_expand = 5,
seed=1,
heightmap = delhi_mat, data_label_column = "DISTRICT")) %>%
plot_3d(heightmap = delhi_mat,
windowsize = c(800,800), # c(800*wr,800*hr)
solid = FALSE,
zscale = .7,
phi = 90,
zoom = .6,
theta = 0,
shadowcolor = "grey50",
linewidth = 6,
background = "white",
solidlinecolor = "#013b39")
您可以通过创建自定义颜色(白色,100% 透明)来填充多边形来实现您想要的效果:
polygon_layer = generate_polygon_overlay(delhi_district_shp,
extent = extent(delhi_raster),
heightmap = delhi_mat,
palette = rgb(0,0,0,0)) # R,G,B,alpha