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