传单中的圆和多边形的联合

时间:2017-07-23 10:12:16

标签: r leaflet

我将两个圆圈和一个多边形添加到传单地图中。这是我的代码,它描绘了这三种形状。有什么办法可以让这三种形状结合起来吗?

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)

rgeos具有gUnion()功能,但我不确定如何将上面代码添加的圈子转换为SpatialPolygons。

2 个答案:

答案 0 :(得分:3)

我建议远离<!DOCTYPE html> <html lang="de"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="pragma" content="no-cache"> <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"> <style type="text/css"> html, body { width: 100%; height: 100%; margin: 0; padding: 0; background-color: grey; } </style> </head> <body> <div style="background: red;height: 20%;"></div> </body> </html>包中的空间对象,而是从sf package查看简单的要素对象。

简单的功能是新的&#39; R的空间类(由sp的同一个人制作)。

因此,要获得圈子的联合,您可以使用

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.... 个对象(MULTIPOINT除外),因此可以直接绘制

sf

enter image description here

答案 1 :(得分:0)

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

  • 我们的想法是能够使用交互式隐藏/显示这些图层 传说(所以你想保持你的图层分开)
  • 我没有看到任何简单的方法来访问每个圈子的点数。坐标

如果你想展示由多个形状组成的东西,你必须自己用点坐标,sp包和这种代码创建一个复杂的SpatialPolygon:

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

如果它无法显示您想要的结果(例如,因为您希望圆圈的颜色与矩形不同),则必须使用多个图层并对其进行分组。

相关问题