R extract vector of points from spatialpolygonsdataframe - r

I have a spatialpolygonsdataframe of the state of Iowa called Iowa.sp, which contains 99 polygons. I'm trying to extract all of the labpt values from each polygon as these are the centroids of each county in Iowa and I need to work with those values as references.
I can get an individual value for labpt from one polygon within the data frame using this:
Iowa.sp#polygons[[1]]#labpt
But have not been able to figure out how to extract the entire list of labpt values and am struggling with how to work within the spatialpolygonsdataframe. Any help is much appreciated.

temp<-data.frame(xcoord=NULL,ycoord=NULL)
for( i in 1: length(türkiye#polygons[[1]]#Polygons)){ temp<-rbind(temp,data.frame(
xcoord=türkiye#polygons[[1]]#Polygons[[i]]#labpt[1],ycoord=türkiye#polygons[[1]]#Polygons[[i]]#labpt[2]))}
temp
adapt your data according to this example.may be there are easier ways.
look at this question too
Value of coordinates() for a SpatialPolygonsDataFrame object?

Related

Convert data frame to spatial object and find the polygon that contains a point

I have two data frames. The first contains the longitude and latitude coordinates for a series of locations. And it looks like this:
The second data frame contains gridded data, the head is as follows. The longitude range is (-10.70, -5.35) and the latitude range is (51.33, 55.38).
I need to find the grid that contains the value of each city given in data frame 1. Do I need to convert it to a spatial object or is it possible to do it directly from the data frames? How can I do that?

st_intersection returning a dataframe with 0 observations [duplicate]

This question already has an answer here:
Why use st_intersection rather than st_intersects?
(1 answer)
Closed 1 year ago.
I have a dataframe that shows bus stop locations in Glasgow and another dataframe that shows Datazone polygons for Glasgow. I am using the sf package and have made both dataframes spatial. I want to do a spatial join to create a new dataframe (joined_ds) to match each bus stop location to a Datazone polygon and its associated characteristics (deprivation score). I'm using st_intersection which gives me a new dataframe with all the correct columns but 0 observations.
joined_ds <- st_intersection(st_buffer(bus_stop_data,0), st_buffer(datazones,0))
Both datasets are using the appropriate CRS (EPSG: 27700 for the British National Grid) and I know that the points and polygons overlap because I have successfully plotted them on a map using ggplot, so no idea why my dataframe is showing 0 obs. I've also tried loading in the datasets from scratch and no luck.
Any suggestions welcome, thanks!
Look at the differences between st_intersection and st_intersectshere:
Why use st_intersection rather than st_intersects?
Since you are only interested if a point intersects with a polygon, you need st_intersects. If I understand you correctly you don't need to use st_buffer but simply use st_join in combination with st_intersects. Something like:
st_join(bus_stop_data, datazones, join = st_intersects)
Keep in mind issues that might arise when using spatial joins, for example when a point intersects with two polygons.

extracting pixel values above a value per polygon in R

I have a shapefile containing 38 polygons i.e. 38 states of a country. This shapefile is overlaid on a raster. I need to extract/reclassify pixel above a certain value, specific to each polygon.
For example, I need to extract the raster pixels> 120 for state/polygon 1, pixels> 189 for polygon 2 etc with the resulting raster being the extracted pixels with value 1 and everything else as NoData. Hence, it seems like I need to extract first and then reclassify.
I have the valuees, for extraction, saved as a data frame with a column containing names, matching the names of the states,which is stored as an attribute "Name" in the shapefile.
Any suggestion on how I could go about this?
Should I extract the raster for each state into 38 separate rasters, then do reclassify () and then mosaic to make one raster i.e. the country?

Given a vector of coordinates, identify the polygon from a shapefile it falls into

I have my polygons stored in a SpatialPolygonsDataFrame and my coordinates in a data frame.
The output I want is to just have an additional column on my data frame that tags the OBJECTID (id of the polygon from the shapefile) that the coordinates fall into.
My problem is kind of the same with this
But its output is a little bit different. Also, it's kinda slow. I tried to tag just 4 coordinates and it took more than 5 minutes. I'm gonna be tagging 16k coordinates so would it be possible to do it faster?
The current methods I know about wouldn't do that exactly (i.e., produce one polygon id per coordinate) because they're generalized in case one point is contained in multiple (overlapping polygons).
See sp::over(), which used to be called overlay().
Example:
over(sr, geometry(meuse), returnList = TRUE)
over(sr, meuse, returnList = TRUE)
Possible duplicates (it's hard to tell without seeing your example data):
Extracting points with polygon in R
Intersecting Points and Polygons in R

Extracting points that belong to a certain area of a RasterLayer (raster)

As per title.
I have a "classified" RasterLayer object which has (apart from NAs) two fixed values, 0 and 1. It is a kind of logical image.
I also have a data frame of points with their coordinates, in form of a SpatialPointsDataFrame.
How can I extract points belonging to a certain area (0 or 1)? Been searching into raster-package help but I couldn't find a solution.
You can use extract from the raster package:
"Extract values from a Raster* object at the locations of other
spatial data (that is, perform a spatial query). You can use
coordinates (points), lines, polygons or an Extent (rectangle) object.
You can also use cell numbers to extract values."
values <- extract(x="YourRasterLayer", y="YourSpatialPointsDataFrame")
For more information type:
?raster::extract
or visit this page.

Resources