New answers tagged r
3
As the name says, a SpatialPolygonsDataFrame is basically just a SpatialPolygons object with data attached (the attribute table).
The data must have at least as many rows as there are features
library(rgdal)
ob <- SpatialPolygons(..)# Your SpatialPolygons Object
spp <- ...
4
Here is an approach using extract() from the raster package. I tested it with altitude and mean temperature data from the WorldClim website (I limit this example to altitude, temperature works similar), and an appropriate shapefile of the US containing state borders is to be found here. Just download the .zip data and decompress it to your working directory.
...
0
Maybe its too late but this error:
"chfactor.c", line 130: singular matrix in function LDLfactor()
Error in predict.gstat(g, newdata = newdata, block = block, nsim = nsim, :
LDLfactor
It's sometimes related with some points at nearly the same position. Did you try zerodist(dat) to check that?
5
I would drop using the maps package and find a state shapefile. Then load that into R using rgdal, and then do some polygon overlay work.
library(raster)
# use state bounds from gadm website:
# us = shapefile("USA_adm1.shp")
us <- getData("GADM", country="USA", level=1)
# extract states (need to uppercase everything)
nestates <- c("Maine", "Vermont", ...
2
By making a buffer you approximate the distances (as you do not get a true circle, but a polygonal approximation of one). You can improve the rgeos answer by increasing the number of segments of the circle with the "quadsegs" argument:
nbg.bff2 <- gBuffer(nuremberg, quadsegs=50, width = 20000)
table( unlist(extract(germany, nbg.bff2)) )
0 1
118 91 ...
2
To summarize the very useful comments by @Spacedman, @JeffreyEvans and @AriB.Friedman, but also to address the questioner's second problem about saving the generated subset and to provide a reproducible example for future audience, let me add a brief example to cover the whole topic. In fact, I'm much more often working with raster data than point or polygon ...
5
I will offer an R solution that is coded in a slightly non-R way to illustrate how it might be approached on other platforms.
The concern in R (as well as some other platforms, especially those that favor a functional programming style) is that constantly updating a large array can be very expensive. Instead, then, this algorithm maintains its own private ...
2
Without ever having performed your operation, and no time to spare for play, I can only add these two links to your list:
Find the Nearest Raster Cell Value Based on a Vector Point (The first answer (with 4 votes) is what intrigued me).
Also: would Hawth's Gridspread help?
3
Assuming that presencias and variables share the same projection, this should be an easy task. I recommend you to add these lines of code after your read.table() statement in order to convert presencias dataframe to a SpatialPointsDataFrame object (just refine the names of the columns containing x and y coordinates if they differ from my example).
...
0
Surely you can just do:
variables_presencia$x <- presencias['x']
variables_presencia$y <- presencias['y']
(presuming your coordinate data is in two columns called 'x' and 'y')
0
ok this was easier than i thought: spatial.subset<-states.shp[5,] if you know which line in your attribute table should be the subset. If you don't know you could do something like spatial.subset<-states.shp[states.shp@data$STATE=="California" ,] for a single state or spatial.subset<-states.shp[c(states.shp@data$POPULATION>1000000),] for several ...
3
This is a concise way to do that in R --- here without intermediate files:
library(raster)
raster_data <- list.files(path=getwd()) #promt user for dir containing raster files
s <- stack(raster_data)
f <- function(x) { rowSums(x >= 4 & x <= 9) }
x <- calc(s, f, progress='text', filename='output.tif')
2
You can do
library(raster)
r <- raster(matrix)
# replace with correct coordinates
extent(r) <- (0, 1, 0, 1)
r <- writeRaster(r, 'filename.tif')
But your question suggests that you would have been better of accessing the ncdf file as a RasterLayer object (and avoid creating matrices and keeping track of coordinates)
pft <- ...
0
You may use something like the code below to create raster from matrix. Notice that no georeference information will be created here.
library(rgdal)
raster <- your_matrix
height <- 79
width <-78
tif_driver <- new("GDALDriver", "GTiff")
tif <- new("GDALTransientDataset", tif_driver, height, width, 1, 'Byte')
pattern <- matrix(raster, width, ...
0
Have you tried the MicroDEM? It's free, and I have heard it works great at this, but have not used it myself.
http://www.usna.edu/Users/oceano/pguth/website/microdem/microdem.htm
0
if you're prepared to ditch the graph paper i've seen the HOPACH package used to map flows between clusters of regions as nodes
e.g. outputs see slide 15
http://www.segra.com.au/segra11ConfProc/presentations/tuesday/concurrents/JonesWarwick.pdf
1
Don't use readGDAL. It reads into a Spatial* object which might not be a good idea..
Use the raster package. It can read GDAL things into Raster objects. These are a good thing. r = raster("/path/to/rasterfile.tif") will read it into r.
Your classification is then t = r > 4 & r <= 9
The big question is whether to output these to new raster files ...
2
As I noticed in comments, generally you should avoid using R for non-statistical purposes due to performance issues in certain aspects (working with cycles is an example). Here is code example for you in Pyhton (thanks to this article) for reclassification of a single file with a single band. You will be able to modify it easily for batch processing if you ...
2
"Past and present predictors RasterStack are the same extension, resolution, etc. This is not the problem." Indeed that is not the problem, as that is not a requirement.
"The names in the Raster object should exactly match those expected by the model (...) so, according to this, I can't predict my model to another RasterStack that was not used to obtain the ...
0
I would expect overlay to be a little slower, but perhaps not much, and the code is perhaps more elegant:
f_NDVI2 <- function(x, y) {
(x-y)/(x+y)
}
NDVI <- overlay(raster[[60]], raster[[75]], fun=f_NDVI2)
0
In manual to 'raster' package it is clearly described in which cases one should use calc, overlay or simple band math.calc is used for operation with a single Raster object and overlay is used when calculations involve several Raster objects (or when dataset is too large to be loaded into memory). In my case (you linked to my question) there was only one ...
1
Welcome to SE-GIS! It is great that you have read the manual (as most people often forget to do it)! As it was said in the instruction - the names of the predictors must be the same that was used for training. The name of the predictors if you use 'raster' package are generated as 'raster_name'(without extension)+'.'+'band_number', e.g. 'imagery.tif' with 2 ...
1
As a footnote to this and a shameless author's plug, the free sDNA (spatial Design Network Analysis) plugin for ArcGIS can do this, among other things that may be of interest to you.
You would first need to ensure your network was correctly noded (i.e. all lines breaking where there are junctions; Topology->Planarize can do that).
2
I expect it will be much quicker this way (using a vectorized approach in the NDVI function)
f_NDVI <- function(x) {
(x[,1]-x[,2])/(x[,1]+x[,2])
}
# subset the two layers you need
s <- raster[[c(60, 75)]]
NDVI <- calc(s, f_NDVI)
2
I'd suggest the spatstat package. Perhaps check out the quadratresample function. They also have several others to simulate random patterns that may fit your need (e.g. rstrat and rsyst). Random sampling should be pretty trivial to accomplish on your own (see the sample function in base R)
3
Looks like the Google reverse geocoding API will break it down for you, take a look at this result:
http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false
If you switch out xml with json, you can get json instead. Another option could be the Nominatim service, which also returns either xml or json.
Top 50 recent answers are included
