使用SF将空间坐标集转换为R中的多边形

时间:2018-10-05 16:23:42

标签: r sf

列表中的每个元素都包含一组空间坐标,我希望使用sf将其转换为多边形。每一组坐标按我想“连接点”的顺序排序,并且第一行和最后一行相同,以关闭多边形。每个列表元素都有一个唯一的标识符命名,我希望将其保留为sf输出中的属性。

我在这里修改了与科幻小说相关的答案中的代码:

Convert sequence of longitude and latitude to polygon via sf in R

但是我的情况有所不同,因为我有多组坐标(每个坐标都应生成一个单独的多边形),而该问题只有一组坐标(导致一个多边形)。

我的具体问题是如何使用sf生成一个sf多边形对象,该对象在单独的行中包含多个多边形,每个多边形都是使用列表元素之一中的坐标创建的。

在此先感谢您的任何建议或帮助。

标记


我的示例数据来自dput(),位于该问题的末尾,我的代码为:

points_df<-arrange(dat,SitePondGpsRep,DateTime_local) #sort on DateTime_local for proper sequence

points_df<-dplyr::select(points_df,SitePondGpsRep,Longitude,Latitude) #drop columns, for upcoming st_polygon call (requires numerics only)

points_ls<-split(points_df,points_df$SitePondGpsRep) #dataframe to list

points_ls<-lapply(points_ls, function(x) { x["SitePondGpsRep"] <- NULL; x }) #delete SitePondGpsRep column, it’s retained in list names

points_ls<-lapply(points_ls,function(x) {as.matrix(x)}) #convert to matrix for upcoming st_sf call

points_ls<-lapply(points_ls,function(x) {rbind(x,x[1,])}) #close poly, first and last point must be same

polys <- st_sf(st_sfc(st_polygon(points_ls)), crs = 4326) #create polys, but only one polygon is created when I expected three polygons

str(polys); glimpse(polys); plot(polys) #check output

样本数据:

dat <- structure(list(SitePondGpsRep = c("BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", "BURR-1-1-1", 
"BURR-1-1-1", "BURR-1-1-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", 
"BURR-1-3-1", "BURR-1-3-1", "BURR-1-3-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", 
"BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1", "BURR-1-4-1"
), DateTime_local = c("2018-05-30 10:49:04", "2018-05-30 10:49:05", 
"2018-05-30 10:49:06", "2018-05-30 10:49:07", "2018-05-30 10:49:08", 
"2018-05-30 10:49:09", "2018-05-30 10:49:10", "2018-05-30 10:49:27", 
"2018-05-30 10:49:28", "2018-05-30 10:49:29", "2018-05-30 10:49:30", 
"2018-05-30 10:49:31", "2018-05-30 10:49:32", "2018-05-30 10:49:33", 
"2018-05-30 10:49:34", "2018-05-30 10:49:35", "2018-05-30 10:49:36", 
"2018-05-30 10:49:37", "2018-05-30 10:49:38", "2018-05-30 10:49:39", 
"2018-05-30 10:49:40", "2018-05-30 10:49:41", "2018-05-30 10:49:42", 
"2018-05-30 10:49:43", "2018-05-30 10:49:44", "2018-05-30 10:49:45", 
"2018-05-30 10:49:46", "2018-05-30 10:49:47", "2018-05-30 10:49:48", 
"2018-05-30 10:49:49", "2018-05-30 10:49:50", "2018-05-30 10:49:51", 
"2018-05-30 10:49:52", "2018-05-30 10:49:54", "2018-05-30 10:49:55", 
"2018-05-30 10:49:56", "2018-05-30 10:49:57", "2018-05-30 10:49:58", 
"2018-05-30 10:50:01", "2018-05-30 10:50:02", "2018-05-30 10:50:03", 
"2018-05-30 10:50:04", "2018-05-30 10:50:05", "2018-05-30 10:50:06", 
"2018-05-30 10:50:07", "2018-05-30 10:50:09", "2018-05-30 10:50:10", 
"2018-05-30 10:50:11", "2018-05-30 10:50:12", "2018-05-30 10:50:13", 
"2018-05-30 10:50:14", "2018-05-30 10:50:15", "2018-05-30 10:50:16", 
"2018-05-30 10:50:17", "2018-05-30 10:50:18", "2018-05-30 10:50:20", 
"2018-05-30 10:50:24", "2018-05-30 10:50:27", "2018-05-30 10:50:36", 
"2018-05-30 10:50:41", "2018-05-30 10:50:42", "2018-05-30 10:50:43", 
"2018-05-30 10:50:44", "2018-05-30 10:50:45", "2018-05-30 10:49:05", 
"2018-05-30 10:49:06", "2018-05-30 10:49:07", "2018-05-30 10:49:08", 
"2018-05-30 10:49:09", "2018-05-30 10:49:10", "2018-05-30 10:49:11", 
"2018-05-30 10:49:12", "2018-05-30 10:49:13", "2018-05-30 10:49:19", 
"2018-05-30 10:49:31", "2018-05-30 10:49:32", "2018-05-30 10:49:33", 
"2018-05-30 10:49:34", "2018-05-30 10:49:35", "2018-05-30 10:49:36", 
"2018-05-30 10:49:37", "2018-05-30 10:49:38", "2018-05-30 10:49:39", 
"2018-05-30 10:49:40", "2018-05-30 10:49:41", "2018-05-30 10:49:42", 
"2018-05-30 10:49:43", "2018-05-30 10:49:44", "2018-05-30 10:49:45", 
"2018-05-30 10:49:46", "2018-05-30 10:49:47", "2018-05-30 10:49:48", 
"2018-05-30 10:49:49", "2018-05-30 10:49:50", "2018-05-30 10:49:51", 
"2018-05-30 10:49:52", "2018-05-30 10:49:53", "2018-05-30 10:49:54", 
"2018-05-30 10:49:55", "2018-05-30 10:49:56", "2018-05-30 10:49:57", 
"2018-05-30 10:49:58", "2018-05-30 10:49:59", "2018-05-30 10:50:00", 
"2018-05-30 10:50:01", "2018-05-30 10:50:02", "2018-05-30 10:50:03", 
"2018-05-30 10:50:04", "2018-05-30 10:50:05", "2018-05-30 10:50:06", 
"2018-05-30 10:50:07", "2018-05-30 10:50:10", "2018-05-30 10:50:11", 
"2018-05-30 10:50:12", "2018-05-30 10:50:13", "2018-05-30 10:50:14", 
"2018-05-30 10:50:15", "2018-05-30 10:50:16", "2018-05-30 10:50:37", 
"2018-05-30 10:50:44", "2018-05-30 10:49:05", "2018-05-30 10:49:06", 
"2018-05-30 10:49:07", "2018-05-30 10:49:08", "2018-05-30 10:49:09", 
"2018-05-30 10:49:10", "2018-05-30 10:49:11", "2018-05-30 10:49:12", 
"2018-05-30 10:49:19", "2018-05-30 10:49:21", "2018-05-30 10:49:22", 
"2018-05-30 10:49:26", "2018-05-30 10:49:27", "2018-05-30 10:49:30", 
"2018-05-30 10:49:31", "2018-05-30 10:49:32", "2018-05-30 10:49:33", 
"2018-05-30 10:49:34", "2018-05-30 10:49:35", "2018-05-30 10:49:36", 
"2018-05-30 10:49:37", "2018-05-30 10:49:38", "2018-05-30 10:49:39", 
"2018-05-30 10:49:40", "2018-05-30 10:49:41", "2018-05-30 10:49:42", 
"2018-05-30 10:49:43", "2018-05-30 10:49:44", "2018-05-30 10:49:45", 
"2018-05-30 10:49:46", "2018-05-30 10:49:47", "2018-05-30 10:49:48", 
"2018-05-30 10:49:49", "2018-05-30 10:49:50", "2018-05-30 10:49:51", 
"2018-05-30 10:49:52", "2018-05-30 10:49:54", "2018-05-30 10:49:57", 
"2018-05-30 10:49:58", "2018-05-30 10:49:59", "2018-05-30 10:50:00", 
"2018-05-30 10:50:01", "2018-05-30 10:50:02", "2018-05-30 10:50:03", 
"2018-05-30 10:50:04", "2018-05-30 10:50:05", "2018-05-30 10:50:06", 
"2018-05-30 10:50:07", "2018-05-30 10:50:08", "2018-05-30 10:50:09", 
"2018-05-30 10:50:10", "2018-05-30 10:50:11", "2018-05-30 10:50:12", 
"2018-05-30 10:50:13", "2018-05-30 10:50:14", "2018-05-30 10:50:15", 
"2018-05-30 10:50:16"), Latitude = c(51.9851623569, 51.9851641171, 
51.9851674698, 51.9851741754, 51.9851825573, 51.9851923641, 51.9852027576, 
51.9853603374, 51.985360086, 51.9853615109, 51.9853631873, 51.9853626005, 
51.9853596669, 51.9853546377, 51.9853501953, 51.9853491057, 51.9853499439, 
51.9853510335, 51.9853526261, 51.9853537157, 51.9853544701, 51.9853550568, 
51.9853562303, 51.985358661, 51.9853618462, 51.985365618, 51.9853699766, 
51.9853755087, 51.9853831362, 51.9853900932, 51.9853944518, 51.9853973016, 
51.9854001515, 51.9854111318, 51.9854149874, 51.9854135625, 51.985412389, 
51.9854097068, 51.9853739161, 51.9853589125, 51.9853450824, 51.9853315037, 
51.9853169192, 51.9853025861, 51.9852880016, 51.985260509, 51.9852461759, 
51.9852311723, 51.9852169231, 51.9852023385, 51.9851880893, 51.985174343, 
51.9851596747, 51.9851456769, 51.9851331878, 51.9851182681, 51.9851253927, 
51.9851476047, 51.9851814676, 51.9851861615, 51.9851861615, 51.9851835631, 
51.9851810485, 51.985178953, 51.9851332717, 51.9851353671, 51.9851413183, 
51.9851502031, 51.9851596747, 51.9851695653, 51.9851823058, 51.9851954654, 
51.9852077868, 51.9852676336, 51.9853508659, 51.9853491057, 51.9853438251, 
51.9853392988, 51.9853389636, 51.985339215, 51.9853398018, 51.9853422325, 
51.9853455853, 51.9853481837, 51.9853506982, 51.9853537995, 51.9853565656, 
51.9853595831, 51.9853626005, 51.9853671268, 51.9853722397, 51.9853787776, 
51.9853857346, 51.9853908475, 51.9853956252, 51.9854015764, 51.9854076114, 
51.9854149874, 51.9854189269, 51.9854177535, 51.9854149874, 51.985412892, 
51.9854062703, 51.985392943, 51.9853766821, 51.9853594154, 51.9853420649, 
51.9853279833, 51.9853130635, 51.9852968026, 51.9852817152, 51.9852411468, 
51.9852255564, 51.9852114748, 51.9851971418, 51.9851819705, 51.9851673022, 
51.9851524662, 51.985171577, 51.9851645362, 51.9851415697, 51.9851436652, 
51.9851481915, 51.9851561543, 51.9851645362, 51.9851733372, 51.9851848204, 
51.9851983991, 51.9852668792, 51.9852864929, 51.9853002392, 51.9853428192, 
51.9853453338, 51.9853506144, 51.9853511173, 51.9853491057, 51.9853441603, 
51.9853404723, 51.9853403047, 51.9853417296, 51.9853436574, 51.9853462558, 
51.985348016, 51.9853496924, 51.9853510335, 51.9853532966, 51.9853569008, 
51.9853605051, 51.9853646122, 51.9853681326, 51.9853726588, 51.9853808731, 
51.985391099, 51.9853972178, 51.9854009897, 51.9854052644, 51.9854188431, 
51.9854213577, 51.985419346, 51.985411467, 51.9853983074, 51.9853829686, 
51.9853670429, 51.9853521232, 51.9853375386, 51.9853222836, 51.9853064418, 
51.9852899294, 51.9852756802, 51.9852615986, 51.9852463435, 51.9852309208, 
51.9852158334, 51.9852004945, 51.9851861615, 51.9851720799, 51.9851580821
), Longitude = c(-105.0767748244, -105.0767996348, -105.0768228527, 
-105.0768438913, -105.0768627506, -105.0768831186, -105.0768996309, 
-105.0768738147, -105.0768491719, -105.0768251996, -105.0768006407, 
-105.0767758302, -105.0767515227, -105.0767283887, -105.0767055061, 
-105.0766806956, -105.0766552985, -105.0766305719, -105.0766069349, 
-105.0765823759, -105.0765584037, -105.0765349343, -105.0765112974, 
-105.076487828, -105.0764643587, -105.0764413923, -105.0764174201, 
-105.0763948727, -105.0763716549, -105.0763481855, -105.0763243809, 
-105.0763000734, -105.0762776099, -105.0762347784, -105.0762113091, 
-105.0761888456, -105.0761658791, -105.0761429127, -105.0761163421, 
-105.0761119835, -105.0761065353, -105.076102512, -105.0761000812, 
-105.076098321, -105.0760969799, -105.0760942139, -105.0760934595, 
-105.0760923699, -105.0760906935, -105.0760900229, -105.0760909449, 
-105.0760921184, -105.0760919508, -105.0760952197, -105.0761054456, 
-105.0761408173, -105.0762129016, -105.0762677193, -105.0764359441, 
-105.0765430648, -105.0765658636, -105.0765891653, -105.0766126346, 
-105.0766350981, -105.0767853856, -105.0768098608, -105.076832911, 
-105.0768533628, -105.0768733118, -105.0768928416, -105.0769080129, 
-105.0769187417, -105.0769294705, -105.0769564603, -105.0768144708, 
-105.0767890736, -105.0767669454, -105.0767435599, -105.0767160673, 
-105.0766881555, -105.0766630936, -105.0766382832, -105.076612886, 
-105.0765872374, -105.0765617564, -105.076536946, -105.0765135605, 
-105.0764897559, -105.0764658675, -105.0764395483, -105.0764154922, 
-105.0763928611, -105.0763694756, -105.0763450842, -105.0763208605, 
-105.0762992352, -105.0762787834, -105.0762574095, -105.0762354489, 
-105.0762114767, -105.076188175, -105.0761637837, -105.0761408173, 
-105.0761313457, -105.0761326868, -105.076130759, -105.0761248916, 
-105.076120114, -105.0761155877, -105.0761135761, -105.0761135761, 
-105.0761101395, -105.0761072896, -105.0761061162, -105.0761060324, 
-105.0761067867, -105.0761083793, -105.0761087146, -105.0764583237, 
-105.0766065158, -105.0767834578, -105.0768087711, -105.0768324919, 
-105.0768561289, -105.0768786762, -105.0768972002, -105.0769114494, 
-105.0769231003, -105.0769605674, -105.0769288, -105.076918155, 
-105.0768983737, -105.0768753234, -105.076808352, -105.0767832901, 
-105.0767600723, -105.0767381117, -105.0767147262, -105.076689329, 
-105.0766637642, -105.0766387023, -105.0766154006, -105.0765913446, 
-105.0765661988, -105.0765423942, -105.0765190087, -105.0764952041, 
-105.0764719862, -105.076448014, -105.0764254667, -105.0764035899, 
-105.0763796177, -105.0763540529, -105.0763287395, -105.0763040129, 
-105.0762819685, -105.0762406457, -105.0761751831, -105.0761494506, 
-105.0761289988, -105.076121036, -105.0761198625, -105.0761174317, 
-105.0761130732, -105.076108044, -105.0761036016, -105.0761017576, 
-105.0761008356, -105.0760996621, -105.0760973152, -105.0760949682, 
-105.0760931242, -105.0760921184, -105.0760929566, -105.0760953873, 
-105.0760967284, -105.0760972314)), class = "data.frame", row.names = c(NA, 
-177L))

2 个答案:

答案 0 :(得分:3)

与使用splitlapply相比,更简单的方法是利用sf的功能与dplyr工具(尤其是分组操作)一起良好地工作。我们可以:

  1. 使用coords的{​​{1}}参数为多边形的每个顶点创建点,
  2. st_as_sf您的ID和group_by,将每个多边形的点合并为summarise
  3. 使用MULTIPOINT转换为st_cast,在每个顶点之间画线。
POLYGON

reprex package(v0.2.0)于2018-10-05创建。

答案 1 :(得分:0)

从20191004开始在CRAN上的

library(sfheaders)可将data.frames转换为sf对象。

library(sf)
library(sfheaders)

sf <- sfheaders::sf_polygon(
  obj = dat
  , x = "Longitude"
  , y = "Latitude"
  , polygon_id = "SitePondGpsRep"
)

sf
# Simple feature collection with 3 features and 1 field
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: -105.077 ymin: 51.98512 xmax: -105.0761 ymax: 51.98542
# epsg (SRID):    NA
# proj4string:    
#   id                       geometry
# 1 BURR-1-1-1 POLYGON ((-105.0768 51.9851...
# 2 BURR-1-3-1 POLYGON ((-105.0768 51.9851...
# 3 BURR-1-4-1 POLYGON ((-105.0768 51.9851...