簡體   English   中英

如何在r中的shapefile中循環遍歷多邊形?

[英]How to loop through polygons in a shapefile in r?

我已經創建了一張地圖,在其中使用Leaflet軟件包使用shapefile繪制了人口密度。 我正在嘗試遍歷地圖上的每個多邊形,這可能嗎,怎么辦?

**地圖:單張地圖

#Create Leaflet map
map <- leaflet()  %>% addTiles() %>% 
  setView(lng = -7.1050, lat=53.2000,zoom=8) %>% 

  addPolygons(data=m,weight=1, popup = map_popup2, fillOpacity = .5) %>% 

  addCircleMarkers(data = m, lng = ~long_xcord, 
                   lat = ~lat_ycord, radius=.5, color="green") %>%

  addCircles(data = m, lng = ~long_xcord, 
             lat = ~lat_ycord, popup = map_popup,
             radius = 4828.03, fillOpacity = 0.1, #3218.69 = 2 miles
             color = 'black', fillColor = 'blue',weight = 2, label=m$` name`) %>%

  addLegend("bottomright", pal = heat, values = m$pop,
            title = "Population",
            opacity = 1)

只需使用[ 這是?"SpatialPolygonsDataFrame-class"的修改示例

library(sp)

Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)

Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)

plot(0,0, xlim = c(0, 10), ylim = c(0, 5), type = "n")

for (i in 1:nrow(coordinates(SpP))) {
  plot(SpP[i, ], add = TRUE)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM