source: trunk/gsdl3/src/java/org/greenstone/gsdl3/service/MapRetrieve.java@ 6490

Last change on this file since 6490 was 6305, checked in by kjdon, 20 years ago

search and retrieve services for the map collection

  • Property svn:keywords set to Author Date Id Revision
File size: 35.1 KB
Line 
1package org.greenstone.gsdl3.service;
2
3// Greenstone classes
4import org.greenstone.gdbm.*;
5import org.greenstone.gsdl3.util.*;
6
7// XML classes
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10import org.w3c.dom.NodeList;
11
12// General Java classes
13import java.io.File;
14import java.io.*;
15import java.util.StringTokenizer;
16import java.util.Vector;
17import java.util.Set;
18import java.util.Iterator;
19import java.lang.reflect.Array;
20import java.util.Arrays;
21import java.util.ArrayList;
22import java.util.LinkedList;
23import javax.swing.tree.DefaultMutableTreeNode;
24
25/**
26 */
27
28public class MapRetrieve
29 extends ServiceRack {
30
31 // the services on offer
32 protected static final String DOCUMENT_STRUCTURE_RETRIEVE_SERVICE = "DocumentStructureRetrieve";
33 protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve";
34 protected static final String DOCUMENT_CONTENT_RETRIEVE_SERVICE = "DocumentContentRetrieve";
35
36 protected static final int DOCUMENT=1;
37
38 protected ArrayList index_name_list = null;
39 protected ArrayList index_display_list = null;
40 protected String files_home_dir = null;
41 protected String temp_files_dir = null;
42 //protected String temp_image_file = null;
43 //protected String temp_image_file2 = null;
44 protected final String jpg_ext = ".jpg";
45 protected String http_image_dir = null;
46 protected String http_temp_image_dir = null;
47 private LinkedList namesInList = new LinkedList();
48
49 private DefaultMutableTreeNode tree;
50
51 /** constructor */
52 public MapRetrieve() {}
53
54
55 /** configure this service */
56 public boolean configure(Element info, Element extra_info)
57 {
58 System.out.println("Configuring MapRetrieve...");
59 this.config_info = info;
60
61 // set up short_service_info_ - for now just has name and type
62 Element dmr_service = doc.createElement(GSXML.SERVICE_ELEM);
63 dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
64 dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
65 short_service_info.appendChild(dmr_service);
66
67 Element dcr_service = doc.createElement(GSXML.SERVICE_ELEM);
68 dcr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
69 dcr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
70 short_service_info.appendChild(dcr_service);
71
72 Element dsr_service = doc.createElement(GSXML.SERVICE_ELEM);
73 dsr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
74 dsr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_STRUCTURE_RETRIEVE_SERVICE);
75 short_service_info.appendChild(dsr_service);
76
77 // set the files_home variable for this collection
78 this.files_home_dir = GSFile.collectionIndexDir(this.site_home, this.cluster_name)+ File.separator+"assoc"+File.separator;
79 this.temp_files_dir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separator + "temp"+File.separator;
80
81 //this.files_home_dir + "maps" + File.separator + "temp" + File.separator;
82 //this.temp_image_file = this.temp_files_dir+"temp_";
83 //this.temp_image_file2 = this.temp_files_dir+"temp2_";
84
85
86 this.http_image_dir = this.site_http_address + "/collect/"+this.cluster_name+"/index/assoc/maps/";
87 this.http_temp_image_dir = this.site_http_address + "/collect/"+this.cluster_name+"/temp/";
88
89 this.index_name_list = new ArrayList();
90 this.index_display_list = new ArrayList();
91 Element index_list = (Element)GSXML.getChildByTagName(this.config_info, GSXML.INDEX_ELEM+GSXML.LIST_MODIFIER);
92 Element display_index_list = (Element)GSXML.getChildByTagName(extra_info, "search");
93 if (index_list != null && display_index_list != null) {
94 NodeList indexes = index_list.getElementsByTagName(GSXML.INDEX_ELEM);
95 for (int i=0; i<indexes.getLength(); i++) {
96 String name = ((Element)indexes.item(i)).getAttribute(GSXML.NAME_ATT);
97 // add the index name
98 this.index_name_list.add(name);
99 // now look for the display name
100 Element this_index = GSXML.getNamedElement(display_index_list, "index", "name", name);
101 if (this_index != null) {
102 Element disp = GSXML.getNamedElement(this_index, "displayItem", "name", "name");
103 if (disp != null) {
104 String display = GSXML.getNodeText(disp);
105 if (!display.equals("")) {
106 this.index_display_list.add(display);
107 continue;
108 }
109
110 }
111 }
112 // add the id as default text
113 this.index_display_list.add(name);
114
115 }
116 }
117
118 // look for document display format
119 String path = GSPath.appendLink(GSXML.DISPLAY_ELEM, GSXML.FORMAT_ELEM);
120 Element display_format = (Element)GSXML.getNodeByPath(extra_info, path);
121 if (display_format != null) {
122 this.format_info_map.put(DOCUMENT_CONTENT_RETRIEVE_SERVICE, this.doc.importNode(display_format, true));
123 // shoudl we make a copy?
124 }
125
126 // load the 2-dimensional tree for searching
127 try{
128 ObjectInputStream objectin = new ObjectInputStream(new FileInputStream(this.files_home_dir+"nametree.dat"));
129 tree = (DefaultMutableTreeNode)objectin.readObject();
130 objectin.close();
131 }catch(IOException ioe){
132 ioe.printStackTrace();
133 }catch(ClassNotFoundException cnfe){
134 cnfe.printStackTrace();
135 }
136
137
138 return true;
139 }
140
141 /** */
142 protected Element getServiceDescription(String service, String lang, String subset)
143 {
144 if (service.equals(DOCUMENT_STRUCTURE_RETRIEVE_SERVICE)) {
145
146 Element tq_service = doc.createElement(GSXML.SERVICE_ELEM);
147 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
148 tq_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_STRUCTURE_RETRIEVE_SERVICE);
149 return tq_service;
150 }
151 else if (service.equals(DOCUMENT_METADATA_RETRIEVE_SERVICE)) {
152
153 Element tq_service = doc.createElement(GSXML.SERVICE_ELEM);
154 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
155 tq_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
156 return tq_service;
157 }
158 else if (service.equals(DOCUMENT_CONTENT_RETRIEVE_SERVICE)) {
159
160 Element tq_service = doc.createElement(GSXML.SERVICE_ELEM);
161 tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
162 tq_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
163 return tq_service;
164 }
165
166 return null;
167
168 }
169
170 /** Retrieve the structure of a document */
171 protected Element processDocumentStructureRetrieve(Element request)
172 {
173 // Create a new (empty) result message
174 Element result = doc.createElement(GSXML.RESPONSE_ELEM);
175 return result;
176 }
177
178 protected Element processDocumentMetadataRetrieve(Element request) {
179 // Create a new (empty) result message
180 try{
181 Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
182
183 String uid = request.getAttribute(GSXML.USER_ID_ATT);
184
185 result.setAttribute(GSXML.FROM_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
186 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
187
188 // Get the parameters of the request
189 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
190 if (param_list == null) {
191 System.err.println("MapRetrieve Error: missing paramList.\n");
192 return result; // Return the empty result
193 }
194
195 // The metadata information required
196 Vector metadata_list = new Vector();
197 boolean all_metadata = false;
198 // Process the request parameters
199 Element param = (Element) param_list.getFirstChild();
200
201 while (param != null) {
202 // Identify the metadata information desired
203 if (param.getAttribute(GSXML.NAME_ATT).equals("metadata")) {
204 String metadata = GSXML.getValue(param);
205 if (metadata.equals("all")) {
206 all_metadata = true;
207 break;
208 }
209 metadata_list.add(metadata);
210 }
211 param = (Element) param.getNextSibling();
212 }
213
214 Element node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
215 result.appendChild(node_list);
216
217 // Get the documents
218 Element request_node_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
219 if (request_node_list == null) {
220 System.err.println("Error: DocumentMetadataRetrieve request had no "+GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
221 return result;
222 }
223
224 NodeList request_nodes = request_node_list.getChildNodes();
225
226 try{
227 //used just to ensure outfile is initialised
228 BufferedWriter outfile = new BufferedWriter(new FileWriter(this.temp_files_dir+"emptynothingness"));
229
230 for (int i = 0; i < request_nodes.getLength(); i++) {
231 Element request_node = (Element) request_nodes.item(i);
232 String node_id = request_node.getAttribute(GSXML.NODE_ID_ATT);
233 String place_data = "";
234 String year = "";
235 String coOrdinates = "";
236 String thumb = "";
237 String link = "";
238 int mapFreq = 0;
239 LinkedList place_data_list = new LinkedList();
240 LinkedList coOrdinates_list = new LinkedList();
241
242 //if this is a first displaying of the map
243 if(node_id.indexOf("```") != -1 && node_id.indexOf("imageChooser") == -1){
244 //get the number of query terms on this map
245 mapFreq = Integer.parseInt(node_id.substring(node_id.lastIndexOf('`',node_id.indexOf("```")-1)+1,node_id.indexOf("```")));
246 //get the place names on this map
247 place_data = node_id.substring(node_id.indexOf("```")+3,node_id.length());
248 //get the map metadata
249 node_id = node_id.substring(0,node_id.indexOf('`',node_id.indexOf("```")));
250
251 try{
252 // loop for each query term on the map
253 for(int r=0; r<mapFreq; r++){
254 // get title, type, location, and the string for creating the hyperlink for this query term
255 String title = place_data.substring(0,place_data.indexOf('`'));
256 String type = place_data.substring(place_data.indexOf('`')+1,place_data.indexOf('`',place_data.indexOf('`')+1));
257 String location = place_data.substring(place_data.indexOf('`',place_data.indexOf('`')+1)+1, place_data.indexOf('`',place_data.indexOf('`',place_data.indexOf('`')+1)+1));
258 if(place_data.indexOf("```") != -1)
259 link=place_data.substring(0,place_data.indexOf("```"));
260 else
261 link=place_data;
262 // resolve the type and location
263 type = getType(type);
264 location = getLocation(location);
265 // remove this query term from the string
266 if(place_data.indexOf("```") != -1)
267 place_data = place_data.substring(place_data.indexOf("```")+3,place_data.length());
268
269 //add the co-ordinates of this query term to the co-ordinates list
270 coOrdinates_list.add("`"+link.substring(link.lastIndexOf('`')+1,link.length())+"`"+link.substring(link.lastIndexOf('`',link.lastIndexOf('`')-1)+2,link.lastIndexOf('`')));
271
272 // add the title, type and location to the places list
273 place_data_list.add(title + ", " + type + ", " + location + ";");
274 }
275 }catch(StringIndexOutOfBoundsException sioobe){sioobe.printStackTrace();}
276 }
277
278 // Add the document to the list
279 Element new_node = (Element)this.doc.importNode(request_node, false);
280 node_list.appendChild(new_node);
281
282 // Add the requested metadata information
283 Element node_meta_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
284 new_node.appendChild(node_meta_list);
285
286 // create the navigation thumbnails. This doesn't seem to work most of the time ???????
287 for (int m = 0; m < metadata_list.size(); m++) {
288 String metadata = (String) metadata_list.get(m);
289 thumb = "";
290 String value = "";
291 if(node_id.indexOf('.') != -1)
292 thumb = node_id.substring(0,node_id.indexOf('.'));
293 if(node_id.indexOf('`') != -1)
294 value = node_id.substring(node_id.lastIndexOf('`',node_id.lastIndexOf('`')-1)+1,node_id.lastIndexOf('`',node_id.lastIndexOf('`')-1)+5);
295 year = value;
296
297 place_data="";
298 if(place_data_list.size() != 0)
299 for(int q=0; q<mapFreq; q++){
300 link = (String)place_data_list.get(q);
301 if(q!=0){
302 place_data = place_data + "<br>" + link;
303 }
304 else{
305 place_data = link;
306 coOrdinates = "``";
307 }
308 coOrdinates = coOrdinates+(String)coOrdinates_list.get(q);
309 }
310
311 link = "<a href=\"library?a=d&c="+this.cluster_name+"&d="+node_id+coOrdinates+"&dt=map\">";
312 thumb = "<img src=\""+this.http_image_dir + thumb + "thumb.jpg\" border=0>";
313 value = "<table><tr><td>"+link+"<p>"+place_data+"<br>"+value+"</a></td><td>"+link+thumb+"</a></td></tr></table>";
314 if(metadata.equals("Title"))
315 if(!(place_data.equals("")) && place_data.indexOf(", , ;")==-1 && node_id.indexOf("```")==-1)
316 GSXML.addMetadata(this.doc, node_meta_list, "Title", value);//metadata, value);
317 else
318 GSXML.addMetadata(this.doc, node_meta_list, metadata, "");
319
320 if(place_data.indexOf(", , ;")==-1){
321 if(i==0)
322 if(!(mapFreq==0)){
323 outfile = new BufferedWriter(new FileWriter(this.temp_files_dir+"links"+uid));
324 outfile.write("<table align=\"center\"><tr>");
325 }
326
327 if(!(mapFreq==0)){
328 if(i%7 == 0)
329 outfile.write("</tr><tr>");
330 outfile.write("<td align=\"center\">"+link+thumb+"</a><br>"+link+year+"</a></td>");
331 }
332
333 if(i == request_nodes.getLength()-1){
334 outfile.write("</tr></table><p>");
335 outfile.flush();
336 outfile.close();
337 }
338 }
339 }
340
341 }
342 }catch(IOException ioe){ioe.printStackTrace();}
343
344 return result;
345 }catch(Exception excep){excep.printStackTrace();}
346 return null;
347
348 }
349
350 protected Element processDocumentContentRetrieve(Element request){
351 // Create a new (empty) result message
352 Element result = doc.createElement(GSXML.RESPONSE_ELEM);
353 result.setAttribute(GSXML.FROM_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
354 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
355
356 String uid = request.getAttribute(GSXML.USER_ID_ATT);
357 String temp_image_file = this.temp_files_dir+"temp_"+uid+".jpg";
358 String temp_image_file2 = this.temp_files_dir+"temp_"+uid+"_2.jpg";
359
360 Element query_doc_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
361 if (query_doc_list == null) {
362 System.err.println("Error: DocumentContentRetrieve request specified no doc nodes.\n");
363 return result;
364 }
365
366 String legend_file = this.temp_files_dir+"legend_"+uid+".jpg";
367 String blank_file = this.files_home_dir + "blank.jpg";
368 Element doc_list = doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
369 result.appendChild(doc_list);
370
371 // Get the documents
372 String[] doc_ids = GSXML.getAttributeValuesFromList(query_doc_list,
373 GSXML.NODE_ID_ATT);
374
375 for (int i = 0; i < doc_ids.length; i++) {
376 String doc_id = doc_ids[i];
377 // img_num is the name of the map file eg. 072.jpg
378 String img_num = doc_id.substring(0,doc_id.indexOf('.'));
379 // strings for inserting image in html
380 String img_left = "<img border=0 src=\""+this.http_temp_image_dir;
381 String doc_content = "";
382 String co_ordinates = "";
383 String img_size = "";
384 int height = 0;
385 int width = 0;
386 // the number for the legend image if adding locations to the map
387 int leg_num = 0;
388 double ratio = 0;
389
390 // if user has clicked on the map. This does not resolve maps that are not North oriented.
391 if(doc_id.indexOf("imageChooser")==0){
392 try{
393 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
394 img_num = doc_id.substring(0,doc_id.indexOf('.'));
395
396 // get the map size
397 String get_size[] = {"identify","-size","10000", temp_image_file};
398 Process proc;
399 proc = Runtime.getRuntime().exec(get_size);
400 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
401 img_size = br.readLine();
402 proc.waitFor();
403 img_size = img_size.substring(img_size.indexOf("JPEG")+5,img_size.indexOf(" ",img_size.indexOf("JPEG")+5));
404 width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x")));
405 height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length()));
406
407 // scale image size according to the ratio
408 ratio = Double.parseDouble(doc_id.substring(doc_id.lastIndexOf('`',doc_id.indexOf("```")-1)+1,doc_id.indexOf("```")));
409 width = (int)(width*ratio);
410 height = (int)(height*ratio);
411
412 // get the position of the mouse click on the image
413 int xclick = Integer.parseInt(doc_id.substring(doc_id.indexOf("```")+3,doc_id.lastIndexOf('`')));
414 int yclick = Integer.parseInt(doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length()));
415 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.indexOf("```"));
416
417 // convert click position to percentage distance accross and down the image.
418 double xpercent = (xclick*1.0)/width;
419 double ypercent = (yclick*1.0)/height;
420
421 // get the top left and bottom right co-ordinates of the map
422 double ytop = Double.parseDouble(doc_id.substring(0,doc_id.indexOf('`')))*-1;
423 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
424 double xleft = Double.parseDouble(doc_id.substring(0,doc_id.indexOf('`')));
425 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
426 double ybot = Double.parseDouble(doc_id.substring(0,doc_id.indexOf('`')))*-1;
427 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
428 double xright = Double.parseDouble(doc_id.substring(0,doc_id.indexOf('`')));
429 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
430
431 // calculate the map co-ordinates of the mouse click
432 xpercent = ((xright-xleft)*xpercent)+xleft;
433 ypercent = ((ybot-ytop)*ypercent)+ytop;
434
435 // search the tree for nearby place names
436 namesInList.clear();
437 findName(xpercent, ypercent, 0.1, 0.1, tree, true);
438
439 int namesInArraySize = namesInList.size();
440 String returnNames[]=new String[namesInArraySize];
441
442 //put the names in an array
443 for(int ri=0; namesInList.size()>0; ri++){
444 returnNames[ri]=(String)namesInList.getFirst();
445 returnNames[ri] = returnNames[ri].substring(0,returnNames[ri].indexOf('`'));
446 namesInList.removeFirst();
447 }
448
449 //sort the names
450 Arrays.sort(returnNames);
451 doc_content = "\n<script>\nfunction openIt(loc){\n\topener.location=loc;\n\tself.close();\n}\n</script>";
452 for(int nameIndex=0; nameIndex < namesInArraySize; nameIndex++){
453 String tempName = returnNames[nameIndex];
454 //convert names with spaces for hyperlinks
455 if(returnNames[nameIndex].indexOf(' ') != -1){
456 returnNames[nameIndex] = returnNames[nameIndex].replaceAll(" ","+");
457 returnNames[nameIndex] = "%22"+returnNames[nameIndex]+"%22";
458 }
459 // add the place name to the html
460 doc_content = doc_content+"<a href=\"\" onClick=openIt('library?a=q&sa=&rt=r&s=MapQuery&c="+this.cluster_name+"&startPage=1&s1.index=none&s1.maxDocs=10&s1.query="+returnNames[nameIndex]+"')>"+tempName+"</a><br>";
461 }
462
463 }catch(Exception ioexception){ioexception.printStackTrace();}
464 }
465 else{
466
467 try{
468 //file for converting image
469 BufferedWriter bw = new BufferedWriter(new FileWriter(this.temp_files_dir + "add_x_"+uid));;
470 Process proc;
471
472 // if a new search
473 if(doc_id.indexOf("```") != -1){
474 // copy requested map to temp.jpg
475 proc = Runtime.getRuntime().exec("cp "+this.files_home_dir+"maps"+File.separator+img_num+".jpg "+temp_image_file);
476 proc.waitFor();
477 }
478
479 //get the image size
480 String get_size[] = {"identify","-size","10000", temp_image_file};
481 proc = Runtime.getRuntime().exec(get_size);
482 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
483 img_size = br.readLine();
484 proc.waitFor();
485 img_size = img_size.substring(img_size.indexOf("JPEG")+5,img_size.indexOf(" ",img_size.indexOf("JPEG")+5));
486 width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x")));
487 height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length()));
488
489 // if a new search
490 if(doc_id.indexOf("```") != -1){
491 co_ordinates = doc_id.substring(doc_id.indexOf("```")+2, doc_id.length());
492 // get the number of places to mark on the map
493 int x_number = Integer.parseInt(doc_id.substring(doc_id.lastIndexOf('`',doc_id.indexOf("```")-1)+1,doc_id.indexOf("```")));
494
495 //write the convert command
496 bw.write("convert -font helvetica -fill red -pointsize 48 ");
497
498 for(int xi=0; xi<x_number; xi++){
499
500 // get the co-ordinates of the place name
501 double xco = Double.parseDouble(co_ordinates.substring(co_ordinates.lastIndexOf('`',co_ordinates.lastIndexOf('`')-1)+1,co_ordinates.lastIndexOf('`',co_ordinates.lastIndexOf('`')-1)+7));
502 double yco = Double.parseDouble(co_ordinates.substring(co_ordinates.lastIndexOf('`')+1,co_ordinates.length()));
503 if(xi != x_number-1)
504 co_ordinates = co_ordinates.substring(0,co_ordinates.lastIndexOf('`',co_ordinates.lastIndexOf('`')-1));
505
506 int index = 0;
507 index = doc_id.indexOf('`')+2;
508
509 // get the zoom ratio and the top left and bottom right co-ordinates of the map
510 ratio = 0.4;
511 double tly = Double.parseDouble(doc_id.substring(index,index+5));
512 index = doc_id.indexOf('`',index)+1;
513 double tlx = Double.parseDouble(doc_id.substring(index,index+6));
514 index = doc_id.indexOf('`',index)+2;
515 double bry = Double.parseDouble(doc_id.substring(index,index+5));
516 index = doc_id.indexOf('`',index)+1;
517 double brx = Double.parseDouble(doc_id.substring(index,index+6));
518 index = doc_id.indexOf('`',index)+1;
519 double orient = Double.parseDouble(doc_id.substring(index,doc_id.indexOf('`',index)));
520
521 // find the centre of the map
522 double xcent = ((brx-tlx)/2)+tlx;
523 double ycent = ((bry-tly)/2)+tly;
524
525 // get the orientation of the map
526 orient = Math.toRadians(orient);
527
528 // rotate the co-ordinates around the centre of the map
529 xco=xco-xcent;
530 yco=yco-ycent;
531 double oldx=xco;
532 xco=xco*Math.cos(orient)-yco*Math.sin(orient);
533 yco=oldx*Math.sin(orient)+yco*Math.cos(orient);
534 xco=xco+xcent;
535 yco=yco+ycent;
536 xco = (xco-tlx)/(brx-tlx);
537 yco = (yco-tly)/(bry-tly);
538
539 // calculate the pixels for placing the mark
540 width = (int)(xco*width)-5;
541 height = (int)(yco*height)-5;
542
543 // write the options for convert
544 bw.write("-draw 'text "+width+","+height+" \"x\"' ");
545 bw.flush();
546
547 // reset the width and height variables for the image
548 width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x")));
549 height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length()));
550 }
551 doc_id = doc_id.substring(0,doc_id.lastIndexOf('`',doc_id.indexOf("```")-1));
552
553 // write the end of the convert command, then command for zooming image to the right level, then command to copy the blank image over legend.jpg
554 bw.write(temp_image_file+" "+temp_image_file+";convert -scale "+((int)(ratio*width))+"x"+((int)(ratio*height))+" "+temp_image_file+" "+temp_image_file2+";cp "+blank_file +" "+legend_file);
555
556 // reset legend number
557 leg_num = 0;
558 }
559
560 else
561 // if changing zoom level
562 if(doc_id.substring(doc_id.lastIndexOf('`'),doc_id.length()).indexOf('.')!=-1){
563 // get the ratio
564 ratio = Double.parseDouble(doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length()));
565 // write the command for scaling image
566 bw.write("convert -scale "+((int)(ratio*width))+"x"+((int)(ratio*height))+" "+temp_image_file+" "+temp_image_file2);
567 doc_id = doc_id.substring(0,doc_id.lastIndexOf('`'));
568 }
569
570 // if adding locations to map
571 else{
572
573 // get the location type to add
574 String restricter= doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length());
575 doc_id = doc_id.substring(0,doc_id.lastIndexOf('`'));
576
577 // get the number for the legend image
578 leg_num = Integer.parseInt(doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length()));
579 doc_id = doc_id.substring(0,doc_id.lastIndexOf('`'));
580
581 //open file for location type
582 BufferedReader inType = new BufferedReader(new FileReader(this.files_home_dir + "place_types"+File.separator + restricter + ".txt"));
583
584 //check through the file and add any places that are in the bounds of this map.
585
586 // check value. set to true if a place to add is found
587 boolean add_place_type = false;
588
589 int index = 0;
590 index = doc_id.indexOf('`')+2;
591 // get zoom ratio
592 ratio = Double.parseDouble(doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length()));
593 // get top left and bottom right co-ordinates of the map
594 double tly = Double.parseDouble(doc_id.substring(index,index+5));
595 index = doc_id.indexOf('`',index)+1;
596 double tlx = Double.parseDouble(doc_id.substring(index,index+6));
597 index = doc_id.indexOf('`',index)+2;
598 double bry = Double.parseDouble(doc_id.substring(index,index+5));
599 index = doc_id.indexOf('`',index)+1;
600 double brx = Double.parseDouble(doc_id.substring(index,index+6));
601 index = doc_id.indexOf('`',index)+1;
602 // get orientation of the map
603 double orient = Double.parseDouble(doc_id.substring(index,doc_id.indexOf('`',index)));
604 // calculate centre of map
605 double xcent = ((brx-tlx)/2)+tlx;
606 double ycent = ((bry-tly)/2)+tly;
607 orient = Math.toRadians(orient);
608
609 String type_point = "";
610 double xco = 0.0;
611 double yco = 0.0;
612
613 // read the file
614 while(inType.ready()){
615 //read a line
616 type_point = inType.readLine();
617
618 // get the co-ordinates of the point
619 xco = Double.parseDouble(type_point.substring(type_point.lastIndexOf('`')+1,type_point.length()));
620 yco = Double.parseDouble(type_point.substring(type_point.lastIndexOf('`',type_point.lastIndexOf('`')-1)+2,type_point.lastIndexOf('`')));
621
622 // if it is within the co-ordinates of the map
623 if(xco >= tlx && xco < brx && yco >= tly && yco < bry){
624 // rotate the point around the centre according to map orientation
625 xco=xco-xcent;
626 yco=yco-ycent;
627 double oldx=xco;
628 xco=xco*Math.cos(orient)-yco*Math.sin(orient);
629 yco=oldx*Math.sin(orient)+yco*Math.cos(orient);
630 xco=xco+xcent;
631 yco=yco+ycent;
632 // get the pixels for where to place the mark
633 xco = (xco-tlx)/(brx-tlx);
634 yco = (yco-tly)/(bry-tly);
635 width = (int)(xco*width)-5;
636 height = (int)(yco*height)-5;
637
638 //if this is the first point to be added
639 if(!add_place_type){
640 // write the initial convert command
641 bw.write("convert -font helvetica -fill red -pointsize 36 ");
642 // toggle check value
643 add_place_type = true;
644 }
645
646 // write the options for the convert command
647 bw.write("-draw 'text "+width+","+height+" \""+leg_num+"\"' ");
648 bw.flush();
649
650 // reset width and height variables for the image
651 width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x")));
652 height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length()));
653 }
654 }
655
656 //if there are places to mark on the map
657 if(add_place_type){
658 // finish the convert command and write command for scaling image
659 bw.write(temp_image_file+" "+temp_image_file+";convert -scale "+((int)(ratio*width))+"x"+((int)(ratio*height))+" "+temp_image_file+" "+temp_image_file2);
660
661 // open file for converting the legend command
662 BufferedWriter buf = new BufferedWriter(new FileWriter(this.temp_files_dir+"add_l_"+uid));
663 if(leg_num == 1)
664 buf.write("cp "+blank_file +" "+legend_file+";");
665 // write the command for adding to the legend
666 buf.write("convert -font helvetica -fill red -pointsize 12 -draw 'text 15,"+(leg_num*15+20)+" \""+leg_num+" "+getType(restricter)+"\"' "+legend_file+" "+legend_file);
667 buf.flush();
668 buf.close();
669 // execute the command for the legend image
670 proc = Runtime.getRuntime().exec("sh "+this.temp_files_dir+"add_l_"+uid);
671 proc.waitFor();
672 }
673 inType.close();
674 }
675 bw.flush();
676 bw.close();
677
678 // execute the convert commands etc.
679 proc = Runtime.getRuntime().exec("sh "+this.temp_files_dir+"add_x_"+uid);
680 proc.waitFor();
681
682 }catch(Exception ioe){ioe.printStackTrace();}
683
684 //write the html for the document
685
686 String doc_content_head = "library?a=d&c="+this.cluster_name+"&dt=map&d=";
687 doc_content = "<td valign=\"top\"><script>document.write('"+img_left+"legend_"+uid+".jpg"+"?'+new Date().getTime())</script>\"></td>";
688 doc_content = doc_content+"<td><script>document.write('"+img_left+"temp_"+uid+"_2.jpg?'+new Date().getTime())</script>\" onclick=\"imgClickHandler(event, this, '"+img_num+"')\"></td>";
689
690 // replace _httpimg_ with the correct address
691 doc_content = "<table><tr>"+doc_content+"</tr></table>";
692
693 String javascript = getJavascript(ratio, doc_id, leg_num);
694
695 doc_content = "<center>You are currently viewing the map at "+(int)(ratio*100)+"%<br>Select a radio button for a new zoom level.</center>"+javascript+doc_content;
696 doc_content = doc_content.replaceAll("zoomhead", doc_content_head);
697 doc_content = doc_content.replaceAll("placeChooserImage", doc_content_head+"imageChooser`"+doc_id+"`"+ratio);
698
699 // read the file for the navigation thumbnails
700 try{
701 BufferedReader infile = new BufferedReader(new FileReader(this.temp_files_dir+"links"+uid));
702 doc_content = doc_content + infile.readLine();
703 infile.close();
704 }catch(Exception ioexc){ioexc.printStackTrace();}
705 }
706
707 // put the html in a text node
708 Element text_doc = doc.createElement(GSXML.DOC_NODE_ELEM);
709 text_doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
710 GSXML.addDocText(doc, text_doc, doc_content);
711 doc_list.appendChild(text_doc);
712
713 }
714 return result;
715 }
716
717 // resolves location codes for places from LINZ database
718 private String getLocation(String location){
719 String read;
720 try{
721 BufferedReader in = new BufferedReader(new FileReader(this.files_home_dir+"files"+File.separator+"landdist.txt"));
722 in.readLine();
723 while(in.ready()){
724 read = in.readLine();
725 if(read.substring(0,2).equals(location)){
726 in.close();
727 return read.substring(3, read.length());
728 }
729 }
730 }catch(Exception e){}
731 return "";
732 }
733
734 // resolves type codes for the LINZ database
735 private String getType(String type){
736 String read;
737 try{
738 BufferedReader in = new BufferedReader(new FileReader(this.files_home_dir+"files"+File.separator+"pointdes.txt"));
739 in.readLine();
740 while(in.ready()){
741 read = in.readLine();
742 if(read.substring(0,read.indexOf('`')).toLowerCase().equals(type.toLowerCase())){
743 in.close();
744 return read.substring(read.indexOf('`')+1, read.indexOf(':'));
745 }
746 }
747 }catch(Exception e){}
748 return "";
749 }
750
751 //recursive pre-order traversal of the 2-dimensional tree
752 // x & y are co-ordinates of mouse click, xoff & yoff are the distance in degrees away from x & y to search,
753 // d is the current node of the tree, xcomp controls whether searching is by x co-ordinate or y co-ordinate.
754 private void findName(double x, double y, double xoff, double yoff, DefaultMutableTreeNode d, boolean xcomp){
755 //if a leaf node return. All leaf nodes are empty
756 if(d.isLeaf())
757 return;
758 //get coordinates of the current node
759 double xco = Double.parseDouble(((String)d.getUserObject()).substring(((String)d.getUserObject()).length()-6,((String)d.getUserObject()).length()));
760 double yco = Double.parseDouble(((String)d.getUserObject()).substring(((String)d.getUserObject()).length()-12,((String)d.getUserObject()).length()-7));
761 //if in range
762 if((x-xoff)<xco && (x+xoff)>xco && (y-yoff)<yco && (y+yoff)>yco){
763 //add to list
764 namesInList.addFirst((String)d.getUserObject());
765 }
766 //if comparing the x axis
767 if(xcomp){
768 //if need to search left subtree
769 if((x-xoff)<xco)
770 findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(0),!xcomp);
771 //if need to search right subtree
772 if((x+xoff)>xco)
773 findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(1),!xcomp);
774 }
775 else{
776 //if need to search left subtree
777 if((y-yoff)<yco)
778 findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(0),!xcomp);
779 //if need to search right subtree
780 if((y+yoff)>yco)
781 findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(1),!xcomp);
782 }
783 }
784
785 // a whole lot of stuff for the html that is hidden at the bottom here because it clutters more than I already have further up!
786 private String getJavascript(double ratio, String doc_id, int leg_num)
787 {
788 String javascript = "\n<script>\nfunction imgClickHandler (evt, img, num) {\n\tif (window.event){\n\t\tvar chooserString = 'placeChooserImage' + '```' + window.event.offsetX + '`' + window.event.offsetY\n\t\tmywindow = window.open(chooserString,'Choose','width=600, height=500, scrollbars=yes, resizable=yes, toolbar=no, location=no, status=yes, menubar=no');\n\t\t//alert(window.event.offsetX + ':' + window.event.offsetY + ':' + num);\n\t}\n\telse if (evt.target) {\n\t\tvar coords = {x: 0, y: 0 };\n\t\tvar el = evt.target;\n\t\tdo {\n\t\t\tcoords.x += el.offsetLeft;\n\t\t\tcoords.y += el.offsetTop;\n\t\t}\n\t\twhile ((el = el.offsetParent));\n\t\tvar offsetX = evt.clientX - coords.x;\n\t\tvar offsetY = evt.clientY - coords.y;\n\t\talert(offsetX + ':' + offsetY + ':' + num);\n\t}\n}\nfunction leapTo (link)\n{\n\tvar new_url=link;\n\tif ( (new_url != \"\") && (new_url != null) ) \n\t\twindow.location=new_url;\n\telse \n\t\talert(\"You must make a selection.\");\n}\nfunction jumpTo (front,link,back)\n{\n\tvar new_url=front+link+back;\n\tif ( (new_url != \"\") && (new_url != null) ) \n\t\twindow.location=new_url;\n\telse \n\t\talert(\"You must make a selection.\");\n}// Deactivate Cloaking -->\n</script>";
789
790 String radioButtons = "\n<center><FORM>";
791 if(ratio != 0.4)
792 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.4')\">40%";
793 if(ratio != 0.5)
794 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.5')\">50%";
795 if(ratio != 0.6)
796 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.6')\">60%";
797 if(ratio != 0.7)
798 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.7')\">70%";
799 if(ratio != 0.8)
800 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.8')\">80%";
801 if(ratio != 0.9)
802 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.9')\">90%";
803 if(ratio != 1.0)
804 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`1.0')\">100%";
805 radioButtons = radioButtons + "\n</FORM></center>";
806
807 leg_num = leg_num+1;
808
809 String doc_link = "library?a=d&c="+this.cluster_name+"&dt=map&d="+doc_id+"`"+ratio+"`"+leg_num+"`";
810
811 StringBuffer dropDownBox = new StringBuffer();
812
813 dropDownBox.append("Add Locations Of Type: <form name=\"myForm\"><select name=\"s1index\" onChange=\"jumpTo('");
814 dropDownBox.append(doc_link);
815 dropDownBox.append("',document.myForm.s1index.options[document.myForm.s1index.selectedIndex].value,'')\">\n");
816
817 for (int i=0; i<this.index_name_list.size(); i++) {
818 String name = (String)this.index_name_list.get(i);
819 dropDownBox.append("<option value=\"");
820 dropDownBox.append(name);
821 dropDownBox.append("\">");
822 String display = (String)this.index_display_list.get(i);
823 dropDownBox.append(display);
824 dropDownBox.append("\n");
825 }
826 // for each option, Malcolm had onClick=\"leapTo('"+doc_link+name+"')\" - dont seem to need this
827 dropDownBox.append("</select></form>\n");
828
829 return javascript + radioButtons + dropDownBox;
830 }
831}
Note: See TracBrowser for help on using the repository browser.