in reply to Re: Permutation and comparison loop
in thread Permutation and comparison loop

Thanks, this works great. The input data I showed you was simplified. My real input data gives the latitude (from -90 to 90) & longitude (0 to 360) for each corner of the image. Now I am trying to adapt the code to these new requirements. The difficult part is that when an image crosses the 0 to 360 longitude line the code can't correctly identify what it overlaps with. I need an elegant solution to this.

Replies are listed 'Best First'.
Re^3: Permutation and comparison loop
by BrowserUk (Patriarch) on Apr 21, 2011 at 01:45 UTC
    • For the latitudes, if you add 90 to minlat and maxlat, then you remove the negative numbers and the normal comparisons work.

      Provided that your images do not cross the North or South poles. If they do, then things get real weird and you need to move to an alternative coordinates system.

    • For the longitudes, if you add 360 to both coordinates, then you remove the 0 from the picture and the math again works
    #! perl -slw use strict; use enum qw[ IMAGENO MINLAT MAXLAT MINLON MAXLON ]; sub overlapLat { my( $a, $b ) = @_; return if $a->[MINLAT]+90 > $b->[MAXLAT]+90 or $a->[MAXLAT]+90 < $b->[MINLAT]+90; return 1; } sub overlapLon { my( $a, $b ) = @_; return if $a->[MINLON]+360 > $b->[MAXLON]+360 or $a->[MAXLON]+360 < $b->[MINLON]+360; return 1; } ##discard header <DATA>; my @rects = map[ split ',' ], <DATA>; for my $first ( 0 .. $#rects ) { for my $second ( $first + 1 .. $#rects ) { if( overlapLat( @rects[ $first, $second ] ) and overlapLon( @rects[ $first, $second ] ) ) { print "@{$rects[ $first ] } overlaps \n@{ $rects[ $second +] }"; } } } __DATA__ "Image number","minlat","maxlat","minlon","maxlon" ...

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Permutation and comparison loop
by LanX (Saint) on Apr 20, 2011 at 23:31 UTC
    Why reinventing the wheel?

    Clipping algorithms are the basis of most computer graphics book.

    IMHO Cohen–Sutherland is quite straight forward...

    Nota bene: you need spherical geometry to calculate "lines" (great circles) connecting "corners". But then it's the same principle.

    Latitudes are not the shortest connections between 2 points!!!

    Cheers Rolf

Re^3: Permutation and comparison loop
by wind (Priest) on Apr 21, 2011 at 09:09 UTC

    This is no problem

    All you have to do is be careful how you define your min and max longitude. If an area starts at longitude 350 and is 20 clicks wide going to 10, then your minimum longitude is 350 and max is 10. The same algorithm works that either I and BrowserUk provided

    If your data is defined so that it gives you a range of 350-370, then all you need to is take modulus 360 of your data ($max = $max % 360). Same thing is true if it's -10 to 20 ($min = $min % 360).

    There's no reason to touch latitudes, as they won't ever be outside of the -90 to 90 range.

      There's no reason to touch latitudes, as they won't ever be outside of the -90 to 90 range.

      Satellites that do global imaging tend to fly in polar orbits. So, it possible to imagine an image taken when the sat is directly over the pole, (I think that this is technically infeasible, but they can get pretty damn close), that has coordinates for its four corners:

      (85,45) 90 (85,135) +--------|--------+ | | North | | | Pole | | | / | 0/360 +--------0--------+ 180 | | | | | | | | | +--------|--------+ (85,315) 270 (85,225)

      If that was represented by the OPs minlat/maxlat, minlon/maxlon system, then you get: nnn, 85, 85, 0, 360 which isn't going to play well for overlap detection.

      The simplest fix is to break such transition images into two (or 4) parts:

      nnn-a, 85, 90, 0, 180 nnn-b, 85, 90, 180, 360

      But even then, the distortions that occur at high latitudes screws the math as soon as you start doing any trig, so spherical coordinates become pretty much de rigueur.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Aye. However, your example image isn't exactly representative of what his coordinates would define, which would be two adjacent pie slices instead of a full rectangle. I doubt that his data would work that way, but yes anything is possible.

        Your solution of breaking up the wierd region would work though, yes. For MaxLong > 90

        MinLat, MaxLat, MinLong, MaxLong
        to
        MinLat, MaxLat, MinLon, 90 # and (MinLat+180)%360, (MaxLat+180)%360, 180-MaxLong, 90