geoTiff Application



I am writing a simple Gtk2 application that loads a geotiff and allows the user to add and remove waypoints. So far the user can add a waypoint by double clicking and is able to drag the window to navigate. I would like to see if anyone can help me with adding some of the other features.
1. ZOOMING. I added a zooming feature to the map but when you zoom in or out and try to add a waypoint, the circle is far off from where the user clicked. Can anyone give examples on how to fix this? (thanks to zentara for the help)
2. Removing Waypoints. Adding them is easy but removing them seems more difficult.
3. Waypoint Connecting Lines. When there are two waypoints a line is drawn to them in sequence. But when there are four of them the circuit is messed up. Its hard to type it so here is a screenshot, geoapp. In the screenshot the connector from 1 to 3 should not be there when the fourth waypoint is added. I can only speculate that the canvas is drawing the new connectors over the old ones. Is there a good way to manage these Gnome2::Canvas items so that they are not layered?

The last two are more important than the zooming, that is just a neat feature. Later, dynamic items such as metar observations and GPS positions will be added but right now I need to learn the basics. Thanks for the help. Here is the entire script.

#!/usr/bin/perl
use warnings;
use strict;
use Glib qw/TRUE FALSE/;
use Gtk2 -init;
use Image::ExifTool;
use Geo::Coordinates::UTM;
use Gnome2::Canvas;
use Gtk2::Ex::Dragger;

my $count = 1;

#geoTiff File (static)
my $filename = 'q3639_DRG24k-c.tif';

#geostats
my $ellipsoid = 23; #static WSG83
my $zone;
my @mtp;
my @ps;
my @cs;

#Waypoint Holder
my %waypoints = ();

#Create Window
my $window = new Gtk2::Window ( "toplevel" );
$window->signal_connect ("delete_event", sub { Gtk2->main_quit; });
$window->set_border_width (10);
$window->set_size_request(640,480);
$window->set_position('center');

#create Table
my $table = Gtk2::Table->new(2, 2, FALSE);

#Create Scrolled Window
my $scwin = Gtk2::ScrolledWindow->new();
$scwin->set_policy('always','always');

#add GeoTiff
my $exifTool = new Image::ExifTool;
my $info = $exifTool->ImageInfo($filename);
foreach (sort keys %$info) {
    print "$_ => $$info{$_}\n";
   
    #Find the right keys(data)
    if ($_ eq "ModelTiePoint"){
        @mtp = split(/ /,$$info{$_});
    }
    if ($_ eq "PixelScale"){
        @ps = split(/ /,$$info{$_});
    }
    if ($_ eq "ProjectedCSType"){
        @cs = split(/ /,$$info{$_});
        $zone = $cs[3];
    }
}
   
#set viewing area
my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file ($filename);

#Create Canvas
my $canvas = Gnome2::Canvas->new_aa();
my $root   = $canvas->root;
my $image = Gnome2::Canvas::Item->new ($root,
                   'Gnome2::Canvas::Pixbuf',
                     pixbuf => $pixbuf,
                   x      => 0,
                          y      => 0,
                   width  => $pixbuf->get_width,
                   height => $pixbuf->get_height,
                   anchor => 'nw',
               );
$canvas->set_scroll_region(0,0,$pixbuf->get_width,$pixbuf->get_height);
$image->lower_to_bottom();
   
#Pack
$window->add($table);
$scwin->add($canvas);
$table->attach_defaults($scwin, 0, 2, 0, 1);

#change the cursor over image
$canvas->realize;
$canvas->window->set_cursor(Gtk2::Gdk::Cursor->new ('hand2'));

#Draggability of window
my $adj = Gtk2::Adjustment->new (1.00, 0.05, 5.00, 0.05, 0.50, 0.50);
my $dragger = Gtk2::Ex::Dragger->new( widget => $scwin,
    hadjustment => $scwin->get_hadjustment,
    vadjustment => $scwin->get_vadjustment,
    cursor => 'hand1'
    );

#handle the events
$canvas->signal_connect (event => \&event_handler);

$window->show_all;

Gtk2->main;
#------------------------------------------------
sub event_handler {
    my ( $widget, $event ) = @_;
    # print $widget ,' ',$event->type,"\n";
   
    #on 2 mouse presses, place waypoint
    if ( $event->type eq "2button-press" ) {
        print 'x->',$event->x,'  ','y->',$event->y;
       
        #convert UTM to Lat and Long
        my $easting = $ps[0] * $event->x + 0.0 * $event->y + $mtp[3];
        my $northing = (-$ps[1]) * $event->y + 0.0 * $event->x + $mtp[4];
        my ($latitude,$longitude)=utm_to_latlon($ellipsoid,$zone,$easting,$northing);
        print " ($latitude, $longitude)\n";
       
        #Drop icon
        my $tgroup = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Group', x => $event->x, y => $event->y);
        Gnome2::Canvas::Item->new($tgroup, 'Gnome2::Canvas::Ellipse',
            x1 => -7.5,
            y1 => -7.5,
            x2 => 7.5,
            y2 => 7.5,
            fill_color => 'purple',
            outline_color => 'black');
        Gnome2::Canvas::Item->new ($tgroup,
            'Gnome2::Canvas::Text',
            "text", "$count",
            "x", 0.0,
            "y", 0.0,
            "font", "Sans Bold",
            "anchor", 'GTK_ANCHOR_NW',
            "weight", 100,
            "fill_color", 'red',
            "size_points", 20);
       
        #add waypoints
        $waypoints{$count } = {'x' => $event->x,'y' => $event->y, 'lat' => $latitude, 'long' => $longitude};
        print "size of hash:  " . keys( %waypoints ) . ".\n";
       
        #draw lines to the waypoints
        if ($count >= 2){
            my @points;
            foreach my $key (sort keys %waypoints){
                foreach my $subkey (sort keys %{$waypoints{$key}}){
                    if ($subkey eq 'x' || $subkey eq 'y'){
                        push(@points, $waypoints{$key}{$subkey});
                        print "$key $subkey = $waypoints{$key}{$subkey}\n";
                    }
                   
                }
               
            }
            if ($count >= 3){
                push(@points, $waypoints{1}{'x'});
                push(@points, $waypoints{1}{'y'});
            }
           
            my $lgroup = Gnome2::Canvas::Item->new ($root, 'Gnome2::Canvas::Group');
            Gnome2::Canvas::Item->new ($lgroup,
                'Gnome2::Canvas::Line',
                points => [ points],
                fill_color => 'black',
                width_units => 4.0);
           
            foreach (@points){
                print $_, "\n";
            }  
        }
       
        #print $count, "\n";
        $tgroup->raise_to_top();
        $count++;
    }
   
    #on mouse press, pan the window
    if ( $event->type eq "button-press" ) {
        $dragger->start ($event);
    }
}

--
Jamie Ryan Lahowetz
University of Nebraska - Lincoln
Graduate Student - Geosciences
402.304.0766
jlahowe2 bigred unl edu

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]