spacegeologist has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to write a code that finds all overlapping images from a list of images and their maximum and minimum latitudes and longitudes.

The Input file looks like this:

"Image number","minlat","maxlat","minlon","maxlon"

1,0,30,20,50

2,10,30,70,90

3,70,80,40,50

4,40,70,20,50

Here is the code, which is likely inefficient and riddled with errors, (this is my first programming language). Right now I'm just trying to compare the minimum latitudes. I've removed some warnings and comments

#Permutation loop my @n; my $n = 1; my %Image_info; ## Sort the data into a new hash. while (<>) { split /,/; $Image_info{$_[0]} = { latmin => $_[1], latmax => $_[2], lonmin => $_[3], lonmax => $_[4], }; }; ## Redifine the hash elements as scalars and begin comparison loop. for (keys %Image_info) { my $latmin1 = $Image_info{$_} -> {latmin}; my $latmax1 = $Image_info{$_} -> {latmax}; while ($n < 11) { my $latmin2 = $Image_info{$n} -> {latmin}; my $latmax2 = $Image_info{$n} -> {latmax}; if ($latmin1 > $latmin2) { print $latmin2, " is less than ", $latmin1, ".\n"; }; $n = $n +1; }; }

This is the output:

Use of implicit split to @_ is deprecated at overlap.plx line 19.

0 is less than 70.

10 is less than 70.

40 is less than 70.

The output is close but it should also print 0 is less than 10 and 10 is less than 40 ect. Can anyone tell me how to code this more effectively? Thanks in advance.

Replies are listed 'Best First'.
Re: Permutation and comparison loop
by BrowserUk (Patriarch) on Apr 20, 2011 at 02:12 UTC

    You might find this a bit simpler:

    #! perl -slw use strict; use enum qw[ IMAGENO MINLAT MAXLAT MINLON MAXLON ]; sub overlapLat { my( $a, $b ) = @_; return if $a->[MINLAT] > $b->[MAXLAT] or $a->[MAXLAT] < $b->[MINLAT]; return 1; } sub overlapLon { my( $a, $b ) = @_; return if $a->[MINLON] > $b->[MAXLON] or $a->[MAXLON] < $b->[MINLON]; return 1; } ##discard header <DATA>; my @rects = map[ split ',' ], <DATA>; my $m = 0; 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" 1,0,30,20,50 2,10,30,70,90 3,70,80,40,50 4,40,70,20,50 5,20,75,40,80

    Output:

    C:\test>junk87 1 0 30 20 50 overlaps 5 20 75 40 80 2 10 30 70 90 overlaps 5 20 75 40 80 3 70 80 40 50 overlaps 4 40 70 20 50 3 70 80 40 50 overlaps 5 20 75 40 80 4 40 70 20 50 overlaps 5 20 75 40 80

    One thing to watch for is whether to consider an image that ends at 70, overlaps with another that starts at 70 as with 3 & 4 above?


    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: Permutation and comparison loop
by wind (Priest) on Apr 20, 2011 at 02:46 UTC

    It only shows the relationships in one directly, but it works:

    use strict; use warnings; <DATA>; # Header my @images = map {chomp; [split ',']} <DATA>; for my $i (0..$#images) { for my $j ($i+1..$#images) { # Test for Min > Max for outside of range. next if $images[$i][1] > $images[$j][2] || $images[$j][1] > $images[$i][2] || $images[$i][3] > $images[$j][4] || $images[$j][3] > $images[$i][4]; print "$images[$i][0] overlaps $images[$j][0]\n"; } } __DATA__ Image number,minlat,maxlat,minlon,maxlon 1,0,30,20,50 2,10,30,70,90 3,70,80,40,50 4,40,70,20,50 5,20,75,40,80

      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.

        • 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.
        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

        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.