1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # GISExtractor.pm -- extension base class to enhance plugins with GIS capabilities
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 | package GISExtractor;
|
---|
27 |
|
---|
28 | use PrintInfo;
|
---|
29 |
|
---|
30 | use util;
|
---|
31 |
|
---|
32 | use gsprintf 'gsprintf';
|
---|
33 | use strict;
|
---|
34 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
35 | no strict 'subs';
|
---|
36 |
|
---|
37 | #field categories in DataBase files
|
---|
38 | #$LAT = 3;
|
---|
39 | #$LONG = 4;
|
---|
40 | my $FC = 9;
|
---|
41 | my $DSG = 10;
|
---|
42 | #$CC1 = 12;
|
---|
43 | my $FULL_NAME = 22;
|
---|
44 |
|
---|
45 | BEGIN {
|
---|
46 | @GISExtractor::ISA = ('PrintInfo');
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | my $arguments =
|
---|
51 | [ { 'name' => "extract_placenames",
|
---|
52 | 'desc' => "{GISExtractor.extract_placenames}",
|
---|
53 | 'type' => "flag",
|
---|
54 | 'reqd' => "no" },
|
---|
55 | { 'name' => "gazetteer",
|
---|
56 | 'desc' => "{GISExtractor.gazetteer}",
|
---|
57 | 'type' => "string",
|
---|
58 | 'reqd' => "no" },
|
---|
59 | { 'name' => "place_list",
|
---|
60 | 'desc' => "{GISExtractor.place_list}",
|
---|
61 | 'type' => "flag",
|
---|
62 | 'reqd' => "no" } ];
|
---|
63 |
|
---|
64 |
|
---|
65 | my $options = { 'name' => "GISExtractor",
|
---|
66 | 'desc' => "{GISExtractor.desc}",
|
---|
67 | 'abstract' => "yes",
|
---|
68 | 'inherits' => "yes",
|
---|
69 | 'args' => $arguments };
|
---|
70 |
|
---|
71 |
|
---|
72 | sub new {
|
---|
73 | my ($class) = shift (@_);
|
---|
74 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
75 | push(@$pluginlist, $class);
|
---|
76 |
|
---|
77 | # can we indicate that these are not available if the map data is not there??
|
---|
78 | #if (has_mapdata()) {
|
---|
79 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
80 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
81 | #}
|
---|
82 |
|
---|
83 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);
|
---|
84 |
|
---|
85 | return bless $self, $class;
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | sub initialise_gis_extractor {
|
---|
90 | my $self = shift (@_);
|
---|
91 |
|
---|
92 | if ($self->{'extract_placenames'}) {
|
---|
93 |
|
---|
94 | my $outhandle = $self->{'outhandle'};
|
---|
95 |
|
---|
96 | my $places_ref
|
---|
97 | = $self->loadGISDatabase($outhandle,$self->{'gazetteer'});
|
---|
98 |
|
---|
99 | if (!defined $places_ref) {
|
---|
100 | print $outhandle "Warning: Error loading mapdata gazetteer \"$self->{'gazetteer'}\"\n";
|
---|
101 | print $outhandle " No placename extraction will take place.\n";
|
---|
102 | $self->{'extract_placenames'} = undef;
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | $self->{'places'} = $places_ref;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 | sub extract_gis_metadata
|
---|
113 | {
|
---|
114 | my $self = shift (@_);
|
---|
115 | my ($doc_obj) = @_;
|
---|
116 |
|
---|
117 | if ($self->{'extract_placenames'}) {
|
---|
118 | my $thissection = $doc_obj->get_top_section();
|
---|
119 | while (defined $thissection) {
|
---|
120 | my $text = $doc_obj->get_text($thissection);
|
---|
121 | $self->extract_placenames (\$text, $doc_obj, $thissection) if $text =~ /./;
|
---|
122 | $thissection = $doc_obj->get_next_section ($thissection);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 | sub has_mapdata
|
---|
129 | {
|
---|
130 | my $db_dir = &util::filename_cat($ENV{'GSDLHOME'}, "lamp", "data");
|
---|
131 | return ( -d $db_dir );
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | #returns a hash table of names from database files (specified in collect.cfg).
|
---|
136 | sub loadGISDatabase {
|
---|
137 | my ($outhandle,$datasets) = @_;
|
---|
138 | my @dbase = map{$_ = $_ . ".txt";} split(/,/, $datasets);
|
---|
139 | if(scalar(@dbase)==0) { #default is to include all databases
|
---|
140 | @dbase=("UK.txt", "NZ.txt", "NF.txt", "CA.txt", "AS.txt", "GM.txt", "BE.txt", "IN.txt", "JA.txt", "USA.txt");
|
---|
141 | }
|
---|
142 | my $counter=0;
|
---|
143 | my %places = ();
|
---|
144 | my @cats = ();
|
---|
145 | while($counter <= $#dbase){ #loop through all the databases
|
---|
146 | my $folder = $dbase[$counter];
|
---|
147 | $folder =~ s/(.*?)\.txt/$1/;
|
---|
148 | #### my $dbName = &util::filename_cat($ENV{'GSDLHOME'}, "etc", "mapdata", "data", $folder, $dbase[$counter]);
|
---|
149 | my $dbName = &util::filename_cat($ENV{'GSDLHOME'}, "lamp", "data", $folder, $dbase[$counter]);
|
---|
150 | if (!open(FILEIN, "<$dbName")) {
|
---|
151 | print $outhandle "Unable to open database $dbName: $!\n";
|
---|
152 | return undef;
|
---|
153 | }
|
---|
154 |
|
---|
155 | my $line = <FILEIN>; #database category details.
|
---|
156 | my @catdetails = split("\t", $line);
|
---|
157 |
|
---|
158 | while ( defined($line = <FILEIN>)){ #stores all place names in file as keys in hash array %places
|
---|
159 | @cats = split("\t", $line);
|
---|
160 | if( #eliminating "bad" place names without missing real ones
|
---|
161 | ($cats[$FC] eq "A" && !($cats[$DSG] =~ /ADMD|ADM2|PRSH/))
|
---|
162 | ||($cats[$FC] eq "P" && ($cats[$DSG] =~ /PPLA|PPLC|PPLS/))
|
---|
163 | ||($cats[$FC] eq "H" && ($cats[$DSG] =~ /BAY|LK/))
|
---|
164 | ||($cats[$FC] eq "L" && !($cats[$DSG] =~ /LCTY|RGN/))
|
---|
165 | ||($cats[$FC] eq "T" && ($cats[$DSG] =~ /CAPE|ISL|GRGE/))
|
---|
166 | ||($dbase[$counter] eq "USA.txt" && ($cats[$DSG] =~ /ppl|island/))
|
---|
167 | ){$places{$cats[$FULL_NAME]} = [@cats];}
|
---|
168 | @cats = ();
|
---|
169 | }
|
---|
170 | close(FILEIN);
|
---|
171 | $counter++;
|
---|
172 | }
|
---|
173 | return \%places;
|
---|
174 | }
|
---|
175 |
|
---|
176 | #returns a unique hash array of all the places found in a document, along with coordinates, description and country data
|
---|
177 | sub getPlaces {
|
---|
178 | my $self = shift @_;
|
---|
179 |
|
---|
180 | my ($textref, $places) = @_;
|
---|
181 | my %tempPlaces = ();
|
---|
182 |
|
---|
183 | foreach my $plc (%$places){ #search for an occurrence of each place in the text
|
---|
184 | if($$textref =~ m/(\W)$plc(\W)/){
|
---|
185 | $tempPlaces{$plc} = $places->{$plc};
|
---|
186 | }
|
---|
187 | }
|
---|
188 | #make sure each place is only there once
|
---|
189 | my %uniquePlaces = ();
|
---|
190 | foreach my $p (keys %tempPlaces) {
|
---|
191 | if(!defined($uniquePlaces{$p})){
|
---|
192 | $uniquePlaces{$p} = $tempPlaces{$p};
|
---|
193 | }
|
---|
194 | }
|
---|
195 | return \%uniquePlaces;
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 | #returns a lowercase version of the place, with no spaces
|
---|
202 | sub placename_to_anchorname {
|
---|
203 | my ($placename) = @_;
|
---|
204 | my $p_tag = lc($placename);
|
---|
205 | $p_tag =~ s/\s+//g;
|
---|
206 | return $p_tag;
|
---|
207 | }
|
---|
208 |
|
---|
209 | #takes a place from the text and wraps an anchor tag, a hyperlink tag and an image tag around it
|
---|
210 | sub anchor_wrapper {
|
---|
211 | my ($p, $p_counter_ref, $path) = @_;
|
---|
212 | my $image = "/gsdlgis/gisimages/nextplace.gif";
|
---|
213 | my $endTag = "</a>";
|
---|
214 | my $hrefTag = "<a href=";
|
---|
215 | $$p_counter_ref++;
|
---|
216 | my $next = $$p_counter_ref + 1;
|
---|
217 | my $p_tag = placename_to_anchorname($p);
|
---|
218 | my $place_anchor = "<a name=\"" . $p_tag . $$p_counter_ref . "\">" . $endTag;
|
---|
219 | my $popup_anchor = $hrefTag . "'" . $path . "'>" . $p . $endTag;
|
---|
220 | my $image_anchor = $hrefTag . "#" . $p_tag . $next . "><img src=\"" . $image . "\" name=\"img" . $p_tag . $$p_counter_ref . "\" border=\"0\">" . $endTag;
|
---|
221 | return $place_anchor . $popup_anchor . $image_anchor;
|
---|
222 | }
|
---|
223 |
|
---|
224 | #takes dangerous place names and checks if they are part of another placename or not.
|
---|
225 | sub place_name_check {
|
---|
226 | my ($pre, $preSpace, $p, $postSpace, $post, $p_counter_ref, $path, $y) = @_;
|
---|
227 | if($pre =~ /$y/ || $post =~ /$y/) {return $pre . $preSpace . $p . $postSpace . $post;}
|
---|
228 | $pre = $pre . $preSpace;
|
---|
229 | $post = $postSpace . $post;
|
---|
230 | return $pre . &anchor_wrapper("", $p, "", $p_counter_ref, $path) . $post;
|
---|
231 | }
|
---|
232 |
|
---|
233 | sub extract_placenames {
|
---|
234 | my $self = shift (@_);
|
---|
235 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
236 | my $outhandle = $self->{'outhandle'};
|
---|
237 |
|
---|
238 | my $GSDLHOME = $ENV{'GSDLHOME'};
|
---|
239 | #field categories in DataBase file for extract_placenames.
|
---|
240 | my $LAT = 3;
|
---|
241 | my $LONG = 4;
|
---|
242 | my $CC1 = 12;
|
---|
243 |
|
---|
244 |
|
---|
245 | &gsprintf($outhandle, " {GISExtractor.extracting_placenames}...\n")
|
---|
246 | if ($self->{'verbosity'} > 2);
|
---|
247 |
|
---|
248 | #get all the places found in the document
|
---|
249 | my $uniquePlaces = $self->getPlaces($textref, $self->{'places'});
|
---|
250 |
|
---|
251 | #finds 'dangerous' placenames (eg York and New York). Dangerous because program will find "York" within "New York"
|
---|
252 | my %danger = ();
|
---|
253 | foreach my $x (keys %$uniquePlaces){
|
---|
254 | foreach my $y (keys %$uniquePlaces){
|
---|
255 | if(($y =~ m/ /) && ($y =~ m/$x/) && ($y ne $x)){
|
---|
256 | $y =~ s/($x\s)|(\s$x)//;
|
---|
257 | $danger{$x} = $y;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | #creates a list of clickable placenames at top of page, linked to first occurrence of name, and reads them into a file
|
---|
263 | my $tempfname = $doc_obj;
|
---|
264 | $tempfname =~ s/.*\(0x(.*)\)/$1/;
|
---|
265 | my $names = "";
|
---|
266 | my $filename = "tmpfile" . $tempfname;
|
---|
267 | my $tempfile = &util::filename_cat($GSDLHOME, "tmp", $filename);
|
---|
268 | open(FOUT, ">$tempfile") || die "Unable to create a temp file: $!";
|
---|
269 | foreach my $name (sort (keys %$uniquePlaces)){
|
---|
270 | if(!defined($danger{$name})){
|
---|
271 | my $name_tag = placename_to_anchorname($name);
|
---|
272 | print FOUT "$name\t" . $uniquePlaces->{$name}->[$LONG] . "\t" . $uniquePlaces->{$name}->[$LAT] . "\n";
|
---|
273 | if($self->{'place_list'}) {$names = $names . "<a href=\"#" . $name_tag . "1\">" . $name . "</a>" . "\n";}
|
---|
274 | }
|
---|
275 | }
|
---|
276 | close(FOUT);
|
---|
277 | $doc_obj->associate_file($tempfile, "places.txt", "text/plain");
|
---|
278 | $self->{'places_filename'} = $tempfile;
|
---|
279 |
|
---|
280 | my %countries = ();
|
---|
281 |
|
---|
282 | foreach my $p (keys %$uniquePlaces){
|
---|
283 | my $place = $p;
|
---|
284 | $place =~ s/\s+|\n+|\r+|\t+/(\\s+)/g;
|
---|
285 | my $cap_place = uc($place);
|
---|
286 | my $long = $uniquePlaces->{$p}->[$LONG];
|
---|
287 | my $lat = $uniquePlaces->{$p}->[$LAT];
|
---|
288 | my $country = $uniquePlaces->{$p}->[$CC1];
|
---|
289 | my $path = "javascript:popUp(\"$long\",\"$lat\",\"$p\",\"$country\")";
|
---|
290 | my $p_counter = 0;
|
---|
291 |
|
---|
292 | if(!defined($danger{$p})){
|
---|
293 | #adds html tags to each place name
|
---|
294 | $$textref =~ s/\b($place|$cap_place)\b/&anchor_wrapper($1,\$p_counter,$path)/sge;
|
---|
295 | }
|
---|
296 | #else {
|
---|
297 | #$y = $danger{$p};
|
---|
298 | #$$textref =~ s/(\w+)(\s+?)($place|$cap_place)(\s+?)(\w+)/&place_name_check($1,$2,$3,$4,$5,\$p_counter,$path, $y)/sge;
|
---|
299 | #}
|
---|
300 |
|
---|
301 | #edits the last place's image, and removes image if place only occurres once.
|
---|
302 | my $p_tag = placename_to_anchorname($p);
|
---|
303 | $p_counter++;
|
---|
304 | $$textref =~ s/#$p_tag$p_counter(><img src="\/gsdl\/images\/)nextplace.gif/#${p_tag}1$1firstplace.gif/;
|
---|
305 | $$textref =~ s/<img src="\/gsdl\/images\/firstplace.gif" name="img$p_tag(1)" border="0">//;
|
---|
306 |
|
---|
307 | #this line removes apostrophes from placenames (they break the javascript function)
|
---|
308 | $$textref =~ s/(javascript:popUp.*?)(\w)'(\w)/$1$2$3/g; #' (to get emacs colours back)
|
---|
309 |
|
---|
310 | #for displaying map of document, count num of places from each country
|
---|
311 | if(defined($countries{$country})){$countries{$country}++;}
|
---|
312 | else{$countries{$country} = 1;}
|
---|
313 |
|
---|
314 | #adds placename to metadata
|
---|
315 | $doc_obj->add_utf8_metadata ($thissection, "Placename", $p);
|
---|
316 | &gsprintf($outhandle, " {AutoExtractMetadata.extracting} $p\n")
|
---|
317 | if ($self->{'verbosity'} > 3);
|
---|
318 | }
|
---|
319 | #finding the country that most places are from, in order to display map of the document
|
---|
320 | my $max = 0;
|
---|
321 | my $CNTRY = "";
|
---|
322 | foreach my $c_key (keys %countries){
|
---|
323 | if($countries{$c_key} > $max){
|
---|
324 | $max = $countries{$c_key};
|
---|
325 | $CNTRY = $c_key;
|
---|
326 | }
|
---|
327 | }
|
---|
328 | #allows user to view map with all places from the document on it
|
---|
329 | #### my $places_filename = &util::filename_cat($GSDLHOME, "collect", "_cgiargc_", "index", "assoc", "_thisOID_", "places.txt");
|
---|
330 | my $places_filename = &util::filename_cat("collect", "_cgiargc_", "index", "assoc", "_thisOID_", "places.txt");
|
---|
331 | my $docmap = "<a href='javascript:popUp(\"$CNTRY\",\"$places_filename\")'>View map for this document<\/a><br><br>\n";
|
---|
332 | $$textref = $docmap . $names . "<br>" . $$textref;
|
---|
333 |
|
---|
334 | $doc_obj->delete_text($thissection);
|
---|
335 | $doc_obj->add_utf8_text($thissection, $$textref);
|
---|
336 | &gsprintf($outhandle, " {GISExtractor.done_places_extract}\n")
|
---|
337 | if ($self->{'verbosity'} > 2);
|
---|
338 | }
|
---|
339 |
|
---|
340 | sub clean_up_temp_files {
|
---|
341 | my $self = shift(@_);
|
---|
342 |
|
---|
343 | if(defined($self->{'places_filename'}) && -e $self->{'places_filename'}){
|
---|
344 | &util::rm($self->{'places_filename'});
|
---|
345 | }
|
---|
346 | $self->{'places_filename'} = undef;
|
---|
347 |
|
---|
348 | }
|
---|