1 /****************************************************************************** 2 * $Id: GDALCountuor.java $ 3 * 4 * Project: GDAL Java applications 5 * Purpose: Contour Generator mainline 6 * Author: Ivan Lucena, ivan.lucena@pmldnet.com, 7 * translated from gdal_counter.cpp 8 * originally written by Frank Warmerdam <warmerdam@pobox.com> 9 ****************************************************************************** 10 * Copyright (c) 2010, Ivan Lucena 11 * 12 * Permission is hereby granted, free of charge, to any person obtaining a copy 13 * of this software and associated documentation files (the "Software"), to deal 14 * in the Software without restriction, including without limitation the rights 15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16 * copies of the Software, and to permit persons to whom the Software is 17 * furnished to do so, subject to the following conditions: 18 * 19 * The above copyright notice and this permission notice shall be included in 20 * all copies or substantial portions of the Software. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 * SOFTWARE. 29 ****************************************************************************/ 30 31 import org.gdal.gdal.Band; 32 import org.gdal.gdal.Dataset; 33 import org.gdal.gdal.ProgressCallback; 34 import org.gdal.gdal.gdal; 35 import org.gdal.gdalconst.gdalconstConstants; 36 import org.gdal.ogr.DataSource; 37 import org.gdal.ogr.Driver; 38 import org.gdal.ogr.FeatureDefn; 39 import org.gdal.ogr.FieldDefn; 40 import org.gdal.ogr.Layer; 41 import org.gdal.ogr.ogr; 42 import org.gdal.osr.SpatialReference; 43 44 public class GDALContour { 45 46 public static void Usage() { 47 System.out 48 .println("" 49 + "Usage: gdal_contour [-b <band>] [-a <attribute_name>] [-3d] [-inodata]\n" 50 + " [-snodata n] [-f <formatname>] [-i <interval>]\n" stripline(char * pszBuf)51 + " [-off <offset>] [-fl <level>]*\n" 52 + " [-nln <outlayername>] [-q]\n" 53 + " <src_filename> <dst_filename>\n"); 54 System.exit(-1); 55 } 56 57 public static void main(String[] args) { 58 59 String sourceFilename = null; 60 String outputFilename = null; 61 int sourceBand = 1; 62 String attributeName = null; 63 boolean threeDimension = false; 64 boolean ignoreNodata = false; 65 boolean hasSourceNodata = false; 66 double sourceNodata = 0.0; 67 String outputFormat = "ESRI Shapefile"; 68 double contourInterval = 0.0; 69 double offset = 0.0; 70 String fixedLevels = null; removeargnames(char * pszBuf)71 String newLayerName = "contour"; 72 boolean quiet = false; 73 ProgressCallback progressCallback = null; 74 75 /* 76 * Register GDAL and OGR format(s) 77 */ 78 79 gdal.AllRegister(); 80 ogr.RegisterAll(); 81 82 /* 83 * Parse arguments 84 */ 85 86 args = ogr.GeneralCmdLineProcessor(args); 87 88 if (args.length < 2) { 89 Usage(); 90 } 91 92 for (int i = 0; i < args.length; i++) { 93 94 if (args[i].equals("---utility_version")) { 95 96 System.out 97 .println("Running against GDAL " + gdal.VersionInfo()); 98 return; 99 100 } else if (args[i].equals("-a") && args.length > i) { 101 102 attributeName = args[++i]; 103 104 } else if (args[i].equals("-off") && args.length > i) { 105 106 offset = Float.parseFloat(args[++i]); 107 108 } else if (args[i].equals("-i") && args.length > i) { 109 110 contourInterval = Float.parseFloat(args[++i]); 111 112 } else if (args[i].equals("-fl") && args.length > i) { 113 114 if (fixedLevels == null) { 115 fixedLevels = args[++i]; 116 } else { 117 fixedLevels += ':' + args[++i]; 118 } 119 120 } else if (args[i].equals("-b") && args.length > i) { 121 122 sourceBand = Integer.parseInt(args[++i]); 123 124 } else if (args[i].equals("-f") && args.length > i) { 125 126 outputFormat = args[++i]; 127 main(int argc,char * argv[])128 } else if (args[i].equals("-3d")) { 129 130 threeDimension = true; 131 132 } else if (args[i].equals("-snodata") && args.length > i) { 133 134 hasSourceNodata = true; 135 sourceNodata = Float.parseFloat(args[++i]); 136 137 } else if (args[i].equals("-nln") && args.length > i) { 138 139 newLayerName = args[++i]; 140 141 } else if (args[i].equals("-inodata")) { 142 143 ignoreNodata = true; 144 145 } else if (args[i].equals("-q") || args[i].equals("-quiet")) { 146 147 quiet = true; 148 149 } else if (sourceFilename == null) { 150 151 sourceFilename = args[i]; 152 153 } else if (outputFilename == null) { 154 155 outputFilename = args[i]; 156 157 } else { 158 159 Usage(); 160 } 161 } 162 163 if (sourceFilename == null || outputFilename == null) { 164 165 Usage(); 166 } 167 168 double[] fixedLevelsDouble = null; 169 170 if (fixedLevels != null) { 171 172 String[] fixedLevelsArray = fixedLevels.split(":"); 173 fixedLevelsDouble = new double[fixedLevelsArray.length]; 174 175 for (int i = 0; i < fixedLevelsDouble.length; i++) { 176 177 if (fixedLevelsDouble[i] == 0.0) { 178 179 Usage(); 180 } 181 } 182 } 183 184 /* 185 * Open source raster file. 186 */ 187 188 Dataset dataset = gdal.Open(sourceFilename, 189 gdalconstConstants.GA_ReadOnly); 190 191 if (dataset == null) { 192 System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo()); 193 System.err.println(gdal.GetLastErrorMsg()); 194 System.exit(2); 195 } 196 197 Band band = dataset.GetRasterBand(sourceBand); 198 199 if (band == null) { 200 System.err.println("Band does not exist on dataset"); 201 System.err.println("GDALOpen failed - " + gdal.GetLastErrorNo()); 202 System.err.println(gdal.GetLastErrorMsg()); 203 System.exit(3); 204 } 205 206 if (!hasSourceNodata && !ignoreNodata) { 207 208 Double val[] = new Double[1]; 209 210 band.GetNoDataValue(val); 211 212 hasSourceNodata = true; 213 214 if (val[0] != null) { 215 sourceNodata = val[0]; 216 } else { 217 hasSourceNodata = false; 218 } 219 } 220 221 /* 222 * Try to get a coordinate system from the raster. 223 */ 224 225 SpatialReference srs = null; 226 227 String wkt = dataset.GetProjection(); 228 229 if (wkt.length() > 0) { 230 231 srs = new SpatialReference(wkt); 232 } 233 234 /* 235 * Create the outputfile. 236 */ 237 238 DataSource dataSource = null; 239 Driver driver = ogr.GetDriverByName(outputFormat); 240 FieldDefn field = null; 241 Layer layer = null; 242 243 if (driver == null) { 244 245 System.err.println("Unable to find format driver named " 246 + outputFormat); 247 System.exit(10); 248 } 249 250 dataSource = driver.CreateDataSource(outputFilename); 251 252 if (dataSource == null) { 253 System.exit(1); 254 } 255 256 if (threeDimension) { 257 layer = dataSource.CreateLayer(newLayerName, srs, 258 ogr.wkbLineString25D); 259 } else { 260 layer = dataSource 261 .CreateLayer(newLayerName, srs, ogr.wkbLineString); 262 } 263 264 if (layer == null) { 265 System.exit(1); 266 } 267 268 field = new FieldDefn("ID", ogr.OFTInteger); 269 field.SetWidth(8); 270 271 layer.CreateField(field, 0); 272 field.delete(); 273 274 if (attributeName != null) { 275 276 field = new FieldDefn(attributeName, ogr.OFTReal); 277 field.SetWidth(12); 278 field.SetPrecision(3); 279 280 layer.CreateField(field, 0); 281 layer.delete(); 282 } 283 284 /* 285 * Use terminal progress report 286 */ 287 288 if (quiet == false) { 289 progressCallback = new ProgressCallback(); 290 } 291 292 /* 293 * Invoke. 294 */ 295 296 FeatureDefn feature = layer.GetLayerDefn(); 297 298 gdal.ContourGenerate(band, contourInterval, offset, fixedLevelsDouble, 299 (ignoreNodata ? 1 : 0), sourceNodata, layer, feature.GetFieldIndex("ID"), 300 (attributeName != null ? feature.GetFieldIndex(attributeName) : -1), 301 progressCallback); 302 303 dataSource.delete(); 304 dataset.delete(); 305 } 306 } 307