[英]How to loop through polygons in a shapefile in r?
I have created a map where i have plotted population density with a shapefile using the Leaflet package. 我已经创建了一张地图,在其中使用Leaflet软件包使用shapefile绘制了人口密度。 I'am trying to loop through each polygon on the map, is this possible and how could do it? 我正在尝试遍历地图上的每个多边形,这可能吗,怎么办?
**Map: Leaflet map **地图:单张地图
#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)
Just use [
. 只需使用[
。 Here's a modified example from ?"SpatialPolygonsDataFrame-class"
这是?"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.