calculate variogram/GLCM with a raster output in R - r

I'd like to use the geostatistical texture to classify my remote sensing data, but I can't find a package that give a result I want---it should result a raster type output, so that I can use it with spectral date (such as TM) in R. I search this for days, nothing useful find. so I need your help.

See the glcm package - it will handle a raster input and will output GLCM textures as a RasterStack. There is an example here.

Related

Data structure and package for a radial dendrogram in R

I'd like to create a radial dendrogram in R, but being new to the software, I don't know if I chose the correct data structure and package.
I've created a YAML file that looks as follows:
Data structure
I know the exact hierachy of the languages, but I need R to calculate x and y values. I'd use hclust for that, I think?
I found this instruction here for example: https://stats.stackexchange.com/questions/4062/how-to-plot-a-fan-polar-dendrogram-in-r, but it uses the mtcars dataset. I'd just like to know whether it makes sense to set up my data as above or whether I should use a different structure. When I try to import the datasets I get an error message saying I've got more columns than column headers so I must be doing something wrong.

Memory problems trying to convert a large csv file to a raster layer using R

I have a large CSV file representation of a geographic raster map - 42011 columns by 18510 rows, around 10GB on disk. The file was created as the output of a convolution using the FFT function, which involved breaking the original raster into six strips, processing each and appending the resulting matrix to the csv file using the overlap-add method.
I am now trying to write the results back to a raster file type (e.g. ESRI bil, GeoTIFF etc) using the R package raster. Each number in the CSV file maps to the equivalent cell of the raster.
The obvious way to do this in R is:
library(raster)
library(data.table)
data <- fread("data.csv") ## Read in data (as data.table)
data_mat <- data.matrix(data) ## Convert to matrix
data_raster <- raster(data_mat, template=existing_raster) ## Write to raster using existing layer as template
However, my PC runs out of memory at the stage of trying to create the matrix:
Error: cannot allocate vector of size 5.8 Gb
In addition: Warning messages:
1: In matrix(if (intOK) NA_integer_ else NA_real_, nrow = d[1L], ncol = d[2L], :
Reached total allocation of 16108Mb: see help(memory.size)
etc
The computer is running 64-bit R with 16GB of RAM.
Things I've tried so far include:
bigmemory - this looked promising, as the raster function can take a big.matrix as it's input, but the package is not currently available for windows.
ff - I haven't fully grasped the capabilities of this package so far, but at present I can't see a way to get from the data.table to an ff object, and I'm not sure there would then be a way to get from that to a raster layer.
Sparse matrices (using package "Matrix") - the CSV is probably more than 50% 0s. However, I can't currently work out a way to read it from disk as a sparse matrix, or to get it from a data.table into a sparse matrix. Whether I would then be able to get to a raster, I have no idea.
I am most familiar with R, but also have a working knowledge of Python and access to ArcGIS. I'm not sure if the question is better here or on gis.stackexchange, but it seems like primarily a programming problem. Any suggestions gratefully received!
Big matrices have always been problems in R. I'm not familiar with geographic raster map. But is it possible to partition your data frame to pieces and generate matrices in a way that does not lose information?
"ff" packages could be of help. I found a related tutorial here, it shows a lot of examples of using "ff" package for large object. Specifically, page 20, "working with array and matrices", hopefully it helps.ff packages examples

Exporting GIS line vector data (roads) to R with intention to use it for network K-function

I am still a beginner to R and I have the following problem. Any tips will be highly appreciated - it will be a big help to start off.
I have a road shapefile for one country in ArcGIS, that I would like to use as a network variable in R to apply network K-function (look for point randomness on the network using spatstat package). I have never worked with this type of problem before in R and I can't figure out how to convert the vector line dataset into network variable in R. I tried to used the shapefile package, but after reading the .shp, .shx and .dbf files, I don't know how it can be used any further.
The comments above are not correct.
In the spatstat package, first convert your shapefile to an object of class "psp" (planar segment pattern) as explained in the accompanying vignette on shapefiles.
Then use the function as.linnet to convert the "psp" object to a linear network object of class "linnet".
For more explanation, see chapter 17 of the spatstat book.

How to interpolate from nonuniform 2D locations to regular grid?

I have nonuniformly located samples of an image, and would like to interpolate to a regular grid because (among other things) most image graphics functions expect a regular grid.
I notice there are some MatLab functions (see Image interpolation from random pixels for example) which apparently will do this, but couldn't find an R-package that does.
Here's a simple example.
#make up some 2D func
y<-matrix(rep(1:10,10) -.5 + runif(100),nrow=10)
x<-matrix(rep(1:10,10) -.5 + runif(100),nrow=10)
inmat<-sin(x) + cos(y)
So the values of inmat are on random locations. I want some sort of outmat<-interpolate(inmat,x,y,gridx,gridy) function where inmat , x,and y are either all matrices or all vectors (unwrapped matrices).
I see also that SciPy has http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp2d.html which does this. Is there such a function in an R package or do I need to port from SciPy or MatLab code?
The linked pages provide pointers to a gazillion R packages which do Kriging or other interpolation functions.
I'm posting my personal choice as an answer just to close out this question.
I found akima::interp to be a straightforward function to do 2D interpolation on arbitrary collections of sample locations.
That doesn't mean it's going to be best for everyone, and my guess is those working with geodata may prefer packages designed to muck with specific geo-survey-related file types and lat/long coordinate systems.

Convert a Graph to a Data Frame in R

So a while back (6 months+) I saw a blog post where the author took a line graph someone had posted on the internet, fed the image into R, and used a function to convert the image into a data frame.
I've looked everywhere, and I can't seem to find this blog post (even though I'm sure I bookmarked it). So I was wondering if any of you had also read said blog post, or if someone knew of a quick and easy way to convert a line graph to a data frame in R?
Was this it? I searched for "R digitize plot". The package used is "ReadImages". For completeness, the steps listed were (see link):
library(ReadImages) #Load package
mygraph <- read.jpeg('plot.jpg') #Import image
plot(mygraph) # Plot the image
calpoints <- locator(n=4,type='p',pch=4,col='blue',lwd=2) # Calibrate the plot by selecting known coordinates
data <- locator(type='p',pch=1,col='red',lwd=1.2,cex=1.2) # Collect the data points in a dataframe
When you say 'the image as a data frame', do you mean you want to get back to the original data that made the line?
It's not R, but I've used Engauge Digitizer for this sort of thing:
http://digitizer.sourceforge.net/
Also look at the updateusr function in the TeachingDemos package. Once you have the image displayed as in Benjamin's post, you can use the updateusr function with the known points to change the user coordinates so that then the results from the locator function do not need any additional transformation.
As i write this, the digitize package and the ReadImages package are no longer available for R 3.0.2. Engauge Digitizer is a good option but if you still want to do this sort of thing in R, take a loook at http://rscriptsandtips.blogspot.no/
You can also use im2graph to convert graphs to data. It's free and available of Windows and Linux (http://www.im2graph.co.il).

Resources