2017-07-23 52 views
2

我將兩個圓和一個多邊形添加到傳單地圖中。這是我繪製這三種形狀的代碼。有什麼辦法可以得到這三種形狀的聯合?傳單中的圓和多邊形聯合

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) 

rgeosgUnion()功能,但我不知道如何已經通過代碼添加上面的圓圈,轉換成SpatialPolygons。

回答

3

我建議遠離sp包中的空間物體,而是從sf package查看簡單的要素對象。

簡單特徵是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.... 

至於繪圖,小葉可以處理sf對象(除點對多點),這樣你就可以直接繪製它

library(leaflet) 

sp <- as(sf_combined, 'Spatial') 

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

enter image description here

+0

感謝您指出'sf'包。這正是我需要的! – Dhiraj

+0

@Dhiraj - 不客氣。我發現習慣'sf'需要一段時間,但最終這是值得的。 – SymbolixAU

+0

**小冊子**支持所有主要的** sf **類,但是'MULTIPOINT',因此在這裏不需要轉換爲** sp **。 – TimSalabim

0

據我所知你不能。每當你的單張地圖其視爲一個獨立的層上加一些東西,有獨立的屬性和數據:

  • 的想法是能夠隱藏/使用交互式 傳說表明這些層(所以你要保持你的層分離)
  • 我看不出有什麼簡單的方法來訪問每個圓圈的點座標

如果你想顯示就由幾個形狀的東西,你就必須自己創建一個複雜的SpatialPolygon與點座標,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 

如果它不能顯示你想要的結果(例如,因爲你希望圓圈的顏色不同於你的矩形),你將不得不使用幾個圖層並對它們進行分組。

+0

謝謝@Kevin Dallaporta我肯定會給它一個鏡頭。你能告訴我如何畫圓作爲空間多邊形嗎? – Dhiraj

+0

當然,我添加了一些代碼行的最小例子:)。如果您想在傳單地圖上顯示自定義形狀,您需要設計「CreateCircle」等功能。 – kdallaporta

+0

@Kevin Dallaporta,非常感謝您的幫助! – Dhiraj