PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://alageospatialportal.googlecode.com/
Java | 717 lines | 510 code | 84 blank | 123 comment | 133 complexity | b69198513c8d42ded6a159cd5f32bfdd 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.File;
  16. import java.io.FileWriter;
  17. import java.util.ArrayList;
  18. import java.util.TreeMap;
  19. import org.ala.layers.client.Client;
  20. import org.ala.layers.intersect.Grid;
  21. import org.ala.layers.intersect.SimpleRegion;
  22. import org.ala.layers.util.SpatialUtil;
  23. import org.ala.spatial.analysis.index.LayerFilter;
  24. /**
  25. * Class for region cutting test data grids
  26. *
  27. * @author adam
  28. */
  29. public class GridCutter {
  30. public static ArrayList<Object> loadCutGridsForAloc(File[] files, String extentsFilename, int pieces, AnalysisJob job) {
  31. ArrayList<Object> data = new ArrayList<Object>();
  32. if (job != null) {
  33. job.setProgress(0);
  34. }
  35. //determine outer bounds of layers
  36. double xmin = Double.MAX_VALUE;
  37. double ymin = Double.MAX_VALUE;
  38. double xmax = Double.MAX_VALUE * -1;
  39. double ymax = Double.MAX_VALUE * -1;
  40. double xres = 0.01;
  41. double yres = 0.01;
  42. for (File f : files) {
  43. String gridFilename = f.getPath().substring(0, f.getPath().length() - 4);
  44. Grid g = new Grid(gridFilename);
  45. xres = g.xres;
  46. yres = g.xres;
  47. if (xmin > g.xmin) {
  48. xmin = g.xmin;
  49. }
  50. if (xmax < g.xmax) {
  51. xmax = g.xmax;
  52. }
  53. if (ymin > g.ymin) {
  54. ymin = g.ymin;
  55. }
  56. if (ymax < g.ymax) {
  57. ymax = g.ymax;
  58. }
  59. }
  60. if (files.length < 2) {
  61. if (job != null) {
  62. job.setCurrentState(AnalysisJob.FAILED);
  63. job.log("Fewer than two layers with postive range.");
  64. } else {
  65. SpatialLogger.log("Fewer than two layers with postive range.");
  66. }
  67. return null;
  68. }
  69. //determine range and width's
  70. double xrange = xmax - xmin;
  71. double yrange = ymax - ymin;
  72. int width = (int) Math.ceil(xrange / xres);
  73. int height = (int) Math.ceil(yrange / yres);
  74. //write extents into a file now
  75. if (extentsFilename != null) {
  76. try {
  77. FileWriter fw = new FileWriter(extentsFilename);
  78. fw.append(String.valueOf(width)).append("\n");
  79. fw.append(String.valueOf(height)).append("\n");
  80. fw.append(String.valueOf(xmin)).append("\n");
  81. fw.append(String.valueOf(ymin)).append("\n");
  82. fw.append(String.valueOf(xmax)).append("\n");
  83. fw.append(String.valueOf(ymax));
  84. fw.close();
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. if (job != null) {
  90. job.setProgress(0.1, "exported extents");
  91. }
  92. //make cells list for outer bounds
  93. int th = height;
  94. int tw = width;
  95. int tp = 0;
  96. int[][] cells = new int[tw * th][2];
  97. for (int i = 0; i < height; i++) {
  98. for (int j = 0; j < width; j++) {
  99. cells[tp][0] = j;
  100. cells[tp][1] = i;
  101. tp++;
  102. }
  103. }
  104. if (job != null) {
  105. job.setProgress(0.2, "determined target cells");
  106. }
  107. if (job != null) {
  108. job.log("Cut cells count: " + cells.length);
  109. } else {
  110. System.out.println("Cut cells count: " + cells.length);
  111. }
  112. //transform cells numbers to long/lat numbers
  113. double[][] points = new double[cells.length][2];
  114. for (int i = 0; i < cells.length; i++) {
  115. points[i][0] = xmin + cells[i][0] * xres;
  116. points[i][1] = ymin + cells[i][1] * yres;
  117. }
  118. //initialize data structure to hold everything
  119. // each data piece: row1[col1, col2, ...] row2[col1, col2, ...] row3...
  120. int remainingLength = cells.length;
  121. int step = (int) Math.floor(remainingLength / (double) pieces);
  122. for (int i = 0; i < pieces; i++) {
  123. if (i == pieces - 1) {
  124. data.add(new float[remainingLength * files.length]);
  125. } else {
  126. data.add(new float[step * files.length]);
  127. remainingLength -= step;
  128. }
  129. }
  130. //iterate for layers
  131. double[] layerExtents = new double[files.length * 2];
  132. for (int j = 0; j < files.length; j++) {
  133. String gridFilename = files[j].getPath().substring(0, files[j].getPath().length() - 4);
  134. Grid g = new Grid(gridFilename);
  135. float[] v = g.getValues2(points);
  136. //row range standardization
  137. float minv = Float.MAX_VALUE;
  138. float maxv = Float.MAX_VALUE * -1;
  139. for (int i = 0; i < v.length; i++) {
  140. if (v[i] < minv) {
  141. minv = v[i];
  142. }
  143. if (v[i] > maxv) {
  144. maxv = v[i];
  145. }
  146. }
  147. float range = maxv - minv;
  148. if (range > 0) {
  149. for (int i = 0; i < v.length; i++) {
  150. v[i] = (v[i] - minv) / range;
  151. }
  152. } else {
  153. for (int i = 0; i < v.length; i++) {
  154. v[i] = 0;
  155. }
  156. }
  157. layerExtents[j * 2] = minv;
  158. layerExtents[j * 2 + 1] = maxv;
  159. //iterate for pieces
  160. for (int i = 0; i < pieces; i++) {
  161. float[] d = (float[]) data.get(i);
  162. for (int k = j, n = i * step; k < d.length; k += files.length, n++) {
  163. d[k] = v[n];
  164. }
  165. }
  166. if (job != null) {
  167. job.setProgress(0.2 + j / (double) files.length * 7 / 10.0, "opened grid: " + files[j].getName());
  168. }
  169. }
  170. if (job != null) {
  171. job.log("finished opening grids");
  172. }
  173. //remove null rows from data and cells
  174. int newCellPos = 0;
  175. int currentCellPos = 0;
  176. for (int i = 0; i < pieces; i++) {
  177. float[] d = (float[]) data.get(i);
  178. int newPos = 0;
  179. for (int k = 0; k < d.length; k += files.length) {
  180. int nMissing = 0;
  181. for (int j = 0; j < files.length; j++) {
  182. if (Float.isNaN(d[k + j])) {
  183. nMissing++;
  184. }
  185. }
  186. //if (nMissing < files.length) {
  187. if (nMissing == 0) {
  188. if (newPos < k) {
  189. for (int j = 0; j < files.length; j++) {
  190. d[newPos + j] = d[k + j];
  191. }
  192. }
  193. newPos += files.length;
  194. if (newCellPos < currentCellPos) {
  195. cells[newCellPos][0] = cells[currentCellPos][0];
  196. cells[newCellPos][1] = cells[currentCellPos][1];
  197. }
  198. newCellPos++;
  199. }
  200. currentCellPos++;
  201. }
  202. if (newPos < d.length) {
  203. d = java.util.Arrays.copyOf(d, newPos);
  204. data.set(i, d);
  205. }
  206. }
  207. //remove zero length data pieces
  208. for (int i = pieces - 1; i >= 0; i--) {
  209. float[] d = (float[]) data.get(i);
  210. if (d.length == 0) {
  211. data.remove(i);
  212. }
  213. }
  214. //add cells reference to output
  215. data.add(cells);
  216. //add extents to output
  217. double[] extents = new double[6 + layerExtents.length];
  218. extents[0] = width;
  219. extents[1] = height;
  220. extents[2] = xmin;
  221. extents[3] = ymin;
  222. extents[4] = xmax;
  223. extents[5] = ymax;
  224. for (int i = 0; i < layerExtents.length; i++) {
  225. extents[6 + i] = layerExtents[i];
  226. }
  227. data.add(extents);
  228. if (job != null) {
  229. job.setProgress(1, "cleaned data");
  230. }
  231. return data;
  232. }
  233. /**
  234. * exports a list of layers cut against a region
  235. *
  236. * Cut layer files generated are input layers with grid cells outside of
  237. * region set as missing.
  238. *
  239. * @param layers list of layer fieldIds to be cut as String[].
  240. * @param resolution target resolution as String
  241. * @param region null or region to cut against as SimpleRegion. Cannot be
  242. * used with envelopes.
  243. * @param envelopes nul or region to cut against as LayerFilter[]. Cannot be
  244. * used with region.
  245. * @param extentsFilename output filename and path for writing output
  246. * extents.
  247. * @return directory containing the cut grid files.
  248. */
  249. public static String cut2(String[] layers, String resolution, SimpleRegion region, LayerFilter[] envelopes, String extentsFilename) {
  250. //check if resolution needs changing
  251. resolution = confirmResolution(layers, resolution);
  252. //get extents for all layers
  253. double[][] extents = getLayerExtents(resolution, layers[0]);
  254. for (int i = 1; i < layers.length; i++) {
  255. extents = internalExtents(extents, getLayerExtents(resolution, layers[i]));
  256. if (!isValidExtents(extents)) {
  257. return null;
  258. }
  259. }
  260. //get mask and adjust extents for filter
  261. byte[][] mask;
  262. int w = 0, h = 0;
  263. double res = Double.parseDouble(resolution);
  264. if (region != null) {
  265. extents = internalExtents(extents, region.getBoundingBox());
  266. if (!isValidExtents(extents)) {
  267. return null;
  268. }
  269. h = (int) Math.ceil((extents[1][1] - extents[0][1]) / res);
  270. w = (int) Math.ceil((extents[1][0] - extents[0][0]) / res);
  271. mask = getRegionMask(res, extents, w, h, region);
  272. } else if (envelopes != null) {
  273. h = (int) Math.ceil((extents[1][1] - extents[0][1]) / res);
  274. w = (int) Math.ceil((extents[1][0] - extents[0][0]) / res);
  275. mask = getEnvelopeMaskAndUpdateExtents(resolution, res, extents, h, w, envelopes);
  276. h = (int) Math.ceil((extents[1][1] - extents[0][1]) / res);
  277. w = (int) Math.ceil((extents[1][0] - extents[0][0]) / res);
  278. } else {
  279. h = (int) Math.ceil((extents[1][1] - extents[0][1]) / res);
  280. w = (int) Math.ceil((extents[1][0] - extents[0][0]) / res);
  281. mask = getMask(res, extents, w, h);
  282. }
  283. //mkdir in index location
  284. String newPath = null;
  285. try {
  286. newPath = AlaspatialProperties.getAnalysisWorkingDir() + System.currentTimeMillis() + java.io.File.separator;
  287. File directory = new File(newPath);
  288. directory.mkdir();
  289. } catch (Exception e) {
  290. e.printStackTrace();
  291. }
  292. //apply mask
  293. for (int i = 0; i < layers.length; i++) {
  294. applyMask(newPath, resolution, extents, w, h, mask, layers[i]);
  295. }
  296. //write extents file
  297. writeExtents(extentsFilename, extents, w, h);
  298. return newPath;
  299. }
  300. static double[][] internalExtents(double[][] e1, double[][] e2) {
  301. double[][] internalExtents = new double[2][2];
  302. internalExtents[0][0] = Math.max(e1[0][0], e2[0][0]);
  303. internalExtents[0][1] = Math.max(e1[0][1], e2[0][1]);
  304. internalExtents[1][0] = Math.min(e1[1][0], e2[1][0]);
  305. internalExtents[1][1] = Math.min(e1[1][1], e2[1][1]);
  306. return internalExtents;
  307. }
  308. static boolean isValidExtents(double[][] e) {
  309. return e[0][0] < e[1][0] && e[0][1] < e[1][1];
  310. }
  311. static double[][] getLayerExtents(String resolution, String layer) {
  312. double[][] extents = new double[2][2];
  313. Grid g = Grid.getGrid(getLayerPath(resolution, layer));
  314. extents[0][0] = g.xmin;
  315. extents[0][1] = g.ymin;
  316. extents[1][0] = g.xmax;
  317. extents[1][1] = g.ymax;
  318. return extents;
  319. }
  320. public static String getLayerPath(String resolution, String layer) {
  321. String field = Layers.getFieldId(layer);
  322. File file = new File(AlaspatialProperties.getAnalysisLayersDir() + File.separator + resolution + File.separator + field + ".grd");
  323. //move up a resolution when the file does not exist at the target resolution
  324. try {
  325. while (!file.exists()) {
  326. TreeMap<Double, String> resolutionDirs = new TreeMap<Double, String>();
  327. for (File dir : new File(AlaspatialProperties.getAnalysisLayersDir()).listFiles()) {
  328. if (dir.isDirectory()) {
  329. try {
  330. resolutionDirs.put(Double.parseDouble(dir.getName()), dir.getName());
  331. } catch (Exception e) {
  332. }
  333. }
  334. }
  335. String newResolution = resolutionDirs.higherEntry(Double.parseDouble(resolution)).getValue();
  336. if (newResolution.equals(resolution)) {
  337. break;
  338. } else {
  339. resolution = newResolution;
  340. file = new File(AlaspatialProperties.getAnalysisLayersDir() + File.separator + resolution + File.separator + field + ".grd");
  341. }
  342. }
  343. } catch (Exception e) {
  344. }
  345. String layerPath = AlaspatialProperties.getAnalysisLayersDir() + File.separator + resolution + File.separator + field;
  346. if (new File(layerPath + ".grd").exists()) {
  347. return layerPath;
  348. } else {
  349. //look for an analysis layer
  350. String[] info = Client.getLayerIntersectDao().getConfig().getAnalysisLayerInfo(layer);
  351. if (info != null) {
  352. return info[1];
  353. } else {
  354. System.out.println("getLayerPath, cannot find for: " + layer + ", " + resolution);
  355. return null;
  356. }
  357. }
  358. }
  359. static void applyMask(String dir, String resolution, double[][] extents, int w, int h, byte[][] mask, String layer) {
  360. //layer output container
  361. double[] dfiltered = new double[w * h];
  362. //open grid and get all data
  363. Grid grid = Grid.getGrid(getLayerPath(resolution, layer));
  364. float[] d = grid.getGrid(); //get whole layer
  365. //set all as missing values
  366. for (int i = 0; i < dfiltered.length; i++) {
  367. dfiltered[i] = Double.NaN;
  368. }
  369. double res = Double.parseDouble(resolution);
  370. for (int i = 0; i < mask.length; i++) {
  371. for (int j = 0; j < mask[0].length; j++) {
  372. if (mask[i][j] > 0) {
  373. dfiltered[j + (h - i - 1) * w] = grid.getValues2(new double[][]{{j * res + extents[0][0], i * res + extents[0][1]}})[0];
  374. }
  375. }
  376. }
  377. grid.writeGrid(dir + layer, dfiltered,
  378. extents[0][0],
  379. extents[0][1],
  380. extents[1][0],
  381. extents[1][1],
  382. res, res, h, w);
  383. }
  384. static void writeExtents(String filename, double[][] extents, int w, int h) {
  385. if (filename != null) {
  386. try {
  387. FileWriter fw = new FileWriter(filename);
  388. fw.append(String.valueOf(w)).append("\n");
  389. fw.append(String.valueOf(h)).append("\n");
  390. fw.append(String.valueOf(extents[0][0])).append("\n");
  391. fw.append(String.valueOf(extents[0][1])).append("\n");
  392. fw.append(String.valueOf(extents[1][0])).append("\n");
  393. fw.append(String.valueOf(extents[1][1]));
  394. fw.close();
  395. } catch (Exception e) {
  396. e.printStackTrace();
  397. }
  398. }
  399. }
  400. /**
  401. * Get a region mask.
  402. *
  403. * Note: using decimal degree grid, probably should be EPSG900913 grid.
  404. *
  405. * @param res resolution as double
  406. * @param extents extents as double[][] with [0][0]=xmin, [0][1]=ymin,
  407. * [1][0]=xmax, [1][1]=ymax.
  408. * @param h height as int.
  409. * @param w width as int.
  410. * @param region area for the mask as SimpleRegion.
  411. * @return
  412. */
  413. private static byte[][] getRegionMask(double res, double[][] extents, int w, int h, SimpleRegion region) {
  414. byte[][] mask = new byte[h][w];
  415. //can also use region.getOverlapGridCells_EPSG900913
  416. region.getOverlapGridCells(extents[0][0], extents[0][1], extents[1][0], extents[1][1], w, h, mask);
  417. for (int i = 0; i < h; i++) {
  418. for (int j = 0; j < w; j++) {
  419. //double tx = (j + 0.5) * res + extents[0][0];
  420. //double ty = (i + 0.5) * res + extents[0][1];
  421. //if (region.isWithin_EPSG900913(tx, ty)) {
  422. // mask[i][j] = 1;
  423. //}
  424. if (mask[i][j] > 0) {
  425. mask[i][j] = 1;
  426. }
  427. }
  428. }
  429. return mask;
  430. }
  431. private static byte[][] getMask(double res, double[][] extents, int w, int h) {
  432. byte[][] mask = new byte[h][w];
  433. for (int i = 0; i < h; i++) {
  434. for (int j = 0; j < w; j++) {
  435. mask[i][j] = 1;
  436. }
  437. }
  438. return mask;
  439. }
  440. /**
  441. * Get a mask, 0=absence, 1=presence, for a given envelope and extents.
  442. *
  443. * @param resolution resolution as String.
  444. * @param res resultions as double.
  445. * @param extents extents as double[][] with [0][0]=xmin, [0][1]=ymin,
  446. * [1][0]=xmax, [1][1]=ymax.
  447. * @param h height as int.
  448. * @param w width as int.
  449. * @param envelopes
  450. * @return mask as byte[][]
  451. */
  452. private static byte[][] getEnvelopeMaskAndUpdateExtents(String resolution, double res, double[][] extents, int h, int w, LayerFilter[] envelopes) {
  453. byte[][] mask = new byte[h][w];
  454. double[][] points = new double[h * w][2];
  455. for (int i = 0; i < w; i++) {
  456. for (int j = 0; j < h; j++) {
  457. points[i + j * w][0] = (double) (extents[0][0] + (i + 0.5) * res);
  458. points[i + j * w][1] = (double) (extents[0][1] + (j + 0.5) * res);
  459. //mask[j][i] = 0;
  460. }
  461. }
  462. for (int k = 0; k < envelopes.length; k++) {
  463. LayerFilter lf = envelopes[k];
  464. Grid grid = Grid.getGrid(getLayerPath(resolution, lf.getLayername()));
  465. float[] d = grid.getValues3(points, 40960);
  466. for (int i = 0; i < d.length; i++) {
  467. if (lf.isValid(d[i])) {
  468. mask[i / w][i % w]++;
  469. }
  470. }
  471. }
  472. for (int i = 0; i < w; i++) {
  473. for (int j = 0; j < h; j++) {
  474. if (mask[j][i] == envelopes.length) {
  475. mask[j][i] = 1;
  476. } else {
  477. mask[j][i] = 0;
  478. }
  479. }
  480. }
  481. //find internal extents
  482. int minx = w;
  483. int maxx = -1;
  484. int miny = h;
  485. int maxy = -1;
  486. for (int i = 0; i < w; i++) {
  487. for (int j = 0; j < h; j++) {
  488. if (mask[j][i] > 0) {
  489. if (minx > i) {
  490. minx = i;
  491. }
  492. if (maxx < i) {
  493. maxx = i;
  494. }
  495. if (miny > j) {
  496. miny = j;
  497. }
  498. if (maxy < j) {
  499. maxy = j;
  500. }
  501. }
  502. }
  503. }
  504. //reduce the size of the mask
  505. int nw = maxx - minx + 1;
  506. int nh = maxy - miny + 1;
  507. byte[][] smallerMask = new byte[nh][nw];
  508. for (int i = minx; i < maxx; i++) {
  509. for (int j = miny; j < maxy; j++) {
  510. smallerMask[j - miny][i - minx] = mask[j][i];
  511. }
  512. }
  513. //update extents, must never be larger than the original extents (res is not negative, minx maxx miny mazy are not negative and < w & h respectively
  514. extents[0][0] = Math.max(extents[0][0] + minx * res,extents[0][0]); //min x value
  515. extents[1][0] = Math.min(extents[1][0] - (w - maxx - 1) * res,extents[1][0]); //max x value
  516. extents[0][1] = Math.max(extents[0][1] + miny * res,extents[0][1]); //min y value
  517. extents[1][1] = Math.min(extents[1][1] - (h - maxy - 1) * res,extents[1][1]); //max y value
  518. return smallerMask;
  519. }
  520. /**
  521. * Write a diva grid to disk for the envelope, 0 = absence, 1 = presence.
  522. *
  523. * @param filename output filename for the grid as String.
  524. * @param resolution target resolution in decimal degrees as String.
  525. * @param envelopes envelope specification as LayerFilter[].
  526. * @return area in sq km as double.
  527. */
  528. public static double makeEnvelope(String filename, String resolution, LayerFilter[] envelopes) {
  529. //get extents for all layers
  530. double[][] extents = getLayerExtents(resolution, envelopes[0].getLayername());
  531. for (int i = 1; i < envelopes.length; i++) {
  532. extents = internalExtents(extents, getLayerExtents(resolution, envelopes[i].getLayername()));
  533. if (!isValidExtents(extents)) {
  534. return -1;
  535. }
  536. }
  537. double res = Double.parseDouble(resolution);
  538. //limit the size of the grid files that can be generated
  539. while ((Math.abs(extents[0][1] - extents[1][0])/res) * (Math.abs(extents[0][0] - extents[1][0])/res) > AlaspatialProperties.getAnalysisLimitGridCells()*2.0) {
  540. res = res * 2;
  541. }
  542. if (res != Double.parseDouble(resolution)) {
  543. resolution = String.format("%f", res);
  544. }
  545. //get mask and adjust extents for filter
  546. byte[][] mask;
  547. int w, h;
  548. h = (int) Math.ceil((extents[1][1] - extents[0][1]) / res);
  549. w = (int) Math.ceil((extents[1][0] - extents[0][0]) / res);
  550. mask = getEnvelopeMaskAndUpdateExtents(resolution, res, extents, h, w, envelopes);
  551. h = (int) Math.ceil((extents[1][1] - extents[0][1]) / res);
  552. if(((int) Math.ceil((extents[1][1] + res - extents[0][1]) / res)) == h) {
  553. extents[1][1] += res;
  554. }
  555. w = (int) Math.ceil((extents[1][0] - extents[0][0]) / res);
  556. if(((int) Math.ceil((extents[1][0] + res - extents[0][0]) / res)) == w) {
  557. extents[1][0] += res;
  558. }
  559. float[] values = new float[w * h];
  560. int pos = 0;
  561. double areaSqKm = 0;
  562. for (int i = h - 1; i >= 0; i--) {
  563. for (int j = 0; j < w; j++) {
  564. if(i < mask.length && j < mask[i].length) {
  565. values[pos] = mask[i][j];
  566. if (mask[i][j] > 0) {
  567. areaSqKm += SpatialUtil.cellArea(res, extents[0][1] + res * i);
  568. }
  569. } else {
  570. values[pos] = 0;
  571. }
  572. pos++;
  573. }
  574. }
  575. Grid grid = new Grid(getLayerPath(resolution, envelopes[0].getLayername()));
  576. grid.writeGrid(filename, values,
  577. extents[0][0],
  578. extents[0][1],
  579. extents[1][0],
  580. extents[1][1],
  581. res, res, h, w);
  582. return areaSqKm;
  583. }
  584. /**
  585. * Test if the layer filter is valid.
  586. *
  587. * The common problem is that a filter may refer to a layer that is not
  588. * available.
  589. *
  590. * @param resolution target resolution as String.
  591. * @param filter layer filter as LayerFilter[].
  592. * @return true iff valid filter.
  593. */
  594. public static boolean isValidLayerFilter(String resolution, LayerFilter[] filter) {
  595. for (LayerFilter lf : filter) {
  596. if (GridCutter.getLayerPath(resolution, lf.getLayername()) == null) {
  597. return false;
  598. }
  599. }
  600. return true;
  601. }
  602. /**
  603. * Determine the grid resolution that will be in use.
  604. *
  605. * @param layers list of layers to be used as String []
  606. * @param resolution target resolution as String
  607. * @return resolution that will be used
  608. */
  609. private static String confirmResolution(String[] layers, String resolution) {
  610. try {
  611. TreeMap<Double, String> resolutions = new TreeMap<Double, String>();
  612. for (String layer : layers) {
  613. String path = GridCutter.getLayerPath(resolution, layer);
  614. int end, start;
  615. if (path != null
  616. && ((end = path.lastIndexOf(File.separator)) > 0)
  617. && ((start = path.lastIndexOf(File.separator, end - 1)) > 0)) {
  618. String res = path.substring(start + 1, end);
  619. Double d = Double.parseDouble(res);
  620. if (d < 1) {
  621. resolutions.put(d, res);
  622. }
  623. }
  624. }
  625. if (resolutions.size() > 0) {
  626. resolution = resolutions.firstEntry().getValue();
  627. }
  628. } catch (Exception e) {
  629. e.printStackTrace();
  630. }
  631. return resolution;
  632. }
  633. }