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

Last change on this file since 6694 was 6694, checked in by nzdl, 20 years ago

removed library from the urls - now they just start with ? which seems to work fine

  • Property svn:keywords set to Author Date Id Revision
File size: 35.3 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 if (uid.equals("")) {
185 System.out.println("in metadata retrieve, uid = ''\n"+converter.getPrettyString(request));
186 }
187 result.setAttribute(GSXML.FROM_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE);
188 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
189
190 // Get the parameters of the request
191 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
192 if (param_list == null) {
193 System.err.println("MapRetrieve Error: missing paramList.\n");
194 return result; // Return the empty result
195 }
196
197 // The metadata information required
198 Vector metadata_list = new Vector();
199 boolean all_metadata = false;
200 // Process the request parameters
201 Element param = (Element) param_list.getFirstChild();
202
203 while (param != null) {
204 // Identify the metadata information desired
205 if (param.getAttribute(GSXML.NAME_ATT).equals("metadata")) {
206 String metadata = GSXML.getValue(param);
207 if (metadata.equals("all")) {
208 all_metadata = true;
209 break;
210 }
211 metadata_list.add(metadata);
212 }
213 param = (Element) param.getNextSibling();
214 }
215
216 Element node_list = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
217 result.appendChild(node_list);
218
219 // Get the documents
220 Element request_node_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
221 if (request_node_list == null) {
222 System.err.println("Error: DocumentMetadataRetrieve request had no "+GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
223 return result;
224 }
225
226 NodeList request_nodes = request_node_list.getChildNodes();
227
228 try{
229 //used just to ensure outfile is initialised
230 BufferedWriter outfile = new BufferedWriter(new FileWriter(this.temp_files_dir+"emptynothingness"));
231
232 for (int i = 0; i < request_nodes.getLength(); i++) {
233 Element request_node = (Element) request_nodes.item(i);
234 String node_id = request_node.getAttribute(GSXML.NODE_ID_ATT);
235 String place_data = "";
236 String year = "";
237 String coOrdinates = "";
238 String thumb = "";
239 String link = "";
240 int mapFreq = 0;
241 LinkedList place_data_list = new LinkedList();
242 LinkedList coOrdinates_list = new LinkedList();
243
244 //if this is a first displaying of the map
245 if(node_id.indexOf("```") != -1 && node_id.indexOf("imageChooser") == -1){
246 //get the number of query terms on this map
247 mapFreq = Integer.parseInt(node_id.substring(node_id.lastIndexOf('`',node_id.indexOf("```")-1)+1,node_id.indexOf("```")));
248 //get the place names on this map
249 place_data = node_id.substring(node_id.indexOf("```")+3,node_id.length());
250 //get the map metadata
251 node_id = node_id.substring(0,node_id.indexOf('`',node_id.indexOf("```")));
252
253 try{
254 // loop for each query term on the map
255 for(int r=0; r<mapFreq; r++){
256 // get title, type, location, and the string for creating the hyperlink for this query term
257 String title = place_data.substring(0,place_data.indexOf('`'));
258 String type = place_data.substring(place_data.indexOf('`')+1,place_data.indexOf('`',place_data.indexOf('`')+1));
259 String location = place_data.substring(place_data.indexOf('`',place_data.indexOf('`')+1)+1, place_data.indexOf('`',place_data.indexOf('`',place_data.indexOf('`')+1)+1));
260 if(place_data.indexOf("```") != -1)
261 link=place_data.substring(0,place_data.indexOf("```"));
262 else
263 link=place_data;
264 // resolve the type and location
265 type = getType(type);
266 location = getLocation(location);
267 // remove this query term from the string
268 if(place_data.indexOf("```") != -1)
269 place_data = place_data.substring(place_data.indexOf("```")+3,place_data.length());
270
271 //add the co-ordinates of this query term to the co-ordinates list
272 coOrdinates_list.add("`"+link.substring(link.lastIndexOf('`')+1,link.length())+"`"+link.substring(link.lastIndexOf('`',link.lastIndexOf('`')-1)+2,link.lastIndexOf('`')));
273
274 // add the title, type and location to the places list
275 place_data_list.add(title + ", " + type + ", " + location + ";");
276 }
277 }catch(StringIndexOutOfBoundsException sioobe){sioobe.printStackTrace();}
278 }
279
280 // Add the document to the list
281 Element new_node = (Element)this.doc.importNode(request_node, false);
282 node_list.appendChild(new_node);
283
284 // Add the requested metadata information
285 Element node_meta_list = this.doc.createElement(GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
286 new_node.appendChild(node_meta_list);
287
288 // create the navigation thumbnails. This doesn't seem to work most of the time ???????
289 for (int m = 0; m < metadata_list.size(); m++) {
290 String metadata = (String) metadata_list.get(m);
291 thumb = "";
292 String value = "";
293 if(node_id.indexOf('.') != -1)
294 thumb = node_id.substring(0,node_id.indexOf('.'));
295 if(node_id.indexOf('`') != -1)
296 value = node_id.substring(node_id.lastIndexOf('`',node_id.lastIndexOf('`')-1)+1,node_id.lastIndexOf('`',node_id.lastIndexOf('`')-1)+5);
297 year = value;
298
299 place_data="";
300 if(place_data_list.size() != 0)
301 for(int q=0; q<mapFreq; q++){
302 link = (String)place_data_list.get(q);
303 if(q!=0){
304 place_data = place_data + "<br>" + link;
305 }
306 else{
307 place_data = link;
308 coOrdinates = "``";
309 }
310 coOrdinates = coOrdinates+(String)coOrdinates_list.get(q);
311 }
312
313 link = "<a href=\"?a=d&c="+this.cluster_name+"&d="+node_id+coOrdinates+"&dt=map\">";
314 thumb = "<img src=\""+this.http_image_dir + thumb + "thumb.jpg\" border=0>";
315 value = "<table><tr><td>"+link+"<p>"+place_data+"<br>"+value+"</a></td><td>"+link+thumb+"</a></td></tr></table>";
316 if(metadata.equals("Title"))
317 if(!(place_data.equals("")) && place_data.indexOf(", , ;")==-1 && node_id.indexOf("```")==-1)
318 GSXML.addMetadata(this.doc, node_meta_list, "Title", value);//metadata, value);
319 else
320 GSXML.addMetadata(this.doc, node_meta_list, metadata, "");
321
322 if(place_data.indexOf(", , ;")==-1){
323 if(i==0)
324 if(!(mapFreq==0)){
325 outfile = new BufferedWriter(new FileWriter(this.temp_files_dir+"links"+uid));
326 outfile.write("<table align=\"center\"><tr>");
327 }
328
329 if(!(mapFreq==0)){
330 if(i%7 == 0)
331 outfile.write("</tr><tr>");
332 outfile.write("<td align=\"center\">"+link+thumb+"</a><br>"+link+year+"</a></td>");
333 }
334
335 if(i == request_nodes.getLength()-1){
336 outfile.write("</tr></table><p>");
337 outfile.flush();
338 outfile.close();
339 }
340 }
341 }
342
343 }
344 }catch(IOException ioe){ioe.printStackTrace();}
345
346 return result;
347 }catch(Exception excep){excep.printStackTrace();}
348 return null;
349
350 }
351
352 protected Element processDocumentContentRetrieve(Element request){
353 // Create a new (empty) result message
354 Element result = doc.createElement(GSXML.RESPONSE_ELEM);
355 result.setAttribute(GSXML.FROM_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE);
356 result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
357
358 String uid = request.getAttribute(GSXML.USER_ID_ATT);
359 String temp_image_file = this.temp_files_dir+"temp_"+uid+".jpg";
360 String temp_image_file2 = this.temp_files_dir+"temp_"+uid+"_2.jpg";
361
362 Element query_doc_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
363 if (query_doc_list == null) {
364 System.err.println("Error: DocumentContentRetrieve request specified no doc nodes.\n");
365 return result;
366 }
367
368 String legend_file = this.temp_files_dir+"legend_"+uid+".jpg";
369 String blank_file = this.files_home_dir + "blank.jpg";
370 Element doc_list = doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
371 result.appendChild(doc_list);
372
373 // Get the documents
374 String[] doc_ids = GSXML.getAttributeValuesFromList(query_doc_list,
375 GSXML.NODE_ID_ATT);
376
377 for (int i = 0; i < doc_ids.length; i++) {
378 String doc_id = doc_ids[i];
379 // img_num is the name of the map file eg. 072.jpg
380 String img_num = doc_id.substring(0,doc_id.indexOf('.'));
381 // strings for inserting image in html
382 String img_left = "<img border=0 src=\""+this.http_temp_image_dir;
383 String doc_content = "";
384 String co_ordinates = "";
385 String img_size = "";
386 int height = 0;
387 int width = 0;
388 // the number for the legend image if adding locations to the map
389 int leg_num = 0;
390 double ratio = 0;
391
392 // if user has clicked on the map. This does not resolve maps that are not North oriented.
393 if(doc_id.indexOf("imageChooser")==0){
394 try{
395 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
396 img_num = doc_id.substring(0,doc_id.indexOf('.'));
397
398 // get the map size
399 String get_size[] = {"identify","-size","10000", temp_image_file};
400 Process proc;
401 proc = Runtime.getRuntime().exec(get_size);
402 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
403 img_size = br.readLine();
404 proc.waitFor();
405 img_size = img_size.substring(img_size.indexOf("JPEG")+5,img_size.indexOf(" ",img_size.indexOf("JPEG")+5));
406 width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x")));
407 height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length()));
408
409 // scale image size according to the ratio
410 ratio = Double.parseDouble(doc_id.substring(doc_id.lastIndexOf('`',doc_id.indexOf("```")-1)+1,doc_id.indexOf("```")));
411 width = (int)(width*ratio);
412 height = (int)(height*ratio);
413
414 // get the position of the mouse click on the image
415 int xclick = Integer.parseInt(doc_id.substring(doc_id.indexOf("```")+3,doc_id.lastIndexOf('`')));
416 int yclick = Integer.parseInt(doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length()));
417 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.indexOf("```"));
418
419 // convert click position to percentage distance accross and down the image.
420 double xpercent = (xclick*1.0)/width;
421 double ypercent = (yclick*1.0)/height;
422
423 // get the top left and bottom right co-ordinates of the map
424 double ytop = Double.parseDouble(doc_id.substring(0,doc_id.indexOf('`')))*-1;
425 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
426 double xleft = Double.parseDouble(doc_id.substring(0,doc_id.indexOf('`')));
427 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
428 double ybot = Double.parseDouble(doc_id.substring(0,doc_id.indexOf('`')))*-1;
429 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
430 double xright = Double.parseDouble(doc_id.substring(0,doc_id.indexOf('`')));
431 doc_id = doc_id.substring(doc_id.indexOf('`')+1,doc_id.length());
432
433 // calculate the map co-ordinates of the mouse click
434 xpercent = ((xright-xleft)*xpercent)+xleft;
435 ypercent = ((ybot-ytop)*ypercent)+ytop;
436
437 // search the tree for nearby place names
438 namesInList.clear();
439 findName(xpercent, ypercent, 0.1, 0.1, tree, true);
440
441 int namesInArraySize = namesInList.size();
442 String returnNames[]=new String[namesInArraySize];
443
444 //put the names in an array
445 for(int ri=0; namesInList.size()>0; ri++){
446 returnNames[ri]=(String)namesInList.getFirst();
447 returnNames[ri] = returnNames[ri].substring(0,returnNames[ri].indexOf('`'));
448 namesInList.removeFirst();
449 }
450
451 //sort the names
452 Arrays.sort(returnNames);
453 doc_content = "\n<script>\nfunction openIt(loc){\n\topener.location=loc;\n\tself.close();\n}\n</script>";
454 for(int nameIndex=0; nameIndex < namesInArraySize; nameIndex++){
455 String tempName = returnNames[nameIndex];
456 //convert names with spaces for hyperlinks
457 if(returnNames[nameIndex].indexOf(' ') != -1){
458 returnNames[nameIndex] = returnNames[nameIndex].replaceAll(" ","+");
459 returnNames[nameIndex] = "%22"+returnNames[nameIndex]+"%22";
460 }
461 // add the place name to the html
462 doc_content = doc_content+"<a href=\"\" onClick=openIt('?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>";
463 }
464
465 }catch(Exception ioexception){ioexception.printStackTrace();}
466 }
467 else{
468
469 try{
470 //file for converting image
471 BufferedWriter bw = new BufferedWriter(new FileWriter(this.temp_files_dir + "add_x_"+uid));;
472 Process proc;
473
474 // if a new search
475 if(doc_id.indexOf("```") != -1){
476 // copy requested map to temp.jpg
477 proc = Runtime.getRuntime().exec("cp "+this.files_home_dir+"maps"+File.separator+img_num+".jpg "+temp_image_file);
478 proc.waitFor();
479 }
480
481 //get the image size
482 String get_size[] = {"identify","-size","10000", temp_image_file};
483 proc = Runtime.getRuntime().exec(get_size);
484 BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
485 img_size = br.readLine();
486 proc.waitFor();
487 img_size = img_size.substring(img_size.indexOf("JPEG")+5,img_size.indexOf(" ",img_size.indexOf("JPEG")+5));
488 if (img_size.indexOf("+")!=-1) {
489 img_size = img_size.substring(0, img_size.indexOf("+"));
490 }
491 width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x")));
492 height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length()));
493
494 // if a new search
495 if(doc_id.indexOf("```") != -1){
496 co_ordinates = doc_id.substring(doc_id.indexOf("```")+2, doc_id.length());
497 // get the number of places to mark on the map
498 int x_number = Integer.parseInt(doc_id.substring(doc_id.lastIndexOf('`',doc_id.indexOf("```")-1)+1,doc_id.indexOf("```")));
499
500 //write the convert command
501 bw.write("convert -font helvetica -fill red -pointsize 48 ");
502
503 for(int xi=0; xi<x_number; xi++){
504
505 // get the co-ordinates of the place name
506 double xco = Double.parseDouble(co_ordinates.substring(co_ordinates.lastIndexOf('`',co_ordinates.lastIndexOf('`')-1)+1,co_ordinates.lastIndexOf('`',co_ordinates.lastIndexOf('`')-1)+7));
507 double yco = Double.parseDouble(co_ordinates.substring(co_ordinates.lastIndexOf('`')+1,co_ordinates.length()));
508 if(xi != x_number-1)
509 co_ordinates = co_ordinates.substring(0,co_ordinates.lastIndexOf('`',co_ordinates.lastIndexOf('`')-1));
510
511 int index = 0;
512 index = doc_id.indexOf('`')+2;
513
514 // get the zoom ratio and the top left and bottom right co-ordinates of the map
515 ratio = 0.4;
516 double tly = Double.parseDouble(doc_id.substring(index,index+5));
517 index = doc_id.indexOf('`',index)+1;
518 double tlx = Double.parseDouble(doc_id.substring(index,index+6));
519 index = doc_id.indexOf('`',index)+2;
520 double bry = Double.parseDouble(doc_id.substring(index,index+5));
521 index = doc_id.indexOf('`',index)+1;
522 double brx = Double.parseDouble(doc_id.substring(index,index+6));
523 index = doc_id.indexOf('`',index)+1;
524 double orient = Double.parseDouble(doc_id.substring(index,doc_id.indexOf('`',index)));
525
526 // find the centre of the map
527 double xcent = ((brx-tlx)/2)+tlx;
528 double ycent = ((bry-tly)/2)+tly;
529
530 // get the orientation of the map
531 orient = Math.toRadians(orient);
532
533 // rotate the co-ordinates around the centre of the map
534 xco=xco-xcent;
535 yco=yco-ycent;
536 double oldx=xco;
537 xco=xco*Math.cos(orient)-yco*Math.sin(orient);
538 yco=oldx*Math.sin(orient)+yco*Math.cos(orient);
539 xco=xco+xcent;
540 yco=yco+ycent;
541 xco = (xco-tlx)/(brx-tlx);
542 yco = (yco-tly)/(bry-tly);
543
544 // calculate the pixels for placing the mark
545 width = (int)(xco*width)-5;
546 height = (int)(yco*height)-5;
547
548 // write the options for convert
549 bw.write("-draw 'text "+width+","+height+" \"x\"' ");
550 bw.flush();
551
552 // reset the width and height variables for the image
553 width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x")));
554 height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length()));
555 }
556 doc_id = doc_id.substring(0,doc_id.lastIndexOf('`',doc_id.indexOf("```")-1));
557
558 // 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
559 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);
560
561 // reset legend number
562 leg_num = 0;
563 }
564
565 else
566 // if changing zoom level
567 if(doc_id.substring(doc_id.lastIndexOf('`'),doc_id.length()).indexOf('.')!=-1){
568 // get the ratio
569 ratio = Double.parseDouble(doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length()));
570 // write the command for scaling image
571 bw.write("convert -scale "+((int)(ratio*width))+"x"+((int)(ratio*height))+" "+temp_image_file+" "+temp_image_file2);
572 doc_id = doc_id.substring(0,doc_id.lastIndexOf('`'));
573 }
574
575 // if adding locations to map
576 else{
577
578 // get the location type to add
579 String restricter= doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length());
580 doc_id = doc_id.substring(0,doc_id.lastIndexOf('`'));
581
582 // get the number for the legend image
583 leg_num = Integer.parseInt(doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length()));
584 doc_id = doc_id.substring(0,doc_id.lastIndexOf('`'));
585
586 //open file for location type
587 BufferedReader inType = new BufferedReader(new FileReader(this.files_home_dir + "place_types"+File.separator + restricter + ".txt"));
588
589 //check through the file and add any places that are in the bounds of this map.
590
591 // check value. set to true if a place to add is found
592 boolean add_place_type = false;
593
594 int index = 0;
595 index = doc_id.indexOf('`')+2;
596 // get zoom ratio
597 ratio = Double.parseDouble(doc_id.substring(doc_id.lastIndexOf('`')+1,doc_id.length()));
598 // get top left and bottom right co-ordinates of the map
599 double tly = Double.parseDouble(doc_id.substring(index,index+5));
600 index = doc_id.indexOf('`',index)+1;
601 double tlx = Double.parseDouble(doc_id.substring(index,index+6));
602 index = doc_id.indexOf('`',index)+2;
603 double bry = Double.parseDouble(doc_id.substring(index,index+5));
604 index = doc_id.indexOf('`',index)+1;
605 double brx = Double.parseDouble(doc_id.substring(index,index+6));
606 index = doc_id.indexOf('`',index)+1;
607 // get orientation of the map
608 double orient = Double.parseDouble(doc_id.substring(index,doc_id.indexOf('`',index)));
609 // calculate centre of map
610 double xcent = ((brx-tlx)/2)+tlx;
611 double ycent = ((bry-tly)/2)+tly;
612 orient = Math.toRadians(orient);
613
614 String type_point = "";
615 double xco = 0.0;
616 double yco = 0.0;
617
618 // read the file
619 while(inType.ready()){
620 //read a line
621 type_point = inType.readLine();
622
623 // get the co-ordinates of the point
624 xco = Double.parseDouble(type_point.substring(type_point.lastIndexOf('`')+1,type_point.length()));
625 yco = Double.parseDouble(type_point.substring(type_point.lastIndexOf('`',type_point.lastIndexOf('`')-1)+2,type_point.lastIndexOf('`')));
626
627 // if it is within the co-ordinates of the map
628 if(xco >= tlx && xco < brx && yco >= tly && yco < bry){
629 // rotate the point around the centre according to map orientation
630 xco=xco-xcent;
631 yco=yco-ycent;
632 double oldx=xco;
633 xco=xco*Math.cos(orient)-yco*Math.sin(orient);
634 yco=oldx*Math.sin(orient)+yco*Math.cos(orient);
635 xco=xco+xcent;
636 yco=yco+ycent;
637 // get the pixels for where to place the mark
638 xco = (xco-tlx)/(brx-tlx);
639 yco = (yco-tly)/(bry-tly);
640 width = (int)(xco*width)-5;
641 height = (int)(yco*height)-5;
642
643 //if this is the first point to be added
644 if(!add_place_type){
645 // write the initial convert command
646 bw.write("convert -font helvetica -fill red -pointsize 36 ");
647 // toggle check value
648 add_place_type = true;
649 }
650
651 // write the options for the convert command
652 bw.write("-draw 'text "+width+","+height+" \""+leg_num+"\"' ");
653 bw.flush();
654
655 // reset width and height variables for the image
656 width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x")));
657 height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length()));
658 }
659 }
660
661 //if there are places to mark on the map
662 if(add_place_type){
663 // finish the convert command and write command for scaling image
664 bw.write(temp_image_file+" "+temp_image_file+";convert -scale "+((int)(ratio*width))+"x"+((int)(ratio*height))+" "+temp_image_file+" "+temp_image_file2);
665
666 // open file for converting the legend command
667 BufferedWriter buf = new BufferedWriter(new FileWriter(this.temp_files_dir+"add_l_"+uid));
668 if(leg_num == 1)
669 buf.write("cp "+blank_file +" "+legend_file+";");
670 // write the command for adding to the legend
671 buf.write("convert -font helvetica -fill red -pointsize 12 -draw 'text 15,"+(leg_num*15+20)+" \""+leg_num+" "+getType(restricter)+"\"' "+legend_file+" "+legend_file);
672 buf.flush();
673 buf.close();
674 // execute the command for the legend image
675 proc = Runtime.getRuntime().exec("sh "+this.temp_files_dir+"add_l_"+uid);
676 proc.waitFor();
677 }
678 inType.close();
679 }
680 bw.flush();
681 bw.close();
682
683 // execute the convert commands etc.
684 proc = Runtime.getRuntime().exec("sh "+this.temp_files_dir+"add_x_"+uid);
685 proc.waitFor();
686
687 }catch(Exception ioe){ioe.printStackTrace();}
688
689 //write the html for the document
690
691 String doc_content_head = "?a=d&c="+this.cluster_name+"&dt=map&d=";
692 doc_content = "<td valign=\"top\"><script>document.write('"+img_left+"legend_"+uid+".jpg"+"?'+new Date().getTime())</script>\"></td>";
693 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>";
694
695 // replace _httpimg_ with the correct address
696 doc_content = "<table><tr>"+doc_content+"</tr></table>";
697
698 String javascript = getJavascript(ratio, doc_id, leg_num);
699
700 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;
701 doc_content = doc_content.replaceAll("zoomhead", doc_content_head);
702 doc_content = doc_content.replaceAll("placeChooserImage", doc_content_head+"imageChooser`"+doc_id+"`"+ratio);
703
704 // read the file for the navigation thumbnails
705 try{
706 BufferedReader infile = new BufferedReader(new FileReader(this.temp_files_dir+"links"+uid));
707 doc_content = doc_content + infile.readLine();
708 infile.close();
709 }catch(Exception ioexc){ioexc.printStackTrace();}
710 }
711
712 // put the html in a text node
713 Element text_doc = doc.createElement(GSXML.DOC_NODE_ELEM);
714 text_doc.setAttribute(GSXML.NODE_ID_ATT, doc_id);
715 GSXML.addDocText(doc, text_doc, doc_content);
716 doc_list.appendChild(text_doc);
717
718 }
719 return result;
720 }
721
722 // resolves location codes for places from LINZ database
723 private String getLocation(String location){
724 String read;
725 try{
726 BufferedReader in = new BufferedReader(new FileReader(this.files_home_dir+"files"+File.separator+"landdist.txt"));
727 in.readLine();
728 while(in.ready()){
729 read = in.readLine();
730 if(read.substring(0,2).equals(location)){
731 in.close();
732 return read.substring(3, read.length());
733 }
734 }
735 }catch(Exception e){}
736 return "";
737 }
738
739 // resolves type codes for the LINZ database
740 private String getType(String type){
741 String read;
742 try{
743 BufferedReader in = new BufferedReader(new FileReader(this.files_home_dir+"files"+File.separator+"pointdes.txt"));
744 in.readLine();
745 while(in.ready()){
746 read = in.readLine();
747 if(read.substring(0,read.indexOf('`')).toLowerCase().equals(type.toLowerCase())){
748 in.close();
749 return read.substring(read.indexOf('`')+1, read.indexOf(':'));
750 }
751 }
752 }catch(Exception e){}
753 return "";
754 }
755
756 //recursive pre-order traversal of the 2-dimensional tree
757 // x & y are co-ordinates of mouse click, xoff & yoff are the distance in degrees away from x & y to search,
758 // d is the current node of the tree, xcomp controls whether searching is by x co-ordinate or y co-ordinate.
759 private void findName(double x, double y, double xoff, double yoff, DefaultMutableTreeNode d, boolean xcomp){
760 //if a leaf node return. All leaf nodes are empty
761 if(d.isLeaf())
762 return;
763 //get coordinates of the current node
764 double xco = Double.parseDouble(((String)d.getUserObject()).substring(((String)d.getUserObject()).length()-6,((String)d.getUserObject()).length()));
765 double yco = Double.parseDouble(((String)d.getUserObject()).substring(((String)d.getUserObject()).length()-12,((String)d.getUserObject()).length()-7));
766 //if in range
767 if((x-xoff)<xco && (x+xoff)>xco && (y-yoff)<yco && (y+yoff)>yco){
768 //add to list
769 namesInList.addFirst((String)d.getUserObject());
770 }
771 //if comparing the x axis
772 if(xcomp){
773 //if need to search left subtree
774 if((x-xoff)<xco)
775 findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(0),!xcomp);
776 //if need to search right subtree
777 if((x+xoff)>xco)
778 findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(1),!xcomp);
779 }
780 else{
781 //if need to search left subtree
782 if((y-yoff)<yco)
783 findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(0),!xcomp);
784 //if need to search right subtree
785 if((y+yoff)>yco)
786 findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(1),!xcomp);
787 }
788 }
789
790 // 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!
791 private String getJavascript(double ratio, String doc_id, int leg_num)
792 {
793 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>";
794
795 String radioButtons = "\n<center><FORM>";
796 if(ratio != 0.4)
797 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.4')\">40%";
798 if(ratio != 0.5)
799 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.5')\">50%";
800 if(ratio != 0.6)
801 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.6')\">60%";
802 if(ratio != 0.7)
803 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.7')\">70%";
804 if(ratio != 0.8)
805 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.8')\">80%";
806 if(ratio != 0.9)
807 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`0.9')\">90%";
808 if(ratio != 1.0)
809 radioButtons = radioButtons + "\n\t<INPUT TYPE=\"radio\" NAME=\"buttons1\" onClick=\"leapTo('zoomhead"+doc_id+"`1.0')\">100%";
810 radioButtons = radioButtons + "\n</FORM></center>";
811
812 leg_num = leg_num+1;
813
814 String doc_link = "?a=d&c="+this.cluster_name+"&dt=map&d="+doc_id+"`"+ratio+"`"+leg_num+"`";
815
816 StringBuffer dropDownBox = new StringBuffer();
817
818 dropDownBox.append("Add Locations Of Type: <form name=\"myForm\"><select name=\"s1index\" onChange=\"jumpTo('");
819 dropDownBox.append(doc_link);
820 dropDownBox.append("',document.myForm.s1index.options[document.myForm.s1index.selectedIndex].value,'')\">\n");
821
822 for (int i=0; i<this.index_name_list.size(); i++) {
823 String name = (String)this.index_name_list.get(i);
824 dropDownBox.append("<option value=\"");
825 dropDownBox.append(name);
826 dropDownBox.append("\">");
827 String display = (String)this.index_display_list.get(i);
828 dropDownBox.append(display);
829 dropDownBox.append("\n");
830 }
831 // for each option, Malcolm had onClick=\"leapTo('"+doc_link+name+"')\" - dont seem to need this
832 dropDownBox.append("</select></form>\n");
833
834 return javascript + radioButtons + dropDownBox;
835 }
836}
Note: See TracBrowser for help on using the repository browser.