简体   繁体   English

如何在r中的shapefile中循环遍历多边形?

[英]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.

相关问题 如何遍历 R 中单个 shapefile 中的多个重叠多边形以获取每个多边形的区域统计信息? - How can I iterate through multiple overlapping polygons in a single shapefile in R to get zonal statistics of each polygon? 如何从R中的另一个shapefile获取包含多边形质心的shapefile的多边形? - How to get polygons of a shapefile containing centroids of polygons from another shapefile in R? R:将栅格聚合为 shapefile 多边形 - R: Aggregating raster to shapefile polygons 使用R将特定颜色分配给shapefile中的多边形 - Assigning specific colors to polygons in a shapefile using R R:将多边形从Shapefile 1匹配到shapefile 2中的区号 - R: Match Polygons from Shapefile 1 to Area Codes in shapefile 2 R 如何合并具有多个多边形的 shapefile 中的多边形特征? (可重现的代码示例) - R How do I merge polygon features in a shapefile with many polygons? (reproducible code example) 如何从大型shapefile计算多边形的面积 - How to calculate area of polygons from a large shapefile 如何在R中按文件夹循环切割带有shapefile对象的栅格? - How to cut rasters with a shapefile object in loop by folder in R? 尝试在 R 中将空间多边形导出为 shapefile 时 writeOGR 出错 - Error in writeOGR when trying to export spatial polygons as shapefile in R R ggplot2与shapefile和csv数据合并以填充特定的多边形 - R ggplot2 merge with shapefile and csv data to fill polygons - specific
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM