/alaspatial/src/main/java/org/ala/spatial/util/AnalysisJobAloc.java

http://alageospatialportal.googlecode.com/ · Java · 338 lines · 259 code · 49 blank · 30 comment · 47 complexity · 9b1d52c969443451cba0ccbb7a899d26 MD5 · raw file

  1. /**
  2. * ************************************************************************
  3. * Copyright (C) 2010 Atlas of Living Australia All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with the
  7. * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  11. * the specific language governing rights and limitations under the License.
  12. * *************************************************************************
  13. */
  14. package org.ala.spatial.util;
  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileReader;
  18. import java.util.Arrays;
  19. import org.ala.layers.intersect.SimpleRegion;
  20. import org.ala.spatial.analysis.index.LayerFilter;
  21. import org.ala.spatial.analysis.service.AlocServiceImpl;
  22. import org.ala.spatial.analysis.service.AlocSettings;
  23. /**
  24. * Use to run ALOC requests.
  25. *
  26. * @author Adam
  27. */
  28. public class AnalysisJobAloc extends AnalysisJob {
  29. Layer[] layers;
  30. int numberOfGroups;
  31. SimpleRegion region;
  32. LayerFilter[] envelope;
  33. String filename;
  34. String filepath;
  35. String currentPath;
  36. int cells;
  37. long[] stageTimes;
  38. public String area;
  39. String envlist;
  40. int layerCount;
  41. String resolution;
  42. public AnalysisJobAloc(String pid, String currentpath_, String envlist_, int numberofgroups_, SimpleRegion region_, LayerFilter[] envelope_, String area_, String resolution_) {
  43. super(pid);
  44. currentPath = currentpath_;
  45. numberOfGroups = numberofgroups_;
  46. //layers = layers_;
  47. envlist = envlist_;
  48. region = region_;
  49. envelope = envelope_;
  50. area = area_;
  51. resolution = resolution_;
  52. layerCount = envlist.split(":").length;
  53. if (region != null) {
  54. cells = (int) Math.ceil((region.getWidth() / Double.parseDouble(resolution))
  55. * (region.getHeight() / Double.parseDouble(resolution)));
  56. } else {
  57. cells = 1000000; //or something
  58. }
  59. stageTimes = new long[4];
  60. setStage(0);
  61. setProgress(0);
  62. }
  63. void exportResults() {
  64. setStage(3); //exporting results
  65. String line;
  66. try {
  67. //get extents from aloc run
  68. StringBuffer extents = new StringBuffer();
  69. BufferedReader br = new BufferedReader(new FileReader(filepath + "extents.txt"));
  70. int width = Integer.parseInt(br.readLine());
  71. int height = Integer.parseInt(br.readLine());
  72. double xmin = Double.parseDouble(br.readLine());
  73. double ymin = Double.parseDouble(br.readLine());
  74. double xmax = Double.parseDouble(br.readLine());
  75. double ymax = Double.parseDouble(br.readLine());
  76. br.close();
  77. br = new BufferedReader(new FileReader(filepath + "extents.txt"));
  78. while ((line = br.readLine()) != null) {
  79. extents.append(line).append("\n");
  80. }
  81. br.close();
  82. setProgress(0.2);
  83. CoordinateTransformer.generateWorldFiles(filepath, "aloc",
  84. String.valueOf((xmax - xmin) / (double) width),
  85. "-" + String.valueOf((ymax - ymin) / (double) height),
  86. String.valueOf(xmin),
  87. String.valueOf(ymax));
  88. setProgress(0.3);
  89. String outputfile = CoordinateTransformer.transformToGoogleMercator(filepath + "aloc.png");
  90. setProgress(0.4);
  91. AnalysisJobMaxent.readReplace(filepath + "classification.html", "<insert job number here>", getName());
  92. /* register with LayerImgService */
  93. StringBuffer legend = new StringBuffer();
  94. BufferedReader flegend = new BufferedReader(new FileReader(filepath + "classification_means.csv"));
  95. while ((line = flegend.readLine()) != null) {
  96. legend.append(line);
  97. legend.append("\r\n");
  98. }
  99. flegend.close();
  100. StringBuffer metadata = new StringBuffer();
  101. BufferedReader fmetadata = new BufferedReader(new FileReader(filepath + "classification.html"));
  102. while ((line = fmetadata.readLine()) != null) {
  103. metadata.append(line);
  104. metadata.append("\n");
  105. }
  106. fmetadata.close();
  107. // generate the readme.txt file
  108. CitationService.generateClassificationReadme(filepath, "Classification");
  109. setProgress(0.5);
  110. //publish layer
  111. String url = (String) AlaspatialProperties.getGeoserverUrl()
  112. + "/rest/workspaces/ALA/coveragestores/aloc_"
  113. + getName()
  114. + "/file.arcgrid?coverageName=aloc_"
  115. + getName();
  116. String extra = "";
  117. String username = (String) AlaspatialProperties.getGeoserverUsername();
  118. String password = (String) AlaspatialProperties.getGeoserverPassword();
  119. String[] infiles = {filepath + "aloc.asc", filepath + "aloc.prj"};
  120. String ascZipFile = filepath + getName() + ".asc.zip";
  121. Zipper.zipFiles(infiles, ascZipFile);
  122. // Upload the file to GeoServer using REST calls
  123. UploadSpatialResource.loadResource(url, extra, username, password, ascZipFile);
  124. //Create style
  125. url = (String) AlaspatialProperties.getGeoserverUrl() + "/rest/styles/";
  126. UploadSpatialResource.loadCreateStyle(url, extra, username, password, "aloc_" + getName());
  127. //Upload sld
  128. url = (String) AlaspatialProperties.getGeoserverUrl() + "/rest/styles/aloc_" + getName();
  129. UploadSpatialResource.loadSld(url, extra, username, password, filepath + "aloc.sld");
  130. //Apply style
  131. String data = "<layer><enabled>true</enabled><defaultStyle><name>aloc_" + getName() + "</name></defaultStyle></layer>";
  132. url = (String) AlaspatialProperties.getGeoserverUrl() + "/rest/layers/ALA:aloc_" + getName();
  133. UploadSpatialResource.assignSld(url, extra, username, password, data);
  134. setProgress(1);
  135. } catch (Exception e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. @Override
  140. public long getEstimate() {
  141. //if(getProgress() == 0) return 0;
  142. if (getStage() < 0 || getStage() >= stageTimes.length) {
  143. return 0;
  144. }
  145. long timeElapsed;
  146. long t1 = 0, t2 = 0, t3 = 0, t4 = 0;
  147. double prog;
  148. long progTime;
  149. synchronized (progress) {
  150. progTime = progressTime;
  151. timeElapsed = progressTime - stageTimes[getStage()];
  152. prog = progress;
  153. }
  154. long timeRemaining = 0;
  155. if (stage <= 0) { //data load; 0 to 0.2
  156. if (prog > 0.2) {
  157. t1 = (long) (timeElapsed * (.2 - prog) / prog); //projected
  158. }
  159. if (t1 <= 0 || prog <= 0.2) {
  160. t1 = (long) (cells * AlaspatialProperties.getAnalysisAlocEstimateMult0() * layerCount
  161. + AlaspatialProperties.getAnalysisAlocEstimateAdd0()); //default
  162. }
  163. }
  164. if (stage <= 2) { //seeding and iterations
  165. if (prog > 0.22) {
  166. t2 = (long) ((cells * AlaspatialProperties.getAnalysisAlocEstimateMult1()) * (double) layerCount * numberOfGroups
  167. + AlaspatialProperties.getAnalysisAlocEstimateAdd1())
  168. + (long) ((cells * AlaspatialProperties.getAnalysisAlocEstimateMult2()) * (double) numberOfGroups * layerCount * layerCount
  169. + AlaspatialProperties.getAnalysisAlocEstimateAdd2()); //default
  170. t2 = t2 + progTime - stageTimes[1];
  171. }
  172. if (t2 <= 0 || prog <= 0.22) {
  173. t2 = (long) ((cells * AlaspatialProperties.getAnalysisAlocEstimateMult1()) * (double) layerCount * numberOfGroups
  174. + AlaspatialProperties.getAnalysisAlocEstimateAdd1())
  175. + (long) ((cells * AlaspatialProperties.getAnalysisAlocEstimateMult2()) * (double) numberOfGroups * layerCount * layerCount
  176. + AlaspatialProperties.getAnalysisAlocEstimateAdd2()); //default
  177. }
  178. }
  179. if (stage <= 3) { //transforming data; 0.9 to 1.0
  180. if (prog > 0.9) {
  181. //t4 = (long) (timeElapsed * (.1 - (prog-.9))/(prog-.9)); //projected
  182. t4 = (long) (2000 * AlaspatialProperties.getAnalysisAlocEstimateMult3()); //default
  183. t4 = t4 + progTime - stageTimes[3];
  184. }
  185. if (t4 <= 0 || prog <= 0.9) {
  186. t4 = (long) (2000 * AlaspatialProperties.getAnalysisAlocEstimateMult3()); //default
  187. }
  188. }
  189. timeRemaining = t1 + t2 + t3 + t4;
  190. return smoothEstimate(timeRemaining);
  191. }
  192. @Override
  193. public void setProgress(double d) {
  194. if (stage == 0) { //data load; 0 to 0.2
  195. progress = d / 5;
  196. } else if (stage == 1 || stage == 2) { //seeding and iterations; 0.2 to 0.9
  197. progress = 0.2 + d * 7 / 10;
  198. } else { //transforming data; 0.9 to 1.0
  199. progress = 0.9 + d / 10;
  200. }
  201. super.setProgress(progress);
  202. }
  203. @Override
  204. public String getStatus() {
  205. if (getProgress() < 1) {
  206. String msg;
  207. if (stage == 0) { //data load; 0 to 0.2
  208. msg = "Data preparation, ";
  209. } else if (stage == 1) { //seeding; 0.2 to 0.3
  210. msg = "Running ALOC, ";
  211. } else if (stage == 2) { //iterations; 0.3 to 0.9
  212. msg = "Running ALOC, ";
  213. } else { //transforming data; 0.9 to 1.0
  214. msg = "Exporting results, ";
  215. }
  216. return msg + "est remaining: " + getEstimateInMinutes() + " min";
  217. } else {
  218. if (stage == -1) {
  219. return "not started, est: " + getEstimateInMinutes() + " min";
  220. } else {
  221. return "finished, total run time=" + Math.round(getRunTime() / 1000) + "s";
  222. }
  223. }
  224. }
  225. @Override
  226. public void setStage(int i) {
  227. super.setStage(i);
  228. if (i < 4) {
  229. stageTimes[i] = System.currentTimeMillis();
  230. }
  231. }
  232. public void setCells(int i) {
  233. cells = i;
  234. }
  235. public String toString() {
  236. StringBuffer sb = new StringBuffer();
  237. sb.append(getName());
  238. sb.append("; Classification");
  239. sb.append("; state=").append(getCurrentState());
  240. sb.append("; status=").append(getStatus());
  241. sb.append("; grid cell count=").append(cells);
  242. sb.append("; number of groups=").append(numberOfGroups);
  243. sb.append("; number of layers=").append(layerCount);
  244. return sb.toString();
  245. }
  246. public String getImage() {
  247. return "output/aloc/" + getName() + "/t_aloc.png";
  248. }
  249. public void run() {
  250. try {
  251. long start = System.currentTimeMillis();
  252. setCurrentState(RUNNING);
  253. setStage(0);
  254. filepath = currentPath + "output" + File.separator + "aloc" + File.separator + getName() + File.separator;
  255. filename = filepath + "aloc.png";
  256. File fDir = new File(filepath);
  257. fDir.mkdir();
  258. String[] envnameslist = envlist.split(":");
  259. String cutDataPath = GridCutter.cut2(envnameslist, resolution, region, envelope, null);
  260. AlocSettings msets = new AlocSettings();
  261. msets.setAlocPath(AlaspatialProperties.getAnalysisAlocCmd());
  262. msets.setEnvList(Arrays.asList(envnameslist));
  263. msets.setNumberOfGroups(numberOfGroups);
  264. msets.setEnvPath(cutDataPath); //use (possibly) cut layers
  265. msets.setOutputPath(currentPath + "output" + File.separator + "aloc" + File.separator + getName() + File.separator);
  266. AlocServiceImpl aloc = new AlocServiceImpl();
  267. aloc.setAlocSettings(msets);
  268. System.out.println("To run: " + msets.toString());
  269. setStage(1);
  270. setProgress(0, "running Aloc");
  271. int exitValue = aloc.process(this);
  272. System.out.println("Completed: " + exitValue);
  273. setProgress(1, "Aloc finished with exit value=" + exitValue);
  274. if (isCancelled()) {
  275. return;
  276. }
  277. exportResults();
  278. long end = System.currentTimeMillis();
  279. setRunTime(end - start);
  280. setCurrentState(SUCCESSFUL);
  281. //write out infor for adjusting input parameters
  282. System.out.println("ALOC:" + cells + "," + numberOfGroups + "," + layerCount + "," + resolution + " " + (stageTimes[1] - stageTimes[0]) + " " + (stageTimes[2] - stageTimes[0]) + " " + (stageTimes[3] - stageTimes[2]) + " " + (end - stageTimes[3]));
  283. } catch (Exception e) {
  284. setProgress(1, "failed: " + e.toString());
  285. setCurrentState(FAILED);
  286. e.printStackTrace();
  287. }
  288. }
  289. }