I have two raster data, both have same resolution and origin. And both have 3600 columns.
I use r <- rotate(r) on one raster data to change the log from 0,360 to -180,180. but after this process, the number of columns of this raster data increases to 3601 from 3600. However I need to do a calculation of two raster data, and need the number of columns of two raster data to be same, which is 3600.
I expect to get a raster data with 3600 columns.
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?
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
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.
I have about 500,000 points in R of occurrence data of a migratory bird species throughout the US.
I am attempting to overlay a grid on these points, and then count the number of occurrences in each grid. Once the counts have been tallied, I then want to reference them to a grid cell ID.
In R, I've used the over() function to just get the points within the range map, which is a shapefile.
#Read in occurrence data
data=read.csv("data.csv", header=TRUE)
coordinates(data)=c("LONGITUDE","LATITUDE")
#Get shapefile of the species' range map
range=readOGR(".",layer="data")
proj4string(data)=proj4string(range)
#Get points within the range map
inside.range=!is.na(over(data,as(range,"SpatialPolygons")))
The above worked exactly as I hoped, but does not address my current problem: how to deal with points that are the type SpatialPointsDataFrame, and a grid that is a raster. Would you recommend polygonizing the raster grid, and using the same method I indicated above? Or would another process be more efficient?
First of all, your R code doesn't work as written. I would suggest copy-pasting it into a clean session, and if it errors out for you as well, correcting syntax errors or including add-on libraries until it runs.
That said, I assume that you are supposed to end up with a data.frame of two-dimensional numeric coordinates. So, for the purposes of binning and counting them, any such data will do, so I took the liberty of simulating such a dataset. Please correct me if this doesn't capture a relevant aspect of your data.
## Skip this line if you are the OP, and substitute the real data instead.
data<-data.frame(LATITUDE=runif(100,1,100),LONGITUDE=runif(100,1,100));
## Add the latitudes and longitudes between which each observation is located
## You can substitute any number of breaks you want. Or, a vector of fixed cutpoints
## LATgrid and LONgrid are going to be factors. With ugly level names.
data$LATgrid<-cut(data$LATITUDE,breaks=10,include.lowest=T);
data$LONgrid<-cut(data$LONGITUDE,breaks=10,include.lowest=T);
## Create a single factor that gives the lat,long of each observation.
data$IDgrid<-with(data,interaction(LATgrid,LONgrid));
## Now, create another factor based on the above one, with shorter IDs and no empty levels
data$IDNgrid<-factor(data$IDgrid);
levels(data$IDNgrid)<-seq_along(levels(data$IDNgrid));
## If you want total grid-cell count repeated for each observation falling into that grid cell, do this:
data$count<- ave(data$LATITUDE,data$IDNgrid,FUN=length);
## You could have also used data$LONGITUDE, doesn't matter in this case
## If you want just a table of counts at each grid-cell, do this:
aggregate(data$LATITUDE,data[,c('LATgrid','LONgrid','IDNgrid')],FUN=length);
## I included the LATgrid and LONgrid vectors so there would be some
## sort of descriptive reference accompanying the anonymous numbers in IDNgrid,
## but only IDNgrid is actually necessary
## If you want a really minimalist table, you could do this:
table(data$IDNgrid);