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.worldimage;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.nio.channels.Channels;
027: import java.nio.channels.ReadableByteChannel;
028: import java.util.HashMap;
029:
030: import net.refractions.udig.catalog.CatalogPlugin;
031: import net.refractions.udig.catalog.IGeoResourceInfo;
032: import net.refractions.udig.catalog.URLUtils;
033: import net.refractions.udig.catalog.rasterings.AbstractRasterGeoResource;
034: import net.refractions.udig.catalog.worldimage.internal.Messages;
035:
036: import org.eclipse.core.runtime.IProgressMonitor;
037: import org.eclipse.core.runtime.IStatus;
038: import org.geotools.data.shapefile.prj.PrjFileReader;
039: import org.geotools.gce.image.WorldImageFormat;
040: import org.geotools.geometry.jts.ReferencedEnvelope;
041: import org.geotools.parameter.DefaultParameterDescriptor;
042: import org.geotools.parameter.DefaultParameterDescriptorGroup;
043: import org.geotools.parameter.ParameterGroup;
044: import org.geotools.referencing.crs.DefaultGeographicCRS;
045: import org.opengis.coverage.grid.GridCoverage;
046: import org.opengis.parameter.GeneralParameterDescriptor;
047: import org.opengis.parameter.ParameterDescriptor;
048: import org.opengis.referencing.FactoryException;
049: import org.opengis.referencing.crs.CoordinateReferenceSystem;
050:
051: import com.vividsolutions.jts.geom.Envelope;
052:
053: /**
054: * Provides a handle to a world image resource allowing the service to be lazily
055: * loaded.
056: * @author mleslie
057: * @since 0.6.0
058: */
059: public class WorldImageGeoResourceImpl extends
060: AbstractRasterGeoResource {
061: private IGeoResourceInfo info;
062: private URL prjURL;
063: String name;
064:
065: /**
066: * Construct <code>WorldImageGeoResourceImpl</code>.
067: *
068: * @param service Service creating this resource.
069: * @param name Human readable name of this resource.
070: * @param prjURL Name a projection file associated with this resource
071: * can be expected to have.
072: */
073: public WorldImageGeoResourceImpl(WorldImageServiceImpl service,
074: String name, URL prjURL) {
075: super (service, name);
076: this .prjURL = prjURL;
077: }
078:
079: public IGeoResourceInfo getInfo(IProgressMonitor monitor)
080: throws IOException {
081: if (this .info == null && getStatus() != Status.BROKEN) {
082: this .info = new IGeoResourceWorldImageInfo();
083: }
084: return this .info;
085: }
086:
087: /**
088: * Convenience method for opening a PrjFileReader. Stolen from Geotools
089: * ShapefileSomethingROther
090: *
091: * @return A new PrjFileReader
092: *
093: * @throws IOException If an error occurs during creation.
094: */
095: private PrjFileReader openPrjReader(URL prjURL) throws IOException,
096: FactoryException {
097: ReadableByteChannel rbc = null;
098: try {
099: rbc = getReadChannel(prjURL);
100: } catch (IOException e) {
101: CatalogPlugin
102: .getDefault()
103: .getLog()
104: .log(
105: new org.eclipse.core.runtime.Status(
106: IStatus.WARNING,
107: "net.refractions.udig.catalog", 0, //$NON-NLS-1$
108: Messages.WorldImageGeoResourceImpl_PrjUnavailable,
109: e));
110: }
111: if (rbc == null) {
112: return null;
113: }
114:
115: return new PrjFileReader(rbc);
116: }
117:
118: /**
119: * Convienience method to create a ReadableByteChannel from a URL.
120: *
121: * @param prjURL
122: * @return A Channel for the given file
123: * @throws IOException
124: */
125: private ReadableByteChannel getReadChannel(URL prjURL)
126: throws IOException {
127: ReadableByteChannel channel = null;
128:
129: if (prjURL.getProtocol().equalsIgnoreCase("file")) { //$NON-NLS-1$
130: File file = URLUtils.urlToFile(prjURL);
131:
132: if (!file.exists() || !file.canRead()) {
133: throw new FileNotFoundException(file.getAbsolutePath());
134: }
135: FileInputStream in = new FileInputStream(file);
136: channel = in.getChannel();
137:
138: } else {
139: InputStream in = prjURL.openConnection().getInputStream();
140: channel = Channels.newChannel(in);
141: }
142:
143: return channel;
144: }
145:
146: public ParameterGroup getReadParameters() {
147: try {
148: PrjFileReader prjRead = null;
149: try {
150: prjRead = openPrjReader(this .prjURL);
151: } catch (FileNotFoundException e) {
152: CatalogPlugin.getDefault().getLog().log(
153: new org.eclipse.core.runtime.Status(
154: IStatus.WARNING,
155: "net.refractions.udig.catalog", 0, //$NON-NLS-1$
156: "", e)); //$NON-NLS-1$
157: }
158: CoordinateReferenceSystem crsSys = null;
159: if (prjRead != null) {
160: crsSys = prjRead.getCoodinateSystem();
161: } else {
162: //prj file not read, default to lat long
163: crsSys = DefaultGeographicCRS.WGS84;
164: }
165:
166: DefaultParameterDescriptor crs = new DefaultParameterDescriptor(
167: "crs", //$NON-NLS-1$
168: CoordinateReferenceSystem.class, null, crsSys);
169: ParameterDescriptor env = WorldImageFormat.ENVELOPE;
170:
171: // Stolen from WorldImageFormat, as mInfo is not externally accesible
172: HashMap<String, String> info1 = new HashMap<String, String>();
173: info1.put("name", "WorldImage"); //$NON-NLS-1$//$NON-NLS-2$
174: info1.put("description", //$NON-NLS-1$
175: "A raster file accompanied by a spatial data file"); //$NON-NLS-1$
176: info1.put("vendor", "Geotools"); //$NON-NLS-1$ //$NON-NLS-2$
177: info1
178: .put(
179: "docURL", "http://www.geotools.org/WorldImageReader+formats"); //$NON-NLS-1$ //$NON-NLS-2$
180: info1.put("version", "1.0"); //$NON-NLS-1$ //$NON-NLS-2$
181: return new ParameterGroup(
182: new DefaultParameterDescriptorGroup(
183: info1,
184: new GeneralParameterDescriptor[] { crs, env }));
185:
186: } catch (MalformedURLException e) {
187: CatalogPlugin.getDefault().getLog().log(
188: new org.eclipse.core.runtime.Status(
189: IStatus.WARNING,
190: "net.refractions.udig.catalog", 0, //$NON-NLS-1$
191: "", e)); //$NON-NLS-1$
192: return super .getReadParameters();
193: } catch (IOException e) {
194: CatalogPlugin.getDefault().getLog().log(
195: new org.eclipse.core.runtime.Status(
196: IStatus.WARNING,
197: "net.refractions.udig.catalog", 0, //$NON-NLS-1$
198: "", e)); //$NON-NLS-1$
199: return super .getReadParameters();
200: } catch (FactoryException e) {
201: CatalogPlugin.getDefault().getLog().log(
202: new org.eclipse.core.runtime.Status(
203: IStatus.WARNING,
204: "net.refractions.udig.catalog", 0, //$NON-NLS-1$
205: "", e)); //$NON-NLS-1$
206: return super .getReadParameters();
207: }
208: }
209:
210: /**
211: * Describes this Resource.
212: * @author mleslie
213: * @since 0.6.0
214: */
215: public class IGeoResourceWorldImageInfo extends IGeoResourceInfo {
216: IGeoResourceWorldImageInfo() {
217: this .keywords = new String[] {
218: "WorldImage", "world image", ".gif", ".jpg", ".jpeg", //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ //$NON-NLS-5$
219: ".tif", ".tiff", ".png" }; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
220: this .name = ((new File(getIdentifier().getFile()))
221: .getName());
222: this .title = name;
223: this .description = getIdentifier().toString();
224: this .bounds = getBounds();
225: }
226:
227: /*
228: * @see net.refractions.udig.catalog.IGeoResourceInfo#getBounds()
229: */
230: public ReferencedEnvelope getBounds() {
231: if (this .bounds == null) {
232: Envelope env = null;
233: try {
234: GridCoverage source = (GridCoverage) findResource();
235: org.opengis.spatialschema.geometry.Envelope ptBounds = source
236: .getEnvelope();
237: env = new Envelope(ptBounds.getMinimum(0), ptBounds
238: .getMaximum(0), ptBounds.getMinimum(1),
239: ptBounds.getMaximum(1));
240:
241: CoordinateReferenceSystem geomcrs = source
242: .getCoordinateReferenceSystem();
243:
244: this .bounds = new ReferencedEnvelope(env, geomcrs);
245: /*
246: if(geomcrs != null) {
247: if(!geomcrs.equals(CRS.decode("EPSG:4269"))) { //$NON-NLS-1$
248: bounds = JTS.transform(bounds, CRS.decode("EPSG:4269")); //$NON-NLS-1$
249: }
250: } else {
251: System.err.println("CRS unknown for WorldImage"); //$NON-NLS-1$
252: }
253: */
254: } catch (Exception e) {
255: CatalogPlugin
256: .getDefault()
257: .getLog()
258: .log(
259: new org.eclipse.core.runtime.Status(
260: IStatus.WARNING,
261: "net.refractions.udig.catalog", 0, //$NON-NLS-1$
262: "Error while getting the bounds of a layer", e)); //$NON-NLS-1$
263:
264: this .bounds = new ReferencedEnvelope(new Envelope(
265: -180, 180, -90, 90),
266: DefaultGeographicCRS.WGS84);
267: }
268: }
269: return this.bounds;
270: }
271: }
272: }
|