Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 小叶中圆与多边形的并集_R_Leaflet - Fatal编程技术网

R 小叶中圆与多边形的并集

R 小叶中圆与多边形的并集,r,leaflet,R,Leaflet,我正在向传单地图添加两个圆和一个多边形。这是我绘制这三种形状的代码。有没有办法把这三种形状结合起来 leaflet(options = leafletOptions(minZoom = 0, maxZoom = 18)) m <- leaflet()%>% addTiles()%>% setView(72.84320,20.43397,zoom=16)%>% #Add mouse position addMouseCoordinates(style = "

我正在向传单地图添加两个圆和一个多边形。这是我绘制这三种形状的代码。有没有办法把这三种形状结合起来

leaflet(options = leafletOptions(minZoom = 0, maxZoom = 18))
m <- leaflet()%>%
  addTiles()%>%
  setView(72.84320,20.43397,zoom=16)%>%
  #Add mouse position
  addMouseCoordinates(style = "basic", epsg = NULL,
                      proj4string = NULL, native.crs = FALSE)

#Runway Extremities
ARP <- c((72 +(50/60)+(32.67/3600)),(20+(26/60)+(1.54/3600)))
ptTop2 <- c((72 +(50/60)+(16.98/3600)),(20+(25/60)+(25.18/3600)))
ptBottom2 <- c((72 +(50/60)+(43.45/3600)),(20+(26/60)+(18.13/3600)))
ptTop1 <- c((72 +(50/60)+(8.64/3600)),(20+(26/60)+(8.08/3600)))
ptBottom1 <- c((72 +(50/60)+(44.21/3600)),(20+(26/60)+(5.63/3600)))
ap1 <- 95
ap2 <- 26

pt1 <- destPoint(ptTop1,ap1+90,4000)
pt2 <- destPoint(ptTop1,ap1-90,4000)
pt3 <- destPoint(ptBottom1,ap1-90,4000)
pt4 <- destPoint(ptBottom1,ap1+90,4000)
iRect <- data.frame(rbind(pt1,pt2,pt3,pt4))

#Inner Horizontal Surface
m%>%addCircles(ptBottom1[1],ptBottom1[2],radius = 4000,color = "red",
                    fillOpacity = 0,weight = 3)%>%
  addCircles(ptTop1[1],ptTop1[2],radius = 4000,color = "red",
             fillOpacity = 0,weight = 3)%>%
  addPolygons(iRect$lon,iRect$lat,color = "blue",
              fillOpacity = 0, weight=3)
单张(选项=单张选项(最小缩放=0,最大缩放=18))
m%
addTiles()%>%
setView(72.84320,20.43397,缩放=16)%>%
#添加鼠标位置
addMouseCoordinates(style=“basic”,epsg=NULL,
proj4string=NULL,native.crs=FALSE)
#跑道末端

据我所知,你不能。无论何时在传单地图上添加内容,都会将其视为具有单独属性和数据的单独图层:

  • 这样做的目的是能够使用交互式界面隐藏/显示这些层 图例(因此您希望保持图层分离)
  • 我看不到任何简单的方法来访问每个圆的点的坐标
如果要显示由多个形状组成的对象,必须使用点坐标、sp包和此类代码自己创建复杂的空间多边形:

require(sp)
require(leaflet)

#Used for sp polygon creation
createPolygon <- function(latitude, longitude, name = "polygon"){

  return(Polygons(list(Polygon(cbind(longitude, latitude))), ID = name))

}

#Will give "res" points coordinates for a circle centered on x0 y0
#with a radius r
CreateCircle <- function(x0, y0, r, res = 50){

  theta = seq(0, 2*pi, length.out = res+1)

  x = r*cos(theta) + x0
  y = r*sin(theta) + y0

  return(data.frame(x, y))

}

#Computes two circles points'
circleA <- CreateCircle (0, 0, 2, res = 200)
circleB <- CreateCircle (10, 10, 4, res = 6)

#Add them to polygons
A = createPolygon(circleA$x, circleA$y, "A")
B = createPolygon(circleB$x, circleB$y, "B")
C = SpatialPolygons(list(A, B))

#Create and display the leaflet map
m = leaflet() %>% addTiles() %>%
  addPolygons(data = C, fillColor = topo.colors(10, alpha = NULL), stroke = TRUE)
m
require(sp)
要求(单张)
#用于创建sp多边形

createPolygon我建议远离
sp
包中的空间对象,而是从

简单功能是R的“新”空间类(由做sp的同一个人制作)

所以,为了得到你的圈的联合,你可以使用

library(rgeos)
library(sf)


## A dataframe of your points
df <- data.frame(lon = c(ptTop1[1], ptTop2[1], ptBottom1[1], ptBottom2[1]),
                 lat = c(ptTop1[2], ptTop2[2], ptBottom1[2], ptBottom2[2]))

## convert them into a simple features data.frame
sf_df <- st_as_sf(df, coords = c("lon", "lat"))

## convert into circles
sf_circles <- st_buffer(sf_df, dist = 0.04)

## find the union of all the circles
sf_combined <- st_union(sf_circles)


## now sf_combined is a single polygon
sf_combined
# Geometry set for 1 feature 
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: 72.79573 ymin: 20.38366 xmax: 72.88561 ymax: 20.47837
# epsg (SRID):    NA
# proj4string:    NA
# POLYGON((72.8745460445306 20.4072956786729, 72....

谢谢@Kevin Dallaporta我一定会试一试的。您能告诉我如何将圆绘制为空间多边形吗?当然,我在最简单的示例中添加了一些代码行:)。如果要在传单上显示自定义形状,则需要设计类似“CreateCircle”的函数map@Kevin达拉波塔,谢谢你的帮助,非常感谢!感谢您指出
sf
软件包。这正是我需要的@迪拉杰-不客气。我发现适应
sf
需要一段时间,但最终还是值得的。传单支持所有主要的sf类,但
MULTIPOINT
,所以这里不需要转换为sp。@TimSalabim-你完全正确!我一定是过时了。现在更新-thanks@SymbolixAU我现在一直在尝试将多边形与圆的并集结合起来。我有一个多边形
rect1
library(leaflet)

sp <- as(sf_combined, 'Spatial')

sf_combined %>%
    leaflet() %>%
    addTiles() %>%
    addPolygons()