001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
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;
009: * version 2.1 of the License.
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: */
017: package net.refractions.udig.catalog.internal.gml;
018:
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URI;
022: import java.net.URL;
023:
024: import net.refractions.udig.catalog.CatalogPlugin;
025: import net.refractions.udig.catalog.IGeoResource;
026: import net.refractions.udig.catalog.IGeoResourceInfo;
027: import net.refractions.udig.catalog.IService;
028: import net.refractions.udig.catalog.gml.internal.Messages;
029: import net.refractions.udig.ui.graphics.Glyph;
030:
031: import org.eclipse.core.runtime.IProgressMonitor;
032: import org.eclipse.core.runtime.IStatus;
033: import org.geotools.data.FeatureSource;
034: import org.geotools.data.FeatureStore;
035: import org.geotools.feature.Feature;
036: import org.geotools.feature.FeatureIterator;
037: import org.geotools.feature.FeatureType;
038: import org.geotools.geometry.jts.ReferencedEnvelope;
039: import org.opengis.referencing.crs.CoordinateReferenceSystem;
040:
041: import com.vividsolutions.jts.geom.Envelope;
042:
043: /**
044: * Connect to a shapefile.
045: *
046: * @author David Zwiers, Refractions Research
047: * @since 0.6
048: */
049: public class GMLGeoResourceImpl extends IGeoResource {
050: GMLServiceImpl parent;
051: String typename = null;
052:
053: private GMLGeoResourceImpl() {/*not for use*/
054: }
055:
056: /**
057: * Construct <code>ShpGeoResourceImpl</code>.
058: *
059: * @param parent
060: * @param typename
061: */
062: public GMLGeoResourceImpl(GMLServiceImpl parent, String typename) {
063: this .parent = parent;
064: this .typename = typename;
065: }
066:
067: public URL getIdentifier() {
068: try {
069: return new URL(parent.getIdentifier().toString()
070: + "#" + typename); //$NON-NLS-1$
071: } catch (MalformedURLException e) {
072: return parent.getIdentifier();
073: }
074: }
075:
076: /*
077: * @see net.refractions.udig.catalog.IGeoResource#getStatus()
078: */
079: public Status getStatus() {
080: return parent.getStatus();
081: }
082:
083: /*
084: * @see net.refractions.udig.catalog.IGeoResource#getStatusMessage()
085: */
086: public Throwable getMessage() {
087: return parent.getMessage();
088: }
089:
090: /*
091: * Required adaptions:
092: * <ul>
093: * <li>IGeoResourceInfo.class
094: * <li>IService.class
095: * </ul>
096: * @see net.refractions.udig.catalog.IResolve#resolve(java.lang.Class, org.eclipse.core.runtime.IProgressMonitor)
097: */
098: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
099: throws IOException {
100: if (adaptee == null)
101: return null;
102:
103: if (adaptee.isAssignableFrom(IGeoResourceInfo.class))
104: return adaptee.cast(getInfo(monitor));
105: if (adaptee.isAssignableFrom(FeatureStore.class)) {
106: FeatureSource fs = parent.getDS(monitor).getFeatureSource(
107: typename);
108: if (fs instanceof FeatureStore)
109: return adaptee.cast(fs);
110: if (adaptee.isAssignableFrom(FeatureSource.class))
111: return adaptee.cast(parent.getDS(monitor)
112: .getFeatureSource(typename));
113: }
114: return super .resolve(adaptee, monitor);
115: }
116:
117: public IService service(IProgressMonitor monitor)
118: throws IOException {
119: return parent;
120: }
121:
122: /*
123: * @see net.refractions.udig.catalog.IResolve#canResolve(java.lang.Class)
124: */
125: public <T> boolean canResolve(Class<T> adaptee) {
126: if (adaptee == null)
127: return false;
128: return (adaptee.isAssignableFrom(IGeoResourceInfo.class)
129: || adaptee.isAssignableFrom(FeatureStore.class)
130: || adaptee.isAssignableFrom(FeatureSource.class) || adaptee
131: .isAssignableFrom(IService.class))
132: || super .canResolve(adaptee);
133: }
134:
135: private volatile IGeoResourceInfo info;
136:
137: public IGeoResourceInfo getInfo(IProgressMonitor monitor)
138: throws IOException {
139: if (info == null && getStatus() != Status.BROKEN) {
140: parent.dsLock.lock();
141: try {
142: if (info == null) {
143: info = new IGeoResourceGMLInfo();
144: }
145: } finally {
146: parent.dsLock.unlock();
147: }
148: }
149: return info;
150: }
151:
152: class IGeoResourceGMLInfo extends IGeoResourceInfo {
153: private FeatureType ft = null;
154:
155: IGeoResourceGMLInfo() throws IOException {
156: ft = parent.getDS(null).getSchema(typename);
157:
158: try {
159: FeatureSource source = parent.getDS(null)
160: .getFeatureSource(typename);
161: bounds = (ReferencedEnvelope) source.getBounds();
162: if (bounds == null) {
163: Envelope temp = new Envelope();
164: CoordinateReferenceSystem crs = ft
165: .getDefaultGeometry().getCoordinateSystem();
166: FeatureIterator iter = source.getFeatures()
167: .features();
168: try {
169: while (iter.hasNext()) {
170: Feature element = iter.next();
171: if (temp.isNull())
172: temp.init(element.getBounds());
173: else
174: temp.expandToInclude(element
175: .getBounds());
176: }
177: } finally {
178: iter.close();
179: }
180: bounds = new ReferencedEnvelope(temp, crs);
181: }
182: } catch (Exception e) {
183: CatalogPlugin
184: .getDefault()
185: .getLog()
186: .log(
187: new org.eclipse.core.runtime.Status(
188: IStatus.WARNING,
189: "net.refractions.udig.catalog", 0, Messages.GmlGeoResourceImpl_error_layer_bounds, e)); //$NON-NLS-1$
190: bounds = new ReferencedEnvelope(new Envelope(),
191: getCRS());
192: }
193:
194: icon = Glyph.icon(ft);
195: keywords = new String[] { ".shp", "Shapefile", //$NON-NLS-1$ //$NON-NLS-2$
196: ft.getTypeName(), ft.getNamespace().toString() };
197: }
198:
199: public CoordinateReferenceSystem getCRS() {
200: return null;
201: }
202:
203: public String getName() {
204: return ft.getTypeName();
205: }
206:
207: public URI getSchema() {
208: return ft.getNamespace();
209: }
210:
211: public String getTitle() {
212: return ft.getTypeName();
213: }
214: }
215: }
|