无法在 r 中使用 st_intersects() 绘制交叉点

Unable to Plot Intersections using st_intersects() in r

我将一些道路数字化为多线,将医院数字化为多点,将边界数字化为多边形,然后通过从 google 地图获取纬度和经度,使用简单要素 (SF) 库创建了多少条道路相交,并使用ggplot2 它运行良好。

然后我想检查并绘制有多少条道路与医院相交,并在它周围创建了一个 200 米的缓冲区,并尝试使用 st_intersects() 函数来实现相同的功能,使用它只给了 1:1作为回答和一条消息说

Sparse geometry binary predicate list of length 1, where the predicate was `intersects' 1: 1

当我尝试绘制它时,使用 ggplot 它给出了这个错误消息

Error: data must be a data frame, or other object coercible by fortify(), not an S3 object with class sgbp/list Run rlang::last_error() to see where the error occurred.

我在下面粘贴我的代码,

#install.packages(c('sf', 'ggplot'))

#loading packages

library(sf)
library(ggplot2)

#Map Boundry

mainPolygon <- rbind(c(26.853143819364902, 80.95281263110337), c(26.797230031536216, 80.95289846178616), c(26.7984558349077, 81.0750355234341), c(26.84854920653372, 81.07563633821381), c(26.853143819364902, 80.95281263110337))
mainPolygon <-  st_transform(st_sfc(st_polygon(list(mainPolygon[,2:1])), crs=4326), crs = 32643)

#Roads


#Shaheed Path is the largest road

ShaheedPath <- rbind(c(26.85253118543053, 81.00851675913755), c(26.849621299544577, 81.00765843742312), c(26.841120854626325, 81.0101475272248), c(26.838210645749143, 81.01229329429522), c(26.83583647258466, 81.01452489204844), c(26.831853876799062, 81.01366658522026), c(26.8299391174402, 81.01229329429522), c(26.815615691059342, 81.01452489204844), c(26.80190333587103, 81.00431104079327), c(26.79677035185694, 81.01212163292958), c(26.79661712488325, 80.99993367596964))

ShaheedPath <- st_transform(st_sfc(st_multilinestring(list(ShaheedPath[,2:1])),crs=4326), crs=32643)

#Several Smaller Roads

SmallRoad1 <- rbind(c(26.830322071901286, 81.01881642618926), c(26.828483878681173, 81.02439542057235),c(26.83139433750476, 81.0277428172022),c(26.82771795603623, 81.03151936724612))
SmallRoad1 <- st_transform(st_sfc(st_multilinestring(list(SmallRoad1[,2:1])),crs=4326), crs = 32643)

SmallRoad2 <- rbind(c(26.83070502506773, 81.0189880875549), c(26.823735074811907, 81.01315160112337), c(26.823735074811907, 81.01315160112337), c(26.82227975636335, 81.01212163292958))
SmallRoad2 <- st_transform(st_sfc(st_multilinestring(list(SmallRoad2[,2:1])),crs=4326), crs = 32643)

SmallRoad3 <- rbind(c(26.85153572609152, 81.00834508288565),c(26.85153572609152, 81.0231079603301))
SmallRoad3 <- st_transform(st_sfc(st_multilinestring(list(SmallRoad3[,2:1])),crs=4326), crs = 32643)

SmallRoad4 <- rbind(c(26.801979945813375, 81.00388188737921),c(26.80879802342012, 80.9873165655956),c(26.809181049325677, 80.98396916896576),c(26.815309287830313, 80.97143788927455),c(26.825956314301678, 80.96354146645542),c(26.812168606980094, 80.96302648235852),c(26.81232181294276, 80.96002240845995))
SmallRoad4 <- st_transform(st_sfc(st_multilinestring(list(SmallRoad4[,2:1])),crs=4326), crs = 32643)

SmallRoad5 <- rbind(c(26.85161230247983, 81.00800176015439),c(26.85283751764597, 81.00010533733527),c(26.85245463933146, 80.99675794070542))
SmallRoad5 <- st_transform(st_sfc(st_multilinestring(list(SmallRoad5[,2:1])),crs=4326), crs = 32643)

SmallRoad6 <- rbind(c(26.808721418083728, 81.0096325431279),c(26.80703608758896, 81.01186414088113),c(26.805580554730454, 81.00989003517635))
SmallRoad6 <- st_transform(st_sfc(st_multilinestring(list(SmallRoad6[,2:1])),crs=4326), crs = 32643)

#Hospitals

Hospitals <- rbind(c(26.806040198704164, 81.00989003517635),c(26.79853244717862, 81.01117749541861),c(26.830398662638146, 81.02293629896447),c(26.827258399963718, 81.03126187519767),c(26.85107626667357, 81.02362294442699),c(26.852531215098004, 80.99650044865697),c(26.81232181294276, 80.9602799005084),c(26.82174358169705, 81.01186414088113))

Hospitals <- st_transform(st_sfc(st_multipoint(Hospitals[,2:1]), crs = 4326), crs = 32643)

# 200 M Buffer Around Hospitals
Buffer200 <- st_buffer(Hospitals, dist = 200)

IntersectionShaheedPath <- st_intersects(Buffer200, ShaheedPath)
IntersectionRoad1 <- st_intersects(Buffer200, SmallRoad1)
IntersectionRoad2 <- st_intersects(Buffer200, SmallRoad2)
IntersectionRoad3 <- st_intersects(Buffer200, SmallRoad3)
IntersectionRoad4 <- st_intersects(Buffer200, SmallRoad4)
IntersectionRoad5 <- st_intersects(Buffer200, SmallRoad5)
IntersectionRoad6 <- st_intersects(Buffer200, SmallRoad6)

#Plotting

ggplot() +
  geom_sf(data = Buffer200, linetype = "dotted", fill = "transparent") +
  geom_sf(data = IntersectionShaheedPath, linetype = "dotted", fill = "transparent") + #this line or for that matter any of the intersections are giving an error
  geom_sf(data = mainPolygon, fill = "transparent") +
  geom_sf(data = Hospitals, size = 2, color = "red") +
  geom_sf(data = ShaheedPath, color = "grey") +
  geom_sf(data = SmallRoad1, color = "grey") +
  geom_sf(data = SmallRoad2, color = "grey") +
  geom_sf(data = SmallRoad3, color = "grey") +
  geom_sf(data = SmallRoad4, color = "grey") +
  geom_sf(data = SmallRoad5, color = "grey") +
  geom_sf(data = SmallRoad6, color = "grey")
#Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class sgbp/list
#Run `rlang::last_error()` to see where the error occurred.

在您的代码中,您使用 st_intersects 来创建道路特征。但是这个函数returns是一个布尔值,不是一个空间特征。请尝试 st_intersection()

我在制作地图几何图形时采用了错误的方法,后来明白了:

这是我的代码。

#install.packages(c('sf', 'ggplot'))

#loading packages

library(sf)
library(ggplot2)
library(dplyr)

#Map Boundry

mainPolygon <- rbind(c(26.853143819364902, 80.95281263110337), c(26.797230031536216, 80.95289846178616), c(26.7984558349077, 81.0750355234341), c(26.84854920653372, 81.07563633821381), c(26.853143819364902, 80.95281263110337))
mainPolygon <-  st_transform(st_sfc(st_polygon(list(mainPolygon[,2:1])), crs=4326), crs = 32643)

#Roads


#Shaheed Path is the largest road

ShaheedPath <- rbind(c(26.85253118543053, 81.00851675913755), c(26.849621299544577, 81.00765843742312), c(26.841120854626325, 81.0101475272248), c(26.838210645749143, 81.01229329429522), c(26.83583647258466, 81.01452489204844), c(26.831853876799062, 81.01366658522026), c(26.8299391174402, 81.01229329429522), c(26.815615691059342, 81.01452489204844), c(26.80190333587103, 81.00431104079327), c(26.79677035185694, 81.01212163292958), c(26.79661712488325, 80.99993367596964))
ShaheedPath <- list(ShaheedPath[,2:1])

#ShaheedPath <- st_transform(st_sfc(st_multilinestring(list(ShaheedPath[,2:1])),crs=4326), crs=32643)

#Several Smaller Roads

SmallRoad1 <- rbind(c(26.830322071901286, 81.01881642618926), c(26.828483878681173, 81.02439542057235),c(26.83139433750476, 81.0277428172022),c(26.82771795603623, 81.03151936724612))
SmallRoad1 <- list(SmallRoad1[,2:1])

SmallRoad2 <- rbind(c(26.83070502506773, 81.0189880875549), c(26.823735074811907, 81.01315160112337), c(26.823735074811907, 81.01315160112337), c(26.82227975636335, 81.01212163292958))
SmallRoad2 <- list(SmallRoad2[,2:1])

SmallRoad3 <- rbind(c(26.85153572609152, 81.00834508288565),c(26.85153572609152, 81.0231079603301))
SmallRoad3 <- list(SmallRoad3[,2:1])

SmallRoad4 <- rbind(c(26.801979945813375, 81.00388188737921),c(26.80879802342012, 80.9873165655956),c(26.809181049325677, 80.98396916896576),c(26.815309287830313, 80.97143788927455),c(26.825956314301678, 80.96354146645542),c(26.812168606980094, 80.96302648235852),c(26.81232181294276, 80.96002240845995))
SmallRoad4 <- list(SmallRoad4[,2:1])

SmallRoad5 <- rbind(c(26.85161230247983, 81.00800176015439),c(26.85283751764597, 81.00010533733527),c(26.85245463933146, 80.99675794070542))
SmallRoad5 <- list(SmallRoad5[,2:1])

SmallRoad6 <- rbind(c(26.808721418083728, 81.0096325431279),c(26.80703608758896, 81.01186414088113),c(26.805580554730454, 81.00989003517635))
SmallRoad6 <- list(SmallRoad6[,2:1])

RoadNames <- c("Shaheed Path","Small Road 1","Small Road 2", "Small Road 3", "Small Road 4", "Small Road 5", "Small Road 6")
RoadGeom <- st_sfc(st_multilinestring(ShaheedPath),
                   st_multilinestring(SmallRoad1),
                   st_multilinestring(SmallRoad2),
                   st_multilinestring(SmallRoad3),
                   st_multilinestring(SmallRoad4),
                   st_multilinestring(SmallRoad5),
                   st_multilinestring(SmallRoad6))
RoadShp <- cbind.data.frame(Roadname = RoadNames, Geometry = RoadGeom)

#transforming to geographic data
RoadShp <- st_transform(st_sf(RoadShp, crs = 4326), crs = 32643)

#Hospitals

HospitalGeom <- st_sfc(st_point(c(81.00989003517635, 26.806040198704164)),st_point(c(81.01117749541861, 26.79853244717862)),st_point(c(81.02293629896447, 26.830398662638146)),st_point(c(81.03126187519767, 26.827258399963718)),st_point(c(81.02362294442699, 26.85107626667357)),st_point(c(80.99650044865697, 26.852531215098004)),st_point(c(80.9602799005084, 26.81232181294276)),st_point(c(81.01186414088113, 26.82174358169705)))

HospitalName <- c("Hospital 1","Hospital 2", "Hospital 3", "Hospital 4", "Hospital 5", "Hospital 6", "Hospital 7", "Hospital 8")

HospitalShp <- cbind.data.frame(Hospitalname = HospitalName, Geometry = HospitalGeom)

#transforming to geographic data
HospitalShp <- st_transform(st_sf(HospitalShp, crs = 4326), crs = 32643)

# 200 M Buffer Around Hospitals
Buffer200 <- st_buffer(HospitalShp, dist = 200)

# 500 M Buffer Around Home
Buffer500 <- st_buffer(HomeShp, dist = 500)

IntersectionRoad <- Buffer200 %>%
  filter(st_intersects(., RoadShp, sparse = FALSE))

#Plotting

ggplot() +
  geom_sf(data = Buffer200, linetype = "dotted", fill = "transparent") +
  geom_sf(data = Buffer500, linetype = "dotted", fill = "transparent") +
  geom_sf(data = mainPolygon, fill = "transparent") +
  geom_sf(data = RoadShp, color = "grey") +
  geom_sf(data = IntersectionRoad, size = 1, color = "red")  +
  geom_sf_text(data = HospitalShp, mapping = aes(label = HospitalName))