For a specific point in space, get the pixel in which that point lies (in R) Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How to get the second nearest neighbor between two point patterns in R?How to get specific coordinate system to get arcGIS point shapefile in r?R: Return the UTM zone that a WGS84 point belongs toFinding the side of a square that a point intersects with R (sp)Finding Statistical Area (SA) in which address (LatLong) lies via R Point in Polygon?QGIS Tool/Script to run that highlights location point features within their respective polygon feature based on the same parcel IDFinding xy-coordinates inside a polygon, which are the closest to the centroid of that specific polygonSelect % of pixel that have a specific value in RR: Annual composite based on the median: How to get the index of the original layer for each pixel?How to get latitude and longitude for a matrix data using specific projection in R?

Is multiple magic items in one inherently imbalanced?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

Co-worker has annoying ringtone

AppleTVs create a chatty alternate WiFi network

How did Fremen produce and carry enough thumpers to use Sandworms as de facto Ubers?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Google .dev domain strangely redirects to https

How were pictures turned from film to a big picture in a picture frame before digital scanning?

How do I tell what width chain my used chainring needs?

Customizing QGIS plugins

Random body shuffle every night—can we still function?

Do I really need to have a message in a novel to appeal to readers?

Dyck paths with extra diagonals from valleys (Laser construction)

Did any compiler fully use 80-bit floating point?

How to align multiple equations

How to run automated tests after each commit?

preposition before coffee

How could we fake a moon landing now?

What is the home of the drow in Flanaess?

What is the meaning of 'breadth' in breadth first search?

Crossing US/Canada Border for less than 24 hours

Do wooden building fires get hotter than 600°C?

Why do early math courses focus on the cross sections of a cone and not on other 3D objects?



For a specific point in space, get the pixel in which that point lies (in R)



Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How to get the second nearest neighbor between two point patterns in R?How to get specific coordinate system to get arcGIS point shapefile in r?R: Return the UTM zone that a WGS84 point belongs toFinding the side of a square that a point intersects with R (sp)Finding Statistical Area (SA) in which address (LatLong) lies via R Point in Polygon?QGIS Tool/Script to run that highlights location point features within their respective polygon feature based on the same parcel IDFinding xy-coordinates inside a polygon, which are the closest to the centroid of that specific polygonSelect % of pixel that have a specific value in RR: Annual composite based on the median: How to get the index of the original layer for each pixel?How to get latitude and longitude for a matrix data using specific projection in R?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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









share|improve this question
























  • 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

















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









share|improve this question
























  • 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













3












3








3


1






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









share|improve this question
















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






r select-by-location






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 at 15:08







Javi_VM

















asked Mar 20 at 9:36









Javi_VMJavi_VM

162




162












  • 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

















  • 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
















Are you looking for the pixel value or the row/column index for that pixel?

– Aaron
Mar 20 at 13:47





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





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





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 Answer
1






active

oldest

votes


















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





share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "79"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f316079%2ffor-a-specific-point-in-space-get-the-pixel-in-which-that-point-lies-in-r%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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





    share|improve this answer



























      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





      share|improve this answer

























        1












        1








        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 11 at 14:25









        César ArqueroCésar Arquero

        879524




        879524



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f316079%2ffor-a-specific-point-in-space-get-the-pixel-in-which-that-point-lies-in-r%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

            Crop image to path created in TikZ? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Crop an inserted image?TikZ pictures does not appear in posterImage behind and beyond crop marks?Tikz picture as large as possible on A4 PageTransparency vs image compression dilemmaHow to crop background from image automatically?Image does not cropTikzexternal capturing crop marks when externalizing pgfplots?How to include image path that contains a dollar signCrop image with left size given

            QGIS export composer to PDF scale the map [closed] Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Print Composer QGIS 2.6, how to export image?QGIS 2.8.1 print composer won't export all OpenCycleMap base layer tilesSave Print/Map QGIS composer view as PNG/PDF using Python (without changing anything in visible layout)?Export QGIS Print Composer PDF with searchable text labelsQGIS Print Composer does not change from landscape to portrait orientation?How can I avoid map size and scale changes in print composer?Fuzzy PDF export in QGIS running on macSierra OSExport the legend into its 100% size using Print ComposerScale-dependent rendering in QGIS PDF output