Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Something like And @@ Thread[SLat <= qLat <= NLat]? –  J. M. 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? –  whuber Oct 22 '12 at 14:49

1 Answer 1

up vote 4 down vote accepted

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}

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.