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.GeneralGridGeometry;
008: import org.geotools.coverage.grid.io.AbstractGridFormat;
009: import org.geotools.factory.Hints;
010: import org.geotools.geometry.GeneralEnvelope;
011: import org.geotools.parameter.DefaultParameterDescriptor;
012: import org.geotools.referencing.CRS;
013: import org.geotools.referencing.operation.BufferedCoordinateOperationFactory;
014: import org.opengis.parameter.GeneralParameterValue;
015: import org.opengis.parameter.ParameterDescriptor;
016: import org.opengis.parameter.ParameterValue;
017: import org.opengis.parameter.ParameterValueGroup;
018: import org.opengis.referencing.FactoryException;
019: import org.opengis.referencing.crs.CoordinateReferenceSystem;
020: import org.opengis.referencing.operation.CoordinateOperation;
021: import org.opengis.referencing.operation.MathTransform;
022: import org.opengis.referencing.operation.OperationNotFoundException;
023: import java.awt.Color;
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Vector;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: /**
034: * DOCUMENT ME!
035: *
036: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
037: * modification)
038: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
039: * modification)
040: */
041: public class CoverageUtils {
042: private final static BufferedCoordinateOperationFactory operationFactory = new BufferedCoordinateOperationFactory(
043: new Hints(Hints.LENIENT_DATUM_SHIFT, Boolean.TRUE));
044: private final static Logger LOGGER = org.geotools.util.logging.Logging
045: .getLogger(CoverageUtils.class.toString());
046: public static final int TRANSPARENT = 0;
047: public static final int OPAQUE = 1;
048:
049: public static GeneralParameterValue[] getParameters(
050: ParameterValueGroup params) {
051: final List parameters = new ArrayList();
052: final String readGeometryKey = AbstractGridFormat.READ_GRIDGEOMETRY2D
053: .getName().toString();
054:
055: if ((params != null) && (params.values().size() > 0)) {
056: List list = params.values();
057: Iterator it = list.iterator();
058: ParameterDescriptor descr = null;
059: ParameterValue val = null;
060:
061: while (it.hasNext()) {
062: val = (ParameterValue) it.next();
063:
064: if (val != null) {
065: descr = (ParameterDescriptor) val.getDescriptor();
066:
067: String _key = descr.getName().toString();
068:
069: if ("namespace".equals(_key)) {
070: // skip namespace as it is *magic* and
071: // appears to be an entry used in all dataformats?
072: //
073: continue;
074: }
075:
076: // /////////////////////////////////////////////////////////
077: //
078: // request param for better management of coverage
079: //
080: // /////////////////////////////////////////////////////////
081: if (_key.equalsIgnoreCase(readGeometryKey)) {
082: // IGNORING READ_GRIDGEOMETRY2D param
083: continue;
084: }
085:
086: Object value = val.getValue();
087:
088: parameters.add(new DefaultParameterDescriptor(_key,
089: value.getClass(), null, value)
090: .createValue());
091: }
092: }
093:
094: return (!parameters.isEmpty()) ? (GeneralParameterValue[]) parameters
095: .toArray(new GeneralParameterValue[parameters
096: .size()])
097: : null;
098: } else {
099: return null;
100: }
101: }
102:
103: public static GeneralParameterValue[] getParameters(
104: ParameterValueGroup params, Map values) {
105: return getParameters(params, values, false);
106: }
107:
108: public static GeneralParameterValue[] getParameters(
109: ParameterValueGroup params, Map values, boolean readGeom) {
110: final List parameters = new ArrayList();
111: final String readGeometryKey = AbstractGridFormat.READ_GRIDGEOMETRY2D
112: .getName().toString();
113:
114: if ((params != null) && (params.values().size() > 0)) {
115: List list = params.values();
116: Iterator it = list.iterator();
117: ParameterDescriptor descr = null;
118: ParameterValue val = null;
119:
120: while (it.hasNext()) {
121: val = (ParameterValue) it.next();
122:
123: if (val != null) {
124: descr = (ParameterDescriptor) val.getDescriptor();
125:
126: String _key = descr.getName().toString();
127:
128: if ("namespace".equals(_key)) {
129: // skip namespace as it is *magic* and
130: // appears to be an entry used in all dataformats?
131: //
132: continue;
133: }
134:
135: // /////////////////////////////////////////////////////////
136: //
137: // request param for better management of coverage
138: //
139: // /////////////////////////////////////////////////////////
140: if (_key.equalsIgnoreCase(readGeometryKey)
141: && !readGeom) {
142: // IGNORING READ_GRIDGEOMETRY2D param
143: continue;
144: }
145:
146: // /////////////////////////////////////////////////////////
147: //
148: // format specific params
149: //
150: // /////////////////////////////////////////////////////////
151: Object value = CoverageUtils.getCvParamValue(_key,
152: val, values);
153:
154: if ((value == null)
155: && (_key
156: .equalsIgnoreCase("InputTransparentColor") || _key
157: .equalsIgnoreCase("OutputTransparentColor"))) {
158: parameters.add(new DefaultParameterDescriptor(
159: _key, Color.class, null, value)
160: .createValue());
161: } else {
162: parameters.add(new DefaultParameterDescriptor(
163: _key, value.getClass(), null, value)
164: .createValue());
165: }
166: }
167: }
168:
169: return (!parameters.isEmpty()) ? (GeneralParameterValue[]) parameters
170: .toArray(new GeneralParameterValue[parameters
171: .size()])
172: : null;
173: } else {
174: return null;
175: }
176: }
177:
178: public static Map getParametersKVP(ParameterValueGroup params) {
179: final Map parameters = new HashMap();
180: final String readGeometryKey = AbstractGridFormat.READ_GRIDGEOMETRY2D
181: .getName().toString();
182:
183: if ((params != null) && (params.values().size() > 0)) {
184: List list = params.values();
185: Iterator it = list.iterator();
186: ParameterDescriptor descr = null;
187: ParameterValue val = null;
188:
189: while (it.hasNext()) {
190: val = (ParameterValue) it.next();
191:
192: if (val != null) {
193: descr = (ParameterDescriptor) val.getDescriptor();
194:
195: String _key = descr.getName().toString();
196:
197: if ("namespace".equals(_key)) {
198: // skip namespace as it is *magic* and
199: // appears to be an entry used in all dataformats?
200: //
201: continue;
202: }
203:
204: // /////////////////////////////////////////////////////////
205: //
206: // request param for better management of coverage
207: //
208: // /////////////////////////////////////////////////////////
209: if (_key.equalsIgnoreCase(readGeometryKey)) {
210: // IGNORING READ_GRIDGEOMETRY2D param
211: continue;
212: }
213:
214: Object value = val.getValue();
215: String text = "";
216:
217: if (value == null) {
218: text = null;
219: } else if (value instanceof String) {
220: text = (String) value;
221: } else {
222: text = value.toString();
223: }
224:
225: parameters.put(_key, (text != null) ? text : "");
226: }
227: }
228:
229: return parameters;
230: } else {
231: return parameters;
232: }
233: }
234:
235: /**
236: * @param paramValues
237: * @param key
238: * @param param
239: * @return
240: */
241: public static Object getCvParamValue(final String key,
242: ParameterValue param, final List paramValues,
243: final int index) {
244: Object value = null;
245:
246: try {
247: if (key.equalsIgnoreCase("crs")) {
248: if ((getParamValue(paramValues, index) != null)
249: && (((String) getParamValue(paramValues, index))
250: .length() > 0)) {
251: if ((paramValues.get(index) != null)
252: && (((String) paramValues.get(index))
253: .length() > 0)) {
254: value = CRS.parseWKT((String) paramValues
255: .get(index));
256: }
257: } else {
258: LOGGER
259: .info("Unable to find a crs for the coverage param, using EPSG:4326");
260: value = CRS.decode("EPSG:4326");
261: }
262: } else if (key.equalsIgnoreCase("envelope")) {
263: if ((getParamValue(paramValues, index) != null)
264: && (((String) getParamValue(paramValues, index))
265: .length() > 0)) {
266: String tmp = (String) getParamValue(paramValues,
267: index);
268:
269: if ((tmp.indexOf("[") > 0)
270: && (tmp.indexOf("]") > tmp.indexOf("["))) {
271: tmp = tmp.substring(tmp.indexOf("[") + 1,
272: tmp.indexOf("]")).trim();
273: tmp = tmp.replaceAll(",", "");
274:
275: String[] strCoords = tmp.split(" ");
276: double[] coords = new double[strCoords.length];
277:
278: if (strCoords.length == 4) {
279: for (int iT = 0; iT < 4; iT++) {
280: coords[iT] = Double
281: .parseDouble(strCoords[iT]
282: .trim());
283: }
284:
285: value = (org.opengis.geometry.Envelope) new GeneralEnvelope(
286: new double[] { coords[0], coords[1] },
287: new double[] { coords[2], coords[3] });
288: }
289: }
290: }
291: } else if (key.equalsIgnoreCase("values_palette")) {
292: if ((getParamValue(paramValues, index) != null)
293: && (((String) getParamValue(paramValues, index))
294: .length() > 0)) {
295: String tmp = (String) getParamValue(paramValues,
296: index);
297: String[] strColors = tmp.split(";");
298: Vector colors = new Vector();
299:
300: for (int col = 0; col < strColors.length; col++) {
301: if (Color.decode(strColors[col]) != null) {
302: colors.add(Color.decode(strColors[col]));
303: }
304: }
305:
306: value = colors.toArray(new Color[colors.size()]);
307: } else {
308: value = "#000000;#3C3C3C;#FFFFFF";
309: }
310: } else {
311: Class[] clArray = { getParamValue(paramValues, index)
312: .getClass() };
313: Object[] inArray = { getParamValue(paramValues, index) };
314: value = param.getValue().getClass().getConstructor(
315: clArray).newInstance(inArray);
316: }
317:
318: // Intentionally generic exception catched
319: } catch (Exception e) {
320: value = null;
321:
322: // errors.add("paramValue[" + i + "]",
323: // new ActionError("error.dataFormatEditor.param.parse", key,
324: // getParamValue(i).getClass(), e));
325: }
326:
327: return value;
328: }
329:
330: private static String getParamValue(final List paramValues,
331: final int index) {
332: return (String) paramValues.get(index);
333: }
334:
335: /**
336: * @param params
337: * @param key
338: * @param param
339: * @return
340: */
341: public static Object getCvParamValue(final String key,
342: ParameterValue param, final Map params) {
343: Object value = null;
344:
345: try {
346: if (key.equalsIgnoreCase("crs")) {
347: if ((params.get(key) != null)
348: && (((String) params.get(key)).length() > 0)) {
349: value = CRS.parseWKT((String) params.get(key));
350: } else {
351: LOGGER
352: .info("Unable to find a crs for the coverage param, using EPSG:4326");
353: value = CRS.decode("EPSG:4326");
354: }
355: } else if (key.equalsIgnoreCase("envelope")) {
356: if ((params.get(key) != null)
357: && (((String) params.get(key)).length() > 0)) {
358: String tmp = (String) params.get(key);
359:
360: if ((tmp.indexOf("[") > 0)
361: && (tmp.indexOf("]") > tmp.indexOf("["))) {
362: tmp = tmp.substring(tmp.indexOf("[") + 1,
363: tmp.indexOf("]")).trim();
364: tmp = tmp.replaceAll(",", "");
365:
366: String[] strCoords = tmp.split(" ");
367: double[] coords = new double[strCoords.length];
368:
369: if (strCoords.length == 4) {
370: for (int iT = 0; iT < 4; iT++) {
371: coords[iT] = Double
372: .parseDouble(strCoords[iT]
373: .trim());
374: }
375:
376: value = (org.opengis.geometry.Envelope) new GeneralEnvelope(
377: new double[] { coords[0], coords[1] },
378: new double[] { coords[2], coords[3] });
379: }
380: }
381: }
382: } else if (key
383: .equalsIgnoreCase(AbstractGridFormat.READ_GRIDGEOMETRY2D
384: .getName().toString())) {
385: if ((params.get(key) != null)
386: && params.get(key) instanceof String
387: && (((String) params.get(key)).length() > 0)) {
388: String tmp = (String) params.get(key);
389:
390: if ((tmp.indexOf("[") > 0)
391: && (tmp.indexOf("]") > tmp.indexOf("["))) {
392: tmp = tmp.substring(tmp.indexOf("[") + 1,
393: tmp.indexOf("]")).trim();
394: tmp = tmp.replaceAll(",", "");
395:
396: String[] strCoords = tmp.split(" ");
397: double[] coords = new double[strCoords.length];
398:
399: if (strCoords.length == 4) {
400: for (int iT = 0; iT < 4; iT++) {
401: coords[iT] = Double
402: .parseDouble(strCoords[iT]
403: .trim());
404: }
405:
406: value = (org.opengis.geometry.Envelope) new GeneralEnvelope(
407: new double[] { coords[0], coords[1] },
408: new double[] { coords[2], coords[3] });
409: }
410: }
411: } else if ((params.get(key) != null)
412: && params.get(key) instanceof GeneralGridGeometry) {
413: value = params.get(key);
414: }
415: } else if (key.equalsIgnoreCase("InputTransparentColor")
416: || key.equalsIgnoreCase("OutputTransparentColor")) {
417: if (params.get(key) != null) {
418: value = Color.decode((String) params.get(key));
419: } else {
420: Class[] clArray = { Color.class };
421: Object[] inArray = { params.get(key) };
422: value = param.getValue().getClass().getConstructor(
423: clArray).newInstance(inArray);
424: }
425: } else {
426: Class[] clArray = { String.class };
427: Object[] inArray = { params.get(key) };
428: value = param.getValue().getClass().getConstructor(
429: clArray).newInstance(inArray);
430: }
431: } catch (Exception e) {
432: value = param.getValue();
433: }
434:
435: return value;
436: }
437:
438: public static MathTransform getMathTransform(
439: CoordinateReferenceSystem sourceCRS,
440: CoordinateReferenceSystem destCRS) {
441: try {
442: CoordinateOperation op = operationFactory.createOperation(
443: sourceCRS, destCRS);
444:
445: if (op != null) {
446: return op.getMathTransform();
447: }
448: } catch (OperationNotFoundException e) {
449: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
450: } catch (FactoryException e) {
451: LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
452: }
453:
454: return null;
455: }
456: }
|