001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006-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.catalog.shapefile;
017:
018: import java.io.IOException;
019: import java.net.URI;
020: import java.net.URISyntaxException;
021:
022: import org.geotools.catalog.AbstractGeoResource;
023: import org.geotools.catalog.GeoResource;
024: import org.geotools.catalog.GeoResourceInfo;
025: import org.geotools.catalog.Service;
026: import org.geotools.catalog.defaults.DefaultGeoResourceInfo;
027: import org.geotools.data.DataStore;
028: import org.geotools.data.FeatureSource;
029: import org.geotools.data.FeatureStore;
030: import org.geotools.feature.Feature;
031: import org.geotools.feature.FeatureIterator;
032: import org.geotools.feature.FeatureType;
033: import org.geotools.geometry.jts.ReferencedEnvelope;
034: import org.geotools.util.ProgressListener;
035: import org.opengis.referencing.crs.CoordinateReferenceSystem;
036:
037: import com.vividsolutions.jts.geom.Envelope;
038:
039: /**
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/catalog/shapefile/ShapefileGeoResource.java $
041: */
042: public class ShapefileGeoResource extends AbstractGeoResource {
043: private ShapefileService parent;
044: private String typeName;
045: private Throwable msg;
046: private GeoResourceInfo info;
047: private FeatureSource featureSource;
048:
049: ShapefileGeoResource(ShapefileService parent, String typeName) {
050: this .parent = parent;
051: this .typeName = typeName;
052: }
053:
054: public boolean canResolve(Class adaptee) {
055: if (adaptee == null) {
056: return false;
057: }
058:
059: return adaptee.isAssignableFrom(Service.class)
060: || adaptee.isAssignableFrom(GeoResourceInfo.class)
061: || adaptee.isAssignableFrom(FeatureStore.class)
062: || adaptee.isAssignableFrom(FeatureSource.class);
063: }
064:
065: public Object resolve(Class adaptee, ProgressListener monitor)
066: throws IOException {
067: if (adaptee == null) {
068: return null;
069: }
070:
071: if (adaptee.isAssignableFrom(Service.class)) {
072: return parent;
073: }
074:
075: if (adaptee.isAssignableFrom(GeoResource.class)) {
076: return getInfo(monitor);
077: }
078:
079: if (adaptee.isAssignableFrom(FeatureStore.class)) {
080: FeatureSource featureSource = getFeatureSource(monitor);
081:
082: if (featureSource instanceof FeatureStore) {
083: return featureSource;
084: }
085: }
086:
087: if (adaptee.isAssignableFrom(FeatureSource.class)) {
088: return getFeatureSource(monitor);
089: }
090:
091: return null;
092: }
093:
094: public GeoResourceInfo getInfo(ProgressListener monitor)
095: throws IOException {
096: if (info == null) {
097: synchronized (parent.getDataStore(monitor)) {
098: if (info == null) {
099: //calculate some meta data based on the feature type
100: FeatureType type = getFeatureSource(monitor)
101: .getSchema();
102: CoordinateReferenceSystem crs = type
103: .getDefaultGeometry().getCoordinateSystem();
104: URI schema = type.getNamespace();
105: String name = type.getTypeName();
106: String title = name;
107: String description = name;
108: String[] keywords = new String[] { ".shp",
109: "Shapefile", name, schema.toString() };
110:
111: //calculate bounds
112: ReferencedEnvelope bounds = null;
113:
114: try {
115: Envelope tmpBounds = getFeatureSource(monitor)
116: .getBounds();
117:
118: if (tmpBounds instanceof ReferencedEnvelope) {
119: bounds = (ReferencedEnvelope) tmpBounds;
120: } else {
121: bounds = new ReferencedEnvelope(tmpBounds,
122: crs);
123: }
124:
125: if (bounds == null) {
126: bounds = new ReferencedEnvelope(
127: new Envelope(), crs);
128: FeatureIterator reader = getFeatureSource(
129: monitor).getFeatures().features();
130: try {
131: while (reader.hasNext()) {
132: Feature element = reader.next();
133:
134: if (bounds.isNull()) {
135: bounds
136: .init(element
137: .getBounds());
138: } else {
139: bounds.expandToInclude(element
140: .getBounds());
141: }
142: }
143: } finally {
144: reader.close();
145: }
146: }
147: } catch (Exception e) {
148: //something bad happend, return an i dont know
149: bounds = new ReferencedEnvelope(new Envelope(),
150: crs);
151: }
152:
153: info = new DefaultGeoResourceInfo(title, name,
154: description, schema, bounds, crs, keywords,
155: null);
156: }
157: }
158: }
159:
160: return info;
161: }
162:
163: protected FeatureSource getFeatureSource(ProgressListener monitor)
164: throws IOException {
165: if (featureSource == null) {
166: synchronized (parent.getDataStore(monitor)) {
167: if (featureSource == null) {
168: try {
169: msg = null;
170:
171: DataStore dataStore = parent
172: .getDataStore(monitor);
173:
174: if (dataStore != null) {
175: featureSource = dataStore
176: .getFeatureSource(typeName);
177: }
178: } catch (IOException ioe) {
179: msg = ioe;
180: throw ioe;
181: } catch (Throwable t) {
182: msg = t;
183: throw (IOException) new IOException()
184: .initCause(t);
185: }
186: }
187: }
188: }
189:
190: return featureSource;
191: }
192:
193: public Status getStatus() {
194: if (msg == null) {
195: if (featureSource != null) {
196: return Status.CONNECTED;
197: }
198:
199: return Status.NOTCONNECTED;
200: }
201:
202: return Status.BROKEN;
203: }
204:
205: public Throwable getMessage() {
206: return msg;
207: }
208:
209: public URI getIdentifier() {
210: URI uri = parent.getIdentifier();
211:
212: if (uri != null) {
213: try {
214: return new URI(uri.toString() + "#" + typeName);
215: } catch (URISyntaxException e) {
216: return null;
217: }
218: }
219:
220: return null;
221: }
222: }
|