source: trunk/gsdl/perllib/plugins/GISBasPlug.pm@ 12970

Last change on this file since 12970 was 12970, checked in by kjdon, 18 years ago

set_keepold and self->{'keepold'} have been changed to set_incremental and self->{'incremental'}

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