3

I am a bit new to the spatial packages of R. I have a hdf file from which I can get a data.frame with the latitude and longitude of the corners of the pixels, and the latitude and longitude of the centre of the pixel.

I have a specific point, and I would like to figure out which pixel covers that point.

Is there some function for this? what would be the best approach?

More information:

The data.frame is something like this:

> tibble(h)
# A tibble: 15,271 x 1
    h$LatA $LonA $LatB $LonB $LatC $LonC $LatD $LonD $LatCentre $LonCentre
    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>      <dbl>      <dbl>
 1   47.5  167.  47.0  167.  49.7  175.  49.3  175.       48.4       171.
 2   47.8  166.  47.3  167.  50.0  174.  49.6  175.       48.7       170.
 3   48.1  166.  47.6  166.  50.3  174.  50.0  174.       49.0       170.
 4   48.4  166.  47.9  166.  50.7  174.  50.3  174.       49.4       170.
 5   48.8  166.  48.3  166.  51.0  174.  50.7  174.       49.7       170.
 6   49.1  165.  48.6  166.  51.4  174.  51.0  174.       50.0       170.
 7   49.4  165.  48.9  166.  51.7  173.  51.3  174.       50.4       169.
 8   49.7  165.  49.2  165.  52.0  173.  51.7  173.       50.7       169.
 9   52.0  173.  51.7  173.  53.8  182.  53.4  182.       52.8       178.
10   50.0  165.  49.5  165.  52.4  173.  52.0  173.       51.0       169.
# … with 15,261 more rows
  • Are you looking for the pixel value or the row/column index for that pixel? – Aaron Mar 20 at 13:47
  • If you can't share your data, show us how you are getting the data frame from the HDF file. Also show us the format of the data frame. – Spacedman Mar 20 at 14:34
  • I am looking for the row index for that pixel @Aaron. Thank you for your comments. I updated the question – Javi_VM Mar 20 at 15:09
1

As Spacedman sujested, you should share with us more than that, almost a piece of your tibble data. In any case, and without knowledge about your data sources, projection, etc, you could find those "cells" where your points are just by searching the closest cell center. This is just valid (and logic) if you tibble represents a regular raster grid. As I said, the "cell" would be that one with the closest cell center. So, Here is the code with some imaginary points and your small data:

library(dplyr)
library(stringr)
library(tibble)
library(rgdal)
library(RANN)

#THIS REPLICATES YOUR h tibble

l <- c("47.5  167.  47.0  167.  49.7  175.  49.3  175.       48.4       171.",
       "47.8  166.  47.3  167.  50.0  174.  49.6  175.       48.7       170.",
       "48.1  166.  47.6  166.  50.3  174.  50.0  174.       49.0       170.",
       "48.4  166.  47.9  166.  50.7  174.  50.3  174.       49.4       170.",
       "48.8  166.  48.3  166.  51.0  174.  50.7  174.       49.7       170.",
       "49.1  165.  48.6  166.  51.4  174.  51.0  174.       50.0       170.",
       "49.4  165.  48.9  166.  51.7  173.  51.3  174.       50.4       169.",
       "49.7  165.  49.2  165.  52.0  173.  51.7  173.       50.7       169.",
       "52.0  173.  51.7  173.  53.8  182.  53.4  182.       52.8       178.",
       "50.0  165.  49.5  165.  52.4  173.  52.0  173.       51.0       169.")

lt <- strsplit(str_replace_all(str_replace_all(l,"  ", ","),",,, ", ","), ",")

h <- tibble(LatA = double(), LonA = double(),
            LatB = double(), LonB = double(),
            LatC = double(), LonC = double(),
            LatD = double(), LonD = double(),
            LatCentre = double(), LonCentre = double())

for (l in 1:length(lt)){
    h[l,] <- as.numeric(lt[[l]])
}

#THIS MAKES THE THING
#
# Create points from centers and try to find the nearest with traning points
centers <- data.frame(X = h$LonCentre, Y = h$LatCentre)
trypoints <- data.frame(X = c(169.9, 171.1), Y = c(49.2, 48.9))

# for each point in trypoints, find the nearest neighbor from centers
closest <- RANN::nn2(centers, trypoints, k = 1)

# Get coordinates of nearest neighbor
trypoints$X_snap <- centers[closest$nn.idx, "X"]
trypoints$Y_snap <- centers[closest$nn.idx, "Y"]

In the end, what you have is the point of sampling and the X and Y (X_snap and Y_snap) of the belonging "cell".

> trypoints
      X    Y X_snap Y_snap
1 169.9 49.2    170   49.4
2 171.1 48.9    171   48.4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.