Adding Boundaries to Spatial Polygons Object
Date : March 29 2020, 07:55 AM
Hope this helps Use writeOGR from the rgdal package to create a shapefile of your metro object. Then install QGIS ( http://www.qgis.org/), a free and open-source GIS, and load the shapefile as a new layer. Then you can edit the layer, add new polygons, edit lines etc, then save as a shapefile to read back into R.
|
Messed up polygons shape when combining google map with Spatial Polygons
Date : March 29 2020, 07:55 AM
this will help I´m having problems when combining a map from Google Map Api, with a map constructed with geom_polygon from ggplot2. When I plot each map on their own, nothing weird shows up, but when I do combine them, some lines(straight) appear, messing up the boundaries I intend to highlight. , You need to add a group mapping to your aesthetic. e.g. geom_polygon(data = distritos.fort,
aes(x = long, y = lat, group = group),
colour = "black",
fill = NA) + coord_map()
|
How to make dynamic labels from a spatial polygons dataframe for Polygons on a Leaflet map in R
Tag : r , By : protagonist
Date : March 29 2020, 07:55 AM
I hope this helps . With this script, I am displaying a map with three isochrones. I would like the map to have labels containing the max time represented by each isochrone/polygon. , It works if you provide the max values as.character to the label. library(osrm)
library(leaflet)
library(viridisLite)
# Making isochrones
iso1 = osrmIsochrone(loc = c(9.2,45.5),
breaks = seq(from = 0,
to = 45,
by = 5),
res=75)
iso2 = osrmIsochrone(loc = c(12.51182,41.92631),
breaks = seq(from = 0,
to = 45,
by = 15),
res=100)
iso3 = osrmIsochrone(loc = c(11.25581,43.76956),
breaks = seq(from = 0,
to = 45,
by = 15),
res=100)
# colors for leaflet
vir = viridis(9)
# palette
pal1 <- colorNumeric(
palette = vir,
domain = iso1@data$id)
pal2 <- colorNumeric(
palette = "Blues",
domain = iso2@data$id)
pal3 <- colorNumeric(
palette = "Reds",
domain = iso3@data$id)
# Plotting interactive map using spdf
leaflet()%>%
addTiles("http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", attribution = 'Google')%>%
addPolygons(data = iso1,
fill = TRUE,
fillOpacity = 0.7,
fillColor = ~pal1(id),stroke = FALSE, label = as.character(iso1@data$max))%>%
addPolygons(data = iso2,
fill = TRUE,
fillOpacity = 0.7,
fillColor = ~pal2(id),stroke = FALSE, label = as.character(iso2@data$max))%>%
addPolygons(data = iso3,
fill = TRUE,
fillOpacity = 0.7,
fillColor = ~pal3(id),stroke = FALSE, label = as.character(iso3@data$max))
|
Merging the polygons inside a spatial polygons data frame based on a field in the @data slot
Tag : r , By : dnyaneshwar
Date : March 29 2020, 07:55 AM
To fix this issue The correct term to describe the act of joining, combining, uniting or merging (making 1 out of multiple) polygons seems to be either dissolving or aggregating. The funtion that works for me is aggregate() from the raster package. library(sp)
library(raster)
#coordinates:
xy1 = cbind(c(1,2,2,1),c(1,1,2,2))
xy2 = cbind(c(2,3,3,2),c(1,1,2,2))
xy3 = cbind(c(1,2,2,1),c(2,2,3,3))
xy4 = cbind(c(2,3,3,2),c(2,2,3,3))
#polygons:
p1 = Polygon(xy1)
ps1 = Polygons(list(p1),ID = "a")
p2 = Polygon(xy2)
ps2 = Polygons(list(p2),ID = "b")
p3 = Polygon(xy3)
ps3 = Polygons(list(p3),ID = "c")
p4 = Polygon(xy4)
ps4 = Polygons(list(p4),ID = "d")
#spatial polygons:
sps_m = SpatialPolygons(list(ps1,ps2,ps3,ps4))
#dataframe:
data_m = data.frame(dt = c("Group A","Group B","Group A","Group C"),row.names = c("a","b","c","d"))
#spatial polygons dataframe:
spdf_m = SpatialPolygonsDataFrame(sps_m,data_m)
groups = aggregate(spdf_m, by = "dt")
#plot spdf:
plot(spdf_m)
plot(groups)
|
Create a spatial polygons data frame that preserves overlapping features with another spatial polygons data frame but do
Date : March 29 2020, 07:55 AM
this one helps. If I understand the question properly, you could use function gIntersects to find out which watersheds intersect your region, and then extract only those from the huc4 dataset. In practice, something like this could work: intersects <- which(gIntersects(huc4, region, byid = TRUE))
huc4_clip <- huc4[intersects, ]
|