Geoinformatica  0.90
Polygonize.pm
Go to the documentation of this file.
1 #** @file Polygonize.pm
2 #*
3 
4 package Geo::Raster::Layer::Dialogs::Polygonize;
5 
6 use strict;
7 use warnings;
8 use Carp;
9 use Glib qw/TRUE FALSE/;
10 use Gtk2::Ex::Geo::Dialogs qw/:all/;
11 
12 sub open {
13  my($self, $gui) = @_;
14 
15  # bootstrap:
16  my($dialog, $boot) = $self->bootstrap_dialog
17  ($gui, 'polygonize_dialog', "Polygonize ".$self->name,
18  {
19  polygonize_dialog => [delete_event => \&cancel_polygonize, [$self, $gui]],
20  polygonize_cancel_button => [clicked => \&cancel_polygonize, [$self, $gui]],
21  polygonize_ok_button => [clicked => \&do_polygonize, [$self, $gui, 1]],
22  });
23 
24  if ($boot) {
25  $dialog->get_widget('polygonize_datasource_button')
26  ->signal_connect( clicked=> sub {
27  my(undef, $self) = @_;
28  my $entry = $self->{polygonize_dialog}->get_widget('polygonize_datasource_entry');
29  file_chooser('Select folder', 'select_folder', $entry);
30  }, $self);
31 
32  my $combo = $dialog->get_widget('polygonize_driver_combobox');
33  my $renderer = Gtk2::CellRendererText->new;
34  $combo->pack_start($renderer, TRUE);
35  $combo->add_attribute($renderer, text => 0);
36 
37  $combo = $dialog->get_widget('polygonize_datasource_combobox');
38  $renderer = Gtk2::CellRendererText->new;
39  $combo->pack_start($renderer, TRUE);
40  $combo->add_attribute($renderer, text => 0);
41  }
42 
43  my $model = Gtk2::ListStore->new('Glib::String');
44  for my $layer (@{$gui->{overlay}->{layers}}) {
45  my $n = $layer->name();
46  next unless $layer->isa('Geo::Vector');
47  next unless $layer->{update};
48  next if $n eq $self->name();
49  $model->set($model->append, 0, $n);
50  }
51  my $combo = $dialog->get_widget('polygonize_name_comboboxentry');
52  $combo->set_model($model);
53  $combo->set_text_column(0);
54  $combo->get_child->set_text($self->name.'_polygonized');
55 
56  $model = Gtk2::ListStore->new('Glib::String');
57  $model->set ($model->append, 0, '');
58  for my $datasource (sort keys %{$self->{gui}{resources}{datasources}}) {
59  $model->set ($model->append, 0, $datasource);
60  }
61  $combo = $dialog->get_widget('polygonize_datasource_combobox');
62  $combo->set_model($model);
63  $combo->set_active(0);
64 
65  $model = Gtk2::ListStore->new('Glib::String');
66  my $i = 1;
67  my $active = 0;
68  $model->set($model->append, 0, ''); # create into existing data source
69  for my $driver (Geo::OGR::Drivers()) {
70  next unless $driver->TestCapability('CreateDataSource');
71  my $name = $driver->GetName;
72  $active = $i if $name eq 'Memory';
73  $model->set($model->append, 0, $name);
74  $i++;
75  }
76  $combo = $dialog->get_widget('polygonize_driver_combobox');
77  $combo->set_model($model);
78  $combo->set_active($active);
79 
80  return $dialog->get_widget('polygonize_dialog');
81 }
82 
83 sub do_polygonize {
84  my($self, $gui, $close) = @{$_[1]};
85  my $dialog = $self->{polygonize_dialog};
86 
87  my %params;
88  my $into = $dialog->get_widget('polygonize_name_comboboxentry')->get_active_text;
89  croak "Store into?" unless $into;
90  my $into_layer = $gui->layer($into);
91  if ($into_layer) {
92  croak $into_layer->name." is not a vector layer" unless $into_layer->isa('Geo::Vector');
93  $params{schema} = $into_layer->schema;
94  } else {
95  my $combo = $dialog->get_widget('polygonize_datasource_combobox');
96  my $active = $combo->get_active();
97  if ($active > 0) {
98  my $model = $combo->get_model;
99  my $iter = $model->get_iter_from_string($active);
100  my $store = $model->get($iter, 0);
101  $params{data_source} = $self->{gui}{resources}{datasources}{$store};
102  } else {
103  $params{data_source} = $dialog->get_widget('polygonize_datasource_entry')->get_text;
104  $params{driver} = $dialog->get_widget('polygonize_driver_combobox')->get_active_text;
105  }
106  $params{create} = $into;
107  my $layers;
108  if ($params{driver} ne 'Memory') {
109  eval {
110  $layers = Geo::Vector::layers($params{driver}, $params{data_source});
111  };
112  }
113  croak "Data source '$params{data_source}' already contains a layer with name '$params{create}'."
114  if ($layers and $layers->{$params{create}});
115  $params{schema} = { Fields => [{ Name => 'value', Type => 'Integer' }] };
116  }
117  if ($dialog->get_widget('connectedness_checkbutton')->get_active) {
118  $params{options} = { '8CONNECTED' => 1 };
119  } else {
120  $params{options} = undef;
121  }
122  $params{callback} = \&progress;
123  $params{callback_date} = $dialog->get_widget('polygonize_progressbar');
124  my $vector = $self->polygonize(%params);
125  print STDERR "pol res $vector\n";
126  if ($vector) {
127  $gui->add_layer($vector, $into, 1);
128  $gui->{overlay}->render;
129  }
130  $self->hide_dialog('polygonize_dialog') if $close;
131  #$gui->set_layer($self);
132  #$gui->{overlay}->render;
133 }
134 
135 sub cancel_polygonize {
136  my($self, $gui);
137  for (@_) {
138  next unless ref CORE::eq 'ARRAY';
139  ($self, $gui) = @{$_};
140  }
141 
142  $self->hide_dialog('polygonize_dialog');
143  $gui->set_layer($self);
144  $gui->{overlay}->render;
145  1;
146 }
147 
148 1;
public method new(hash params)