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

http://alageospatialportal.googlecode.com/ · Java · 169 lines · 8 code · 5 blank · 156 comment · 0 complexity · a0bfc26a85d32fad7f531bbcaf876f0c 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 org.ala.layers.intersect.SimpleRegion;
  16. /**
  17. * Generates a integer-based domain grid based on a set of grid values
  18. *
  19. * 1 for positive intersecting values -9999 for no intersecting values
  20. *
  21. * @author ajay
  22. */
  23. public class DomainGrid {
  24. public static void generate(String envPath, Layer[] layers, SimpleRegion region, String outputdir) {
  25. // TabulationSettings.load();
  26. // TabulationSettings.index_path = "/Users/ajay/Downloads/gdm/test/work/";
  27. // TabulationSettings.environmental_data_path = "/Users/ajay/Downloads/gdm/test/layers/";
  28. // TabulationSettings.grd_xmin = 112.900000;
  29. // TabulationSettings.grd_ymin = -43.800000;
  30. // TabulationSettings.grd_xmax = 153.640000;
  31. // TabulationSettings.grd_ymax = -9.000000;
  32. // TabulationSettings.grd_ncols = 4074;
  33. // TabulationSettings.grd_nrows = 3480;
  34. // TabulationSettings.grd_xdiv = 0.010000;
  35. // TabulationSettings.grd_ydiv = 0.010000;
  36. // //mkdir in index location
  37. // String newPath = null;
  38. // try {
  39. // newPath = TabulationSettings.index_path + System.currentTimeMillis() + java.io.File.separator;
  40. // File directory = new File(newPath);
  41. // directory.mkdir();
  42. // } catch (Exception e) {
  43. // e.printStackTrace();
  44. // }
  45. /*
  46. * get data, remove missing values, restrict by optional region
  47. */
  48. // int i;
  49. // int width;
  50. // int height;
  51. // int xmin;
  52. // int ymin;
  53. // int xmax;
  54. // int ymax;
  55. // String layerPath = envPath;
  56. // if (layerPath == null) {
  57. // layerPath = "";
  58. // }
  59. // Grid grid = null;
  60. //
  61. // //identify cells to keep
  62. // int[][] cells = (int[][]) region.getAttribute("cells");
  63. // if (cells == null) {
  64. // cells = region.getOverlapGridCells(
  65. // TabulationSettings.grd_xmin, TabulationSettings.grd_ymin,
  66. // TabulationSettings.grd_xmax, TabulationSettings.grd_ymax,
  67. // TabulationSettings.grd_ncols, TabulationSettings.grd_nrows,
  68. // null);
  69. // }
  70. //
  71. // //find minx, miny, width and height
  72. // xmin = TabulationSettings.grd_ncols;
  73. // ymin = TabulationSettings.grd_nrows;
  74. // xmax = 0;
  75. // ymax = 0;
  76. // for (i = 0; i < cells.length; i++) {
  77. // if (cells[i][0] < xmin) {
  78. // xmin = cells[i][0];
  79. // }
  80. // if (cells[i][1] < ymin) {
  81. // ymin = cells[i][1];
  82. // }
  83. // if (cells[i][0] > xmax) {
  84. // xmax = cells[i][0];
  85. // }
  86. // if (cells[i][1] > ymax) {
  87. // ymax = cells[i][1];
  88. // }
  89. // }
  90. //
  91. // width = xmax - xmin + 1;
  92. // height = ymax - ymin + 1;
  93. //
  94. // //layer output container
  95. // int[] dfiltered = new int[width * height];
  96. //
  97. // // initially set all the grid cell values
  98. // // to 1, and when iterating thru' layers
  99. // // if there is a no_data then it gets set
  100. // for (i = 0; i < dfiltered.length; i++) {
  101. // dfiltered[i] = 1;
  102. // }
  103. //
  104. // //process layers
  105. // for (Layer l : layers) {
  106. // System.out.println("loading predictor: " + layerPath + l.name);
  107. // grid = Grid.getGrid(layerPath + l.name);
  108. //// grid = new Grid(layerPath + l.name, false);
  109. //
  110. // float[] d = grid.getGrid(); //get whole layer
  111. //
  112. // //Translate between data source grid and output grid
  113. // int xoff = (int) ((grid.xmin - (xmin * TabulationSettings.grd_xdiv + TabulationSettings.grd_xmin)) / TabulationSettings.grd_xdiv);
  114. // int yoff = (int) ((grid.ymin - (TabulationSettings.grd_ymin + ymin * TabulationSettings.grd_ydiv)) / TabulationSettings.grd_ydiv);
  115. //
  116. // //popuplate missing values
  117. // if (cells == null) {
  118. // //TODO: common grids source
  119. // //grid.writeGrid(newPath + l.name, d,xmin,ymin,xmax,ymax, TabulationSettings.grd_xdiv, TabulationSettings.grd_ydiv,height,width);
  120. // } else {
  121. // for (i = 0; i < cells.length; i++) {
  122. // int x = cells[i][0] - xmin - xoff;
  123. // int y = cells[i][1] - ymin - yoff;
  124. // if (x >= 0 && x < grid.ncols
  125. // && y >= 0 && y < grid.nrows) {
  126. // int pSrc = x + (grid.nrows - y - 1) * grid.ncols;
  127. // int pDest = (cells[i][0] - xmin) + (height - (cells[i][1] - ymin) - 1) * width;
  128. // if (Float.isNaN(d[pSrc])) {
  129. // dfiltered[pDest] = -9999;
  130. // }
  131. // }
  132. // }
  133. // }
  134. // }
  135. //
  136. // double minx = Math.rint(xmin + TabulationSettings.grd_xmin / TabulationSettings.grd_xdiv) * TabulationSettings.grd_xdiv;
  137. // double miny = Math.rint(ymin + TabulationSettings.grd_ymin / TabulationSettings.grd_ydiv) * TabulationSettings.grd_ydiv;
  138. // double maxx = Math.rint(width + minx / TabulationSettings.grd_xdiv) * TabulationSettings.grd_xdiv;
  139. // double maxy = Math.rint(height + miny / TabulationSettings.grd_ydiv) * TabulationSettings.grd_ydiv;
  140. // grid.writeGrid(outputdir + "domain", dfiltered,
  141. // minx,
  142. // miny,
  143. // maxx,
  144. // maxy,
  145. // TabulationSettings.grd_xdiv, TabulationSettings.grd_ydiv, height, width);
  146. }
  147. /**
  148. * @param args the command line arguments
  149. */
  150. public static void main(String[] args) {
  151. // List<Layer> layerlist = new ArrayList<Layer>();
  152. //
  153. // layerlist.add(new Layer("evap_mean", "Evaporation - average", "", "environmental", null));
  154. // layerlist.add(new Layer("evapm", "Evaporation - annual mean", "", "environmental", null));
  155. //
  156. // Layer[] layers = layerlist.toArray(new Layer[layerlist.size()]);
  157. //
  158. // String area = "POLYGON((89.264 -50.06,89.264 6.322,178.736 6.322,178.736 -50.06,89.264 -50.06))";
  159. //
  160. // SimpleRegion region = SimpleShapeFile.parseWKT(area);
  161. //
  162. // generate(layers, region);
  163. }
  164. }