lists of conditions

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.

• Something like And @@ Thread[SLat <= qLat <= NLat]? Oct 22 '12 at 12:36
• 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? Oct 22 '12 at 14:49

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}};

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}

• Thanks a lot that works fantastic, exactly what I wanted and also thanks for the explanation i didn't know about the "vanishing function" Oct 22 '12 at 12:53
• @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. Oct 22 '12 at 12:56
• yeap that's true I will wait a bit longer next time, thanks again Oct 22 '12 at 13:03
• Does this deal correctly with grids crossing the 180 deg E/W line? Oct 22 '12 at 13:59
• @Sjoerd I just transcribed Khali's test. Feel free to extend this answer or post a better one. Oct 22 '12 at 14:01