001: //$HeadURL$
002: /*---------------- FILE HEADER ------------------------------------------
003: This file is part of deegree.
004: Copyright (C) 2001-2008 by:
005: Department of Geography, University of Bonn
006: http://www.giub.uni-bonn.de/deegree/
007: lat/lon GmbH
008: http://www.lat-lon.de
009:
010: This library is free software; you can redistribute it and/or
011: modify it under the terms of the GNU Lesser General Public
012: License as published by the Free Software Foundation; either
013: version 2.1 of the License, or (at your option) any later version.
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: Contact:
022:
023: Andreas Poth
024: lat/lon GmbH
025: Aennchenstr. 19
026: 53177 Bonn
027: Germany
028: E-Mail: poth@lat-lon.de
029:
030: Prof. Dr. Klaus Greve
031: Department of Geography
032: University of Bonn
033: Meckenheimer Allee 166
034: 53115 Bonn
035: Germany
036: E-Mail: greve@giub.uni-bonn.de
037: ---------------------------------------------------------------------------*/
038:
039: package org.deegree.tools.raster;
040:
041: import java.awt.image.BufferedImage;
042: import java.awt.image.renderable.ParameterBlock;
043: import java.io.File;
044: import java.net.URI;
045: import java.util.ArrayList;
046: import java.util.List;
047: import java.util.Properties;
048:
049: import javax.media.jai.Interpolation;
050: import javax.media.jai.InterpolationBilinear;
051: import javax.media.jai.JAI;
052: import javax.media.jai.RenderedOp;
053:
054: import org.deegree.datatypes.QualifiedName;
055: import org.deegree.framework.log.ILogger;
056: import org.deegree.framework.log.LoggerFactory;
057: import org.deegree.framework.util.ConvenienceFileFilter;
058: import org.deegree.framework.util.ImageUtils;
059: import org.deegree.io.dbaseapi.DBaseFile;
060: import org.deegree.model.coverage.grid.WorldFile;
061: import org.deegree.ogcbase.CommonNamespaces;
062:
063: /**
064: *
065: *
066: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
067: * @author last edited by: $Author: poth $
068: *
069: * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
070: */
071: public class Rescaler {
072:
073: private static final ILogger LOG = LoggerFactory
074: .getLogger(Rescaler.class);
075:
076: private static final URI DEEGREEAPP = CommonNamespaces
077: .buildNSURI("http://www.deegree.org/app");
078:
079: private static final String APP_PREFIX = "app";
080:
081: private String outDir;
082:
083: private String format;
084:
085: private Interpolation interpolation = new InterpolationBilinear();
086:
087: private List<String> fileList;
088:
089: private double resolution = 0;
090:
091: /**
092: *
093: * @param resolution
094: * @param rootDir
095: * @param outDir
096: * @param format
097: * @param subDirs
098: */
099: Rescaler(double resolution, String rootDir, String outDir,
100: String format, boolean subDirs) {
101: this .resolution = resolution;
102: this .outDir = outDir;
103: this .format = format;
104: fileList = getFileList(rootDir, subDirs);
105: }
106:
107: /**
108: *
109: * @param resolution
110: * @param rootDir
111: * @param outDir
112: * @param dBase
113: * @param format
114: * @param fileColumn
115: * @throws Exception
116: */
117: Rescaler(double resolution, String rootDir, String outDir,
118: String dBase, String format, String fileColumn)
119: throws Exception {
120: this .resolution = resolution;
121: this .outDir = outDir;
122: this .format = format;
123: fileList = getFileList(dBase, fileColumn, rootDir);
124: }
125:
126: /**
127: * returns the list of image map files to consider read from a dbase file defined by the dbase
128: * parameter
129: *
130: * @param dbaseFile
131: * name of the dbase file
132: * @param fileColumn
133: * name of the column containing the image map files names
134: * @param baseDir
135: * name of the directory where the image map files are stored if this parameter is
136: * <code>null</code> it is assumed that the image map files are full referenced
137: * within the dbase
138: * @return the list of image map files to consider read from a dbase file defined by the dbase
139: * parameter
140: * @throws Exception
141: */
142: private static List<String> getFileList(String dBaseFile,
143: String fileColumn, String baseDir) throws Exception {
144:
145: // handle dbase file extension and file location/reading problems
146: if (dBaseFile.endsWith(".dbf")) {
147: dBaseFile = dBaseFile.substring(0, dBaseFile
148: .lastIndexOf("."));
149: }
150: DBaseFile dbf = new DBaseFile(dBaseFile);
151:
152: // sort dbase file contents chronologicaly (oldest first)
153: int cnt = dbf.getRecordNum();
154:
155: Object[] mapItems = new Object[cnt];
156: QualifiedName fileC = new QualifiedName(APP_PREFIX, fileColumn
157: .toUpperCase(), DEEGREEAPP);
158:
159: for (int i = 0; i < cnt; i++) {
160: // name of map file
161: mapItems[i] = dbf.getFRow(i + 1).getDefaultProperty(fileC)
162: .getValue();
163: }
164:
165: // extract names of image files from dBase file and attach them to rootDir
166: if (baseDir == null) {
167: baseDir = "";
168: } else if (!baseDir.endsWith("/") && !baseDir.endsWith("\\")) {
169: baseDir = baseDir + '/';
170: }
171: List<String> imageFiles = new ArrayList<String>(mapItems.length);
172: for (int i = 0; i < mapItems.length; i++) {
173: if (mapItems[i] != null) {
174: imageFiles.add(baseDir + mapItems[i]);
175: }
176: }
177:
178: return imageFiles;
179: }
180:
181: /**
182: * returns the list of image map files to consider read from a defined root directory.
183: *
184: * @param rootDir
185: * root directory where to read image map files
186: * @param subdirs
187: * true if subdirectories of the root directory shall be parsed for image maps too
188: * @return the list of image map files to consider read from a defined root directory.
189: */
190: private static List<String> getFileList(String rootDir,
191: boolean subdirs) {
192: List<String> list = new ArrayList<String>(10000);
193: File file = new File(rootDir);
194: List<String> extensions = new ArrayList<String>();
195: extensions.add("JPEG");
196: extensions.add("JPG");
197: extensions.add("BMP");
198: extensions.add("PNG");
199: extensions.add("GIF");
200: extensions.add("TIF");
201: extensions.add("TIFF");
202: extensions.add("GEOTIFF");
203: ConvenienceFileFilter cff = new ConvenienceFileFilter(
204: extensions, true);
205: String[] entries = file.list(cff);
206: if (entries != null) {
207: for (int i = 0; i < entries.length; i++) {
208: File entry = new File(rootDir + '/' + entries[i]);
209: if (entry.isDirectory() && subdirs) {
210: list = readSubDirs(entry, list, cff);
211: } else {
212: list.add(rootDir + '/' + entries[i]);
213: }
214: }
215: }
216: return list;
217: }
218:
219: /**
220: *
221: * @param file
222: * @param list
223: * @return
224: */
225: private static List<String> readSubDirs(File file,
226: List<String> list, ConvenienceFileFilter cff) {
227:
228: String[] entries = file.list(cff);
229: if (entries != null) {
230: for (int i = 0; i < entries.length; i++) {
231: File entry = new File(file.getAbsolutePath() + '/'
232: + entries[i]);
233: if (entry.isDirectory()) {
234: list = readSubDirs(entry, list, cff);
235: } else {
236: list.add(file.getAbsolutePath() + '/' + entries[i]);
237: }
238: }
239: }
240: return list;
241: }
242:
243: /**
244: *
245: * @throws Exception
246: */
247: public void process() throws Exception {
248: for (int i = 0; i < fileList.size(); i++) {
249: System.out.print(fileList.get(i) + "\r");
250: File file = new File(fileList.get(i));
251: BufferedImage image = ImageUtils.loadImage(file);
252: WorldFile wf = WorldFile.readWorldFile(fileList.get(i),
253: WorldFile.TYPE.CENTER, image.getWidth(), image
254: .getHeight());
255: float qx = (float) (wf.getResx() / resolution);
256: float qy = (float) (wf.getResy() / resolution);
257:
258: ParameterBlock pb = new ParameterBlock();
259: pb.addSource(image);
260: pb.add(qx); // The xScale
261: pb.add(qy); // The yScale
262: pb.add(0.0F); // The x translation
263: pb.add(0.0F); // The y translation
264: pb.add(interpolation); // The interpolation
265: // Create the scale operation
266: RenderedOp ro = JAI.create("scale", pb, null);
267: try {
268: image = ro.getAsBufferedImage();
269: } catch (Exception e) {
270: e.printStackTrace();
271: }
272:
273: wf = new WorldFile(resolution, resolution, 0, 0, wf
274: .getEnvelope());
275: int p = file.getName().lastIndexOf('.');
276: String fileBaseName = file.getName().substring(0, p);
277: ImageUtils.saveImage(image, outDir + fileBaseName + '.'
278: + format, 1);
279: WorldFile.writeWorldFile(wf, outDir + fileBaseName);
280: System.gc();
281: }
282: }
283:
284: private static void printHelp() {
285: System.out.println();
286: System.out.println("Parameter description for RasterSplitter:");
287: System.out.println("-res : desired raster resolution");
288: System.out
289: .println("-format : desired image format of result images (mandatory)");
290: System.out
291: .println("-outDir : directory where result images shall be stored (mandatory)");
292:
293: System.out
294: .println("-rootDir : directory from where images to split will be read (mandatory)");
295: System.out
296: .println("-subDirs : (true|false). If 'true' all sub directories of the 'rootDir' ");
297: System.out
298: .println(" will be searched for images too (optional; default = false)");
299: }
300:
301: private static boolean validate(Properties map) {
302: if (map.getProperty("-res") == null) {
303: System.out.println("-res must be set!");
304: return false;
305: }
306: if (map.getProperty("-format") == null) {
307: System.out.println("-format must be set!");
308: return false;
309: }
310: if (map.getProperty("-outDir") == null) {
311: System.out.println("-outDir must be set!");
312: return false;
313: }
314: if (map.getProperty("-rootDir") == null) {
315: System.out.println("-rootDir must be set!");
316: return false;
317: }
318: if (map.getProperty("-subDirs") != null
319: && !"true".equals(map.getProperty("-subDirs"))
320: && !"false".equals(map.getProperty("-subDirs"))) {
321: System.out
322: .println("if -subDirs is set it must be true or false!");
323: return false;
324: }
325: return true;
326: }
327:
328: /**
329: * @param args
330: * @throws Exception
331: */
332: public static void main(String[] args) throws Exception {
333: Properties map = new Properties();
334: for (int i = 0; i < args.length; i += 2) {
335: map.put(args[i], args[i + 1]);
336: }
337: if (!validate(map)) {
338: printHelp();
339: return;
340: }
341:
342: String format = map.getProperty("-format");
343: String outDir = map.getProperty("-outDir");
344: double res = Double.parseDouble(map.getProperty("-res"));
345: Rescaler rescaler = null;
346: if (map.get("-dbaseFile") != null) {
347: String dBaseFile = map.getProperty("-dbaseFile");
348: String fileColum = map.getProperty("-fileColumn");
349: String baseDir = map.getProperty("-baseDir");
350: if (baseDir == null) {
351: baseDir = map.getProperty("-rootDir");
352: }
353: rescaler = new Rescaler(res, baseDir, outDir, dBaseFile,
354: format, fileColum);
355: } else if (map.get("-rootDir") != null) {
356: String rootDir = map.getProperty("-rootDir");
357: boolean subDirs = "true".equals(map.get("-subDirs"));
358: rescaler = new Rescaler(res, rootDir, outDir, format,
359: subDirs);
360: } else {
361: LOG.logInfo(map.toString());
362: System.out
363: .println("-rootDir or -dbaseFile parameter must be defined");
364: return;
365: }
366: rescaler.process();
367:
368: }
369:
370: }
|