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.crs.configuration;
040:
041: import java.io.BufferedWriter;
042: import java.io.File;
043: import java.io.FileWriter;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.Properties;
051:
052: import org.deegree.crs.coordinatesystems.CoordinateSystem;
053: import org.deegree.crs.coordinatesystems.ProjectedCRS;
054: import org.deegree.crs.exceptions.CRSConfigurationException;
055: import org.deegree.crs.projections.Projection;
056: import org.deegree.crs.projections.ProjectionUtils;
057: import org.deegree.framework.log.ILogger;
058: import org.deegree.framework.log.LoggerFactory;
059: import org.deegree.i18n.Messages;
060:
061: /**
062: * The <code>CRSConfiguration</code> creates, instantiates and supplies a configured CRS-Provider. Because only one
063: * crs-configuration is needed inside the JVM, this implementation uses a singleton pattern.
064: * <p>
065: * The configuration will try to read the file: crs_providers.properties. It uses following strategie to load this file,
066: * first the root directory (e.g. '/' or WEB-INF/classes ) will be searched. If no file was found there, it will try to
067: * load from the package. The properties file must denote a property with name 'CRS_PROVIDER' followed by a '=' and a
068: * fully qualified name denoting the class (an instance of CRSProvider) which should be available in the classpath. This
069: * class must have an empty constructor.
070: * </p>
071: *
072: * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
073: *
074: * @author last edited by: $Author:$
075: *
076: * @version $Revision:$, $Date:$
077: *
078: */
079:
080: public class CRSConfiguration {
081: private static ILogger LOG = LoggerFactory
082: .getLogger(CRSConfiguration.class);
083:
084: private CRSProvider provider;
085:
086: //
087: private final static String PROVIDER_CONFIG = "crs_providers.properties";
088:
089: private static CRSConfiguration CONFIG = null;
090:
091: /**
092: * @param provider
093: * to get the CRS's from.
094: */
095: private CRSConfiguration(CRSProvider provider) {
096: this .provider = provider;
097: }
098:
099: /**
100: * Creates or returns an instance of the CRSConfiguration by reading the DEFAULT property configured in the
101: * 'crs_providers.properties'. If no key is given (or no string could be loaded), the {@link DeegreeCRSProvider}
102: * will be used.
103: *
104: * @return an instance of a CRS-Configuration with the configured CRSProvider.
105: * @throws CRSConfigurationException
106: * if --anything-- went wrong while instantiating the CRSProvider.
107: */
108: public synchronized static CRSConfiguration getCRSConfiguration()
109: throws CRSConfigurationException {
110: if (CONFIG != null) {
111: return CONFIG;
112: }
113: CRSProvider provider = null;
114:
115: LOG
116: .logDebug("Trying to load configured CRS provider from configuration (/crs_providers.properties).");
117: InputStream is = CRSConfiguration.class.getResourceAsStream("/"
118: + PROVIDER_CONFIG);
119: if (is == null) {
120: LOG
121: .logDebug("Trying to load configured CRS provider from configuration (org.deegree.crs.configuration.crs_providers.properties).");
122: is = CRSConfiguration.class
123: .getResourceAsStream(PROVIDER_CONFIG);
124: }
125: if (is == null) {
126: LOG.logWarning(Messages.getMessage(
127: "CRS_CONFIG_NO_PROVIDER_DEFS_FOUND",
128: PROVIDER_CONFIG));
129: // create the standard deegree-crs-provider.
130: provider = new DeegreeCRSProvider(null);
131: } else {
132: Properties props = new Properties();
133: try {
134: props.load(is);
135: } catch (IOException e) {
136: LOG.logError(e.getMessage(), e);
137: } finally {
138: try {
139: is.close();
140: } catch (IOException e) {
141: // no output if the stream can't be closed, just leave it as it is.
142: }
143: }
144:
145: String className = props.getProperty("CRS_PROVIDER");
146: if (className == null || "".equals(className.trim())) {
147: LOG.logWarning(Messages
148: .getMessage("CRS_CONFIG_NO_PROVIDER_FOUND",
149: PROVIDER_CONFIG));
150: provider = new DeegreeCRSProvider(null);
151: } else if ("org.deegree.crs.configuration.DeegreeCRSProvider"
152: .equals(className)) {
153: LOG
154: .logDebug("The configured CRS provider is a Deegree CRSProvider with name: "
155: + className);
156: provider = new DeegreeCRSProvider(null);
157: } else {
158: // use reflection to instantiate the configured provider.
159: try {
160: LOG
161: .logDebug("Trying to load configured CRS provider from classname: "
162: + className);
163: provider = (CRSProvider) Class.forName(className)
164: .newInstance();
165: } catch (InstantiationException e) {
166: LOG.logError(Messages.getMessage(
167: "CRS_CONFIG_INSTANTIATION_ERROR",
168: className, e.getMessage()));
169: } catch (IllegalAccessException e) {
170: LOG.logError(Messages.getMessage(
171: "CRS_CONFIG_INSTANTIATION_ERROR",
172: className, e.getMessage()), e);
173: } catch (ClassNotFoundException e) {
174:
175: LOG.logError(Messages.getMessage(
176: "CRS_CONFIG_INSTANTIATION_ERROR",
177: className, e.getMessage()), e);
178: } finally {
179: if (provider == null) {
180: LOG
181: .logInfo("The configured class: "
182: + className
183: + " was not created. Trying to create a deegree-crs-provider");
184: provider = new DeegreeCRSProvider(null);
185: }
186: }
187: }
188: }
189: CONFIG = new CRSConfiguration(provider);
190: return CONFIG;
191:
192: }
193:
194: /**
195: * export the given file to the deegree-crs format.
196: *
197: * @param args
198: */
199: public static void main(String[] args) {
200: if (args.length == 0) {
201: outputHelp();
202: }
203: Map<String, String> params = new HashMap<String, String>(5);
204: for (int i = 0; i < args.length; i++) {
205: String arg = args[i];
206: if (arg != null && !"".equals(arg.trim())) {
207: arg = arg.trim();
208: if (arg.equalsIgnoreCase("-?")
209: || arg.equalsIgnoreCase("-h")) {
210: outputHelp();
211: } else {
212: if (i + 1 < args.length) {
213: String val = args[++i];
214: if (val != null && !"".equals(val.trim())) {
215: params.put(arg, val.trim());
216: } else {
217: System.out
218: .println("Invalid value for parameter: "
219: + arg);
220: }
221: } else {
222: System.out.println("No value for parameter: "
223: + arg);
224: }
225: }
226: }
227: }
228: String inFormat = params.get("-inFormat");
229: if (inFormat == null || "".equals(inFormat.trim())) {
230: System.out
231: .println("No input format (inFormat) defined, setting to proj4");
232: inFormat = "proj4";
233: }
234: String inFile = params.get("-inFile");
235: if (inFile == null || "".equals(inFile.trim())) {
236: System.out.println("No input file set, exiting\n");
237: outputHelp();
238: System.exit(1);
239: }
240: File inputFile = new File(inFile);
241:
242: String outFile = params.get("-outFile");
243: String outFormat = params.get("-outFormat");
244: if (outFormat == null || "".equals(outFormat.trim())) {
245: System.out
246: .println("No output format (outFormat) defined, setting to deegree");
247: outFormat = "deegree";
248: }
249:
250: String veri = params.get("-verify");
251: boolean verify = (veri != null && !"".equals(inFile.trim()));
252:
253: CRSProvider in = new PROJ4CRSProvider(inputFile);
254: if ("deegree".equalsIgnoreCase(inFormat)) {
255: try {
256: in = new DeegreeCRSProvider(inputFile);
257: } catch (CRSConfigurationException e) {
258: e.printStackTrace();
259: }
260: }
261:
262: CRSProvider out = new DeegreeCRSProvider();
263: if ("proj4".equalsIgnoreCase(outFormat)) {
264: out = new PROJ4CRSProvider();
265: }
266:
267: try {
268: List<CoordinateSystem> allSystems = in.getAvailableCRSs();
269: if (verify) {
270: out = new DeegreeCRSProvider(null);
271: List<CoordinateSystem> notExported = new ArrayList<CoordinateSystem>(
272: 1000);
273: for (CoordinateSystem inCRS : allSystems) {
274: if (inCRS.getType() == CoordinateSystem.PROJECTED_CRS) {
275: String id = inCRS.getIdentifier();
276: CoordinateSystem outCRS = out.getCRSByID(id);
277: //System.out.print( "Getting crs: " + id + " and projection: " + ((ProjectedCRS)inCRS).getProjection().getDeegreeSpecificName() );
278: if (outCRS != null
279: && outCRS.getType() == CoordinateSystem.PROJECTED_CRS) {
280: //System.out.println( "... [SUCCESS] to retrieve from deegree-config with projection: " + ((ProjectedCRS)outCRS).getProjection().getDeegreeSpecificName() );
281: Projection inProj = ((ProjectedCRS) inCRS)
282: .getProjection();
283: Projection outProj = ((ProjectedCRS) outCRS)
284: .getProjection();
285: if (Math.abs(inProj.getProjectionLatitude()
286: - outProj.getProjectionLatitude()) > ProjectionUtils.EPS11) {
287: System.out
288: .println("For the projection with id: "
289: + id
290: + " the projectionLatitude differs:\n in ("
291: + ((ProjectedCRS) inCRS)
292: .getProjection()
293: .getDeegreeSpecificName()
294: + "): "
295: + Math
296: .toDegrees(inProj
297: .getProjectionLatitude())
298: + "\nout("
299: + ((ProjectedCRS) outCRS)
300: .getProjection()
301: .getDeegreeSpecificName()
302: + " with id: "
303: + ((ProjectedCRS) outCRS)
304: .getProjection()
305: .getIdentifier()
306: + "): "
307: + Math
308: .toDegrees(outProj
309: .getProjectionLatitude()));
310: }
311: } else {
312: notExported.add(inCRS);
313: System.out
314: .println(id
315: + " [FAILED] to retrieve from deegree-config.");
316: }
317: }
318: }
319: if (notExported.size() > 0) {
320: StringBuilder sb = new StringBuilder(notExported
321: .size() * 2000);
322: out.export(sb, allSystems);
323: if (outFile != null && !"".equals(outFile.trim())) {
324: File outputFile = new File(outFile);
325: BufferedWriter writer = new BufferedWriter(
326: new FileWriter(outputFile));
327: writer.write(sb.toString());
328: writer.flush();
329: writer.close();
330: } else {
331: System.out.println(sb.toString());
332: }
333: }
334: } else {
335:
336: StringBuilder sb = new StringBuilder(
337: allSystems.size() * 2000);
338: out.export(sb, allSystems);
339: if (outFile != null && !"".equals(outFile.trim())) {
340: File outputFile = new File(outFile);
341: BufferedWriter writer = new BufferedWriter(
342: new FileWriter(outputFile));
343: writer.write(sb.toString());
344: writer.flush();
345: writer.close();
346: } else {
347: System.out.println(sb.toString());
348: }
349: }
350: } catch (CRSConfigurationException e) {
351: e.printStackTrace();
352: } catch (IOException e) {
353: e.printStackTrace();
354: }
355:
356: // CRSConfiguration config = new CRSConfiguration(
357: }
358:
359: private static void outputHelp() {
360: StringBuilder sb = new StringBuilder();
361: sb
362: .append("The CRSConfiguration program can be used to create a deegree-crs-configuration, from other crs definition-formats. Following parameters are supported:\n");
363: sb.append("-inFile the /path/to/crs-definitions-file\n");
364: sb
365: .append("-inFormat the format of the input file, valid values are proj4(default),deegree \n");
366: sb
367: .append("-outFormat the format of the output file, valid values are deegree (default)\n");
368: sb
369: .append("-outFile the /path/to/the/output/file or standard output if not supplied.\n");
370: sb
371: .append("[-verify] checks the projection parameters of the inFormat against the deegree configuration.\n");
372: sb.append("-?|-h output this text\n");
373: sb
374: .append("example usage: java -cp deegree.jar org.deegree.crs.configuration.CRSConfiguration -inFormat 'proj4' -inFile '/home/proj4/nad/epsg' -outFormat 'deegree' -outFile '/home/deegree/crs-definitions.xml'\n");
375: System.out.println(sb.toString());
376: System.exit(1);
377: }
378:
379: /**
380: * @return the crs provider.
381: */
382: public final CRSProvider getProvider() {
383: return provider;
384: }
385: }
|