I digitized some roads as multilines, hospitals as multipoints, boundary as polygon, then created how many roads intersect using the Simple Features (SF) library by getting latitudes and longitudes from google maps and plotted it using ggplot2 it worked well.
I then wanted to check and plot how many roads intersects with a hospital and created a 200mtr buffer around it and tried using st_intersects() function for the same, using this only gave 1:1 as answer and a message saying
Sparse geometry binary predicate list of length 1, where the predicate was `intersects' 1: 1
And when I tried plotting it, using ggplot it gives this error message
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.
I am pasting my code below:
#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.