1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # GISBasPlug.pm -- 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 GISBasPlug;
|
---|
27 |
|
---|
28 | use util;
|
---|
29 | use locale;
|
---|
30 |
|
---|
31 | use strict;
|
---|
32 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
33 |
|
---|
34 | #field categories in DataBase files
|
---|
35 | #$LAT = 3;
|
---|
36 | #$LONG = 4;
|
---|
37 | my $FC = 9;
|
---|
38 | my $DSG = 10;
|
---|
39 | #$CC1 = 12;
|
---|
40 | my $FULL_NAME = 22;
|
---|
41 |
|
---|
42 | BEGIN {
|
---|
43 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | use BasPlug; # uses BasPlug, but is not inherited
|
---|
48 |
|
---|
49 |
|
---|
50 | my $options = { 'name' => "GISBasPlug",
|
---|
51 | 'desc' => "{GISBasPlug.desc}",
|
---|
52 | 'abstract' => "yes",
|
---|
53 | 'inherits' => "no" };
|
---|
54 |
|
---|
55 |
|
---|
56 | sub new {
|
---|
57 | my $class = shift (@_);
|
---|
58 | my $plugin_name = shift (@_);
|
---|
59 |
|
---|
60 | my $self = {};
|
---|
61 | $self->{'plugin_type'} = "GISBasPlug";
|
---|
62 |
|
---|
63 | $self->{'option_list'} = [ $options ];
|
---|
64 |
|
---|
65 | return bless $self, $class;
|
---|
66 | }
|
---|
67 |
|
---|
68 | sub init {
|
---|
69 | }
|
---|
70 |
|
---|
71 | sub print_xml_usage
|
---|
72 | {
|
---|
73 | BasPlug::print_xml_usage(@_);
|
---|
74 | }
|
---|
75 |
|
---|
76 | sub print_xml
|
---|
77 | {
|
---|
78 | BasPlug::print_xml(@_);
|
---|
79 | }
|
---|
80 |
|
---|
81 | sub print_txt_usage
|
---|
82 | {
|
---|
83 | BasPlug::print_txt_usage(@_);
|
---|
84 | }
|
---|
85 |
|
---|
86 | sub set_keepold
|
---|
87 | {
|
---|
88 | BasPlug::set_keepold(@_);
|
---|
89 | }
|
---|
90 |
|
---|
91 | sub has_mapdata
|
---|
92 | {
|
---|
93 | my $db_dir = &util::filename_cat($ENV{'GSDLHOME'}, "lamp", "data");
|
---|
94 | return ( -d $db_dir );
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | #returns a hash table of names from database files (specified in collect.cfg).
|
---|
99 | sub loadGISDatabase {
|
---|
100 | my ($outhandle,$datasets) = @_;
|
---|
101 | my @dbase = map{$_ = $_ . ".txt";} split(/,/, $datasets);
|
---|
102 | if(scalar(@dbase)==0) { #default is to include all databases
|
---|
103 | @dbase=("UK.txt", "NZ.txt", "NF.txt", "CA.txt", "AS.txt", "GM.txt", "BE.txt", "IN.txt", "JA.txt", "USA.txt");
|
---|
104 | }
|
---|
105 | my $counter=0;
|
---|
106 | my %places = ();
|
---|
107 | my @cats = ();
|
---|
108 | while($counter <= $#dbase){ #loop through all the databases
|
---|
109 | my $folder = $dbase[$counter];
|
---|
110 | $folder =~ s/(.*?)\.txt/$1/;
|
---|
111 | #### my $dbName = &util::filename_cat($ENV{'GSDLHOME'}, "etc", "mapdata", "data", $folder, $dbase[$counter]);
|
---|
112 | my $dbName = &util::filename_cat($ENV{'GSDLHOME'}, "lamp", "data", $folder, $dbase[$counter]);
|
---|
113 | if (!open(FILEIN, "<$dbName")) {
|
---|
114 | print $outhandle "Unable to open database $dbName: $!\n";
|
---|
115 | return undef;
|
---|
116 | }
|
---|
117 |
|
---|
118 | my $line = <FILEIN>; #database category details.
|
---|
119 | my @catdetails = split("\t", $line);
|
---|
120 |
|
---|
121 | while ( defined($line = <FILEIN>)){ #stores all place names in file as keys in hash array %places
|
---|
122 | @cats = split("\t", $line);
|
---|
123 | if( #eliminating "bad" place names without missing real ones
|
---|
124 | ($cats[$FC] eq "A" && !($cats[$DSG] =~ /ADMD|ADM2|PRSH/))
|
---|
125 | ||($cats[$FC] eq "P" && ($cats[$DSG] =~ /PPLA|PPLC|PPLS/))
|
---|
126 | ||($cats[$FC] eq "H" && ($cats[$DSG] =~ /BAY|LK/))
|
---|
127 | ||($cats[$FC] eq "L" && !($cats[$DSG] =~ /LCTY|RGN/))
|
---|
128 | ||($cats[$FC] eq "T" && ($cats[$DSG] =~ /CAPE|ISL|GRGE/))
|
---|
129 | ||($dbase[$counter] eq "USA.txt" && ($cats[$DSG] =~ /ppl|island/))
|
---|
130 | ){$places{$cats[$FULL_NAME]} = [@cats];}
|
---|
131 | @cats = ();
|
---|
132 | }
|
---|
133 | close(FILEIN);
|
---|
134 | $counter++;
|
---|
135 | }
|
---|
136 | return \%places;
|
---|
137 | }
|
---|
138 |
|
---|
139 | #returns a unique hash array of all the places found in a document, along with coordinates, description and country data
|
---|
140 | sub getPlaces {
|
---|
141 | my $self = shift @_;
|
---|
142 |
|
---|
143 | my ($textref, $places) = @_;
|
---|
144 | my %tempPlaces = ();
|
---|
145 |
|
---|
146 | foreach my $plc (%$places){ #search for an occurrence of each place in the text
|
---|
147 | if($$textref =~ m/(\W)$plc(\W)/){
|
---|
148 | $tempPlaces{$plc} = $places->{$plc};
|
---|
149 | }
|
---|
150 | }
|
---|
151 | #make sure each place is only there once
|
---|
152 | my %uniquePlaces = ();
|
---|
153 | foreach my $p (keys %tempPlaces) {
|
---|
154 | if(!defined($uniquePlaces{$p})){
|
---|
155 | $uniquePlaces{$p} = $tempPlaces{$p};
|
---|
156 | }
|
---|
157 | }
|
---|
158 | return \%uniquePlaces;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 | #returns a lowercase version of the place, with no spaces
|
---|
165 | sub placename_to_anchorname {
|
---|
166 | my ($placename) = @_;
|
---|
167 | my $p_tag = lc($placename);
|
---|
168 | $p_tag =~ s/\s+//g;
|
---|
169 | return $p_tag;
|
---|
170 | }
|
---|
171 |
|
---|
172 | #takes a place from the text and wraps an anchor tag, a hyperlink tag and an image tag around it
|
---|
173 | sub anchor_wrapper {
|
---|
174 | my ($p, $p_counter_ref, $path) = @_;
|
---|
175 | my $image = "/gsdlgis/gisimages/nextplace.gif";
|
---|
176 | my $endTag = "</a>";
|
---|
177 | my $hrefTag = "<a href=";
|
---|
178 | $$p_counter_ref++;
|
---|
179 | my $next = $$p_counter_ref + 1;
|
---|
180 | my $p_tag = placename_to_anchorname($p);
|
---|
181 | my $place_anchor = "<a name=\"" . $p_tag . $$p_counter_ref . "\">" . $endTag;
|
---|
182 | my $popup_anchor = $hrefTag . "'" . $path . "'>" . $p . $endTag;
|
---|
183 | my $image_anchor = $hrefTag . "#" . $p_tag . $next . "><img src=\"" . $image . "\" name=\"img" . $p_tag . $$p_counter_ref . "\" border=\"0\">" . $endTag;
|
---|
184 | return $place_anchor . $popup_anchor . $image_anchor;
|
---|
185 | }
|
---|
186 |
|
---|
187 | #takes dangerous place names and checks if they are part of another placename or not.
|
---|
188 | sub place_name_check {
|
---|
189 | my ($pre, $preSpace, $p, $postSpace, $post, $p_counter_ref, $path, $y) = @_;
|
---|
190 | if($pre =~ /$y/ || $post =~ /$y/) {return $pre . $preSpace . $p . $postSpace . $post;}
|
---|
191 | $pre = $pre . $preSpace;
|
---|
192 | $post = $postSpace . $post;
|
---|
193 | return $pre . &anchor_wrapper("", $p, "", $p_counter_ref, $path) . $post;
|
---|
194 | }
|
---|
195 |
|
---|
196 | sub extract_placenames {
|
---|
197 | my $self = shift (@_);
|
---|
198 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
199 | my $outhandle = $self->{'outhandle'};
|
---|
200 |
|
---|
201 | my $GSDLHOME = $ENV{'GSDLHOME'};
|
---|
202 | #field categories in DataBase file for extract_placenames.
|
---|
203 | my $LAT = 3;
|
---|
204 | my $LONG = 4;
|
---|
205 | my $CC1 = 12;
|
---|
206 |
|
---|
207 |
|
---|
208 | &gsprintf($outhandle, " {BasPlug.extracting_placenames}...\n")
|
---|
209 | if ($self->{'verbosity'} > 2);
|
---|
210 |
|
---|
211 | #get all the places found in the document
|
---|
212 | my $uniquePlaces = $self->getPlaces($textref, $self->{'places'});
|
---|
213 |
|
---|
214 | #finds 'dangerous' placenames (eg York and New York). Dangerous because program will find "York" within "New York"
|
---|
215 | my %danger = ();
|
---|
216 | foreach my $x (keys %$uniquePlaces){
|
---|
217 | foreach my $y (keys %$uniquePlaces){
|
---|
218 | if(($y =~ m/ /) && ($y =~ m/$x/) && ($y ne $x)){
|
---|
219 | $y =~ s/($x\s)|(\s$x)//;
|
---|
220 | $danger{$x} = $y;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | #creates a list of clickable placenames at top of page, linked to first occurrence of name, and reads them into a file
|
---|
226 | my $tempfname = $doc_obj;
|
---|
227 | $tempfname =~ s/.*\(0x(.*)\)/$1/;
|
---|
228 | my $names = "";
|
---|
229 | my $filename = "tmpfile" . $tempfname;
|
---|
230 | my $tempfile = &util::filename_cat($GSDLHOME, "tmp", $filename);
|
---|
231 | open(FOUT, ">$tempfile") || die "Unable to create a temp file: $!";
|
---|
232 | foreach my $name (sort (keys %$uniquePlaces)){
|
---|
233 | if(!defined($danger{$name})){
|
---|
234 | my $name_tag = placename_to_anchorname($name);
|
---|
235 | print FOUT "$name\t" . $uniquePlaces->{$name}->[$LONG] . "\t" . $uniquePlaces->{$name}->[$LAT] . "\n";
|
---|
236 | if($self->{'place_list'}) {$names = $names . "<a href=\"#" . $name_tag . "1\">" . $name . "</a>" . "\n";}
|
---|
237 | }
|
---|
238 | }
|
---|
239 | close(FOUT);
|
---|
240 | $doc_obj->associate_file($tempfile, "places.txt", "text/plain");
|
---|
241 | $self->{'places_filename'} = $tempfile;
|
---|
242 | my %countries = ();
|
---|
243 |
|
---|
244 | foreach my $p (keys %$uniquePlaces){
|
---|
245 | my $place = $p;
|
---|
246 | $place =~ s/\s+|\n+|\r+|\t+/(\\s+)/g;
|
---|
247 | my $cap_place = uc($place);
|
---|
248 | my $long = $uniquePlaces->{$p}->[$LONG];
|
---|
249 | my $lat = $uniquePlaces->{$p}->[$LAT];
|
---|
250 | my $country = $uniquePlaces->{$p}->[$CC1];
|
---|
251 | my $path = "javascript:popUp(\"$long\",\"$lat\",\"$p\",\"$country\")";
|
---|
252 | my $p_counter = 0;
|
---|
253 |
|
---|
254 | if(!defined($danger{$p})){
|
---|
255 | #adds html tags to each place name
|
---|
256 | $$textref =~ s/\b($place|$cap_place)\b/&anchor_wrapper($1,\$p_counter,$path)/sge;
|
---|
257 | }
|
---|
258 | #else {
|
---|
259 | #$y = $danger{$p};
|
---|
260 | #$$textref =~ s/(\w+)(\s+?)($place|$cap_place)(\s+?)(\w+)/&place_name_check($1,$2,$3,$4,$5,\$p_counter,$path, $y)/sge;
|
---|
261 | #}
|
---|
262 |
|
---|
263 | #edits the last place's image, and removes image if place only occurres once.
|
---|
264 | my $p_tag = placename_to_anchorname($p);
|
---|
265 | $p_counter++;
|
---|
266 | $$textref =~ s/#$p_tag$p_counter(><img src="\/gsdl\/images\/)nextplace.gif/#${p_tag}1$1firstplace.gif/;
|
---|
267 | $$textref =~ s/<img src="\/gsdl\/images\/firstplace.gif" name="img$p_tag(1)" border="0">//;
|
---|
268 |
|
---|
269 | #this line removes apostrophes from placenames (they break the javascript function)
|
---|
270 | $$textref =~ s/(javascript:popUp.*?)(\w)'(\w)/$1$2$3/g;
|
---|
271 |
|
---|
272 | #for displaying map of document, count num of places from each country
|
---|
273 | if(defined($countries{$country})){$countries{$country}++;}
|
---|
274 | else{$countries{$country} = 1;}
|
---|
275 |
|
---|
276 | #adds placename to metadata
|
---|
277 | $doc_obj->add_utf8_metadata ($thissection, "Placename", $p);
|
---|
278 | &gsprintf($outhandle, " {BasPlug.extracting} $p\n")
|
---|
279 | if ($self->{'verbosity'} > 3);
|
---|
280 | }
|
---|
281 | #finding the country that most places are from, in order to display map of the document
|
---|
282 | my $max = 0;
|
---|
283 | my $CNTRY = "";
|
---|
284 | foreach my $c_key (keys %countries){
|
---|
285 | if($countries{$c_key} > $max){
|
---|
286 | $max = $countries{$c_key};
|
---|
287 | $CNTRY = $c_key;
|
---|
288 | }
|
---|
289 | }
|
---|
290 | #allows user to view map with all places from the document on it
|
---|
291 | #### my $places_filename = &util::filename_cat($GSDLHOME, "collect", "_cgiargc_", "index", "assoc", "_thisOID_", "places.txt");
|
---|
292 | my $places_filename = &util::filename_cat("collect", "_cgiargc_", "index", "assoc", "_thisOID_", "places.txt");
|
---|
293 | my $docmap = "<a href='javascript:popUp(\"$CNTRY\",\"$places_filename\")'>View map for this document<\/a><br><br>\n";
|
---|
294 | $$textref = $docmap . $names . "<br>" . $$textref;
|
---|
295 |
|
---|
296 | $doc_obj->delete_text($thissection);
|
---|
297 | $doc_obj->add_utf8_text($thissection, $$textref);
|
---|
298 | &gsprintf($outhandle, " {BasPlug.done_places_extract}\n")
|
---|
299 | if ($self->{'verbosity'} > 2);
|
---|
300 | }
|
---|