source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/MapRetrieve.java@ 26198

Last change on this file since 26198 was 25981, checked in by sjm84, 12 years ago

Reformatting this file

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