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