001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.wms.gce;
017:
018: import java.awt.image.BufferedImage;
019: import java.io.IOException;
020: import java.util.List;
021: import java.util.NoSuchElementException;
022:
023: import javax.imageio.ImageIO;
024:
025: import org.geotools.coverage.FactoryFinder;
026: import org.geotools.coverage.grid.GridCoverageFactory;
027: import org.geotools.data.wms.WebMapServer;
028: import org.geotools.data.wms.request.GetMapRequest;
029: import org.geotools.data.wms.response.GetMapResponse;
030: import org.geotools.geometry.GeneralEnvelope;
031: import org.geotools.parameter.Parameter;
032: import org.geotools.parameter.ParameterGroup;
033: import org.geotools.referencing.CRS;
034: import org.geotools.referencing.crs.DefaultGeographicCRS;
035: import org.opengis.coverage.MetadataNameNotFoundException;
036: import org.opengis.coverage.grid.Format;
037: import org.opengis.coverage.grid.GridCoverage;
038: import org.opengis.coverage.grid.GridCoverageReader;
039: import org.opengis.parameter.GeneralParameterValue;
040: import org.opengis.parameter.ParameterValue;
041: import org.opengis.referencing.FactoryException;
042: import org.opengis.referencing.crs.CoordinateReferenceSystem;
043: import org.opengis.referencing.operation.OperationNotFoundException;
044: import org.opengis.geometry.Envelope;
045: import org.xml.sax.SAXException;
046:
047: /**
048: *
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wms/src/main/java/org/geotools/data/wms/gce/WMSReader.java $
050: */
051: public class WMSReader implements GridCoverageReader {
052: private Object source;
053: private boolean hasNext = true;
054: private WMSFormat format;
055: private WebMapServer wms;
056:
057: /**
058: * Source must be a WebMapServer object
059: *
060: * @param source
061: * @throws IOException
062: *
063: * @throws RuntimeException DOCUMENT ME!
064: */
065: public WMSReader(Object source) throws IOException {
066: this .source = source;
067:
068: if (source instanceof WebMapServer) {
069: wms = (WebMapServer) source;
070: } else {
071: throw new RuntimeException(
072: "source is not of type WebMapServer");
073: }
074:
075: this .format = new WMSFormat(wms.getCapabilities());
076: }
077:
078: public Object getSource() {
079: return source;
080: }
081:
082: public String[] getMetadataNames() throws IOException {
083: // TODO Auto-generated method stub
084: return null;
085: }
086:
087: public String getMetadataValue(String arg0) throws IOException,
088: MetadataNameNotFoundException {
089: // TODO Auto-generated method stub
090: return null;
091: }
092:
093: public String[] listSubNames() throws IOException {
094: // TODO Auto-generated method stub
095: return null;
096: }
097:
098: public String getCurrentSubname() throws IOException {
099: // TODO Auto-generated method stub
100: return null;
101: }
102:
103: public boolean hasMoreGridCoverages() throws IOException {
104: return hasNext;
105: }
106:
107: public void skip() throws IOException {
108: hasNext = false;
109: }
110:
111: public void dispose() throws IOException {
112: hasNext = false;
113: }
114:
115: public void setFormat(WMSFormat format) {
116: this .format = format;
117: }
118:
119: /* (non-Javadoc)
120: * @see org.opengis.coverage.grid.GridCoverageReader#getFormat()
121: */
122: public Format getFormat() {
123: return format;
124: }
125:
126: /* (non-Javadoc)
127: * @see org.opengis.coverage.grid.GridCoverageReader#read(org.opengis.parameter.GeneralParameterValue[])
128: */
129: public GridCoverage read(GeneralParameterValue[] parameters)
130: throws IllegalArgumentException, IOException {
131: GetMapRequest request = wms.createGetMapRequest();
132: double minx = 0;
133: double miny = 0;
134: double maxx = 0;
135: double maxy = 0;
136:
137: CoordinateReferenceSystem crs = null;
138:
139: for (int i = 0; i < parameters.length; i++) {
140: GeneralParameterValue generalValue = parameters[i];
141:
142: String paramName = generalValue.getDescriptor().getName()
143: .getCode();
144:
145: if ((generalValue == null)) {
146: continue;
147: }
148:
149: if (paramName.equals("LAYERS")) {
150: ParameterGroup groupValue = (ParameterGroup) generalValue;
151: String layers = "";
152: String styles = "";
153:
154: List layerList = (List) groupValue.values();
155:
156: for (int j = 0; j < layerList.size(); j++) {
157: Parameter parameter = (Parameter) layerList.get(j);
158:
159: layers = layers
160: + parameter.getDescriptor().getName()
161: .getCode();
162: styles = styles + (String) parameter.getValue();
163:
164: if (j < (layerList.size() - 1)) {
165: layers = layers + ",";
166: styles = styles + ",";
167: }
168: }
169:
170: request.setProperty("LAYERS", layers);
171: request.setProperty("STYLES", styles);
172:
173: continue;
174: }
175:
176: ParameterValue value = (ParameterValue) generalValue;
177:
178: if (paramName.equals("BBOX_MINX")) {
179: minx = ((Double) value.getValue()).doubleValue();
180:
181: continue;
182: }
183:
184: if (paramName.equals("BBOX_MINY")) {
185: miny = ((Double) value.getValue()).doubleValue();
186:
187: continue;
188: }
189:
190: if (paramName.equals("BBOX_MAXX")) {
191: maxx = ((Double) value.getValue()).doubleValue();
192:
193: continue;
194: }
195:
196: if (paramName.equals("BBOX_MAXY")) {
197: maxy = ((Double) value.getValue()).doubleValue();
198:
199: continue;
200: }
201:
202: if (paramName.equals("HEIGHT")) {
203: request.setProperty("HEIGHT", ((Integer) value
204: .getValue()).toString());
205:
206: continue;
207: }
208:
209: if (paramName.equals("WIDTH")) {
210: request.setProperty("WIDTH", ((Integer) value
211: .getValue()).toString());
212:
213: continue;
214: }
215:
216: if (paramName.equals("TRANSPARENT")) {
217: if (value.booleanValue()) {
218: request.setProperty("TRANSPARENT", "TRUE");
219: } else {
220: request.setProperty("TRANSPARENT", "FALSE");
221: }
222:
223: continue;
224: }
225:
226: if (value.getValue() == null) {
227: continue;
228: }
229:
230: if (paramName.equals("SRS")) {
231: String srs = value.stringValue();
232:
233: try {
234: crs = CRS.decode(srs);
235: } catch (FactoryException e1) {
236: // TODO Auto-generated catch block
237: e1.printStackTrace();
238: }
239: }
240:
241: request.setProperty(value.getDescriptor().getName()
242: .getCode(), value.stringValue());
243: }
244:
245: String bbox = minx + "," + miny + "," + maxx + "," + maxy;
246: request.setProperty("BBOX", bbox);
247:
248: GetMapResponse response;
249: try {
250: response = (GetMapResponse) wms.issueRequest(request);
251: } catch (SAXException e) {
252: e.printStackTrace();
253: throw new IOException(e.getLocalizedMessage());
254: }
255: BufferedImage image = ImageIO.read(response.getInputStream());
256:
257: if (image == null) {
258: throw new IOException("Image cannot be read from:"
259: + response);
260: }
261:
262: if (crs == null) {
263: crs = DefaultGeographicCRS.WGS84;
264: }
265:
266: GeneralEnvelope envelope = new GeneralEnvelope(new double[] {
267: minx, miny }, new double[] { maxx, maxy });
268: envelope.setCoordinateReferenceSystem(crs);
269:
270: hasNext = false;
271:
272: GridCoverageFactory factory = FactoryFinder
273: .getGridCoverageFactory(null);
274: GridCoverage coverage = factory.create("wmsMap", image,
275: envelope);
276:
277: return coverage;
278: }
279: }
|