001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/tools/srs/SRSInfo.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.tools.srs;
044:
045: import java.io.File;
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.HashMap;
051: import java.util.List;
052:
053: import org.deegree.crs.configuration.CRSConfiguration;
054: import org.deegree.crs.configuration.CRSProvider;
055: import org.deegree.crs.coordinatesystems.CoordinateSystem;
056:
057: /**
058: * A utility program to inform the callee about the availability (-isAvailable param) of a certain crs or to retrieve
059: * all available crs's from the deegree crs configuration.
060: *
061: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
062: * @author last edited by: $Author: rbezema $
063: *
064: * @version $Revision: 10291 $, $Date: 2008-02-25 09:02:31 -0800 (Mon, 25 Feb 2008) $
065: */
066: public class SRSInfo {
067: private final CRSProvider provider;
068:
069: SRSInfo() {
070: CRSConfiguration config = CRSConfiguration
071: .getCRSConfiguration();
072: provider = config.getProvider();
073: if (provider == null) {
074: System.out
075: .println("Could not retrieve a deegree crs-provider instance, this may not be, please make sure your deegree installation uses a correct crs-configuration. Exiting!");
076: }
077: }
078:
079: /**
080: * returns true if the the passed SRS is available in deegree
081: *
082: * @param srs
083: * @return <code>true</code> if the the passed SRS is available in deegree
084: */
085: private boolean isAvailable(String srs) {
086: return provider.getCRSByID(srs) != null;
087: }
088:
089: /**
090: * @return a list of crs's with following layout 1) crsid[0], crsid[1] ... etc.
091: */
092: private List<String> getAll() {
093: List<CoordinateSystem> avCRS = provider.getAvailableCRSs();
094: List<String> allCRSs = new ArrayList<String>();
095: if (avCRS != null && avCRS.size() > 0) {
096: for (CoordinateSystem crs : avCRS) {
097: String[] ids = crs.getIdentifiers();
098: if (ids != null) {
099: StringBuilder sb = new StringBuilder(300);
100: //sb.append( ( count++ ) + ") " );
101: for (int i = 0; i < ids.length; ++i) {
102: sb.append(ids[i]);
103: if (i + 1 < ids.length) {
104: sb.append(", ");
105: }
106: }
107: allCRSs.add(sb.toString());
108: }
109: }
110: Collections.sort(allCRSs);
111: /*
112: StringBuilder out = new StringBuilder( 20000000 );
113: provider.export( out, avCRS );
114:
115: try {
116: BufferedWriter bw = new BufferedWriter( new FileWriter( "/dev/shm/crs-config.xml" ) );
117: bw.write( out.toString() );
118: bw.flush();
119: bw.close();
120: } catch ( IOException e ) {
121: System.out.println( e );
122: }*/
123: }
124:
125: return allCRSs;
126: }
127:
128: /**
129: * @param args
130: * following parameters are supported:
131: * <ul>
132: * <li>[-isAvailable srsName]</li>
133: * <li>[-file outputfile]</li>
134: * </ul>
135: */
136: public static void main(String[] args) {
137:
138: SRSInfo srsinfo = new SRSInfo();
139:
140: HashMap<String, String> params = new HashMap<String, String>();
141: for (int i = 0; i < args.length; i++) {
142: String arg = args[i];
143: if (arg != null && !"".equals(arg.trim())) {
144: arg = arg.trim();
145: if (arg.equalsIgnoreCase("-?")
146: || arg.equalsIgnoreCase("-h")) {
147: outputHelp();
148: } else {
149: if (i + 1 < args.length) {
150: String val = args[++i];
151: if (val != null && !"".equals(val.trim())) {
152: params.put(arg, val.trim());
153: } else {
154: System.out
155: .println("Invalid value for parameter: "
156: + arg);
157: }
158: } else {
159: System.out.println("No value for parameter: "
160: + arg);
161: }
162: }
163: }
164: }
165: String availableCRS = params.get("-isAvailable");
166: if (availableCRS != null && !"".equals(availableCRS.trim())) {
167: System.out.println("Coordinates System: "
168: + availableCRS
169: + " is "
170: + ((srsinfo.isAvailable(availableCRS.trim())) ? ""
171: : "not ") + "available in deegree");
172: } else {
173: List<String> availableCRSs = srsinfo.getAll();
174: if (availableCRSs != null && availableCRSs.size() > 0) {
175: String file = params.get("-file");
176: if (file != null && !"".equals(file.trim())) {
177: File f = new File(file);
178: if (!f.exists() && f.canWrite()) {
179: System.out.println("Writing to file: "
180: + f.getAbsoluteFile());
181: try {
182: FileWriter fw = new FileWriter(f);
183:
184: for (String crs : availableCRSs) {
185: fw.write(crs);
186: }
187: fw.close();
188: } catch (IOException e) {
189: System.out
190: .println("An exception occurred while trying to write to file: "
191: + f.getAbsoluteFile()
192: + "\n message:\n"
193: + e.getMessage());
194: e.printStackTrace();
195: }
196:
197: System.exit(1);
198: } else {
199: System.out
200: .println("The given file: "
201: + f.getAbsoluteFile()
202: + " could not be written to, outputting to standard out.");
203: }
204: } else {
205: System.out
206: .println("No File given (-file param) writing to standard out.");
207: }
208: int count = 1;
209: for (String crs : availableCRSs) {
210: System.out.println((count++) + ")" + crs);
211: }
212: } else {
213: System.out
214: .println("No Coordinate Systems configured, this is very strange!");
215: }
216: }
217: }
218:
219: private static void outputHelp() {
220: StringBuilder sb = new StringBuilder();
221: sb
222: .append("The SRSInfo program can be used to output all available crs's configured in deegree and\n");
223: sb
224: .append("will give you an affirmation on an available crs.\n\n");
225: sb.append("Following parameters are supported:\n");
226: sb
227: .append("[-isAvailable] crs_id -- will give you an affirmation if the given crs is available.\n");
228: sb
229: .append("[-file] if [-isAvailable] is not given, the -file parameter can be used to write all configured crs_s to, if not given standard out will be used.\n");
230: sb.append("-?|-h output this text\n");
231: System.out.println(sb.toString());
232: System.exit(1);
233: }
234: }
|