r - 组合多边形保持宽度
问题描述
如果长度小于 6000 英尺,我想组合多边形。我附上了一个我想合并的区域的例子。我想用一个脚本来做这件事,因为我有成千上万的多边形要经过。任何帮助深表感谢!
源数据:
structure(list(OBJECTID = c(18, 85, 130, 171, 173, 195, 212,
255, 278, 295, 398, 402, 404, 410, 414, 608, 611, 616, 620, 622,
191535, 191596, 191643, 191679), geometry = structure(list(structure(list(
structure(c(790772.665095329, 795904.962015067, 795472.274595637,
794819.684650924, 789702.337778083, 790772.665095329, 55111.376137689,
54042.8837942318, 52039.046812771, 48862.3220103642, 49927.461344996,
55111.376137689), .Dim = c(6L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(796612.78343804, 795904.962015067,
790772.665095329, 791304.482601099, 791579.937704929, 791852.041080199,
796999.435683555, 796612.78343804, 57336.4489387319, 54042.8837942318,
55111.376137689, 57716.054447086, 58991.1885930025, 60295.399870716,
59201.7374421435, 57336.4489387319), .Dim = c(8L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(797734.154390534,
797201.624709836, 796999.435683555, 791852.041080199, 792383.947950958,
792912.660267584, 795484.332670061, 798093.692520351, 797734.154390534,
62633.767892387, 60203.5702778084, 59201.7374421435, 60295.399870716,
62863.7415608288, 65428.4236166867, 64890.6613532156, 64342.4628359582,
62633.767892387), .Dim = c(9L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(795484.332670061, 792912.660267584,
793994.470344538, 799177.835454204, 798842.652002334, 798093.692520351,
795484.332670061, 64890.6613532156, 65428.4236166867, 70612.5829219612,
69534.0760361488, 67931.049719191, 64342.4628359582, 64890.6613532156
), .Dim = c(7L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(794819.684650924, 799636.528996167, 799981.263539694,
799763.980295563, 799625.316078716, 798915.162525192, 793752.758832907,
794819.684650924, 48862.3220103642, 47857.532466591, 47787.3435792962,
46712.6490712543, 46002.2938342407, 42603.1707699664, 43692.8731840489,
48862.3220103642), .Dim = c(8L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(799636.528996167, 794819.684650924,
795472.274595637, 799601.503665015, 799636.528996167, 47857.532466591,
48862.3220103642, 52039.046812771, 52138.699895762, 47857.532466591
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(799601.503665015, 795472.274595637, 795904.962015067,
796612.78343804, 799559.038835281, 799601.503665015, 52138.699895762,
52039.046812771, 54042.8837942318, 57336.4489387319, 57416.4914524728,
52138.699895762), .Dim = c(6L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(797734.154390534, 799504.161914641,
799519.096766394, 799523.689747001, 799559.038835281, 796612.78343804,
796999.435683555, 797201.624709836, 797734.154390534, 62633.767892387,
62679.6580485063, 60257.1450638027, 60060.7433085733, 57416.4914524728,
57336.4489387319, 59201.7374421435, 60203.5702778084, 62633.767892387
), .Dim = c(9L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(798667.422087686, 798915.162525192, 799625.316078716,
799675.110751155, 798667.422087686, 41404.585298834, 42603.1707699664,
46002.2938342407, 41415.711561777, 41404.585298834), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(797734.154390534, 798093.692520351, 798842.652002334,
799477.407371662, 799500.099509694, 799504.161914641, 797734.154390534,
62633.767892387, 64342.4628359582, 67931.049719191, 67943.0558978718,
65320.5209551165, 62679.6580485063, 62633.767892387), .Dim = c(7L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(799625.316078716, 799763.980295563, 802088.724803159,
802223.263591026, 804882.877008304, 804922.838745296, 802128.435622406,
799675.110751155, 799625.316078716, 46002.2938342407, 46712.6490712543,
46750.6814760173, 46752.8926281596, 46787.5460917902, 41527.9361136832,
41468.0690686702, 41415.711561777, 46002.2938342407), .Dim = c(9L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(799636.528996167, 799601.503665015, 802047.960395525,
802119.914713131, 804823.025000485, 804882.877008304, 802223.263591026,
802088.724803159, 799763.980295563, 799981.263539694, 799636.528996167,
47857.532466591, 52138.699895762, 52171.1430120492, 52172.0902911819,
52192.5345130677, 46787.5460917902, 46752.8926281596, 46750.6814760173,
46712.6490712543, 47787.3435792962, 47857.532466591), .Dim = c(11L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(802114.494253742, 804813.536856739, 804823.025000485,
802119.914713131, 802047.960395525, 799601.503665015, 799559.038835281,
802008.148001249, 802114.494253742, 57464.7329639106, 57554.278370772,
52192.5345130677, 52172.0902911819, 52171.1430120492, 52138.699895762,
57416.4914524728, 57462.7230153075, 57464.7329639106), .Dim = c(9L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(802114.494253742, 802008.148001249, 799559.038835281,
799523.689747001, 799519.096766394, 799504.161914641, 801968.3458121,
804782.760205713, 804787.182601505, 804813.536856739, 802114.494253742,
57464.7329639106, 57462.7230153075, 57416.4914524728, 60060.7433085733,
60257.1450638027, 62679.6580485063, 62750.767555581, 62832.1968387173,
60194.9664029238, 57554.278370772, 57464.7329639106), .Dim = c(11L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(804739.496925173, 804761.88531762, 804762.773230746,
804769.592161392, 804782.760205713, 801968.3458121, 799504.161914641,
799500.099509694, 799477.407371662, 801928.73953879, 802019.409522007,
804739.496925173, 68106.3930222505, 66807.9401796348, 65458.3953441062,
64152.5487757422, 62832.1968387173, 62750.767555581, 62679.6580485063,
65320.5209551165, 67943.0558978718, 68010.5408628952, 68013.0434007888,
68106.3930222505), .Dim = c(12L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(804922.838745296, 804882.877008304,
810114.14861501, 810145.907565417, 804922.838745296, 41527.9361136832,
46787.5460917902, 46900.4548796072, 41589.8156082104, 41527.9361136832
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(810110.073021085, 810118.538415086, 810114.14861501,
804882.877008304, 804823.025000485, 810110.073021085, 52269.5158709572,
49563.208246226, 46900.4548796072, 46787.5460917902, 52192.5345130677,
52269.5158709572), .Dim = c(6L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(810110.073021085, 804823.025000485,
804813.536856739, 807434.479329642, 810089.844191036, 810110.073021085,
52269.5158709572, 52192.5345130677, 57554.278370772, 57628.8701270838,
57700.2990023741, 52269.5158709572), .Dim = c(6L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(804813.536856739,
804787.182601505, 804782.760205713, 807390.853225214, 810055.199132969,
810061.919256549, 810089.844191036, 807434.479329642, 804813.536856739,
57554.278370772, 60194.9664029238, 62832.1968387173, 62899.4318649497,
62970.9290954731, 60493.7685195097, 57700.2990023741, 57628.8701270838,
57554.278370772), .Dim = c(9L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(804739.496925173, 807375.326414109,
810020.487459965, 810055.199132969, 807390.853225214, 804782.760205713,
804769.592161392, 804762.773230746, 804761.88531762, 804739.496925173,
68106.3930222505, 68173.8400793809, 68248.8573495335, 62970.9290954731,
62899.4318649497, 62832.1968387173, 64152.5487757422, 65458.3953441062,
66807.9401796348, 68106.3930222505), .Dim = c(10L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(790772.665095329,
789702.337778083, 787092.050596808, 784500.54150305, 785549.734241135,
786884.535917274, 790772.665095329, 55111.376137689, 49927.461344996,
50476.317793333, 51036.4057055915, 56216.3557296265, 55916.5970048284,
55111.376137689), .Dim = c(7L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(789226.847106716, 791852.041080199,
791579.937704929, 791304.482601099, 790772.665095329, 786884.535917274,
785549.734241135, 786629.889714253, 787954.997612727, 789226.847106716,
60843.9708908502, 60295.399870716, 58991.1885930025, 57716.054447086,
55111.376137689, 55916.5970048284, 56216.3557296265, 61392.938091041,
61111.3484171478, 60843.9708908502), .Dim = c(10L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(789226.847106716,
787954.997612727, 786629.889714253, 787159.189681444, 787716.264357803,
792912.660267584, 792383.947950958, 791852.041080199, 789226.847106716,
60843.9708908502, 61111.3484171478, 61392.938091041, 63939.3317737952,
66525.9576669169, 65428.4236166867, 62863.7415608288, 60295.399870716,
60843.9708908502), .Dim = c(9L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(788606.470246906, 789702.337778083,
794819.684650924, 793752.758832907, 788606.470246906, 44765.2468782581,
49927.461344996, 48862.3220103642, 43692.8731840489, 44765.2468782581
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, crs = structure(list(
input = "+init=epsg:2257", wkt = "PROJCRS[\"NAD83 / New Mexico East (ftUS)\",\n BASEGEOGCRS[\"NAD83\",\n DATUM[\"North American Datum 1983\",\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4269]],\n CONVERSION[\"SPCS83 New Mexico East zone (US Survey feet)\",\n METHOD[\"Transverse Mercator\",\n ID[\"EPSG\",9807]],\n PARAMETER[\"Latitude of natural origin\",31,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8801]],\n PARAMETER[\"Longitude of natural origin\",-104.333333333333,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8802]],\n PARAMETER[\"Scale factor at natural origin\",0.999909091,\n SCALEUNIT[\"unity\",1],\n ID[\"EPSG\",8805]],\n PARAMETER[\"False easting\",541337.5,\n LENGTHUNIT[\"US survey foot\",0.304800609601219],\n ID[\"EPSG\",8806]],\n PARAMETER[\"False northing\",0,\n LENGTHUNIT[\"US survey foot\",0.304800609601219],\n ID[\"EPSG\",8807]],\n ID[\"EPSG\",15339]],\n CS[Cartesian,2],\n AXIS[\"(E)\",east,\n ORDER[1],\n LENGTHUNIT[\"US survey foot\",0.304800609601219,\n ID[\"EPSG\",9003]]],\n AXIS[\"(N)\",north,\n ORDER[2],\n LENGTHUNIT[\"US survey foot\",0.304800609601219,\n ID[\"EPSG\",9003]]],\n USAGE[\n SCOPE[\"unknown\"],\n AREA[\"USA - New Mexico - SPCS - E\"],\n BBOX[32,-105.72,37,-102.99]]]"), class = "crs"), class = c("sfc_POLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = 784500.54150305,
ymin = 41404.585298834, xmax = 810145.907565417, ymax = 70612.5829219612
), class = "bbox"))), row.names = c(NA, 24L), sf_column = "geometry", agr = structure(c(OBJECTID = NA_integer_), .Label = c("constant",
"aggregate", "identity"), class = "factor"), class = c("sf",
"tbl_df", "tbl", "data.frame"))
更新:如果多边形朝北/朝南,下面的代码效果很好。但是,当多边形是角度时,我相信质心将无法用作过滤器和分组功能。
polygon_corrds<- st_centroid(polygon)
polygon_corrds<- st_coordinates(polygon_corrds)%>%as_tibble()
polygon<- polygon%>%mutate(RowID=row_number())
polygon_corrds<- polygon_corrds%>%mutate(RowID=row_number())
polygon<- left_join(polygon,polygon_corrds)
polygon<-polygon%>%ungroup()%>%arrange(X,Y)%>%mutate(RowID2=row_number())
polygon<- polygon%>%mutate(RowID3= ifelse( RowID %%2==0,RowID,RowID2+1))
polygonx=polygon %>%
group_by(RowID3) %>%
summarise(geometry = sf::st_union(geometry)) %>%
ungroup()
ggplot(polygonx) +
geom_sf(aes(fill=as.factor(RowID3)))+theme(legend.position = 'none')
解决方案
推荐阅读
- r - 在 R 中使用 Akima 插值获得 NA
- python - Python 绘图显示异常缩放
- javascript - 如何记忆选择器数组?
- primefaces - PrimeFaces 中 RichFaces 的“rich:findComponent”函数的类似物是什么?
- javascript - Three.js GPGPUComputationRenderer 和 OES_texture_float
- mongodb - 无法远程连接到mongodb
- python - 以相反的顺序打印映射项
- java - 使用 CSVBeanReader 忽略不需要的列
- css - 雷跟随十字准线
- java - BLE 发送/接收;尝试从空对象引用上的“android.bluetooth.BluetoothGattCallback BluetoothLeService.mgattCallback”字段读取