/webportal/src/main/java/au/org/emii/portal/composer/LayerLegendComposer2.java

http://alageospatialportal.googlecode.com/ · Java · 875 lines · 704 code · 128 blank · 43 comment · 160 complexity · 19d9ab6a13cc624f86c2c5405f22e884 MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package au.org.emii.portal.composer;
  6. import au.org.emii.portal.menu.MapLayer;
  7. import au.org.emii.portal.settings.SettingsSupplementary;
  8. import au.org.emii.portal.util.GeoJSONUtilities;
  9. import au.org.emii.portal.util.LayerUtilities;
  10. import java.awt.Color;
  11. import java.net.URLEncoder;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. import org.ala.spatial.data.BiocacheQuery;
  18. import org.ala.spatial.data.LegendObject;
  19. import org.ala.spatial.data.Query;
  20. import org.ala.spatial.data.QueryField;
  21. import org.ala.spatial.data.UploadQuery;
  22. import org.ala.spatial.util.CommonData;
  23. import org.ala.spatial.util.LegendMaker;
  24. import org.apache.commons.httpclient.HttpClient;
  25. import org.apache.commons.httpclient.methods.GetMethod;
  26. import org.apache.commons.lang.StringEscapeUtils;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.zkoss.zk.ui.Executions;
  29. import org.zkoss.zk.ui.event.Event;
  30. import org.zkoss.zk.ui.event.EventListener;
  31. import org.zkoss.zul.Button;
  32. import org.zkoss.zul.Checkbox;
  33. import org.zkoss.zul.Combobox;
  34. import org.zkoss.zul.Comboitem;
  35. import org.zkoss.zul.Div;
  36. import org.zkoss.zul.Doublebox;
  37. import org.zkoss.zul.Hbox;
  38. import org.zkoss.zul.Image;
  39. import org.zkoss.zul.Intbox;
  40. import org.zkoss.zul.Label;
  41. import org.zkoss.zul.Listbox;
  42. import org.zkoss.zul.Radio;
  43. import org.zkoss.zul.Radiogroup;
  44. import org.zkoss.zul.Slider;
  45. import org.zkoss.zul.Textbox;
  46. /**
  47. *
  48. * @author Adam
  49. */
  50. public class LayerLegendComposer2 extends GenericAutowireAutoforwardComposer {
  51. SettingsSupplementary settingsSupplementary = null;
  52. Slider opacitySlider;
  53. Label opacityLabel;
  54. Slider redSlider;
  55. Slider greenSlider;
  56. Slider blueSlider;
  57. Slider sizeSlider;
  58. Checkbox chkUncertaintySize;
  59. public Button btnPointsCluster;
  60. Label lblFupload;
  61. Label redLabel;
  62. Label greenLabel;
  63. Label blueLabel;
  64. Label sizeLabel;
  65. Listbox activeLayersList;
  66. Div layerControls;
  67. Div clusterpoints;
  68. Div uncertainty;
  69. Hbox uncertaintyLegend;
  70. Div colourChooser;
  71. Div sizeChooser;
  72. Image legendImg;
  73. Image legendImgUri;
  74. Div legendHtml;
  75. Label legendLabel;
  76. Div divUserColours;
  77. Hbox dAnimationStep;
  78. Combobox cbColour;
  79. Comboitem ciColourUser; //User selected colour
  80. Label layerName;
  81. EventListener listener;
  82. Query query;
  83. public Radiogroup pointtype;
  84. public Radio rPoint, rCluster, rGrid;
  85. MapLayer mapLayer;
  86. boolean inInit = false;
  87. Textbox txtLayerName;
  88. String sLayerName;
  89. Button btnLayerName;
  90. Label lInGroupCount;
  91. Button btnCreateGroupLayers;
  92. Div dGroupBox;
  93. Combobox cbClassificationGroup;
  94. Div divClassificationPicker;
  95. Div divAnimation;
  96. Combobox cbAnimationDenomination;
  97. Button btnAnimationStart;
  98. Button btnAnimationStop;
  99. Intbox intAnimationStep;
  100. Intbox intAnimationYearStart;
  101. Intbox intAnimationYearEnd;
  102. //Label lblAnimationLabel;
  103. Doublebox dblAnimationSeconds;
  104. @Override
  105. public void afterCompose() {
  106. super.afterCompose();
  107. cbColour.setSelectedIndex(0);
  108. }
  109. public void onScroll$opacitySlider(Event e) {
  110. float opacity = ((float) opacitySlider.getCurpos()) / 100;
  111. int percentage = (int) (opacity * 100);
  112. opacitySlider.setCurpos(percentage);
  113. opacityLabel.setValue(percentage + "%");
  114. refreshLayer();
  115. }
  116. public void updateLegendImage() {
  117. LegendMaker lm = new LegendMaker();
  118. int red = redSlider.getCurpos();
  119. int blue = blueSlider.getCurpos();
  120. int green = greenSlider.getCurpos();
  121. Color c = new Color(red, green, blue);
  122. legendImg.setContent(lm.singleCircleImage(c, 60, 60, 50.0));
  123. // sizeChooser.setVisible(true);
  124. if (cbColour.getSelectedItem() != ciColourUser) {
  125. legendHtml.setVisible(true);
  126. legendImg.setVisible(false);
  127. showPointsColourModeLegend();
  128. } else {
  129. legendImg.setVisible(true);
  130. legendHtml.setVisible(false);
  131. }
  132. }
  133. public void onScroll$sizeSlider() {
  134. int size = sizeSlider.getCurpos();
  135. sizeLabel.setValue(String.valueOf(size));
  136. refreshLayer();
  137. }
  138. public void onScroll$blueSlider() {
  139. int blue = blueSlider.getCurpos();
  140. blueLabel.setValue(String.valueOf(blue));
  141. updateLegendImage();
  142. refreshLayer();
  143. }
  144. public void onScroll$redSlider() {
  145. int red = redSlider.getCurpos();
  146. redLabel.setValue(String.valueOf(red));
  147. updateLegendImage();
  148. refreshLayer();
  149. }
  150. public void onScroll$greenSlider() {
  151. int green = greenSlider.getCurpos();
  152. greenLabel.setValue(String.valueOf(green));
  153. updateLegendImage();
  154. refreshLayer();
  155. }
  156. public void selectColour(Object obj) {
  157. Div div = (Div) obj;
  158. String style = div.getStyle();
  159. String background_color = "background-color";
  160. int a = style.indexOf(background_color);
  161. if (a >= 0) {
  162. String colour = style.substring(a + background_color.length() + 2, a + background_color.length() + 8);
  163. int r = Integer.parseInt(colour.substring(0, 2), 16);
  164. int g = Integer.parseInt(colour.substring(2, 4), 16);
  165. int b = Integer.parseInt(colour.substring(4, 6), 16);
  166. redSlider.setCurpos(r);
  167. greenSlider.setCurpos(g);
  168. blueSlider.setCurpos(b);
  169. redLabel.setValue(String.valueOf(r));
  170. greenLabel.setValue(String.valueOf(g));
  171. blueLabel.setValue(String.valueOf(b));
  172. updateLegendImage();
  173. refreshLayer();
  174. }
  175. }
  176. public void onChange$cbColour(Event event) {
  177. mapLayer.setHighlight(null);
  178. updateUserColourDiv();
  179. updateLegendImage();
  180. refreshLayer();
  181. }
  182. void updateUserColourDiv() {
  183. if (cbColour.getSelectedItem() == ciColourUser) {
  184. divUserColours.setVisible(true);
  185. } else {
  186. divUserColours.setVisible(false);
  187. }
  188. }
  189. void updateComboBoxesColour(MapLayer currentSelection) {
  190. if (currentSelection.isClustered()) {
  191. cbColour.setSelectedItem(ciColourUser);
  192. cbColour.setDisabled(true);
  193. } else {
  194. cbColour.setDisabled(false);
  195. for (int i = 0; i < cbColour.getItemCount(); i++) {
  196. if (cbColour.getItemAtIndex(i).getValue() != null
  197. && cbColour.getItemAtIndex(i).getValue().equals(currentSelection.getColourMode())) {
  198. cbColour.setSelectedIndex(i);
  199. }
  200. }
  201. updateUserColourDiv();
  202. }
  203. }
  204. void showPointsColourModeLegend() {
  205. //remove all
  206. while (legendHtml.getChildren().size() > 0) {
  207. legendHtml.removeChild(legendHtml.getFirstChild());
  208. }
  209. //TODO: make work for query instead of lsid
  210. //1. register legend
  211. //String pid = registerPointsColourModeLegend(lsid, (String) cbColour.getSelectedItem().getValue());
  212. //put any parameters into map
  213. Map map = new HashMap();
  214. //map.put("pid", pid);
  215. map.put("query", query);
  216. map.put("layer", mapLayer);
  217. map.put("readonly", "true");
  218. String colourmode = (String) cbColour.getSelectedItem().getValue();
  219. if (!mapLayer.getColourMode().equals("grid")
  220. && query.getLegend(colourmode).getCategories() != null) {
  221. map.put("checkmarks", "true");
  222. }
  223. try {
  224. LegendObject lo = query.getLegend(colourmode);
  225. if (lo != null) {
  226. mapLayer.setData("legendobject", lo);
  227. }
  228. } catch (Exception e) {
  229. e.printStackTrace();
  230. }
  231. map.put("colourmode", colourmode);
  232. try {
  233. Executions.createComponents(
  234. "/WEB-INF/zul/AnalysisClassificationLegend.zul", legendHtml, map);
  235. } catch (Exception e) {
  236. e.printStackTrace();
  237. }
  238. }
  239. public void init(MapLayer ml, Query query, int red, int green, int blue, int size, int opacity, String colourMode, int type, boolean uncertainty, EventListener listener) {
  240. mapLayer = ml;
  241. inInit = true;
  242. txtLayerName.setValue(ml.getDisplayName());
  243. sLayerName = ml.getDisplayName();
  244. this.query = query;
  245. opacitySlider.setCurpos(opacity);
  246. onScroll$opacitySlider(null);
  247. redSlider.setCurpos(red);
  248. onScroll$redSlider();
  249. greenSlider.setCurpos(green);
  250. onScroll$greenSlider();
  251. blueSlider.setCurpos(blue);
  252. onScroll$blueSlider();
  253. sizeSlider.setCurpos(size);
  254. onScroll$sizeSlider();
  255. for (Comboitem item : (List<Comboitem>) cbColour.getItems()) {
  256. if (item.getValue() != null && item.getValue().equals(colourMode)) {
  257. cbColour.setSelectedItem(item);
  258. //System.out.println("LAYER LEGEND COMPOSER 2: set colour by item: " + item);
  259. break;
  260. }
  261. }
  262. this.listener = listener;
  263. if (type == 0) {
  264. pointtype.setSelectedItem(rGrid);
  265. } else if (type == 1) {
  266. pointtype.setSelectedItem(rCluster);
  267. } else if (type == 2) {
  268. pointtype.setSelectedItem(rPoint);
  269. }
  270. chkUncertaintySize.setChecked(uncertainty);
  271. updateUserColourDiv();
  272. updateLegendImage();
  273. setupLayerControls(ml);
  274. updateAnimationDiv();
  275. String script = "mapFrame.stopAllAnimations();";
  276. getMapComposer().getOpenLayersJavascript().execute(script);
  277. inInit = false;
  278. }
  279. public int getRed() {
  280. return redSlider.getCurpos();
  281. }
  282. public int getGreen() {
  283. return greenSlider.getCurpos();
  284. }
  285. public int getBlue() {
  286. return blueSlider.getCurpos();
  287. }
  288. public int getSize() {
  289. return sizeSlider.getCurpos();
  290. }
  291. public int getOpacity() {
  292. return opacitySlider.getCurpos();
  293. }
  294. public String getColourMode() {
  295. if (pointtype.getSelectedItem() == rGrid) {
  296. return "grid";
  297. } else {
  298. return (String) cbColour.getSelectedItem().getValue();
  299. }
  300. }
  301. public void onClick$btnApply(Event event) {
  302. if (listener != null) {
  303. try {
  304. listener.onEvent(null);
  305. } catch (Exception e) {
  306. e.printStackTrace();
  307. }
  308. }
  309. }
  310. public void onClick$btnClose(Event event) {
  311. this.detach();
  312. }
  313. private void showPointsColourModeLegend(MapLayer m) {
  314. //remove all
  315. while (legendHtml.getChildren().size() > 0) {
  316. legendHtml.removeChild(legendHtml.getFirstChild());
  317. }
  318. //1. register legend
  319. String colourMode = (String) cbColour.getSelectedItem().getValue();
  320. if (pointtype.getSelectedItem() == rGrid) {
  321. colourMode = "grid";
  322. }
  323. //put any parameters into map
  324. Map map = new HashMap();
  325. map.put("query", m.getData("query"));
  326. map.put("layer", m);
  327. map.put("readonly", "true");
  328. map.put("colourmode", colourMode);
  329. String colourmode = (String) cbColour.getSelectedItem().getValue();
  330. if (!m.getColourMode().equals("grid")
  331. && query.getLegend(colourmode).getCategories() != null) {
  332. map.put("checkmarks", "true");
  333. }
  334. try {
  335. LegendObject lo = ((Query) m.getData("query")).getLegend(colourmode);
  336. if (lo != null) {
  337. m.setData("legendobject", lo);
  338. }
  339. } catch (Exception e) {
  340. e.printStackTrace();
  341. }
  342. try {
  343. Executions.createComponents(
  344. "/WEB-INF/zul/AnalysisClassificationLegend.zul", legendHtml, map);
  345. } catch (Exception e) {
  346. e.printStackTrace();
  347. }
  348. }
  349. public int getPointType() {
  350. if (pointtype.getSelectedItem() == rGrid) {
  351. return 0;
  352. } else if (pointtype.getSelectedItem() == rCluster) {
  353. return 1;
  354. } else {//if(pointtype.getSelectedItem() == rPoint) {
  355. return 2;
  356. }
  357. }
  358. public boolean getUncertainty() {
  359. return chkUncertaintySize.isChecked();
  360. }
  361. public void onCheck$chkUncertaintySize() {
  362. refreshLayer();
  363. uncertaintyLegend.setVisible(chkUncertaintySize.isChecked());
  364. }
  365. public void onCheck$pointtype(Event event) {
  366. Radio selectedItem = pointtype.getSelectedItem();
  367. try {
  368. selectedItem = (Radio) ((org.zkoss.zk.ui.event.ForwardEvent) event).getOrigin().getTarget();
  369. pointtype.setSelectedItem(selectedItem);
  370. mapLayer.setHighlight(null);
  371. } catch (Exception e) {
  372. }
  373. refreshLayer();
  374. setupLayerControls(mapLayer);
  375. }
  376. void refreshLayer() {
  377. sLayerName = txtLayerName.getValue();
  378. if (listener != null && !inInit) {
  379. try {
  380. listener.onEvent(null);
  381. } catch (Exception e) {
  382. e.printStackTrace();
  383. }
  384. }
  385. }
  386. public void setupLayerControls(MapLayer m) {
  387. MapLayer currentSelection = m;
  388. if (currentSelection != null) {
  389. if (currentSelection.isDynamicStyle()) {
  390. if (m.getColourMode().equals("grid")) {
  391. pointtype.setSelectedItem(rGrid);
  392. } else {
  393. pointtype.setSelectedItem(rPoint);
  394. }
  395. //fill cbColour
  396. setupCBColour(m);
  397. updateComboBoxesColour(currentSelection);
  398. updateAdhocGroupContols(m);
  399. if (currentSelection.getColourMode().equals("-1")) {
  400. divUserColours.setVisible(true);
  401. } else {
  402. divUserColours.setVisible(false);
  403. }
  404. if (currentSelection.getGeometryType() != GeoJSONUtilities.POINT) {
  405. sizeChooser.setVisible(false);
  406. uncertainty.setVisible(false);
  407. } else {
  408. sizeChooser.setVisible(pointtype.getSelectedItem() != rGrid);
  409. if (m.getGeoJSON() != null && m.getGeoJSON().length() > 0) {
  410. uncertainty.setVisible(false);
  411. } else {
  412. uncertainty.setVisible(!(query instanceof UploadQuery));
  413. }
  414. }
  415. colourChooser.setVisible(pointtype.getSelectedItem() != rGrid);
  416. uncertainty.setVisible(pointtype.getSelectedItem() != rGrid);
  417. if ((cbColour.getSelectedItem() != ciColourUser || pointtype.getSelectedItem() == rGrid)
  418. && m.isSpeciesLayer() /*&& !m.isClustered()*/) {
  419. legendHtml.setVisible(true);
  420. legendImg.setVisible(false);
  421. showPointsColourModeLegend(m);
  422. } else {
  423. legendImg.setVisible(true);
  424. legendHtml.setVisible(false);
  425. }
  426. } else if (currentSelection.getSelectedStyle() != null) {
  427. /* 1. classification legend has uri with ".zul" content
  428. * 2. prediction legend works here
  429. * TODO: do this nicely when implementing editable prediction layers
  430. */
  431. String legendUri = currentSelection.getSelectedStyle().getLegendUri();
  432. if (legendUri != null && legendUri.indexOf(".zul") >= 0) {
  433. //remove all
  434. while (legendHtml.getChildren().size() > 0) {
  435. legendHtml.removeChild(legendHtml.getFirstChild());
  436. }
  437. //put any parameters into map
  438. Map map = null;
  439. if (legendUri.indexOf("?") > 0) {
  440. String[] parameters = legendUri.substring(legendUri.indexOf("?") + 1,
  441. legendUri.length()).split("&");
  442. if (parameters.length > 0) {
  443. map = new HashMap();
  444. }
  445. for (String p : parameters) {
  446. String[] parameter = p.split("=");
  447. if (parameter.length == 2) {
  448. map.put(parameter[0], parameter[1]);
  449. }
  450. }
  451. legendUri = legendUri.substring(0, legendUri.indexOf("?"));
  452. }
  453. //open .zul with parameters
  454. Executions.createComponents(
  455. legendUri, legendHtml, map);
  456. legendHtml.setVisible(true);
  457. legendImgUri.setVisible(false);
  458. legendLabel.setVisible(true);
  459. } else {
  460. legendImgUri.setSrc(legendUri);
  461. legendImgUri.setVisible(true);
  462. legendHtml.setVisible(false);
  463. legendLabel.setVisible(false);
  464. }
  465. legendImg.setVisible(false);
  466. colourChooser.setVisible(false);
  467. sizeChooser.setVisible(false);
  468. } else if (currentSelection.getCurrentLegendUri() != null) {
  469. // works for normal wms layers
  470. legendImgUri.setSrc(currentSelection.getCurrentLegendUri());
  471. legendImgUri.setVisible(true);
  472. legendHtml.setVisible(false);
  473. legendLabel.setVisible(false);
  474. legendImg.setVisible(false);
  475. colourChooser.setVisible(false);
  476. sizeChooser.setVisible(false);
  477. } else {
  478. //image layer?
  479. legendImgUri.setVisible(false);
  480. legendHtml.setVisible(false);
  481. legendLabel.setVisible(false);
  482. legendImg.setVisible(false);
  483. colourChooser.setVisible(false);
  484. sizeChooser.setVisible(false);
  485. }
  486. layerControls.setVisible(true);
  487. layerControls.setAttribute("activeLayerName", currentSelection.getName());
  488. setupForClassificationLayers();
  489. }
  490. if (m != null && m.isSpeciesLayer()) {
  491. clusterpoints.setVisible(true);
  492. cbColour.setDisabled(m.isClustered());
  493. } else {
  494. clusterpoints.setVisible(false);
  495. cbColour.setDisabled(true);
  496. }
  497. uncertaintyLegend.setVisible(chkUncertaintySize.isChecked());
  498. }
  499. public String getDisplayName() {
  500. return txtLayerName.getValue();
  501. }
  502. // public void onChanging$txtLayerName(Event event) {
  503. // refreshLayer();
  504. // }
  505. // public void onChange$txtLayerName(Event event) {
  506. // refreshLayer();
  507. // }
  508. public void onOK$txtLayerName(Event event) {
  509. refreshLayer();
  510. btnLayerName.setDisabled(true);
  511. }
  512. public void onBlur$txtLayerName(Event event) {
  513. if (sLayerName.equals(txtLayerName.getValue())) {
  514. btnLayerName.setDisabled(true);
  515. }
  516. }
  517. private void setupCBColour(MapLayer m) {
  518. for (int i = 0; i < cbColour.getItemCount(); i++) {
  519. if (cbColour.getItemAtIndex(i) != ciColourUser) {
  520. cbColour.removeItemAt(i);
  521. i--;
  522. }
  523. }
  524. Query q = (Query) m.getData("query");
  525. if (q != null) {
  526. ArrayList<QueryField> fields = q.getFacetFieldList();
  527. Collections.sort(fields, new QueryField.QueryFieldComparator());
  528. Comboitem seperator = new Comboitem("seperator");
  529. String lastGroup = null;
  530. for(QueryField field : fields){
  531. String newGroup = field.getGroup().getName();
  532. if(!newGroup.equals(lastGroup)){
  533. Comboitem sep = new Comboitem("seperator");
  534. sep.setLabel("---------------" + StringUtils.center(newGroup, 19) + "---------------");
  535. sep.setParent(cbColour);
  536. sep.setDisabled(true);
  537. lastGroup = newGroup;
  538. }
  539. Comboitem ci = new Comboitem(field.getDisplayName());
  540. ci.setValue(field.getName());
  541. ci.setParent(cbColour);
  542. }
  543. }
  544. }
  545. public void onClick$btnCreateGroupLayers(Event event) {
  546. Query query;
  547. if (mapLayer != null
  548. && (query = (Query) mapLayer.getData("query")) != null
  549. && query.flagRecordCount() != 0) {
  550. Query inGroup = query.newFlaggedRecords(true);
  551. Query outGroup = query.newFlaggedRecords(false);
  552. getMapComposer().mapSpecies(inGroup, mapLayer.getDisplayName() + " in group", "species", -1, LayerUtilities.SPECIES, null, -1, MapComposer.DEFAULT_POINT_SIZE, MapComposer.DEFAULT_POINT_OPACITY, MapComposer.nextColour());
  553. getMapComposer().mapSpecies(outGroup, mapLayer.getDisplayName() + " out group", "species", -1, LayerUtilities.SPECIES, null, -1, MapComposer.DEFAULT_POINT_SIZE, MapComposer.DEFAULT_POINT_OPACITY, MapComposer.nextColour());
  554. }
  555. }
  556. private void updateAdhocGroupContols(MapLayer m) {
  557. if (m == null) {
  558. dGroupBox.setVisible(false);
  559. return;
  560. }
  561. Query query = (Query) m.getData("query");
  562. if (query == null || query.flagRecordCount() == 0) {
  563. dGroupBox.setVisible(false);
  564. } else {
  565. dGroupBox.setVisible(true);
  566. lInGroupCount.setValue(query.flagRecordCount() + (query.flagRecordCount() == 1 ? " record" : " records"));
  567. }
  568. }
  569. private void setupForClassificationLayers() {
  570. if (mapLayer != null && mapLayer.getSubType() == LayerUtilities.ALOC) {
  571. divClassificationPicker.setVisible(true);
  572. //reset content
  573. Integer groupCount = (Integer) mapLayer.getData("classificationGroupCount");
  574. if (groupCount == null) {
  575. mapLayer.setData("classificationGroupCount", getClassificationGroupCount(mapLayer.getName().replace("aloc_", "")));
  576. groupCount = 0;
  577. }
  578. for (int i = cbClassificationGroup.getItemCount() - 1; i >= 0; i--) {
  579. cbClassificationGroup.removeItemAt(i);
  580. }
  581. Comboitem ci = new Comboitem("none");
  582. ci.setParent(cbClassificationGroup);
  583. for (int i = 1; i <= groupCount; i++) {
  584. new Comboitem("Group " + i).setParent(cbClassificationGroup);
  585. }
  586. //is there a current selection?
  587. Integer groupSelection = (Integer) mapLayer.getData("classificationSelection");
  588. if (groupSelection == null) {
  589. groupSelection = 0;
  590. mapLayer.setData("classificationSelection", groupSelection);
  591. }
  592. cbClassificationGroup.setSelectedIndex(groupSelection);
  593. } else {
  594. divClassificationPicker.setVisible(false);
  595. }
  596. }
  597. //sld substitution strings
  598. private static final String SUB_LAYERNAME = "*layername*";
  599. private static final String SUB_COLOUR = "0xff0000"; //"*colour*";
  600. private static final String SUB_MIN_MINUS_ONE = "*min_minus_one*";
  601. private static final String SUB_MIN = "*min*";
  602. private static final String SUB_MAX_PLUS_ONE = "*max_plus_one*";
  603. String polygonSld =
  604. "<?xml version=\"1.0\" encoding=\"UTF-8\"?><StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\">"
  605. + "<NamedLayer><Name>ALA:" + SUB_LAYERNAME + "</Name>"
  606. + "<UserStyle><FeatureTypeStyle><Rule><RasterSymbolizer><Geometry></Geometry>"
  607. + "<ColorMap>"
  608. + "<ColorMapEntry color=\"" + SUB_COLOUR + "\" opacity=\"0\" quantity=\"" + SUB_MIN_MINUS_ONE + "\"/>"
  609. + "<ColorMapEntry color=\"" + SUB_COLOUR + "\" opacity=\"1\" quantity=\"" + SUB_MIN + "\"/>"
  610. + "<ColorMapEntry color=\"" + SUB_COLOUR + "\" opacity=\"0\" quantity=\"" + SUB_MAX_PLUS_ONE + "\"/>"
  611. + "</ColorMap></RasterSymbolizer></Rule></FeatureTypeStyle></UserStyle></NamedLayer></StyledLayerDescriptor>";
  612. public void onChange$cbClassificationGroup(Event event) {
  613. if (mapLayer != null) {
  614. mapLayer.setData("classificationSelection", new Integer(cbClassificationGroup.getSelectedIndex()));
  615. String baseUri = mapLayer.getUri();
  616. int pos = baseUri.indexOf("&sld_body=");
  617. if (pos > 0) {
  618. baseUri = baseUri.substring(0, pos);
  619. }
  620. String layername = mapLayer.getName();
  621. int n = cbClassificationGroup.getSelectedIndex();
  622. if (n > 0) {
  623. try {
  624. String sldBodyParam = "&sld_body=" + formatSld(URLEncoder.encode(polygonSld, "UTF-8"), layername, String.valueOf(n - 1), String.valueOf(n), String.valueOf(n), String.valueOf(n + 1));
  625. mapLayer.setUri(baseUri + sldBodyParam);
  626. } catch (Exception e) {
  627. e.printStackTrace();
  628. }
  629. } else {
  630. mapLayer.setUri(baseUri);
  631. }
  632. getMapComposer().reloadMapLayerNowAndIndexes(mapLayer);
  633. }
  634. }
  635. private String formatSld(String sld, String layername, String min_minus_one, String min, String max, String max_plus_one) {
  636. return sld.replace(SUB_LAYERNAME, layername).replace(SUB_MIN_MINUS_ONE, min_minus_one).replace(SUB_MIN, min).replace(SUB_MAX_PLUS_ONE, max_plus_one);
  637. }
  638. public Integer getClassificationGroupCount(String pid) {
  639. Integer i = 0;
  640. try {
  641. StringBuffer sbProcessUrl = new StringBuffer();
  642. sbProcessUrl.append(CommonData.satServer + "/output/aloc/" + pid + "/classification_means.csv");
  643. HttpClient client = new HttpClient();
  644. GetMethod get = new GetMethod(sbProcessUrl.toString());
  645. get.addRequestHeader("Accept", "text/plain");
  646. int result = client.executeMethod(get);
  647. String slist = get.getResponseBodyAsString();
  648. String[] s = slist.split("\n");
  649. i = s.length - 1;
  650. } catch (Exception e) {
  651. e.printStackTrace();
  652. }
  653. return i;
  654. }
  655. public void onClick$btnAnimationStart(Event event) {
  656. try {
  657. Integer monthOrYear = 0; //0=month, 1=year
  658. if("1".equals(cbAnimationDenomination.getValue()) || "Year".equalsIgnoreCase(cbAnimationDenomination.getValue())){
  659. monthOrYear = 1;
  660. }
  661. System.out.println("Animation: " + monthOrYear);
  662. Integer step = 1;
  663. if(monthOrYear != 0){
  664. step = intAnimationStep.getValue();
  665. if(step < 1) {
  666. step = 1;
  667. intAnimationStep.setValue(1);
  668. }
  669. }
  670. Double interval = dblAnimationSeconds.getValue();
  671. if(interval < 0.2) {
  672. interval = 0.2;
  673. dblAnimationSeconds.setValue(0.2);
  674. }
  675. mapLayer.setData("animation_step", step);
  676. mapLayer.setData("animation_interval", interval);
  677. Integer start = (Integer) intAnimationYearStart.getValue();
  678. Integer end = (Integer) intAnimationYearEnd.getValue();
  679. String script = "mapFrame.animateStart('" + StringEscapeUtils.escapeJavaScript(mapLayer.getNameJS()) + "',"
  680. + monthOrYear + ","
  681. + interval * 1000 + ","
  682. + start + ","
  683. + end + ","
  684. + step + ");";
  685. System.out.println("Script: " + script );
  686. getMapComposer().getOpenLayersJavascript().execute(script);
  687. btnAnimationStop.setDisabled(false);
  688. } catch (Exception e) {
  689. e.printStackTrace();
  690. }
  691. }
  692. public void selectYearOrMonth() {
  693. if("1".equals(cbAnimationDenomination.getValue()) || "Year".equals(cbAnimationDenomination.getValue())){
  694. dAnimationStep.setVisible(true);
  695. //lblAnimationLabel.setVisible(true);
  696. } else {
  697. dAnimationStep.setVisible(false);
  698. //lblAnimationLabel.setVisible(false);
  699. }
  700. }
  701. private void updateAnimationDiv() {
  702. if(dblAnimationSeconds == null) {
  703. return;
  704. }
  705. try {
  706. Query q = (Query) mapLayer.getData("query");
  707. if(q != null && q instanceof BiocacheQuery) {
  708. Integer firstYear = (Integer) mapLayer.getData("first_year");
  709. Integer lastYear = (Integer) mapLayer.getData("last_year");
  710. if(firstYear == null) {
  711. try {
  712. LegendObject lo = ((BiocacheQuery)q).getLegend("occurrence_year");
  713. if(lo != null && lo.getMinMax() != null) {
  714. firstYear = (int) lo.getMinMax()[0];
  715. lastYear = (int) lo.getMinMax()[1];
  716. mapLayer.setData("first_year", firstYear);
  717. mapLayer.setData("last_year", lastYear);
  718. }
  719. } catch (Exception e) {
  720. //this will fail if there are no records
  721. }
  722. }
  723. Integer step = (Integer) mapLayer.getData("animation_step");
  724. if(step != null) {
  725. intAnimationStep.setValue(step);
  726. }
  727. Double interval = (Double) mapLayer.getData("animation_interval");
  728. if(interval != null) {
  729. dblAnimationSeconds.setValue(interval);
  730. }
  731. if(firstYear < lastYear) {
  732. //lblAnimationLabel.setValue("years " + firstYear + " to " + lastYear);
  733. intAnimationYearStart.setValue(firstYear);
  734. intAnimationYearEnd.setValue(lastYear);
  735. divAnimation.setVisible(true);
  736. }
  737. }
  738. } catch (Exception e) {
  739. e.printStackTrace();
  740. }
  741. }
  742. public void onClick$btnAnimationStop(Event event) {
  743. String script = "mapFrame.animateStop('" + StringEscapeUtils.escapeJavaScript(mapLayer.getNameJS()) + "');";
  744. getMapComposer().getOpenLayersJavascript().execute(script);
  745. btnAnimationStop.setDisabled(true);
  746. }
  747. }