/webportal/src/main/java/org/ala/spatial/analysis/web/ImportAnalysisController.java

http://alageospatialportal.googlecode.com/ · Java · 515 lines · 416 code · 79 blank · 20 comment · 52 complexity · 8326246832cefa7aaf0a722a6e7e54e6 MD5 · raw file

  1. package org.ala.spatial.analysis.web;
  2. import au.org.emii.portal.composer.UtilityComposer;
  3. import au.org.emii.portal.menu.MapLayer;
  4. import au.org.emii.portal.menu.MapLayerMetadata;
  5. import au.org.emii.portal.settings.SettingsSupplementary;
  6. import au.org.emii.portal.util.LayerUtilities;
  7. import au.org.emii.portal.wms.WMSStyle;
  8. import java.net.URL;
  9. import org.ala.logger.client.RemoteLogger;
  10. import org.ala.spatial.util.CommonData;
  11. import org.apache.commons.httpclient.HttpClient;
  12. import org.apache.commons.httpclient.methods.GetMethod;
  13. import org.zkoss.zk.ui.Executions;
  14. import org.zkoss.zk.ui.event.Event;
  15. import org.zkoss.zul.Filedownload;
  16. import org.zkoss.zul.Textbox;
  17. /**
  18. *
  19. * @author ajay
  20. */
  21. public class ImportAnalysisController extends UtilityComposer {
  22. SettingsSupplementary settingsSupplementary;
  23. RemoteLogger remoteLogger;
  24. Textbox refNum;
  25. String pid;
  26. boolean isAloc = false;
  27. boolean isMaxent = false;
  28. boolean isSxS = false;
  29. boolean isGdm = false;
  30. boolean sxsSitesBySpecies = false;
  31. boolean sxsOccurrenceDensity = false;
  32. boolean sxsSpeciesDensity = false;
  33. String[] gdmEnvlist;
  34. @Override
  35. public void afterCompose() {
  36. super.afterCompose();
  37. }
  38. public void onClick$btnOk(Event event) {
  39. pid = refNum.getValue();
  40. pid = pid.trim();
  41. if (getParametersAloc()) {
  42. isAloc = true;
  43. openProgressBarAloc();
  44. remoteLogger.logMapAnalysis("Import Classification", "Tool - Restore", "", "", "", pid, "classification", "IMPORTED");
  45. } else if (getParametersMaxent()) {
  46. isMaxent = true;
  47. openProgressBarMaxent();
  48. remoteLogger.logMapAnalysis("Import Prediction", "Tool - Restore", "", "", "", pid, "prediction", "IMPORTED");
  49. } else if (getParametersSxS()) {
  50. isSxS = true;
  51. openProgressBarSxS();
  52. remoteLogger.logMapAnalysis("Import Species to Grid", "Tool - Restore", "", "", "", pid, "species to grid", "IMPORTED");
  53. } else if (getParametersGdm()) {
  54. isGdm = true;
  55. openProgressBarGdm();
  56. remoteLogger.logMapAnalysis("Import GDM", "Tool - Restore", "", "", "", pid, "gdm", "IMPORTED");
  57. } else {
  58. getMapComposer().showMessage("Invalid reference number.");
  59. }
  60. }
  61. public void onClick$btnCancel(Event event) {
  62. this.detach();
  63. }
  64. boolean getParametersAloc() {
  65. String txt = get("inputs");
  66. try {
  67. int pos = 0;
  68. int p1 = txt.indexOf("pid:", pos);
  69. if (p1 < 0) {
  70. return false;
  71. }
  72. int p2 = txt.indexOf("gc:", pos);
  73. if (p2 < 0) {
  74. return false;
  75. }
  76. int p3 = txt.indexOf("area:", pos);
  77. int p4 = txt.indexOf("envlist:", pos);
  78. int p5 = txt.indexOf("pid:", p1 + 4);
  79. if (p5 < 0) {
  80. p5 = txt.length();
  81. }
  82. pos = p5 - 5;
  83. String pid = txt.substring(p1 + 4, p2).trim();
  84. String gc = txt.substring(p2 + 3, p3).trim();
  85. String area = txt.substring(p3 + 5, p4).trim();
  86. String envlist = txt.substring(p4 + 8, p5).trim();
  87. //remove ';' from end
  88. if (gc.endsWith(";")) {
  89. gc = gc.substring(0, gc.length() - 1);
  90. }
  91. if (area.endsWith(";")) {
  92. area = area.substring(0, area.length() - 1);
  93. }
  94. if (envlist.endsWith(";")) {
  95. envlist = envlist.substring(0, envlist.length() - 1);
  96. }
  97. System.out.println("got [" + pid + "][" + gc + "][" + area + "][" + envlist + "]");
  98. //apply job input parameters to selection
  99. // groupCount.setValue(Integer.parseInt(gc));
  100. //
  101. // lbListLayers.clearSelection();
  102. // lbListLayers.selectLayers(envlist.split(":"));
  103. return true;
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. return false;
  108. }
  109. String get(String type) {
  110. try {
  111. StringBuffer sbProcessUrl = new StringBuffer();
  112. sbProcessUrl.append(CommonData.satServer + "/ws/jobs/").append(type).append("?pid=").append(pid);
  113. HttpClient client = new HttpClient();
  114. GetMethod get = new GetMethod(sbProcessUrl.toString());
  115. get.addRequestHeader("Accept", "text/plain");
  116. int result = client.executeMethod(get);
  117. String slist = get.getResponseBodyAsString();
  118. return slist;
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. }
  122. return "";
  123. }
  124. void openProgressBarAloc() {
  125. ProgressWCController window = (ProgressWCController) Executions.createComponents("WEB-INF/zul/AnalysisProgress.zul", getMapComposer(), null);
  126. window.parent = this;
  127. window.start(pid, "Classification");
  128. try {
  129. window.doModal();
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. }
  133. }
  134. public void loadMap(Event event) {
  135. if (isAloc) {
  136. loadMapAloc(event);
  137. } else if (isMaxent) {
  138. loadMapMaxent(event);
  139. } else if (isSxS) {
  140. loadMapSxS(event);
  141. }
  142. }
  143. public void loadMapAloc(Event event) {
  144. String layerLabel = "Classification - " + pid;
  145. String mapurl = CommonData.geoServer + "/wms?service=WMS&version=1.1.0&request=GetMap&layers=ALA:aloc_" + pid + "&FORMAT=image%2Fpng";
  146. String legendurl = CommonData.geoServer
  147. + "/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=10&HEIGHT=1"
  148. + "&LAYER=ALA:aloc_" + pid;
  149. System.out.println(legendurl);
  150. getMapComposer().addWMSLayer("aloc_" + pid, layerLabel, mapurl, (float) 0.5, null, legendurl, LayerUtilities.ALOC, null, null);
  151. MapLayer mapLayer = getMapComposer().getMapLayer("aloc_" + pid);
  152. mapLayer.setData("pid", pid);
  153. if (mapLayer != null) {
  154. WMSStyle style = new WMSStyle();
  155. style.setName("Default");
  156. style.setDescription("Default style");
  157. style.setTitle("Default");
  158. style.setLegendUri(legendurl);
  159. System.out.println("legend:" + legendurl);
  160. mapLayer.addStyle(style);
  161. mapLayer.setSelectedStyleIndex(1);
  162. MapLayerMetadata md = mapLayer.getMapLayerMetadata();
  163. if (md == null) {
  164. md = new MapLayerMetadata();
  165. }
  166. String infoUrl = CommonData.satServer + "/output/layers/" + pid + "/metadata.html" + "\nClassification output\npid:" + pid;
  167. md.setMoreInfo(infoUrl);
  168. md.setId(Long.valueOf(pid));
  169. getMapComposer().updateLayerControls();
  170. try {
  171. // set off the download as well
  172. String fileUrl = CommonData.satServer + "/ws/download/" + pid;
  173. Filedownload.save(new URL(fileUrl).openStream(), "application/zip", layerLabel.replaceAll(" ", "_") + ".zip"); // "ALA_Prediction_"+pid+".zip"
  174. } catch (Exception ex) {
  175. System.out.println("Error generating download for classification model:");
  176. ex.printStackTrace(System.out);
  177. }
  178. }
  179. this.detach();
  180. }
  181. double[] getExtents() {
  182. double[] d = new double[6];
  183. try {
  184. StringBuffer sbProcessUrl = new StringBuffer();
  185. sbProcessUrl.append(CommonData.satServer + "/output/aloc/" + pid + "/aloc.pngextents.txt");
  186. HttpClient client = new HttpClient();
  187. GetMethod get = new GetMethod(sbProcessUrl.toString());
  188. get.addRequestHeader("Accept", "text/plain");
  189. int result = client.executeMethod(get);
  190. String slist = get.getResponseBodyAsString();
  191. System.out.println("getExtents:" + slist);
  192. String[] s = slist.split("\n");
  193. for (int i = 0; i < 6 && i < s.length; i++) {
  194. d[i] = Double.parseDouble(s[i]);
  195. }
  196. } catch (Exception e) {
  197. e.printStackTrace();
  198. }
  199. return d;
  200. }
  201. String getJob(String type) {
  202. try {
  203. StringBuffer sbProcessUrl = new StringBuffer();
  204. sbProcessUrl.append(CommonData.satServer + "/ws/jobs/").append(type).append("?pid=").append(pid);
  205. System.out.println(sbProcessUrl.toString());
  206. HttpClient client = new HttpClient();
  207. GetMethod get = new GetMethod(sbProcessUrl.toString());
  208. get.addRequestHeader("Accept", "text/plain");
  209. int result = client.executeMethod(get);
  210. String slist = get.getResponseBodyAsString();
  211. System.out.println(slist);
  212. return slist;
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. }
  216. return "";
  217. }
  218. void openProgressBarMaxent() {
  219. ProgressWCController window = (ProgressWCController) Executions.createComponents("WEB-INF/zul/AnalysisProgress.zul", getMapComposer(), null);
  220. window.parent = this;
  221. window.start(pid, "Prediction");
  222. try {
  223. window.doModal();
  224. } catch (Exception e) {
  225. e.printStackTrace();
  226. }
  227. }
  228. boolean getParametersMaxent() {
  229. String txt = get("inputs");
  230. try {
  231. int pos = 0;
  232. int p1 = txt.indexOf("pid:", pos);
  233. if (p1 < 0) {
  234. return false;
  235. }
  236. int p2 = txt.indexOf("taxonid:", pos);
  237. if (p2 < 0) {
  238. return false;
  239. }
  240. return true;
  241. } catch (Exception e) {
  242. e.printStackTrace();
  243. }
  244. return false;
  245. }
  246. boolean getParametersSxS() {
  247. String txt = get("inputs");
  248. try {
  249. int pos = 0;
  250. int p1 = txt.indexOf("pid:", pos);
  251. if (p1 < 0) {
  252. return false;
  253. }
  254. int p2 = txt.indexOf("gridsize:", pos);
  255. if (p2 < 0) {
  256. return false;
  257. }
  258. if (txt.indexOf("sitesbyspecies") > 0) {
  259. sxsSitesBySpecies = true;
  260. }
  261. if (txt.indexOf("occurrencedensity") > 0) {
  262. sxsOccurrenceDensity = true;
  263. }
  264. if (txt.indexOf("speciesdensity") > 0) {
  265. sxsSpeciesDensity = true;
  266. }
  267. return true;
  268. } catch (Exception e) {
  269. e.printStackTrace();
  270. }
  271. return false;
  272. }
  273. void openProgressBarSxS() {
  274. ProgressWCController window = (ProgressWCController) Executions.createComponents("WEB-INF/zul/AnalysisProgress.zul", getMapComposer(), null);
  275. window.parent = this;
  276. window.start(pid, "Points to Grid");
  277. try {
  278. window.doModal();
  279. } catch (Exception e) {
  280. e.printStackTrace();
  281. }
  282. }
  283. public void loadMapMaxent(Event event) {
  284. String mapurl = CommonData.geoServer + "/wms?service=WMS&version=1.1.0&request=GetMap&layers=ALA:species_" + pid + "&styles=alastyles&FORMAT=image%2Fpng";
  285. String legendurl = CommonData.geoServer
  286. + "/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=10&HEIGHT=1"
  287. + "&LAYER=ALA:species_" + pid
  288. + "&STYLE=alastyles";
  289. System.out.println(legendurl);
  290. //get job inputs
  291. String speciesName = "";
  292. try {
  293. for (String s : getJob("inputs").split(";")) {
  294. if (s.startsWith("scientificName")) {
  295. speciesName = s.split(":")[1];
  296. if (speciesName != null && speciesName.length() > 1) {
  297. speciesName = speciesName.substring(0, 1).toUpperCase() + speciesName.substring(1);
  298. }
  299. break;
  300. }
  301. }
  302. } catch (Exception e) {
  303. e.printStackTrace();
  304. }
  305. //taxon = "species";
  306. String layername = "Maxent - " + pid;
  307. getMapComposer().addWMSLayer("species_" + pid, layername, mapurl, (float) 0.5, null, legendurl, LayerUtilities.MAXENT, null, null);
  308. MapLayer ml = getMapComposer().getMapLayer("species_" + pid);
  309. ml.setData("pid", pid);
  310. String infoUrl = CommonData.satServer + "/output/maxent/" + pid + "/species.html";
  311. MapLayerMetadata md = ml.getMapLayerMetadata();
  312. if (md == null) {
  313. md = new MapLayerMetadata();
  314. ml.setMapLayerMetadata(md);
  315. }
  316. md.setMoreInfo(infoUrl + "\nMaxent Output\npid:" + pid);
  317. md.setId(Long.valueOf(pid));
  318. try {
  319. // set off the download as well
  320. String fileUrl = CommonData.satServer + "/ws/download/" + pid;
  321. Filedownload.save(new URL(fileUrl).openStream(), "application/zip", layername.replaceAll(" ", "_") + ".zip"); // "ALA_Prediction_"+pid+".zip"
  322. } catch (Exception ex) {
  323. System.out.println("Error generating download for prediction model:");
  324. ex.printStackTrace(System.out);
  325. }
  326. this.detach();
  327. //getMapComposer().showMessage("Reference number to retrieve results: " + pid);
  328. //showInfoWindow("/output/maxent/" + pid + "/species.html");
  329. }
  330. private void loadMapSxS(Event event) {
  331. try {
  332. if (sxsOccurrenceDensity) {
  333. String mapurl = CommonData.geoServer + "/wms?service=WMS&version=1.1.0&request=GetMap&layers=ALA:odensity_" + pid + "&styles=odensity_" + pid + "&FORMAT=image%2Fpng";
  334. String legendurl = CommonData.geoServer
  335. + "/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=10&HEIGHT=1"
  336. + "&LAYER=ALA:odensity_" + pid
  337. + "&STYLE=odensity_" + pid;
  338. System.out.println(legendurl);
  339. String layername = getMapComposer().getNextAreaLayerName("Occurrence Density");
  340. getMapComposer().addWMSLayer(pid + "_odensity", layername, mapurl, (float) 0.5, null, legendurl, LayerUtilities.ODENSITY, null, null);
  341. MapLayer ml = getMapComposer().getMapLayer(pid + "_odensity");
  342. ml.setData("pid", pid + "_odensity");
  343. String infoUrl = CommonData.satServer + "/output/sitesbyspecies/" + pid + "/odensity_metadata.html";
  344. MapLayerMetadata md = ml.getMapLayerMetadata();
  345. if (md == null) {
  346. md = new MapLayerMetadata();
  347. ml.setMapLayerMetadata(md);
  348. }
  349. md.setMoreInfo(infoUrl + "\nOccurrence Density\npid:" + pid);
  350. md.setId(Long.valueOf(pid));
  351. }
  352. if (sxsSpeciesDensity) {
  353. String mapurl = CommonData.geoServer + "/wms?service=WMS&version=1.1.0&request=GetMap&layers=ALA:srichness_" + pid + "&styles=srichness_" + pid + "&FORMAT=image%2Fpng";
  354. String legendurl = CommonData.geoServer
  355. + "/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=10&HEIGHT=1"
  356. + "&LAYER=ALA:srichness_" + pid
  357. + "&STYLE=srichness_" + pid;
  358. System.out.println(legendurl);
  359. // String layername = tToolName.getValue();
  360. String layername = getMapComposer().getNextAreaLayerName("Species Richness");
  361. getMapComposer().addWMSLayer(pid + "_srichness", layername, mapurl, (float) 0.5, null, legendurl, LayerUtilities.SRICHNESS, null, null);
  362. MapLayer ml = getMapComposer().getMapLayer(pid + "_srichness");
  363. ml.setData("pid", pid + "_srichness");
  364. String infoUrl = CommonData.satServer + "/output/sitesbyspecies/" + pid + "/srichness_metadata.html";
  365. MapLayerMetadata md = ml.getMapLayerMetadata();
  366. if (md == null) {
  367. md = new MapLayerMetadata();
  368. ml.setMapLayerMetadata(md);
  369. }
  370. md.setMoreInfo(infoUrl + "\nSpecies Richness\npid:" + pid);
  371. md.setId(Long.valueOf(pid));
  372. }
  373. // set off the download as well
  374. String fileUrl = CommonData.satServer + "/ws/download/" + pid;
  375. Filedownload.save(new URL(fileUrl).openStream(), "application/zip", "sites_by_species.zip");
  376. } catch (Exception ex) {
  377. System.out.println("Error generating download for prediction model:");
  378. ex.printStackTrace(System.out);
  379. }
  380. this.detach();
  381. }
  382. boolean getParametersGdm() {
  383. try {
  384. StringBuffer sbProcessUrl = new StringBuffer();
  385. //TODO: analysis output url into config
  386. sbProcessUrl.append(CommonData.satServer.replace("/alaspatial", "") + "/output/gdm/").append(pid).append("/ala.properties");
  387. HttpClient client = new HttpClient();
  388. GetMethod get = new GetMethod(sbProcessUrl.toString());
  389. get.addRequestHeader("Accept", "text/plain");
  390. int result = client.executeMethod(get);
  391. if (result == 200) {
  392. String slist = get.getResponseBodyAsString();
  393. for (String row : slist.split("\n")) {
  394. if (row.startsWith("envlist")) {
  395. gdmEnvlist = row.replace("envlist=", "").split("\\\\:");
  396. }
  397. }
  398. return true;
  399. }
  400. } catch (Exception e) {
  401. e.printStackTrace();
  402. }
  403. return false;
  404. }
  405. void openProgressBarGdm() {
  406. String[] envlist = gdmEnvlist;
  407. for (String env : envlist) {
  408. String mapurl = CommonData.geoServer + "/wms?service=WMS&version=1.1.0&request=GetMap&layers=ALA:gdm_" + env + "Tran_" + pid + "&styles=alastyles&FORMAT=image%2Fpng";
  409. String legendurl = CommonData.geoServer
  410. + "/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=10&HEIGHT=1"
  411. + "&LAYER=ALA:gdm_" + env + "Tran_" + pid
  412. + "&STYLE=alastyles";
  413. System.out.println(legendurl);
  414. String layername = "Tranformed " + CommonData.getLayerDisplayName(env);
  415. //System.out.println("Converting '" + env + "' to '" + layername.substring(10) + "' (" + CommonData.getFacetLayerDisplayName(CommonData.getLayerFacetName(env)) + ")");
  416. getMapComposer().addWMSLayer(pid + "_" + env, layername, mapurl, (float) 0.5, null, legendurl, LayerUtilities.GDM, null, null);
  417. MapLayer ml = getMapComposer().getMapLayer(pid + "_" + env);
  418. ml.setData("pid", pid + "_" + env);
  419. String infoUrl = CommonData.satServer + "/output/gdm/" + pid + "/gdm.html";
  420. MapLayerMetadata md = ml.getMapLayerMetadata();
  421. if (md == null) {
  422. md = new MapLayerMetadata();
  423. ml.setMapLayerMetadata(md);
  424. }
  425. md.setMoreInfo(infoUrl + "\nGDM Output\npid:" + pid);
  426. md.setId(Long.valueOf(pid));
  427. }
  428. String fileUrl = CommonData.satServer + "/ws/download/" + pid;
  429. try {
  430. Filedownload.save(new URL(fileUrl).openStream(), "application/zip", "gdm_" + pid + ".zip"); // "ALA_Prediction_"+pid+".zip"
  431. } catch (Exception e) {
  432. e.printStackTrace();
  433. }
  434. this.detach();
  435. }
  436. }