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

            Romeo and Juliet ContentsCharactersSynopsisSourcesDate and textThemes and motifsCriticism and interpretationLegacyScene by sceneSee alsoNotes and referencesSourcesExternal linksNavigation menu"Consumer Price Index (estimate) 1800–"10.2307/28710160037-3222287101610.1093/res/II.5.31910.2307/45967845967810.2307/2869925286992510.1525/jams.1982.35.3.03a00050"Dada Masilo: South African dancer who breaks the rules"10.1093/res/os-XV.57.1610.2307/28680942868094"Sweet Sorrow: Mann-Korman's Romeo and Juliet Closes Sept. 5 at MN's Ordway"the original10.2307/45957745957710.1017/CCOL0521570476.009"Ram Leela box office collections hit massive Rs 100 crore, pulverises prediction"Archived"Broadway Revival of Romeo and Juliet, Starring Orlando Bloom and Condola Rashad, Will Close Dec. 8"Archived10.1075/jhp.7.1.04hon"Wherefore art thou, Romeo? To make us laugh at Navy Pier"the original10.1093/gmo/9781561592630.article.O006772"Ram-leela Review Roundup: Critics Hail Film as Best Adaptation of Romeo and Juliet"Archived10.2307/31946310047-77293194631"Romeo and Juliet get Twitter treatment""Juliet's Nurse by Lois Leveen""Romeo and Juliet: Orlando Bloom's Broadway Debut Released in Theaters for Valentine's Day"Archived"Romeo and Juliet Has No Balcony"10.1093/gmo/9781561592630.article.O00778110.2307/2867423286742310.1076/enst.82.2.115.959510.1080/00138380601042675"A plague o' both your houses: error in GCSE exam paper forces apology""Juliet of the Five O'Clock Shadow, and Other Wonders"10.2307/33912430027-4321339124310.2307/28487440038-7134284874410.2307/29123140149-661129123144728341M"Weekender Guide: Shakespeare on The Drive""balcony"UK public library membership"romeo"UK public library membership10.1017/CCOL9780521844291"Post-Zionist Critique on Israel and the Palestinians Part III: Popular Culture"10.2307/25379071533-86140377-919X2537907"Capulets and Montagues: UK exam board admit mixing names up in Romeo and Juliet paper"Istoria Novellamente Ritrovata di Due Nobili Amanti2027/mdp.390150822329610820-750X"GCSE exam error: Board accidentally rewrites Shakespeare"10.2307/29176390149-66112917639"Exam board apologises after error in English GCSE paper which confused characters in Shakespeare's Romeo and Juliet""From Mariotto and Ganozza to Romeo and Guilietta: Metamorphoses of a Renaissance Tale"10.2307/37323537323510.2307/2867455286745510.2307/28678912867891"10 Questions for Taylor Swift"10.2307/28680922868092"Haymarket Theatre""The Zeffirelli Way: Revealing Talk by Florentine Director""Michael Smuin: 1938-2007 / Prolific dance director had showy career"The Life and Art of Edwin BoothRomeo and JulietRomeo and JulietRomeo and JulietRomeo and JulietEasy Read Romeo and JulietRomeo and Julieteeecb12003684p(data)4099369-3n8211610759dbe00d-a9e2-41a3-b2c1-977dd692899302814385X313670221313670221