logo
down
shadow

Subsetting Polygons from Spatial Polygons object by slot


Subsetting Polygons from Spatial Polygons object by slot

Content Index :

Subsetting Polygons from Spatial Polygons object by slot
Tag : r , By : nemof
Date : November 29 2020, 04:01 AM

will be helpful for those in need I have 'SpatialPolygons' object and I want to subset the polygons where @hole == FALSE and make a new object. , The easiest way to do this is with sapply:
hasHole <- sapply(
  aDis@polygons[[1]]@Polygons,
  slot,
  "hole"
)
aDis@polygons[[1]]@Polygons[!hasHole]

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Adding Boundaries to Spatial Polygons Object


Tag : r , By : Dave
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


Tag : r , By : Andrew
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


Tag : r , By : unadopted
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, ] 
Related Posts Related QUESTIONS :
  • Generating the sequence 111122222333334
  • Unable to use has_goog_key() in R
  • how to multiply each row with a scaler in corresponding column?
  • R is not recognizing levels of a factor as the same. Is there a way to do this?
  • Calculating mean of replicate experiment result values in a column based on multiple columns using R
  • Best method to extract the first instance of a string between specified keywords using data.table
  • ignore optional combination of alphanumeric characters in str_extract
  • Why tracemem shows two copies when modification occurs inside function body?
  • Can't use mppm on multitype point patterns
  • How to move selected matrix rows to top of matrix based on a selection vector of row names
  • Combining expressions with a common operator
  • Passing string through multiple filters for matching
  • Convert two columns in R to rows of unique occurrence
  • How to create a dataframe using a function based on user-input?
  • How to access the visited vertices in a given shortest path using R igraph
  • Differences in Unicode character output with print()
  • Extracting Function or Objects from a String and then Piping Them with Magrittr/Dplyr
  • renderUI not evaluated until it is rendered
  • Find the maximum absolute value by row in an R data frame
  • Extracting data from irregular lists using purrr:map()
  • transforming data based on range of column in r
  • Identify and subset rows with some similar information
  • converting character from mongolite to timestamp in R
  • Create list from two vectors with every combo of each
  • Error in running a spread because of unique 'key combinations'; combining rows of data
  • visualize numerical strings as a matrixed heatmap
  • how to make a blocked matrix?
  • How to summarize with two functions using with dplyr
  • Dataframe is no longer the same after being saved to Excel and read back in
  • Create duplicate rows using based on availability of data
  • Keep empty groups when grouping with data.table in R
  • Grouping of Event Time Data based on multiple, iterative conditions
  • Formatting Numbers in Flextable for Specific Columns
  • How to store results from for-loop into a dataframe
  • How to select the values in my dataframe which has logical operator "<" (less than), divide them by two, an
  • Rowwise extract data between two strings
  • Convert a string separate by . and +
  • stacking function for values in R
  • dplyr coerces characters to factors
  • How do I use spread and group_by on a single row dataset
  • Replacing values in one matrix with values from another
  • Aggregate data and exclude duplicates in one column
  • Perform an R data.table binary search with OR select
  • How can I include a function in the Standard Deviation parameter of pnorm
  • How to get a tidy excel output of P values from R
  • Rotate boxplot legend (R, ggplot2)
  • dplyr::n() returns “Error: Error: n() should only be called in a data context ”
  • Extract fix columns and one variable column from a list of df´s in R
  • A function that can translate DNA sequence to binary code
  • I want to extract 365 netcdf files using loop
  • rvest vs RSelenium results for text extracting
  • Converting wide data to tall data
  • How to remove vertical white lines when using ggsave in R?
  • R-Shiny error: "renderDataTable" and "server=FALSE"
  • Read csv file with selected rows using data.table's fread
  • how to resolve an error like non numeric argument to binary argument?
  • If value exists in environment
  • R get one value according to some rules in each group
  • Use any apply method to find difference between max and min score for each students
  • subsetting a dataframe by existing object
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com