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.rasterings;
018:
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.List;
023:
024: import net.refractions.udig.catalog.IGeoResource;
025: import net.refractions.udig.catalog.IGeoResourceInfo;
026: import net.refractions.udig.catalog.IService;
027: import net.refractions.udig.catalog.rasterings.internal.Messages;
028:
029: import org.eclipse.core.runtime.IProgressMonitor;
030: import org.geotools.parameter.ParameterGroup;
031: import org.opengis.coverage.grid.Format;
032: import org.opengis.coverage.grid.GridCoverage;
033: import org.opengis.coverage.grid.GridCoverageReader;
034: import org.opengis.parameter.GeneralParameterValue;
035: import org.opengis.parameter.ParameterValueGroup;
036:
037: /**
038: * Provides a handle to a raster resource allowing the service to be lazily
039: * loaded.
040: * <p>
041: * This class provides functionality common to GridCoverage based resources.
042: * @author mleslie
043: * @since 0.6.0
044: */
045: public abstract class AbstractRasterGeoResource extends IGeoResource {
046: /**
047: * <code>service</code> field recalls the service that created
048: * this resource
049: */
050: protected AbstractRasterService service;
051: private GridCoverage coverage;
052: private ParameterGroup readParams;
053: private String name;
054: private Throwable msg;
055:
056: /**
057: * Construct <code>AbstractRasterGeoResource</code>.
058: *
059: * @param service The service creating this resource.
060: * @param name Human readable name of this resource.
061: */
062: public AbstractRasterGeoResource(AbstractRasterService service,
063: String name) {
064: this .service = service;
065: if (name == null) {
066: name = service.getIdentifier().getFile();
067: int slash = name.lastIndexOf('/');
068: name = name.substring((slash == -1
069: && slash < name.length() - 1 ? 0 : name
070: .lastIndexOf('/')) + 1,
071: (name.lastIndexOf('.') == -1 ? name.length() : name
072: .lastIndexOf('.')));
073:
074: }
075: this .name = name;
076: }
077:
078: public Status getStatus() {
079: return this .service.getStatus();
080: }
081:
082: public Throwable getMessage() {
083: if (msg != null) {
084: return msg;
085: } else {
086: return this .service.getMessage();
087: }
088: }
089:
090: /**
091: * Retrieves the parameters used to create the GridCoverageReader
092: * for this resource. This simply delegates the creation of these
093: * parameters to a GridFormat.
094: *
095: * @return ParameterGroup describing the GeoResource
096: */
097: public ParameterGroup getReadParameters() {
098: if (this .readParams == null) {
099: ParameterValueGroup desc = ((Format) this .service
100: .getFormat()).getReadParameters();
101: if (desc == null)
102: return null;
103: if (desc instanceof ParameterGroup) {
104: this .readParams = (ParameterGroup) desc;
105: }
106: }
107: return this .readParams;
108: }
109:
110: /**
111: * Finds or creates the GridCoverage for this resource.
112: *
113: * @return GridCoverage for this GeoResource
114: * @throws IOException
115: */
116: public Object findResource() throws IOException {
117: if (this .coverage == null) {
118: try {
119: GridCoverageReader reader = this .service
120: .getReader(null);
121: ParameterGroup pvg = getReadParameters();
122: List list = pvg.values();
123: @SuppressWarnings("unchecked") GeneralParameterValue[] values = //$NON-NLS-1$
124: (GeneralParameterValue[]) list
125: .toArray(new GeneralParameterValue[0]);
126: this .coverage = reader.read(values);
127: } catch (Throwable t) {
128: msg = t;
129: }
130: }
131: return this .coverage;
132: }
133:
134: public URL getIdentifier() {
135: try {
136: return new URL(this .service.getIdentifier().toString()
137: + "#" + this .name); //$NON_NLS-1$ //$NON-NLS-1$
138: } catch (MalformedURLException ex) {
139: msg = ex;
140: return this .service.getIdentifier();
141: }
142: }
143:
144: public <T> T resolve(Class<T> adaptee, IProgressMonitor monitor)
145: throws IOException {
146: if (monitor != null)
147: monitor.beginTask(
148: Messages.AbstractRasterGeoResource_resolve, 3);
149: if (adaptee == null) {
150: if (monitor != null)
151: monitor.done();
152: return null;
153: }
154: if (adaptee.isAssignableFrom(GridCoverage.class)) {
155: if (monitor != null)
156: monitor.done();
157: return adaptee.cast(findResource());
158: }
159: if (adaptee.isAssignableFrom(GridCoverageReader.class)) {
160: return adaptee.cast(service.getReader(monitor));
161: }
162: if (monitor != null)
163: monitor.worked(1);
164: if (monitor != null)
165: monitor.worked(1);
166: if (adaptee.isAssignableFrom(IGeoResourceInfo.class)) {
167: if (monitor != null)
168: monitor.done();
169: return adaptee.cast(getInfo(monitor));
170: }
171: return super .resolve(adaptee, monitor);
172: }
173:
174: public IService service(IProgressMonitor monitor)
175: throws IOException {
176: return service;
177: }
178:
179: public <T> boolean canResolve(Class<T> adaptee) {
180: if (adaptee == null)
181: return false;
182: return (adaptee.isAssignableFrom(IGeoResourceInfo.class)
183: || adaptee.isAssignableFrom(IService.class)
184: || adaptee.isAssignableFrom(GridCoverage.class) || adaptee
185: .isAssignableFrom(GridCoverageReader.class))
186: || super .canResolve(adaptee);
187: }
188:
189: public abstract IGeoResourceInfo getInfo(IProgressMonitor monitor)
190: throws IOException;
191:
192: }
|