001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2005, Refractions Research Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.catalog.wms;
018:
019: import java.io.IOException;
020: import java.net.URI;
021: import java.util.ArrayList;
022: import java.util.Arrays;
023: import java.util.List;
024:
025: import org.geotools.catalog.AbstractGeoResource;
026: import org.geotools.catalog.GeoResource;
027: import org.geotools.catalog.GeoResourceInfo;
028: import org.geotools.catalog.Resolve;
029: import org.geotools.catalog.ResolveChangeEvent;
030: import org.geotools.catalog.ResolveDelta;
031: import org.geotools.catalog.Service;
032: import org.geotools.catalog.defaults.DefaultGeoResourceInfo;
033: import org.geotools.catalog.defaults.DefaultResolveChangeEvent;
034: import org.geotools.catalog.defaults.DefaultResolveDelta;
035: import org.geotools.data.ows.WMSCapabilities;
036: import org.geotools.data.wms.WebMapServer;
037: import org.geotools.data.wms.xml.WMSSchema;
038: import org.geotools.geometry.jts.ReferencedEnvelope;
039: import org.geotools.referencing.CRS;
040: import org.geotools.referencing.crs.DefaultGeographicCRS;
041: import org.geotools.util.ProgressListener;
042: import org.opengis.referencing.FactoryException;
043: import org.opengis.referencing.crs.CoordinateReferenceSystem;
044:
045: import com.vividsolutions.jts.geom.Envelope;
046:
047: /**
048: * FeatureType provided by WFS.
049: * </p>
050: *
051: * @author David Zwiers, Refractions Research
052: * @author Richard Gould
053: * @author Justin Deoliveira, The Open Planning Project
054: * @since 0.6
055: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wms/src/main/java/org/geotools/catalog/wms/WMSGeoResource.java $
056: */
057: public class WMSGeoResource extends AbstractGeoResource {
058:
059: WMSService parent;
060: org.geotools.data.ows.Layer layer;
061: private GeoResourceInfo info;
062:
063: private WMSGeoResource() {
064: // should not be used
065: }
066:
067: /**
068: * Construct <code>WMSGeoResourceImpl</code>.
069: *
070: * @param parent
071: * @param layer
072: */
073: public WMSGeoResource(WMSService parent,
074: org.geotools.data.ows.Layer layer) {
075: this .parent = parent;
076: this .layer = layer;
077: }
078:
079: public Resolve parent(ProgressListener monitor) throws IOException {
080: return parent;
081: }
082:
083: /*
084: * @see net.refractions.udig.catalog.IGeoResource#getStatus()
085: */
086: public Status getStatus() {
087: return parent.getStatus();
088: }
089:
090: public GeoResourceInfo getInfo(ProgressListener monitor)
091: throws IOException {
092: if (info == null) {
093: info = new WMSResourceInfo(monitor);
094: ResolveDelta delta = new DefaultResolveDelta(this ,
095: ResolveDelta.Kind.CHANGED);
096: parent(monitor)
097: .parent(monitor)
098: .fire(
099: new DefaultResolveChangeEvent(
100: this ,
101: ResolveChangeEvent.Type.POST_CHANGE,
102: delta));
103:
104: }
105: return info;
106: }
107:
108: public URI getIdentifier() {
109: try {
110: return new URI(parent.getIdentifier().toString()
111: + "#" + layer.getName()); //$NON-NLS-1$
112: } catch (Throwable e) {
113: return parent.getIdentifier();
114: }
115: }
116:
117: /*
118: * @see net.refractions.udig.catalog.IGeoResource#resolve(java.lang.Class,
119: * org.eclipse.core.runtime.IProgressMonitor)
120: */
121: public Object resolve(Class adaptee, ProgressListener monitor)
122: throws IOException {
123: if (adaptee == null) {
124: return null;
125: }
126:
127: if (adaptee.isAssignableFrom(Service.class)) {
128: return parent;
129: }
130:
131: if (adaptee.isAssignableFrom(GeoResource.class)) {
132: return this ;
133: }
134:
135: if (adaptee.isAssignableFrom(GeoResourceInfo.class)) {
136: return getInfo(monitor);
137: }
138:
139: if (adaptee.isAssignableFrom(WebMapServer.class)) {
140: return parent.getWMS(monitor);
141: }
142:
143: if (adaptee.isAssignableFrom(org.geotools.data.ows.Layer.class)) {
144: return layer;
145: }
146:
147: return null;
148: }
149:
150: /*
151: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
152: */
153: public boolean canResolve(Class adaptee) {
154: if (adaptee == null) {
155: return false;
156: }
157:
158: if (adaptee.isAssignableFrom(GeoResource.class)
159: || adaptee.isAssignableFrom(WebMapServer.class)
160: || adaptee
161: .isAssignableFrom(org.geotools.data.ows.Layer.class)
162: || adaptee.isAssignableFrom(Service.class)) {
163: return true;
164: }
165:
166: return false;
167: }
168:
169: /*
170: * @see net.refractions.udig.catalog.IResolve#getMessage()
171: */
172: public Throwable getMessage() {
173: return parent.getMessage();
174: }
175:
176: private class WMSResourceInfo extends DefaultGeoResourceInfo {
177:
178: WMSResourceInfo(ProgressListener monitor) throws IOException {
179: WebMapServer wms = parent.getWMS(monitor);
180: WMSCapabilities caps = wms.getCapabilities();
181:
182: org.opengis.geometry.Envelope env = null;
183: CoordinateReferenceSystem crs = null;
184: try {
185: crs = CRS.decode("EPSG:4326"); //$NON-NLS-1$
186: env = parent.getWMS(null).getEnvelope(layer, crs);
187: } catch (FactoryException e) {
188: throw (IOException) new IOException(
189: "Bounds not available").initCause(e); //$NON-NLS-1$
190: }
191: bounds = new ReferencedEnvelope(new Envelope(env
192: .getMinimum(0), env.getMaximum(0), env
193: .getMinimum(1), env.getMaximum(1)),
194: DefaultGeographicCRS.WGS84);
195:
196: String parentid = parent != null
197: && parent.getIdentifier() != null ? getIdentifier()
198: .toString() : ""; //$NON-NLS-1$
199: name = layer.getName();
200: List keywordsFromWMS = new ArrayList();
201: if (caps.getService().getKeywordList() != null) {
202: keywordsFromWMS.addAll(Arrays.asList(caps.getService()
203: .getKeywordList()));
204: }
205:
206: if (layer.getKeywords() != null) {
207: keywordsFromWMS.addAll(Arrays.asList(layer
208: .getKeywords()));
209: }
210: keywordsFromWMS.add("WMS"); //$NON-NLS-1$
211: keywordsFromWMS.add(layer.getName());
212: keywordsFromWMS.add(caps.getService().getName());
213: keywordsFromWMS.add(parentid);
214: keywords = (String[]) keywordsFromWMS
215: .toArray(new String[keywordsFromWMS.size()]);
216:
217: if (layer.get_abstract() != null
218: && layer.get_abstract().length() != 0) {
219: description = layer.get_abstract();
220: } else {
221: description = caps.getService().get_abstract();
222: }
223: description = caps.getService().get_abstract();
224:
225: if (layer.getTitle() != null
226: && layer.getTitle().length() != 0) {
227: title = layer.getTitle();
228: } else {
229: title = caps.getService().getTitle();
230: }
231: }
232:
233: public String getName() {
234: return layer.getName();
235: }
236:
237: public URI getSchema() {
238: return WMSSchema.NAMESPACE;
239: }
240:
241: public String getTitle() {
242: return getName();
243: }
244: }
245: }
|