raster

Plotting netcdf file with levels in R

夙愿已清 提交于 2019-11-28 05:41:29
问题 I have recently started to work with netcdf in R. Sample data is here: http://www.earthstat.org/data-download/ > Harvested area and yield for 175 crops > individual crops > soybean_HarvAreaYield2000_NetCDF In this folder, there is a netcdf file called soybean_AreaYieldProduction.nc This is how I open the netcdf library(ncdf4) dat <- nc_open("soybean_AreaYieldProduction.nc") print(soy) 1 variables (excluding dimension variables): float soybeanData[longitude,latitude,level,time]

R - plotting multiple rasters using matrix layout

荒凉一梦 提交于 2019-11-28 03:54:24
问题 In R (Win64), I'm trying to plot a combination of raster images and histograms in a single plot window using the layout() command with a matrix defining the layout. Here's some sample code with simplified data: library(raster) r <- raster(ncols=5, nrows=5, xmn=1, xmx=5, ymn=1, ymx=5) rast1 <- rasterize(expand.grid(1:5,1:5), r, rnorm(25)) rast2 <- rasterize(expand.grid(1:5,1:5), r, rnorm(25)) rast3 <- rasterize(expand.grid(1:5,1:5), r, rnorm(25)) layout(matrix(c(1,2,3,4,1,2,3,5,1,2,3,6), 3, 4,

Find second highest value on a raster stack in R

旧街凉风 提交于 2019-11-28 02:05:42
问题 In R I can easily compute the max/min value of each cell in a georeferenced raster stack using the max/min commands. set.seed(42) require(raster) r1 <- raster(nrows=10, ncols=10) r2=r3=r4=r1 r1[]= runif(ncell(r1)) r2[]= runif(ncell(r1))+0.2 r3[]= runif(ncell(r1))-0.2 r4[]= runif(ncell(r1)) rs=stack(r1,r2,r3,r4) plot(rs) max(rs) min(rs) However, I have been trying to find a way to find the second highest values across a stack. In my case, each raster on the stack denotes performance of a

How to extract values from rasterstack with xy coordinates?

风格不统一 提交于 2019-11-27 22:29:21
I have a rasterstack (5 raster layers) that actually is a time series raster. r <- raster(nrow=20, ncol=200) s <- stack( sapply(1:5, function(i) setValues(r, rnorm(ncell(r), i, 3) )) ) s class : RasterStack dimensions : 20, 200, 4000, 5 (nrow, ncol, ncell, nlayers) resolution : 1.8, 9 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 names : layer.1, layer.2, layer.3, layer.4, layer.5 min values : -9.012146, -9.165947, -9.707269, -7.829763, -5.332007 max values : 11.32811, 11.97328, 15.99459, 15.66769, 16.72236 My

How to extract data from a RasterBrick?

試著忘記壹切 提交于 2019-11-27 21:55:14
问题 I have a RasterBrick consisting of monthly rainfall data over 7 years, so it has 7 layers with 12 slots each: rainfall <- brick("Rainfall.tif") > rainfall class : RasterBrick dimensions : 575, 497, 285775, 7 (nrow, ncol, ncell, nlayers) resolution : 463.3127, 463.3127 (x, y) extent : 3763026, 3993292, -402618.8, -136213.9 (xmin, xmax, ymin, ymax) coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs data source : in memory names : layer.1.1, layer.2.1

Legend of a raster map with categorical data

与世无争的帅哥 提交于 2019-11-27 20:16:37
I would like to plot a raster containing 4 different values ( 1 ) with a categorical text legend describing the categories such as 2 but with colour boxes: I've tried using legend such as : legend( 1,-20,legend = c("land","ocean/lake", "rivers","water bodies")) but I don't know how to associate one value to the displayed color. Is there a way to retrieve the colour displayed with 'plot' and to use it in the legend? The rasterVis package includes a Raster method for levelplot() , which plots categorical variables, and produces an appropriate legend: library(raster) library(rasterVis) ## Example

extract RGB channels from a jpeg image in R

女生的网名这么多〃 提交于 2019-11-27 19:10:28
In order to classify a jpeg image in R, I would like to get the RGB values of each pixel. My question: Is there a way to extract RGB channels from a jpeg image in R ? You have several package to read in JPEG. Here I use package jpeg : library(jpeg) img <- readJPEG("Rlogo.jpg") dim(img) [1] 76 100 3 As you can see, there is 3 layers: they correspond to your R, G and B values. In each layer, each cell is a pixel. img[35:39,50:54,] , , 1 [,1] [,2] [,3] [,4] [,5] [1,] 0.5098039 0.5921569 0.4549020 0.3372549 0.1921569 [2,] 0.5098039 0.6000000 0.4549020 0.3372549 0.1921569 [3,] 0.5137255 0.6000000 0

Stacking an existing RasterStack multiple times

落爺英雄遲暮 提交于 2019-11-27 18:52:58
问题 My question is pretty similar to an unfortunately unanswered issue recently posted on Stackoverflow. I am dealing with a RasterStack object consisting of twelve layers (one for each month of the year), and I would like to replicate the layers ten times, ending up with a RasterStack consisting of 120 layers, with every 12th layer being similar (i.e., layer 1 is the same like layer 13 is the same like layer 25 and so on). For replication purposes, let's take an example from the raster package:

How to save figures to pdf as raster images in matplotlib

我与影子孤独终老i 提交于 2019-11-27 14:50:51
问题 I have some complex graphs made using matplotlib. Saving them to a pdf using the savefig command uses a vector format, and the pdf takes ages to open. Is there any way to save the figure to pdf as a raster image to get around this problem? 回答1: You can force individual figure elements to be rasterized like this: text(1,1,'foobar',rasterized=True) 回答2: Not that I know, but you can use the 'convert' program (ImageMagick') to convert a jpg to a pdf: `convert file.jpg file.pdf'. 来源: https:/

Clipping raster using shapefile in R, but keeping the geometry of the shapefile

[亡魂溺海] 提交于 2019-11-27 11:42:16
问题 I am using {raster} to clip (or crop) a raster based on an irregular shapefile (the Amazon biome) but the output always has a rectangular extent. However, I need the output in the exact same geometry of the shapefile. Any tips? Cheers. library(raster) library(rgdal) myshp <- readOGR("Amazon.shp", layer="Amazon") e <- extent(myshp) myraster <- raster("Temperature.tif") myraster.crop <- crop(myraster, e, snap="out", filename="myoutput.tif") 回答1: One option is to use raster::mask() library