001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.project.internal.impl;
016:
017: import java.net.URI;
018:
019: import org.eclipse.jface.resource.ImageDescriptor;
020: import org.geotools.geometry.jts.JTS;
021: import org.geotools.geometry.jts.ReferencedEnvelope;
022: import org.geotools.referencing.CRS;
023: import org.geotools.referencing.crs.DefaultGeographicCRS;
024: import org.opengis.referencing.FactoryException;
025: import org.opengis.referencing.crs.CoordinateReferenceSystem;
026: import org.opengis.referencing.operation.TransformException;
027:
028: import com.vividsolutions.jts.geom.Envelope;
029:
030: import net.refractions.udig.catalog.IGeoResourceInfo;
031: import net.refractions.udig.project.ILayer;
032: import net.refractions.udig.project.IResourceInterceptor;
033: import net.refractions.udig.project.internal.ProjectPlugin;
034:
035: /**
036: * Intercepts the IGeoResource Info and wraps it with a decorator that modifies getCRS and getBounds if the CRS has been set on the layer.
037: * <p>
038: * This is for the use case where the CRS on the IGeoResource is wrong
039: *
040: * @author Jesse
041: * @since 1.1.0
042: */
043: public class GeoResourceInfoInterceptor implements
044: IResourceInterceptor<IGeoResourceInfo> {
045:
046: /**
047: * @deprecated here as a work around for a bug in the CRS Generation not having a valid area
048: */
049: static final ReferencedEnvelope UNKNOWN_BOUNDS = new ReferencedEnvelope(
050: -179.99, 179.99, -79.99, 79.99, DefaultGeographicCRS.WGS84);
051:
052: public IGeoResourceInfo run(ILayer layer,
053: IGeoResourceInfo resource, Class requestedType) {
054: if (layer instanceof LayerImpl) {
055: LayerImpl impl = (LayerImpl) layer;
056: return new Wrapper(impl, resource);
057: }
058: return resource;
059: }
060:
061: private static class Wrapper extends IGeoResourceInfo {
062: private final IGeoResourceInfo info;
063: private LayerImpl layer;
064:
065: public Wrapper(LayerImpl impl, final IGeoResourceInfo info) {
066: super ();
067: this .info = info;
068: this .layer = impl;
069: }
070:
071: public ReferencedEnvelope getBounds() {
072:
073: ReferencedEnvelope tmp = info.getBounds();
074: if (tmp == null) {
075:
076: ReferencedEnvelope referencedEnvelope = MapImpl
077: .toReferencedEnvelope(getCRS().getValidArea(),
078: getCRS());
079: if (referencedEnvelope != null)
080: return referencedEnvelope;
081:
082: tmp = UNKNOWN_BOUNDS;
083: }
084:
085: if (tmp.isNull()) {
086: return new ReferencedEnvelope(getCRS());
087: }
088: Envelope env;
089: try {
090: if (tmp.getCoordinateReferenceSystem() == null) {
091: tmp = new ReferencedEnvelope(tmp, getCRS());
092: } else {
093: env = JTS.transform(tmp, CRS.findMathTransform(tmp
094: .getCoordinateReferenceSystem(), getCRS()));
095: tmp = new ReferencedEnvelope(env, getCRS());
096: }
097: } catch (TransformException e) {
098: ProjectPlugin.log("Transformation Error", e);
099: } catch (FactoryException e) {
100: ProjectPlugin.log("Failure looking up math transform",
101: e);
102: }
103: return tmp;
104: }
105:
106: public CoordinateReferenceSystem getCRS() {
107: if (layer.cRS != null)
108: return layer.cRS;
109: return info.getCRS();
110: }
111:
112: public String getDescription() {
113: return info.getDescription();
114: }
115:
116: public ImageDescriptor getIcon() {
117: return info.getIcon();
118: }
119:
120: public String[] getKeywords() {
121: return info.getKeywords();
122: }
123:
124: public String getName() {
125: return info.getName();
126: }
127:
128: public URI getSchema() {
129: return info.getSchema();
130: }
131:
132: public String getTitle() {
133: return info.getTitle();
134: }
135:
136: public String toString() {
137: return info.toString();
138: }
139:
140: @Override
141: public int hashCode() {
142: final int PRIME = 31;
143: int result = 1;
144: result = PRIME * result
145: + ((info == null) ? 0 : info.hashCode());
146: return result;
147: }
148:
149: @Override
150: public boolean equals(Object obj) {
151: if (this == obj)
152: return true;
153: if (obj == null)
154: return false;
155: if (getClass() != obj.getClass())
156: return false;
157: final Wrapper other = (Wrapper) obj;
158: if (info == null) {
159: if (other.info != null)
160: return false;
161: } else if (!info.equals(other.info))
162: return false;
163: return true;
164: }
165:
166: }
167:
168: }
|