FAQ Database Discussion Community


How to rasterise and not to get duplicate information in r

r,environment,raster,rasterize
I'm having trouble rasterizing a data.frame using the raster package in R. My data.frame contains environmental data for the world oceans (temperature etc.) with their coordinates (grid 0.5*0.5), decimal longitude from -90 to 90 and decimal latitude from -180 to 180. So the base contains 90*2*2 x 180*2*2 = 360*720...

Use a raster Stack in foreach loop with doParallel in R

r,foreach,raster
I want to perform a series of calculations on each layer in a large raster stack in R, and save the results for each layer as a separate raster for later use. I am trying to speed up the process using foreach and doParallel. Here is the basic code, with...

Writing compressed netCDF4 files with raster

r,raster,gdal,rgdal
I want to write compressed netCDF4 files using the raster package. ncdf4 and rgdal are installed correctly. The following, however, fails to return a netCDF4 compressed file: library(raster) r <- raster() <- runif(86400) r[] <- <- runif(86400) writeRaster(r, "test.nc", options=c("COMPRESS=DEFLATE", "FORMAT=NC4")) In fact it just returns a standard netCDF file:...

R raster plot cropping plotRGB image

r,image,plot,raster
I have R code as below. 1st plot() and 1st plotRGB() command work fine. I would like to split the images in the second block of code plot() works but plotRGB() fails. How can I make plotRGB work in that second case? My end goal is to get the cropped...

crop image, convert to black and white and sharpen it

r,image,image-processing,colors,raster
I want to take x, convert it into a black and white image. Then i want to convert all pixels that are below 100 to 0 and convert all pixels that are above 100 to 255. I want to get a sharper image where background will be white and objects...

R raster - counting black pixels within an extent

r,image,image-processing,raster
My question is based upon this. Answers on that page solved the problem that i was facing at that time. My new question is - Below code plots two square on my image. For each square, I want to count how many black color pixels are within it. My original...

Raster and file does not exist

r,raster
I'm completely lost on this as the file exists in the directory and my previous code has worked fine. But for whatever reason I'm getting this error: Error: file.exists(filename) is not TRUE I found a question/answer but it didn't work. Data Code : library(raster) r1 = raster("/.../PRISM_tmin_stable_4kmM2_189501_bil.bil") Any suggestions why...

How to convert raster byte array of an image to original image in java?

java,image,image-processing,raster
I need to convert bytes of an image to original image. My byte array which is created by WritableRaster.And Itried to convert to original form ,but it produced image without original colors.My tries are below try { BufferedImage readImage = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")); byte imageData[] = get_byte_data(readImage); // Then I...

Combining a ggplot and plotRGB in one PNG plot

r,plot,ggplot2,raster
I am trying to combine a ggplot result and a plotRGB of a spatial raster into one plot and write it to PNG. I have already found how to combine ggplots or do a multiplot but this specific setup I do not know how to get to work. This code...

R - Function to combine index from vector and raster stack layers

r,function,stack,raster
I am trying to write an efficient script to calibrate hundreds of Landsat 8 images. At a certain point of the calibration steps, I need to apply some coefficients in each layer of a raster stack. This is one sample stack: fn <- system.file("external/test.grd", package="raster") s <- stack(fn, fn) And...

How to find maximum value in rasterbrick?

r,stack,raster
I know there is a similar question but it is not in rasterbrick. I would like to find the maximum value in this raster brick, I tried which.max but I don't understand the input. I would like to know what is the maximum value, and also get a list of...

Moving window regression

r,linear-regression,raster
I want to perform a moving window regression on every pixel of two raster stacks representing Band3 and Band4 of Landsat data. The result should be two additional stacks, one representing the Intercept and the other one representing the slope of the regression. So layer 1 of stack "B3" and...

How to constrain the colorpalette in ggplot2

r,ggplot2,raster
From the plot below, I'd like to constrain the transition from green to red (the yellow part) to be at the red line. I'd like green colors when z<1 and red when z>1, and the yellow colour to be centered on z=1. library(ggplot2) Data (just necessary to illustrate the plot)...

how i can rotate image by array of byte in java?

java,arrays,byte,raster,image-rotation
I have array of byte for image , and I want to rotate image by this array , this is my code : BufferedImage img = ImageUtil.load(inputImagePath); WritableRaster raster = img .getRaster(); DataBufferByte data = (DataBufferByte) raster.getDataBuffer(); byte [] pixel = data.getData(); how i can do this ? , thanks...

Changing par() settings to default when plotting rasters

r,plot,raster,par
When running the following code, R is returning the image show below. How should I change my par() settings (provided below) so that it returns a single image? I think that this happened because I changed par() settings manually. Is there a way to set par() to default settings, given...

How to rotate an image R raster

r,plot,rotation,raster
I have code below which saves an image to my pc. I would like to rotate that image by 45,90 and 135 degrees around its center (or bottom left hand corner) and then save as 3 different images. How could I do that? library(raster) r1 <- brick(system.file("external/rlogo.grd", package="raster")) x <-...

Raster not cropping to shapefile

r,raster,shapefile,maptools
I'm attempting to merge some states from a shapefile, and produce a raster that I can use downstream. I have gotten the states merged, however when I am creating an empty raster to rasterize with the crop function seems to fail. I'm pretty new to the GIS features in R...

R: Crop GeoTiff Raster using packages “rgdal” and “raster”

r,crop,raster,rgdal,r-raster
I'd like to crop GeoTiff Raster Files using the two mentioned packages, "rgdal" and "raster". Everything works fine, except that the quality of the resulting output tif is very poor and in greyscale rather than colour. The original data are high quality raster maps from the swiss federal office of...

Change matrix/raster coordinates on R

r,matrix,coordinates,raster
I'm a beginner and I have worked a lot on my problem, but I am still stuck... I have a matrix containing values of world temperature. The size is 360*720. The values are from -180, -90 (Longitude, latitude) to 180, 90 with a resolution of 0.5. > head(matrix) NaN. NaN..1...

Plot Circle on Raster with Known Coordinates and Radius

r,plot,raster,r-raster
I am attempting to plot circles with a defined radius onto a raster image. I have successfully plotted my raster and put points onto the image: # open image to save png("RasterImage.png", width=10, height=10, units="in", res=144) # plot raster plot(VI.SOS.mean, main="VI Mean SOS", col=terrain.colors(length(seq(100,220,20))-1), axes=F, breaks=seq(100,200,20)) # add points points(sensors$X,...

polynomial fitting on spectral data

r,curve-fitting,raster,polynomials
I want to fit a polynomial function (max. 3rd order) on each raster cell over all my spectral bands (Landsat 1-7) creating a new raster(stack) representing the coefficients. I got my data (including NA values) in a stack with 6 Layer (Landsat Band 1-7[excluding 6]). I guess somehow I should...

extract buffer mean by simulation (RasterStack)

r,extract,raster
I have two files: a RasterStack (where each layer represents 1 simulated map of air quality index) named m_stack; a SpatialPointsDataFrame (where each point represents a place of residence) named shph; I want to extract mean air quality index of each place of residence, by simulation. To compute mean air...

Adding a line to a scatter plot in R

r,scatter-plot,raster
I have a scatter plot of two raster images. What I need is to create a triangle around the scatter plot. So far what I have it minveg <- quantile(veg, 0.01, na.rm=TRUE) mintemp <- quantile(temps, 0.01, na.rm=TRUE) plot(veg, temps, xlab='veg', ylab='temp') abline(h=mintemp, v=minveg) This give me the following image What...

Extract xy coordinates from raster using buffer function

r,buffer,extract,spatial,raster
I have some xy coordinates as a SpatialPoints (points) object and have used them to extract temperature values at these locations from a RasterLayer (raster): extract = extract(raster, points) However several of the points are falling outside of the raster layer (i.e. not plotting on land) and I want to...

Generate a common legend (scale) for 2 rasters underneath the plots using scientific notation

r,plot,legend,raster,scientific-notation
I am trying to plot 2 rasters with different values with the same scale for comparison. I have been trying to work out the answer using this and this. I am running into two issues: I would like to be able to have the legend labels in scientific notation (there...

How to set column or rows in a raster to NA (or another value) in R?

r,raster
I have a raster that I want to set the outer rows or columns to NA (or specific value). Setting the 1st row and last row is reasonably straightforward as I know the cell numbers of the 1st and last rows as the cell numbers start at 1 and increase...

Is there true type font file for 'raster font'?

fonts,tesseract,raster,true-type-fonts,python-tesseract
I am using Tesseract to do OCR for some screenshots. The characters in screenshots are in raster fonts. But Tesseract requires True Type Font file for training. I can find many true type font files at Windows/Fonts folder. I am wondering if there's one for raster fonts?...

Poor resolution of raster plot when output to file

r,raster
I have a reasonably high definition global map raster and want to plot to file, but cannot seem to maintain resolution. Plotting a restricted region works ok, but the whole world always ends up with reduced resolution, no matter what method I've used. Am I missing something here? I've often...

R raster recognizing black color raster image

r,image,image-processing,colors,raster
The code below produces two boxes on my image. I am planning to analyze pixels within those boxes further. I want to put a condition that if along an edge of a box, there is a black color (or a similar color such as grey) pixel then don't proceed. How...

Outline a raster cell of interest by cell number

r,plot,selection,cell,raster
I would like to outline a raster cell given the cell number in a plot. I have made a simplified example, that I was able to make in power point, but may have a resolution that will be more difficult to do this for (720 x 360 vs 3 x...

Using grid2poly with raster

r,kml,raster,r-raster
I am trying to take data from a raster and push it to KML format so that I get a series of gridded polygons. Looking at samples on the web it would appear that the way to go is to using grid2poly with plotKML. Unfortunately I have hit an error...

calculate average correlation for neighboring pixels through time

r,raster
I have a stack of 4 rasters. I would like the average correlation through time between a pixel and each of its 8 neighbors. some data: library(raster) r1=raster(matrix(runif(25),nrow=5)) r2=raster(matrix(runif(25),nrow=5)) r3=raster(matrix(runif(25),nrow=5)) r4=raster(matrix(runif(25),nrow=5)) s=stack(r1,r2,r3,r4) so for a pixel at position x, which has 8 neighbors at the NE, E, SE, S etc...

Match color with numbers Raster Map R

r,maps,raster
I would like to match color to raster class. Assume I have the following colors and these rasters: library(rasterVis) mycolors=c("darkred","red3", "orange", "yellow", "lightskyblue", "royalblue3","darkblue") s <- stack(replicate(6, raster(matrix(runif(100), 10)))) levelplot(s, layout=c(3, 2), col.regions=mycolors, index.cond=list(c(1, 3, 5, 2, 4, 6))) I would like to classify the rasters in "s" such that...

Extracting raster values, from maximum, to cumulatively sum to x

r,sum,raster
I am trying to determine the location of raster cells that add up to a given amount, starting with the maximum value and progressing down. Eg, my raster of 150,000 cells has a total sum value of 52,000,000; raster1 <- raster("myvalues.asc") cellStats(raster1,sum) = 52,000,000 I can extract the cells above...

R raster package plot() producing monochrome images

r,plot,raster,monochrome
I would like to get monochrome images using plot function. But it keeps on producing color images. I even had monochrome images that i plotted using plot function and i got them in some weird green and orange color. How can i make plot function provide monochrome images? I tried...

How can I clip large shapefiles in program R without the computer crashing?

r,gis,ram,raster,shapefile
I am wanting to clip a large shapefile (67MB) in program R and derive a much smaller raster from around ~5% of it. Once loaded the shapefile has 221388 features and 5 fields - and explodes to 746 MB. My difficulty comes when trying to clip the file to a...

What does the terms Sampling,Scanline Mean in Raster Graphics(Bresenham's Algorithm)

algorithm,graphics,raster,vector-graphics
Im a novice when it comes to computer graphics.Im trying to learn Bresenham's Scanline algorithm. It says Pixels positions along a Line are determined by sampling at unit X intervals What exactly does this sampling mean,in layman's terms. Then it says Plot the pixel whose scan line value is closest...

Finding nearest grid value in spatial dataframe in R

r,geospatial,raster,gstat
I am learning about spatial dataframes. I have created a simple example that uses inverse distance weighting (IDW) to interpolate values across a grid, based on an initial set of points. I would now like to obtain the interpolated value for each of the initial points. I would presume that...

How to move a cell value of a raster to another target cell using R

r,raster
I have a raster, lets imagine it is 3*3 for now (although it is actually a lot bigger). Each of the raster cells has a value, a different value - i.e. a different Z value. I know the cell numbers of each cell within the raster (i.e. they are cell...