[9398] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15872] | 3 | # GISExtractor.pm -- extension base class to enhance plugins with GIS capabilities
|
---|
[9398] | 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 |
|
---|
[15872] | 26 | package GISExtractor;
|
---|
[9398] | 27 |
|
---|
[15872] | 28 | use PrintInfo;
|
---|
| 29 |
|
---|
[9398] | 30 | use util;
|
---|
| 31 |
|
---|
[11677] | 32 | use gsprintf 'gsprintf';
|
---|
[10254] | 33 | use strict;
|
---|
| 34 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[11677] | 35 | no strict 'subs';
|
---|
[15872] | 36 |
|
---|
[9398] | 37 | #field categories in DataBase files
|
---|
| 38 | #$LAT = 3;
|
---|
| 39 | #$LONG = 4;
|
---|
| 40 | my $FC = 9;
|
---|
| 41 | my $DSG = 10;
|
---|
| 42 | #$CC1 = 12;
|
---|
[10254] | 43 | my $FULL_NAME = 22;
|
---|
[9398] | 44 |
|
---|
| 45 | BEGIN {
|
---|
[15872] | 46 | @GISExtractor::ISA = ('PrintInfo');
|
---|
[9398] | 47 | }
|
---|
| 48 |
|
---|
| 49 |
|
---|
[15872] | 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" } ];
|
---|
[9398] | 63 |
|
---|
| 64 |
|
---|
[15872] | 65 | my $options = { 'name' => "GISExtractor",
|
---|
| 66 | 'desc' => "{GISExtractor.desc}",
|
---|
[9398] | 67 | 'abstract' => "yes",
|
---|
[15872] | 68 | 'inherits' => "yes",
|
---|
| 69 | 'args' => $arguments };
|
---|
[9398] | 70 |
|
---|
| 71 |
|
---|
| 72 | sub new {
|
---|
[15872] | 73 | my ($class) = shift (@_);
|
---|
| 74 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 75 | push(@$pluginlist, $class);
|
---|
[9398] | 76 |
|
---|
[15872] | 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 | #}
|
---|
[15919] | 82 |
|
---|
[15881] | 83 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);
|
---|
[9398] | 84 |
|
---|
[15919] | 85 | return bless $self, $class;
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | sub initialise_gis_extractor {
|
---|
| 90 | my $self = shift (@_);
|
---|
| 91 |
|
---|
[15872] | 92 | if ($self->{'extract_placenames'}) {
|
---|
[15919] | 93 |
|
---|
[15872] | 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 | }
|
---|
[15919] | 108 |
|
---|
| 109 |
|
---|
[9398] | 110 | }
|
---|
| 111 |
|
---|
[15872] | 112 | sub extract_gis_metadata
|
---|
[9398] | 113 | {
|
---|
[15872] | 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 | }
|
---|
[9398] | 125 |
|
---|
[10254] | 126 | }
|
---|
[9398] | 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 {
|
---|
[18752] | 137 | my $self = shift (@_);
|
---|
[9398] | 138 | my ($outhandle,$datasets) = @_;
|
---|
| 139 | my @dbase = map{$_ = $_ . ".txt";} split(/,/, $datasets);
|
---|
| 140 | if(scalar(@dbase)==0) { #default is to include all databases
|
---|
| 141 | @dbase=("UK.txt", "NZ.txt", "NF.txt", "CA.txt", "AS.txt", "GM.txt", "BE.txt", "IN.txt", "JA.txt", "USA.txt");
|
---|
| 142 | }
|
---|
| 143 | my $counter=0;
|
---|
| 144 | my %places = ();
|
---|
| 145 | my @cats = ();
|
---|
| 146 | while($counter <= $#dbase){ #loop through all the databases
|
---|
| 147 | my $folder = $dbase[$counter];
|
---|
| 148 | $folder =~ s/(.*?)\.txt/$1/;
|
---|
| 149 | #### my $dbName = &util::filename_cat($ENV{'GSDLHOME'}, "etc", "mapdata", "data", $folder, $dbase[$counter]);
|
---|
| 150 | my $dbName = &util::filename_cat($ENV{'GSDLHOME'}, "lamp", "data", $folder, $dbase[$counter]);
|
---|
| 151 | if (!open(FILEIN, "<$dbName")) {
|
---|
| 152 | print $outhandle "Unable to open database $dbName: $!\n";
|
---|
| 153 | return undef;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | my $line = <FILEIN>; #database category details.
|
---|
| 157 | my @catdetails = split("\t", $line);
|
---|
| 158 |
|
---|
| 159 | while ( defined($line = <FILEIN>)){ #stores all place names in file as keys in hash array %places
|
---|
| 160 | @cats = split("\t", $line);
|
---|
| 161 | if( #eliminating "bad" place names without missing real ones
|
---|
| 162 | ($cats[$FC] eq "A" && !($cats[$DSG] =~ /ADMD|ADM2|PRSH/))
|
---|
| 163 | ||($cats[$FC] eq "P" && ($cats[$DSG] =~ /PPLA|PPLC|PPLS/))
|
---|
| 164 | ||($cats[$FC] eq "H" && ($cats[$DSG] =~ /BAY|LK/))
|
---|
| 165 | ||($cats[$FC] eq "L" && !($cats[$DSG] =~ /LCTY|RGN/))
|
---|
| 166 | ||($cats[$FC] eq "T" && ($cats[$DSG] =~ /CAPE|ISL|GRGE/))
|
---|
| 167 | ||($dbase[$counter] eq "USA.txt" && ($cats[$DSG] =~ /ppl|island/))
|
---|
| 168 | ){$places{$cats[$FULL_NAME]} = [@cats];}
|
---|
| 169 | @cats = ();
|
---|
| 170 | }
|
---|
| 171 | close(FILEIN);
|
---|
| 172 | $counter++;
|
---|
| 173 | }
|
---|
| 174 | return \%places;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | #returns a unique hash array of all the places found in a document, along with coordinates, description and country data
|
---|
| 178 | sub getPlaces {
|
---|
| 179 | my $self = shift @_;
|
---|
| 180 |
|
---|
| 181 | my ($textref, $places) = @_;
|
---|
| 182 | my %tempPlaces = ();
|
---|
| 183 |
|
---|
| 184 | foreach my $plc (%$places){ #search for an occurrence of each place in the text
|
---|
| 185 | if($$textref =~ m/(\W)$plc(\W)/){
|
---|
| 186 | $tempPlaces{$plc} = $places->{$plc};
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | #make sure each place is only there once
|
---|
| 190 | my %uniquePlaces = ();
|
---|
| 191 | foreach my $p (keys %tempPlaces) {
|
---|
| 192 | if(!defined($uniquePlaces{$p})){
|
---|
| 193 | $uniquePlaces{$p} = $tempPlaces{$p};
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | return \%uniquePlaces;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | #returns a lowercase version of the place, with no spaces
|
---|
| 203 | sub placename_to_anchorname {
|
---|
[18752] | 204 | my $self = shift (@_);
|
---|
| 205 | my ($placename) = @_;
|
---|
| 206 | my $p_tag = lc($placename);
|
---|
| 207 | $p_tag =~ s/\s+//g;
|
---|
| 208 | return $p_tag;
|
---|
[9398] | 209 | }
|
---|
| 210 |
|
---|
| 211 | #takes a place from the text and wraps an anchor tag, a hyperlink tag and an image tag around it
|
---|
| 212 | sub anchor_wrapper {
|
---|
| 213 | my ($p, $p_counter_ref, $path) = @_;
|
---|
| 214 | my $image = "/gsdlgis/gisimages/nextplace.gif";
|
---|
| 215 | my $endTag = "</a>";
|
---|
| 216 | my $hrefTag = "<a href=";
|
---|
| 217 | $$p_counter_ref++;
|
---|
| 218 | my $next = $$p_counter_ref + 1;
|
---|
| 219 | my $p_tag = placename_to_anchorname($p);
|
---|
| 220 | my $place_anchor = "<a name=\"" . $p_tag . $$p_counter_ref . "\">" . $endTag;
|
---|
| 221 | my $popup_anchor = $hrefTag . "'" . $path . "'>" . $p . $endTag;
|
---|
| 222 | my $image_anchor = $hrefTag . "#" . $p_tag . $next . "><img src=\"" . $image . "\" name=\"img" . $p_tag . $$p_counter_ref . "\" border=\"0\">" . $endTag;
|
---|
| 223 | return $place_anchor . $popup_anchor . $image_anchor;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | #takes dangerous place names and checks if they are part of another placename or not.
|
---|
| 227 | sub place_name_check {
|
---|
| 228 | my ($pre, $preSpace, $p, $postSpace, $post, $p_counter_ref, $path, $y) = @_;
|
---|
| 229 | if($pre =~ /$y/ || $post =~ /$y/) {return $pre . $preSpace . $p . $postSpace . $post;}
|
---|
| 230 | $pre = $pre . $preSpace;
|
---|
| 231 | $post = $postSpace . $post;
|
---|
| 232 | return $pre . &anchor_wrapper("", $p, "", $p_counter_ref, $path) . $post;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | sub extract_placenames {
|
---|
| 236 | my $self = shift (@_);
|
---|
| 237 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
| 238 | my $outhandle = $self->{'outhandle'};
|
---|
| 239 |
|
---|
| 240 | my $GSDLHOME = $ENV{'GSDLHOME'};
|
---|
| 241 | #field categories in DataBase file for extract_placenames.
|
---|
| 242 | my $LAT = 3;
|
---|
| 243 | my $LONG = 4;
|
---|
| 244 | my $CC1 = 12;
|
---|
| 245 |
|
---|
| 246 |
|
---|
[16013] | 247 | &gsprintf($outhandle, " {GISExtractor.extracting_placenames}...\n")
|
---|
[9398] | 248 | if ($self->{'verbosity'} > 2);
|
---|
| 249 |
|
---|
| 250 | #get all the places found in the document
|
---|
| 251 | my $uniquePlaces = $self->getPlaces($textref, $self->{'places'});
|
---|
| 252 |
|
---|
| 253 | #finds 'dangerous' placenames (eg York and New York). Dangerous because program will find "York" within "New York"
|
---|
| 254 | my %danger = ();
|
---|
[10254] | 255 | foreach my $x (keys %$uniquePlaces){
|
---|
| 256 | foreach my $y (keys %$uniquePlaces){
|
---|
[9398] | 257 | if(($y =~ m/ /) && ($y =~ m/$x/) && ($y ne $x)){
|
---|
| 258 | $y =~ s/($x\s)|(\s$x)//;
|
---|
| 259 | $danger{$x} = $y;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | #creates a list of clickable placenames at top of page, linked to first occurrence of name, and reads them into a file
|
---|
| 265 | my $tempfname = $doc_obj;
|
---|
| 266 | $tempfname =~ s/.*\(0x(.*)\)/$1/;
|
---|
| 267 | my $names = "";
|
---|
| 268 | my $filename = "tmpfile" . $tempfname;
|
---|
| 269 | my $tempfile = &util::filename_cat($GSDLHOME, "tmp", $filename);
|
---|
| 270 | open(FOUT, ">$tempfile") || die "Unable to create a temp file: $!";
|
---|
| 271 | foreach my $name (sort (keys %$uniquePlaces)){
|
---|
| 272 | if(!defined($danger{$name})){
|
---|
[10254] | 273 | my $name_tag = placename_to_anchorname($name);
|
---|
[9398] | 274 | print FOUT "$name\t" . $uniquePlaces->{$name}->[$LONG] . "\t" . $uniquePlaces->{$name}->[$LAT] . "\n";
|
---|
| 275 | if($self->{'place_list'}) {$names = $names . "<a href=\"#" . $name_tag . "1\">" . $name . "</a>" . "\n";}
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | close(FOUT);
|
---|
| 279 | $doc_obj->associate_file($tempfile, "places.txt", "text/plain");
|
---|
| 280 | $self->{'places_filename'} = $tempfile;
|
---|
[15872] | 281 |
|
---|
[9398] | 282 | my %countries = ();
|
---|
| 283 |
|
---|
| 284 | foreach my $p (keys %$uniquePlaces){
|
---|
| 285 | my $place = $p;
|
---|
| 286 | $place =~ s/\s+|\n+|\r+|\t+/(\\s+)/g;
|
---|
| 287 | my $cap_place = uc($place);
|
---|
| 288 | my $long = $uniquePlaces->{$p}->[$LONG];
|
---|
| 289 | my $lat = $uniquePlaces->{$p}->[$LAT];
|
---|
| 290 | my $country = $uniquePlaces->{$p}->[$CC1];
|
---|
| 291 | my $path = "javascript:popUp(\"$long\",\"$lat\",\"$p\",\"$country\")";
|
---|
| 292 | my $p_counter = 0;
|
---|
| 293 |
|
---|
| 294 | if(!defined($danger{$p})){
|
---|
| 295 | #adds html tags to each place name
|
---|
| 296 | $$textref =~ s/\b($place|$cap_place)\b/&anchor_wrapper($1,\$p_counter,$path)/sge;
|
---|
| 297 | }
|
---|
| 298 | #else {
|
---|
| 299 | #$y = $danger{$p};
|
---|
| 300 | #$$textref =~ s/(\w+)(\s+?)($place|$cap_place)(\s+?)(\w+)/&place_name_check($1,$2,$3,$4,$5,\$p_counter,$path, $y)/sge;
|
---|
| 301 | #}
|
---|
| 302 |
|
---|
| 303 | #edits the last place's image, and removes image if place only occurres once.
|
---|
| 304 | my $p_tag = placename_to_anchorname($p);
|
---|
| 305 | $p_counter++;
|
---|
| 306 | $$textref =~ s/#$p_tag$p_counter(><img src="\/gsdl\/images\/)nextplace.gif/#${p_tag}1$1firstplace.gif/;
|
---|
| 307 | $$textref =~ s/<img src="\/gsdl\/images\/firstplace.gif" name="img$p_tag(1)" border="0">//;
|
---|
| 308 |
|
---|
| 309 | #this line removes apostrophes from placenames (they break the javascript function)
|
---|
[15872] | 310 | $$textref =~ s/(javascript:popUp.*?)(\w)'(\w)/$1$2$3/g; #' (to get emacs colours back)
|
---|
[9398] | 311 |
|
---|
| 312 | #for displaying map of document, count num of places from each country
|
---|
| 313 | if(defined($countries{$country})){$countries{$country}++;}
|
---|
| 314 | else{$countries{$country} = 1;}
|
---|
| 315 |
|
---|
| 316 | #adds placename to metadata
|
---|
| 317 | $doc_obj->add_utf8_metadata ($thissection, "Placename", $p);
|
---|
[16013] | 318 | &gsprintf($outhandle, " {AutoExtractMetadata.extracting} $p\n")
|
---|
[9398] | 319 | if ($self->{'verbosity'} > 3);
|
---|
| 320 | }
|
---|
| 321 | #finding the country that most places are from, in order to display map of the document
|
---|
| 322 | my $max = 0;
|
---|
| 323 | my $CNTRY = "";
|
---|
| 324 | foreach my $c_key (keys %countries){
|
---|
| 325 | if($countries{$c_key} > $max){
|
---|
| 326 | $max = $countries{$c_key};
|
---|
| 327 | $CNTRY = $c_key;
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
| 330 | #allows user to view map with all places from the document on it
|
---|
[9963] | 331 | #### my $places_filename = &util::filename_cat($GSDLHOME, "collect", "_cgiargc_", "index", "assoc", "_thisOID_", "places.txt");
|
---|
| 332 | my $places_filename = &util::filename_cat("collect", "_cgiargc_", "index", "assoc", "_thisOID_", "places.txt");
|
---|
[9398] | 333 | my $docmap = "<a href='javascript:popUp(\"$CNTRY\",\"$places_filename\")'>View map for this document<\/a><br><br>\n";
|
---|
| 334 | $$textref = $docmap . $names . "<br>" . $$textref;
|
---|
| 335 |
|
---|
| 336 | $doc_obj->delete_text($thissection);
|
---|
| 337 | $doc_obj->add_utf8_text($thissection, $$textref);
|
---|
[16013] | 338 | &gsprintf($outhandle, " {GISExtractor.done_places_extract}\n")
|
---|
[9398] | 339 | if ($self->{'verbosity'} > 2);
|
---|
| 340 | }
|
---|
[15872] | 341 |
|
---|
| 342 | sub clean_up_temp_files {
|
---|
| 343 | my $self = shift(@_);
|
---|
| 344 |
|
---|
| 345 | if(defined($self->{'places_filename'}) && -e $self->{'places_filename'}){
|
---|
| 346 | &util::rm($self->{'places_filename'});
|
---|
| 347 | }
|
---|
| 348 | $self->{'places_filename'} = undef;
|
---|
| 349 |
|
---|
| 350 | }
|
---|