5
$\begingroup$

I have got coordinates in two list of Latitude and Longitude and would like to check if those coordinates are in a certain grid

Example lists are:

qLon={-7.48333, -10.4667, -8.66667, -7.48333}
qLat={53.5, 52.5, 53.1167, 51.9833, 51.0167}

Grid borders:

  SLat=47
  NLat=55
  ELon=-15
  SLon=-5

My idea was:

If[ELon <= # <= WLon, "","Outside Grid"] & /@ qLon
If[SLat <= # <= NLat, "","Outside Grid"] & /@ qLat

I would like to merge them, to do the test in one go, like:

If[ELon <= #1 <= WLon && SLat <= #2 <= NLat,...

but could not get it working.

Another thing is the output; it should only show something if there is a point outside the grid.

$\endgroup$
  • $\begingroup$ Something like And @@ Thread[SLat <= qLat <= NLat]? $\endgroup$ – J. M. is away Oct 22 '12 at 12:36
  • $\begingroup$ Normally, queries of this sort are not performed against a single fixed cell of a grid. (When they are, the process is called "clipping" the points against the rectangle defined by the cell.) A more typical application is to identify which of many cells in a raster array each of a given set of points falls in. Would this perhaps be what you are doing? $\endgroup$ – whuber Oct 22 '12 at 14:49
4
$\begingroup$

I assume the latitude and longitude lists should be the same length, and that SLon should be WLon.

qLon = {-7.48333, -10.4667, -8.66667, -7.48333, -8, 3, 99};
qLat = {53.5, 52.5, 53.1167, 51.9833, 51.0167, 62.1, 50};

{{SLat, NLat}, {ELon, WLon}} = {{47, 55}, {-15, -5}};

MapThread[
 If[ELon <= #1 <= WLon && SLat <= #2 <= NLat, "In", "Out"] &,
 {qLon, qLat}
]
{"In", "In", "In", "In", "In", "Out", "Out"}

For the second part of your question you can use the "vanishing function" ## &[], which works inside If where Sequence[] fails because If does not have the attribute SequenceHold. Perhaps you want:

MapThread[
 If[
    ELon <= #1 <= WLon && SLat <= #2 <= NLat,
    ## &[],
    Row[{"Out:", #, #2}, "  "]
 ]&,
 {qLon, qLat}
]

{Out: 3 62.1, Out: 99 50}

$\endgroup$
  • $\begingroup$ Thanks a lot that works fantastic, exactly what I wanted and also thanks for the explanation i didn't know about the "vanishing function" $\endgroup$ – Khali Oct 22 '12 at 12:53
  • $\begingroup$ @Khali I'm glad I could help. I always recommend waiting a bit longer before Accepting an answer, as it may discourage someone from posting a better one. $\endgroup$ – Mr.Wizard Oct 22 '12 at 12:56
  • $\begingroup$ yeap that's true I will wait a bit longer next time, thanks again $\endgroup$ – Khali Oct 22 '12 at 13:03
  • $\begingroup$ Does this deal correctly with grids crossing the 180 deg E/W line? $\endgroup$ – Sjoerd C. de Vries Oct 22 '12 at 13:59
  • $\begingroup$ @Sjoerd I just transcribed Khali's test. Feel free to extend this answer or post a better one. $\endgroup$ – Mr.Wizard Oct 22 '12 at 14:01

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.