'How to clip SHP (road map) based on 4 lines (roads)?

How to clip SHP (road map) based on 4 lines (roads)?

I have OSM street grid from http://download.geofabrik.de/ and I want to clip it only to selected city centre (based on inner ring) and to each street I have assigned value (that will determine colour on visualization). The data I have is for every segment of a road (from intersection to intersection) and I would like to visualize it on that level. For ease of argument lets say it is number of cars parked. The data I have is only determined in subset all roads in analysed city. I am trying to visualize it now. I have a vision, but lack skills.

Dummy data:

Street_Grid <- data.frame(
  x = c(rep(seq(0, 9, 1), 2), rep(c(0, 9), each = 10)),
  y = c(rep(c(0, 9), each = 10), rep(seq(0, 9, 1), 2)),
  label = c(rep(seq(0, 9, 1), 2), rep(seq(10, 19, 1), 2)),
  data = runif(20, 1, 10)
) %>% 
  sf::st_as_sf(coords = c("x", "y")) %>% 
  sf::st_set_crs(4326)


Street_Grid_SHP <- Street_Grid %>% group_by(label) %>% summarize(m = mean(data)) %>% st_cast("LINESTRING")


Border <- Street_Grid_SHP %>% filter(label %in% c(4, 6, 14, 16))


ggplot() + 
  theme(panel.grid.major = element_line(colour = "transparent")) + 
  geom_sf(data = Street_Grid_SHP, size = 1) + 
  geom_sf(data = Border, size = 2, colour = "red")

Initail roads

And I would like the results to look something like this:

  • all other roads clipped, but still visible to set the scene
  • borders of analysis clearly visible
  • roads inside borders to have colour determined by variable

.

#subset the roads into a circle.
pt <- data.frame(lat = 5, long = 5) # center point 
pt_trans <- pt %>% st_as_sf(coords = c("long", "lat"), crs = 4326)
st_transform(pt_trans, 4326)
pt_trans_2 <- st_transform(pt_trans, crs) 
circle <- st_buffer(pt_trans_2, dist = 500000)  #radius, in meters, around the center point to map
circle <- circle %>% st_transform(st_crs(Street_Grid_SHP))
Street_Grid_cliped <- st_intersection(circle, Street_Grid_SHP)
   

  
Target_Border <- data.frame(
  x = c(4, 4, 6, 6, 4, 6, 4, 6),
  y = c(4, 6, 4, 6, 4, 4, 6, 6),
  label = c(1, 1, 2, 2, 3, 3, 4, 4),
  data = 1) %>% 
  sf::st_as_sf(coords = c("x", "y")) %>% 
  sf::st_set_crs(4326) %>% 
  group_by(label) %>% 
  summarize(m = mean(data)) %>% 
  st_cast("LINESTRING")

Targer_Street_Grid <- data.frame(
  x = c(5, 5, 5, 5, 4, 5, 5, 6),
  y = c(4, 5, 5, 6, 5, 5, 5, 5),
  label = c(1, 1, 2, 2, 3, 3, 4, 4),
  data = c(1, 1, 2, 2, 3, 3, 4, 4)) %>% 
  sf::st_as_sf(coords = c("x", "y")) %>% 
  sf::st_set_crs(4326) %>% 
  group_by(label) %>% 
  summarize(m = mean(data)) %>% 
  st_cast("LINESTRING")


ggplot() + 
  theme(panel.grid.major = element_line(colour = "transparent")) + 
  geom_sf(data = Street_Grid_cliped, size = 0.5, colour = "gray", alpha = 0.5) + 
  geom_sf(data = circle, size = 0.5, colour = "gray", alpha = 0.2) + 
  geom_sf(data = Target_Border, size = 2, colour = "gray") + 
  geom_sf(data = Targer_Street_Grid, size = 2, aes(colour = m))

target visualisation

Code can be in R or Python (i prefer R as pretty visualisation are much easier)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source