001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.util;
006:
007: import org.geotools.coverage.grid.io.GridFormatFactorySpi;
008: import org.geotools.coverage.grid.io.GridFormatFinder;
009: import org.geotools.geometry.GeneralEnvelope;
010: import org.geotools.referencing.CRS;
011: import org.geotools.referencing.crs.DefaultGeographicCRS;
012: import org.geotools.resources.CRSUtilities;
013: import org.opengis.coverage.grid.Format;
014: import org.opengis.parameter.ParameterDescriptor;
015: import org.opengis.parameter.ParameterValue;
016: import org.opengis.parameter.ParameterValueGroup;
017: import org.opengis.referencing.FactoryException;
018: import org.opengis.referencing.crs.CoordinateReferenceSystem;
019: import org.opengis.referencing.operation.MathTransform;
020: import org.opengis.referencing.operation.TransformException;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.logging.Logger;
029: import javax.servlet.ServletContext;
030:
031: /**
032: * A collection of utilties for dealing with GeotTools Format.
033: *
034: * @author Richard Gould, Refractions Research, Inc.
035: * @author cholmesny
036: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
037: * modification)
038: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
039: * modification)
040: * @version $Id: CoverageStoreUtils.java,v 1.12 2004/09/21 21:14:48 cholmesny
041: * Exp $
042: */
043: public final class CoverageStoreUtils {
044: private final static Logger LOGGER = org.geotools.util.logging.Logging
045: .getLogger(CoverageStoreUtils.class.toString());
046: public final static Format[] formats = GridFormatFinder
047: .getFormatArray();
048:
049: private CoverageStoreUtils() {
050: }
051:
052: public static Format acquireFormat(String type, ServletContext sc)
053: throws IOException {
054: Format[] formats = GridFormatFinder.getFormatArray();
055: Format format = null;
056: final int length = formats.length;
057:
058: for (int i = 0; i < length; i++) {
059: if (formats[i].getName().equals(type)) {
060: format = formats[i];
061:
062: break;
063: }
064: }
065:
066: if (format == null) {
067: throw new IOException("Cannot handle format: " + type);
068: } else {
069: return format;
070: }
071: }
072:
073: // public static Map getParams(Map m, ServletContext sc) {
074: // String baseDir = sc.getRealPath("/");
075: // return Collections.synchronizedMap(getParams(m, baseDir));
076: // }
077: //
078: // /**
079: // * Get Connect params.
080: // */
081: // public static Map getParams(Map m, String baseDir) {
082: // return Collections.synchronizedMap(CoverageStoreInfo.getParams(m, baseDir));
083: // }
084:
085: /**
086: * Utility method for finding Params
087: *
088: * @param factory
089: * DOCUMENT ME!
090: * @param key
091: * DOCUMENT ME!
092: *
093: * @return DOCUMENT ME!
094: */
095: public static ParameterValue find(Format format, String key) {
096: return find(format.getReadParameters(), key);
097: }
098:
099: /**
100: * Utility methods for find param by key
101: *
102: * @param params
103: * DOCUMENT ME!
104: * @param key
105: * DOCUMENT ME!
106: *
107: * @return DOCUMENT ME!
108: */
109: public static ParameterValue find(ParameterValueGroup params,
110: String key) {
111: List list = params.values();
112: Iterator it = list.iterator();
113: ParameterDescriptor descr;
114: ParameterValue val;
115:
116: while (it.hasNext()) {
117: val = (ParameterValue) it.next();
118: descr = (ParameterDescriptor) val.getDescriptor();
119:
120: if (key.equalsIgnoreCase(descr.getName().toString())) {
121: return val;
122: }
123: }
124:
125: return null;
126: }
127:
128: /**
129: * When loading from DTO use the params to locate factory.
130: *
131: * <p>
132: * bleck
133: * </p>
134: *
135: * @return
136: */
137: public static Format aquireFactoryByType(String type) {
138: final Format[] formats = GridFormatFinder.getFormatArray();
139: Format format = null;
140: final int length = formats.length;
141:
142: for (int i = 0; i < length; i++) {
143: format = formats[i];
144:
145: if (format.getName().equals(type)) {
146: return format;
147: }
148: }
149:
150: return null;
151: }
152:
153: /**
154: * After user has selected Description can aquire Format based on
155: * description.
156: *
157: * @param description
158: *
159: * @return
160: */
161: public static Format aquireFactory(String description) {
162: Format[] formats = GridFormatFinder.getFormatArray();
163: Format format = null;
164: final int length = formats.length;
165:
166: for (int i = 0; i < length; i++) {
167: format = formats[i];
168:
169: if (format.getDescription().equals(description)) {
170: return format;
171: }
172: }
173:
174: return null;
175: }
176:
177: /**
178: * Returns the descriptions for the available DataFormats.
179: *
180: * <p>
181: * Arrrg! Put these in the select box.
182: * </p>
183: *
184: * @return Descriptions for user to choose from
185: */
186: public static List listDataFormatsDescriptions() {
187: List list = new ArrayList();
188: Format[] formats = GridFormatFinder.getFormatArray();
189: final int length = formats.length;
190:
191: for (int i = 0; i < length; i++) {
192: if (!list.contains(formats[i].getDescription())) {
193: list.add(formats[i].getDescription());
194: }
195: }
196:
197: return Collections.synchronizedList(list);
198: }
199:
200: public static List listDataFormats() {
201: List list = new ArrayList();
202: Format[] formats = GridFormatFinder.getFormatArray();
203: final int length = formats.length;
204:
205: for (int i = 0; i < length; i++) {
206: if (!list.contains(formats[i])) {
207: list.add(formats[i]);
208: }
209: }
210:
211: return Collections.synchronizedList(list);
212: }
213:
214: public static Map defaultParams(String description) {
215: return Collections
216: .synchronizedMap(defaultParams(aquireFactory(description)));
217: }
218:
219: public static Map defaultParams(Format factory) {
220: Map defaults = new HashMap();
221: ParameterValueGroup params = factory.getReadParameters();
222:
223: if (params != null) {
224: List list = params.values();
225: Iterator it = list.iterator();
226: ParameterDescriptor descr = null;
227: ParameterValue val = null;
228: String key;
229: Object value;
230:
231: while (it.hasNext()) {
232: val = (ParameterValue) it.next();
233: descr = (ParameterDescriptor) val.getDescriptor();
234:
235: key = descr.getName().toString();
236: value = null;
237:
238: if (val.getValue() != null) {
239: // Required params may have nice sample values
240: //
241: if ("values_palette".equalsIgnoreCase(key)) {
242: value = val.getValue();
243: } else {
244: value = val.getValue().toString();
245: }
246: }
247:
248: if (value == null) {
249: // or not
250: value = "";
251: }
252:
253: if (value != null) {
254: defaults.put(key, value);
255: }
256: }
257: }
258:
259: return Collections.synchronizedMap(defaults);
260: }
261:
262: /**
263: * Convert map to real values based on factory Params.
264: *
265: * @param factory
266: * @param params
267: *
268: * @return Map with real values that may be acceptable to GDSFactory
269: *
270: * @throws IOException
271: * DOCUMENT ME!
272: */
273: public static Map toParams(GridFormatFactorySpi factory, Map params)
274: throws IOException {
275: final Map map = new HashMap(params.size());
276:
277: final ParameterValueGroup info = factory.createFormat()
278: .getReadParameters();
279: String key;
280: Object value;
281:
282: // Convert Params into the kind of Map we actually need
283: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
284: key = (String) i.next();
285: value = find(info, key).getValue();
286:
287: if (value != null) {
288: map.put(key, value);
289: }
290: }
291:
292: return Collections.synchronizedMap(map);
293: }
294:
295: /**
296: * Retrieve a WGS84 lon,lat envelope from the provided one.
297: *
298: * @param sourceCRS
299: * @param targetEnvelope
300: * @return
301: * @throws IndexOutOfBoundsException
302: * @throws FactoryException
303: * @throws TransformException
304: */
305: public static GeneralEnvelope getWGS84LonLatEnvelope(
306: GeneralEnvelope envelope) throws IndexOutOfBoundsException,
307: FactoryException, TransformException {
308: final CoordinateReferenceSystem sourceCRS = envelope
309: .getCoordinateReferenceSystem();
310:
311: ////
312: //
313: // Do we need to transform?
314: //
315: ////
316: if (CRSUtilities.equalsIgnoreMetadata(sourceCRS,
317: DefaultGeographicCRS.WGS84)) {
318: return new GeneralEnvelope(envelope);
319: }
320:
321: ////
322: //
323: //transform
324: //
325: ////
326: final CoordinateReferenceSystem targetCRS = DefaultGeographicCRS.WGS84;
327: final MathTransform mathTransform = CRS.findMathTransform(
328: sourceCRS, targetCRS, true);
329: final GeneralEnvelope targetEnvelope;
330:
331: if (!mathTransform.isIdentity()) {
332: targetEnvelope = CRS.transform(mathTransform, envelope);
333: } else {
334: targetEnvelope = new GeneralEnvelope(envelope);
335: }
336:
337: targetEnvelope.setCoordinateReferenceSystem(targetCRS);
338:
339: return targetEnvelope;
340: }
341:
342: // /**
343: // * Get a generic envelope and retrieve a lon,lat envelope.
344: // *
345: // * @param sourceCRS
346: // * @param envelope
347: // * @return
348: // * @throws IndexOutOfBoundsException
349: // * @throws MismatchedDimensionException
350: // * @throws NoSuchAuthorityCodeException
351: // */
352: // public static GeneralEnvelope adjustEnvelopeLongitudeFirst(
353: // final CoordinateReferenceSystem sourceCRS, GeneralEnvelope envelope)
354: // throws IndexOutOfBoundsException, MismatchedDimensionException,
355: // NoSuchAuthorityCodeException {
356: //
357: // // /////////////////////////////////////////////////////////////////////
358: // //
359: // // Is Lon first?
360: // //
361: // // /////////////////////////////////////////////////////////////////////
362: // final CoordinateReferenceSystem crs2D;
363: // try {
364: // crs2D = CRSUtilities.getCRS2D(sourceCRS);
365: // } catch (TransformException e) {
366: // LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
367: // return null;
368: // }
369: // final CoordinateSystem sourceCS = crs2D.getCoordinateSystem();
370: // final boolean lonFirst = !GridGeometry2D.swapXY(sourceCS);
371: //
372: // // /////////////////////////////////////////////////////////////////////
373: // //
374: // // Creating a new envelope lon,lat
375: // //
376: // // /////////////////////////////////////////////////////////////////////
377: // final GeneralEnvelope lonLatEnvelope = lonFirst ? new GeneralEnvelope(
378: // envelope) : new GeneralEnvelope(new double[] {
379: // envelope.getLowerCorner().getOrdinate(1),
380: // envelope.getLowerCorner().getOrdinate(0) }, new double[] {
381: // envelope.getUpperCorner().getOrdinate(1),
382: // envelope.getUpperCorner().getOrdinate(0) });
383: // lonLatEnvelope.setCoordinateReferenceSystem(crs2D);
384: // return lonLatEnvelope;
385: // }
386: }
|