Hello, I have created a spatial map of temperature over an area thanks to interpolation. So I have temperature data everywhere on my map. My question is: how can I find temperature data on my map for a specific location according to coordinates? For this, I have a data frame containing 4 columns: "x" for longitude, "y" for latitude, "z" for altitude" and "temperature" for my data, for each pixel of my map. My real data has more than 9 million of rows (because I have a temperature data for each 0.0008? of longitude or latitude). Let's take a smallest example with 10 rows and so just 1? of LAT and LON for each pixel (just with "x", "y" and my data): test <- data.frame(x=c(1:10),y=c(41:50),temperature=rnorm(1:10)) In this example, I would like to ask the user of the algorithm to type on R the coordinates (x and y) for which the user wants the temperature. For example: cat("choose your coordinates:\n") x: "HERE THE USER SHOULD TYPE A NUMERIC VALUE" y: "HERE THE USER SHOULD TYPE A NUMERIC VALUE" and then R gives the value for temperature (in my third column). In this example, if the user type 6 for "x" and "46" for y, R should give as a result: "0.9713693" And if the coordinates typed by the user are between the coordinates in my data.frame, it should response the temperature value of the nearest pixel. For example, if the user type 3.89 for "x" and 43.62 for "y", R should give as a result: "0.6871172" (value of the nearest pixel: 4 for "x" and 44 for "y"). I absolutely don't know how to do this, and if it's easy to do or not. I didn't find anything about it on the web for R. Have you any idea or suggestions about a package, function or a way to do this? Hope you've understood! thanks everybody! -- View this message in context: http://r.789695.n4.nabble.com/How-to-find-data-in-a-map-according-to-coordinates-tp4639724.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2012-Aug-09 12:44 UTC
[R] How to find data in a map according to coordinates?
Hello, You have three coordinates but the problem description only envolves two of them, x and y. The code below is valid for any number of dimensions. min.dist <- function(p, coord){ which.min( colSums((t(coord) - p)^2) ) } # Use set.seed to have reproducible simulations set.seed(1) test <- data.frame(x=c(1:10),y=c(41:50),temperature=rnorm(1:10)) imin <- min.dist(c(4, 46), test[, 1:2]) test[imin, "temperature"] Hope this helps Rui Barradas Em 09-08-2012 07:56, jeff6868 escreveu:> Hello, > > I have created a spatial map of temperature over an area thanks to > interpolation. So I have temperature data everywhere on my map. > My question is: how can I find temperature data on my map for a specific > location according to coordinates? > > For this, I have a data frame containing 4 columns: "x" for longitude, "y" > for latitude, "z" for altitude" and "temperature" for my data, for each > pixel of my map. My real data has more than 9 million of rows (because I > have a temperature data for each 0.0008? of longitude or latitude). > > Let's take a smallest example with 10 rows and so just 1? of LAT and LON for > each pixel (just with "x", "y" and my data): > > test <- data.frame(x=c(1:10),y=c(41:50),temperature=rnorm(1:10)) > > In this example, I would like to ask the user of the algorithm to type on R > the coordinates (x and y) for which the user wants the temperature. > > For example: > cat("choose your coordinates:\n") > x: "HERE THE USER SHOULD TYPE A NUMERIC VALUE" > y: "HERE THE USER SHOULD TYPE A NUMERIC VALUE" > > and then R gives the value for temperature (in my third column). > > In this example, if the user type 6 for "x" and "46" for y, R should give > as a result: "0.9713693" > > And if the coordinates typed by the user are between the coordinates in my > data.frame, it should response the temperature value of the nearest pixel. > > For example, if the user type 3.89 for "x" and 43.62 for "y", R should give > as a result: "0.6871172" (value of the nearest pixel: 4 for "x" and 44 for > "y"). > > I absolutely don't know how to do this, and if it's easy to do or not. I > didn't find anything about it on the web for R. > Have you any idea or suggestions about a package, function or a way to do > this? > Hope you've understood! > > thanks everybody! > > > > > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-find-data-in-a-map-according-to-coordinates-tp4639724.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.