create hexagonal cells grid using lat/lon coordinates - r

I would like to create a spatial grid with hexagonal cells using WGS84 coordinates (ie cells defined by 2 coordinates X=Latitude and Y=Longitude)
So, this is what I was thinkin about :
library(ggplot2);library(hexbin)
X<-seq(-10,20,by=0.1) # create coordinates vectors X and Y
Y<-seq(35,65,by=0.1)
z<-rnorm(301,0.5,1)
df<-as.data.frame(cbind(X,Y,z)) # create data frame with a z value for each cells (X,Y)
pl<-ggplot2(data=mat,aes(x=X,y=Y,z=z))+stat_summury_hex(fun=function(x) sum(x))
plot(pl)
But doing this does not provide what I wanted.
So, my question is : how to do a spatial grid with hexagonal cells using lat/lon coordinates ?
And second question : how to create a grid centered from one point (that would represent the centroid, and not the left bottom corner as usual?)

If I understand properly, you're looking for expand.grid():
xy <- expand.grid(X=X,Y=Y)
z<-rnorm(nrow(xy),0.5,1)
df<-as.data.frame(cbind(xy,z)) # create data frame with a z value for each cells (X,Y)
head(df)
pl<-ggplot(data=df,aes(x=X,y=Y,z=z))+stat_summary_hex(fun=function(x) sum(x))
plot(pl)
As for the second question, I'm not sure, but since all hexagons are the same size and will require the same operation to center, you can shift them uniformly by changing X and Y appropriately. Perhaps this can also be done via arguments also, not sure.
[[Edit July 23]]
second question was how to get a data.frame of hex coordinates. Took some digging, but here's an example:
library(hexbin)
coords <- hcell2xy( hexbin(x=X,y=Y))
head(coords)
x y
1 -10.0 35.00000
2 -9.5 35.86603
3 -8.5 35.86603
4 -9.0 36.73205
5 -8.0 36.73205
6 -7.5 37.59808
hcell2xy() is the key function called by ggplot2, and you may need to be explicit about specifying the argument xbins, which is determined automatically inside ggplot2, but appears to default to 30 in both cases.
[[Edit 3, to include z level]]
This is an answer to the comment asking for z levels as well. Ripped from ggplot2:::hexBin
hb <- hexbin(x=X,y=Y)
# Convert to data frame
data.frame(
hcell2xy(hb),
count = hb#count,
density = hb#count / sum(hb#count, na.rm=TRUE)
)
You can choose whether to use count or density for colors later, but warning: those are different from your z variable fed to ggplot2. If you'd like to summarize based on some other statistic, then I suggest you also look into the guts of those functions to see how things are passed around. That's what I've been doing.

Related

Convert between individual STARS pixels and SF lat/long coordinates in R

I have a STARS raster object raster in R (e.g of size 100x100) and can get the coordinates using st_coordinates(raster)
But I'm having problems converting between the two. For example, I'd like to know the lat/long coordinates for only a particular raster pixel, e.g. (50, 50).
I was assuming that st_coordinates would give me a 1D array, so I can simply convert from the 2D raster matrix to the 1D array (e.g. by converting the 2D index (50, 50) into a 1D index using something like #columns*i+j, which in my example is 100*50+50).
This isn't working though, which makes me think I'm misunderstanding how the STARS raster object and coordinates indices map onto each other. Does anyone know how to bridge between the two, on a pixel-by-coordinate basis?
===
Updated with an example of what I'm trying to do:
r <- read_stars(system.file("tif/L7_ETMs.tif", package = "stars"))
## Let's say I want to grab the pixel 50x50 from the first band.
## I can do that going into the raster matrix like this:
pixel <- r[[1]][50,50,1]
## now I can get the coordinates for the raster
## using sf like this
## this gives me a coordinate df with columns x, y and band.
coordinates <- st_coordinates(r)
## what I want to do now is get the coordinate for the
## pixel I listed earlier, (50, 50, 1) -- in my case, I only
## have one band so I'm not going to worry about band index logic
## to do this, I assume that (50, 50) is the same as
## ncol(r)*(50-1)+50 or 17298. I say 50-1 since R is using 1 indexing.
## I use this to get a coordinate from coordinates like this:
coord <- coordinates[ncol(r)*(50-1)+50,]
## This returns me the following:
> 17298 294376.5 9119350 1
## If I wanted to do this many times, I could make a list
## of coordinates for various pixels, then put them into
## a new sf object using st_as_sf(...)
When I tried doing the above in a loop and plotting the results, there was a substantial mismatch... the raster pixels did not map to the right coordinates after plotting them in a new sf object. This has me thinking my conversion from raster 2D array to coordinate 1D list is not right. In fact, I'm realizing I have no idea at all what logic sf uses to convert the raster to a 1D list, which might explain the problem... Do you have any ideas about how these map to each other and how to index the coordinates array for a given raster pixel? Please let me know if I still need to clarify further. Thanks!
I think the key here is to subset a stars object then feed the subsetted object into st_coordinates.
r <- read_stars(system.file("tif/L7_ETMs.tif", package = "stars"))
# extract band 1 raster value at position 50, 50
# two methods with same result
# convert to matrix then subset
r[[1]][50,50,1]
#[1] 56
# subset stars raster then extract value
r[,50,50,1][[1]]
#, , 1
#
# [,1]
#[1,] 56
If you use the subset stars raster then extract value workflow, you can use the subsetted raster in the st_coordinates function.
st_coordinates(r[,50,50,1])
# x y band
#1 290187 9119350 1

Error: in points2grid(points, tolerance, round) : dimension 1: coordinate intervals are not constant [duplicate]

I was trying to make a raster image from an irregularly spaced point database. The data looks like-
> head(s100_ras)
x y z
1 267573.9 2633781 213.29545
2 262224.4 2633781 69.78261
3 263742.7 2633781 51.21951
4 259328.4 2633781 301.98413
5 264109.8 2633781 141.72414
6 255094.8 2633781 88.90244
I want these 'z' values within a mesh which I created by
# Create a fine mesh grid
my_mesh=expand.grid(seq(min(s100_ras$Y),max(s100_ras$Y),l=100),
seq(min(s100_ras$X),max(s100_ras$X),l=100))
I also want the z-values to be assigned as 'NA' for those mesh points that are outside the data points. The points over the mesh looks like this: https://drive.google.com/file/d/0B6GUNg-8d30vYzlwTkhvaHBFTnc/edit?usp=sharing
when I plot
plot(my_mesh)
points(s100_ras$Y, s100_ras$X, pch="*", col='blue')
The problem is that I'm not sure how to build on this, the following steps don't work because my mesh grid and data points are not of the same scale!!
library(rgdal)
library(raster)
xyz<-cbind(my_mesh, s100_ras)
r <- rasterFromXYZ(xyz)
image(r)
If I try to make a raster just by using the data points (without any mesh), R throws an error since my data is irregularly spaced!
library(sp)
s100_ras <- data.frame(expand.grid(x = s100_ras$Y, y = s100_ras$X),
z = as.vector(s100_ras$mean))
coordinates(s100_ras) <- ~x+y
proj4string(s100_ras) <- CRS("+proj=utm +zone=46 +datum=WGS84")
gridded(s100_ras) = TRUE
suggested tolerance minimum: 0.916421
Error in points2grid(points, tolerance, round) :
dimension 1 : coordinate intervals are not constant
Moreover, I was trying to play with 'rasterize' function (for irregular grids) of 'raster package', but couldn't get a way with it :(. I know how to interpolate and make a regular grid, but for the sake of originality, I want to AVOID interpolation. Is it possible to make a raster of irregularly spaced data points without idw or kriging methods? Thanks in advance.
The clear-cut solution to the answer has been provided by Robert here: https://gis.stackexchange.com/questions/79062/how-to-make-raster-from-irregular-point-data-without-interpolation

Aligning two set of points into a same reference point, centroid

I have two sets of 2d points (in x,y coordinates). How can I align these two set of points into a same reference point (centroid)?
Let my two sets are
A
x y
1 3
4 2
7 8
3 5
B
x y
9 5
14 7
17 3
30 25
By centroid I mean: it is the mean position of all points in all the co-ordinate direction.
Could any one please suggest me how to do it using R.
Thanks in advance.
Added later:
data1<-data.frame(x=c(1,4,7,3), y=c(3,2,8,5))
data2<-data.frame(x=c(9,14,17,30), y=c(5,7,3,25))
The centroids of the clusters are :
m1=c(mean(data1[,1]),mean(data1[,2]))
m2=c(mean(data2[,1]),mean(data2[,2]))
Now I want to align (coinside) m1 and m2 and get all the cluster points with respect to the common
aligned centroid. How to do this.
Thanks in advance.
Bit late to the party, but:
the technical term for aligning the origin is centering.
if your groups are in different variables, you can use scale:
data1<-data.frame(x=c(1,4,7,3), y=c(3,2,8,5))
data2<-data.frame(x=c(9,14,17,30), y=c(5,7,3,25))
scale (data1, center = TRUE, scale = FALSE)
scale (data2, center = TRUE, scale = FALSE)
if the groups are within the same data frame, ave can help (see also aggregate:
data1$group <- "A"
data2$group <- "B"
data <- rbind (data1, data2)
data$x <- data$x - ave (data$x, data$group)
data$y <- data$y - ave (data$y, data$group)
Typically you would use kmeans to do something like this. Normally you would attempt to determine the optimal number of clusters but in your case if you are assuming just a global mean and therefore one cluster then something like this:
#Two datasets
data1<-data.frame(x=c(1,4,7,3), y=c(3,2,8,5))
data2<-data.frame(x=c(9,14,17,30), y=c(5,7,3,25))
#combine datasets into one data frame
comb.data<-rbind(data1,data2)
#find the center of the data by assuming there is only one
fit<-kmeans(comb.data, centers=1)
#print out coordinates of center:
fit$centers
You can also plot it with the cluster package with the above code and this added:
library(cluster)
clusplot(comb.data, fit$cluster, color=T)
More info can be found here http://www.statmethods.net/advstats/cluster.html but there's a thousand references for clustering in R depending on if your interests are biology, chemometrics, etc.

Octave 3D mesh, data from file

I have a big file with 3 columns: density, dimension, value.
example:
10 0.3 200
10 0.4 300
20 0.3 250
20 0.4 320
I am trying to draw a 3d plot - mesh with mesh() function in octave, like this:
data = load ("file.txt");
mesh(data(:,1), data (:,2), data (:,3));
Problem I have is , I always get error:
rows (z) must be the same as length (y), columns (z) must be the same as length (x).
It worked with function plot3(), but I would like a mesh kind of plot.
The problem is that mesh(X,Y,Z) is expecting your X and Y matrices to be generated using X = meshgrid(x) and Y = meshgrid(y) where x and y only contain unique points. Your data basically already defines the meshgrid, but it is difficult to get it out.
I suggest using reshape as:
X = reshape(data(:,1),m,n);
Y = reshape(data(:,2),m,n); % might be reshape(data(:,2),n,m)
Z = reshape(data(:,3),m,n);
mesh(X,Y,Z);
In this case the assumption is that you have m unique values in Y, and n unique values in X. You may have to transpose these in your call to mesh as mesh(X',Y',Z) or something like that.

R: Converting cartesian coordinates to polar coordinates, and then calculating distance from origin

I've been looking for a solution to convert cartesian coordinates (lat, long) that I have to polar coordinates in order to facilitate a simulation that I want to run, but I haven't found any questions or answers here for doing this in R. There are a number of options, including the built in function cart2pol in Matlab, but all of my data are in R and I'd like to continue getting comfortable working in this framework.
Question:
I have lat/long coordinates from tagging data, and I want to convert these to polar coordinates (meaning jump size and angle: http://en.wikipedia.org/wiki/Polar_coordinate_system) so that I can then shuffle or bootstrap them (haven't decided which) about 1,000 times, and calculate the straight-line distance of each simulated track from the starting point. I have a true track, and I'm interested in determining if this animal is exhibiting site affinity by simulating 1,000 random tracks with the same jump sizes and turning angles, but in completely different orders and combinations. So I need 1,000 straight-line distances from the origin to create a distribution of distances and then compare this to my true data set's straight-line distance.
I'm comfortable doing the bootstrapping, but I'm stuck at the very first step, which is converting my cartesian lat/long coordinates to polar coordinates (jump size and turning angle). I know there are built in functions to do this in other programs such as Matlab, but I can't find any way to do it in R. I could do it manually by hand in a for-loop, but if there's a package out there or any easier way to do it, I'd much prefer that.
Ideally I'd like to convert the data to polar coordinates, run the simulation, and then for each random track output an end point as cartesian coordinates, lat/long, so I can then calculate the straight-line distance traveled.
I didn't post any sample data, as it would just be a two-column data frame of lat and long coordinates.
Thanks for any help you can provide! If there's an easy explanation somewhere on this site or others that I missed, please point me in that direction! I couldn't find anything.
Cheers
For x-y coordinates that are in the same units (e.g. meters rather than degrees of latitude and degrees of longitude), you can use this function to get a data.frame of jump sizes and turning angles (in degrees).
getSteps <- function(x,y) {
d <- diff(complex(real = x, imaginary = y))
data.frame(size = Mod(d),
angle = c(NA, diff(Arg(d)) %% (2*pi)) * 360/(2*pi))
}
## Try it out
set.seed(1)
x <- rnorm(10)
y <- rnorm(10)
getSteps(x, y)
# size angle
# 1 1.3838360 NA
# 2 1.4356900 278.93771
# 3 2.9066189 101.98625
# 4 3.5714584 144.00231
# 5 1.6404354 114.73369
# 6 1.3082132 135.76778
# 7 0.9922699 74.09479
# 8 0.2036045 141.67541
# 9 0.9100189 337.43632
## A plot helps check that this works
plot(x, y, type = "n", asp = 1)
text(x, y, labels = 1:10)
You can do a transformation bewteen cartesian and polar this way:
polar2cart <- function(r, theta) {
data.frame(x = r * cos(theta), y = r * sin(theta))
}
cart2polar <- function(x, y) {
data.frame(r = sqrt(x^2 + y^2), theta = atan2(y, x))
}
Since it is fairly straight forward, you can write your own function. Matlab-like cart2pol function in R:
cart2pol <- function(x, y)
{
r <- sqrt(x^2 + y^2)
t <- atan(y/x)
c(r,t)
}
I used Josh O'Brien's code and got what appear to be reasonable jumps and angles—they match up pretty well to eyeballing the rough distance and heading between points. I then used a formula from his suggestions to create a function to turn the polar coordinates back to cartesian coordinates, and a for loop to apply the function to the data frame of all of the polar coordinates. The loops appear to work, and the outputs are in the correct units, but I don't believe the values that it's outputting are corresponding to my data. So either I did a miscalculation with my formula, or there's something else going on. More details below:
Here's the head of my lat long data:
> head(Tag1SSM[,3:4])
lon lat
1 130.7940 -2.647957
2 130.7873 -2.602994
3 130.7697 -2.565903
4 130.7579 -2.520757
5 130.6911 -2.704841
6 130.7301 -2.752182
When I plot the full dataset just as values, I get this plot:
which looks exactly the same as if I were to plot this using any spatial or mapping package in R.
I then used Josh's function to convert my data to polar coordinates:
x<-Tag1SSM$lon
y<-Tag1SSM$lat
getSteps <- function(x,y) {
d <- diff(complex(real = x, imaginary = y))
data.frame(size = Mod(d),
angle = c(NA, diff(Arg(d)) %% (2*pi)) * 360/(2*pi))
}
which produced the following polar coordinates appropriately:
> polcoords<-getSteps(x,y)
> head(polcoords)
size angle
1 0.04545627 NA
2 0.04103718 16.88852
3 0.04667590 349.38153
4 0.19581350 145.35439
5 0.06130271 59.37629
6 0.01619242 31.86359
Again, these look right to me, and correspond well to the actual angles and relative distances between points. So far so good.
Now I want to convert these back to cartesian coordinates and calculate a euclidian distance from the origin. These don't have to be in true lat/long, as I'm just comparing them amongst themselves. So I'm happy for the origin to be set as (0,0) and for distances to be calculated in reference x,y values instead of kilometers or something like that.
So, I used this function with Josh's help and a bit of web searching:
polar2cart<-function(x,y,size,angle){
#convert degrees to radians (dividing by 360/2*pi, or multiplying by pi/180)
angle=angle*pi/180
if(is.na(x)) {x=0} #this is for the purpose of the for loop below
if(is.na(y)) {y=0}
newx<-x+size*sin(angle) ##X #this is how you convert back to cartesian coordinates
newy<-y+size*cos(angle) ##Y
return(c("x"=newx,"y"=newy)) #output the new x and y coordinates
}
And then plugged it into this for loop:
u<-polcoords$size
v<-polcoords$angle
n<-162 #I want 162 new coordinates, starting from 0
N<-cbind(rep(NA,163),rep(NA,163)) #need to make 163 rows, though, for i+1 command below— first row will be NA
for(i in 1:n){
jump<-polar2cart(N[i,1],N[i,2],u[i+1],v[i+1]) #use polar2cart function above, jump from previous coordinate in N vector
N[i+1,1]<-jump[1] #N[1,] will be NA's which sets the starting point to 0,0—new coords are then calculated from each previous N entry
N[i+1,2]<-jump[2]
Dist<-sqrt((N[163,1]^2)+(N[163,2]^2))
}
And then I can take a look at N, with my new coordinates based on those jumps:
> N
[,1] [,2]
[1,] NA NA
[2,] 0.011921732 0.03926732
[3,] 0.003320851 0.08514394
[4,] 0.114640605 -0.07594871
[5,] 0.167393509 -0.04472125
[6,] 0.175941466 -0.03096891
This is where the problem is... the x,y coordinates from N get progressively larger—there's a bit of variation in there, but if you scroll down the list, y goes from 0.39 to 11.133, with very few backward steps to lower values. This isn't what my lat/long data do, and if I calculated the cart->pol and pol->cart properly, these new values from N should match my lat/long data, just in a different coordinate system. This is what the N values look like plotted:
Not the same at all... The last point in N is the farthest point from the origin, while in my lat/long data, the last point is actually quite close to the first point, and definitely not the farthest point away. I think the issue must be in my conversion from polar coordinates back to cartesian coordinates, but I'm not sure how to fix it...
Any help in solving this would be much appreciated!
Cheers
I think this code I wrote converts to polar coordinates:
# example data
x<-runif(30)
y<-runif(30)
# center example around 0
x<-x-mean(x)
y<-y-mean(y)
# function to convert to polar coordinates
topolar<-function(x,y){
# calculate angles
alphas<-atan(y/x)
# correct angles per quadrant
quad2<-which(x<0&y>0)
quad3<-which(x<0&y<0)
quad4<-which(x>0&y<0)
alphas[quad2]<-alphas[quad2]+pi
alphas[quad3]<-alphas[quad3]+pi
alphas[quad4]<-alphas[quad4]+2*pi
# calculate distances to 0,0
r<-sqrt(x^2+y^2)
# create output
polar<-data.frame(alphas=alphas,r=r)
}
# call function
polar_out<-topolar(x,y)
# get out angles
the_angles<-polar_out$alphas
Another option only in degree
pol2car = function(angle, dist){
co = dist*sin(angle)
ca = dist*cos(angle)
return(list(x=ca, y=co))
}
pol2car(angle = 45, dist = sqrt(2))
cart2sph {pracma} Transforms between cartesian, spherical, polar, and cylindrical coordinate systems in two and three dimensions.

Resources