001: /**
002: * <copyright></copyright> $Id: AbstractContextImpl.java 24865 2007-03-22 00:58:04Z rgould $
003: */package net.refractions.udig.project.internal.impl;
004:
005: import java.awt.Dimension;
006: import java.awt.Point;
007: import java.awt.Rectangle;
008: import java.awt.Shape;
009: import java.awt.geom.AffineTransform;
010: import java.awt.geom.Point2D;
011: import java.io.IOException;
012: import java.util.List;
013:
014: import net.refractions.udig.project.IEditManager;
015: import net.refractions.udig.project.ILayer;
016: import net.refractions.udig.project.IMap;
017: import net.refractions.udig.project.IProject;
018: import net.refractions.udig.project.internal.AbstractContext;
019: import net.refractions.udig.project.internal.EditManager;
020: import net.refractions.udig.project.internal.Map;
021: import net.refractions.udig.project.internal.Project;
022: import net.refractions.udig.project.internal.render.RenderManager;
023: import net.refractions.udig.project.internal.render.ViewportModel;
024: import net.refractions.udig.project.render.IRenderManager;
025: import net.refractions.udig.project.render.IViewportModel;
026: import net.refractions.udig.project.render.displayAdapter.IMapDisplay;
027:
028: import org.geotools.data.FeatureSource;
029: import org.geotools.data.Query;
030: import org.geotools.feature.FeatureCollection;
031: import org.geotools.geometry.jts.JTS;
032: import org.geotools.geometry.jts.ReferencedEnvelope;
033: import org.geotools.referencing.CRS;
034: import org.geotools.referencing.FactoryFinder;
035: import org.geotools.referencing.operation.matrix.GeneralMatrix;
036: import org.geotools.renderer.lite.Decimator;
037: import org.geotools.renderer.lite.LiteShape2;
038: import org.opengis.referencing.FactoryException;
039: import org.opengis.referencing.crs.CoordinateReferenceSystem;
040: import org.opengis.referencing.operation.MathTransform;
041: import org.opengis.referencing.operation.MathTransform2D;
042: import org.opengis.referencing.operation.MathTransformFactory;
043: import org.opengis.referencing.operation.TransformException;
044:
045: import com.vividsolutions.jts.geom.Coordinate;
046: import com.vividsolutions.jts.geom.Envelope;
047: import com.vividsolutions.jts.geom.Geometry;
048: import com.vividsolutions.jts.geom.GeometryFactory;
049:
050: /**
051: * Default implementation
052: *
053: * @author Jesse
054: * @since 1.0.0
055: */
056: public abstract class AbstractContextImpl implements AbstractContext {
057:
058: /**
059: * The cached value of the '
060: * {@link #getRenderManagerInternal() <em>Render Manager Internal</em>}' reference.
061: *
062: * @see #getRenderManagerInternal()
063: */
064: protected RenderManager renderManagerInternal = null;
065:
066: /**
067: * The cached value of the '{@link #getMapInternal() <em>Map Internal</em>}' reference.
068: *
069: * @see #getMapInternal()
070: */
071: protected Map mapInternal = null;
072:
073: public AbstractContextImpl() {
074: // do nothing just so subclasses can call it
075: }
076:
077: public AbstractContextImpl(AbstractContextImpl impl) {
078: setMapInternal(impl.getMapInternal());
079: setRenderManagerInternal(impl.getRenderManagerInternal());
080: }
081:
082: public IViewportModel getViewportModel() {
083: RenderManager renderManagerInternal2 = getRenderManagerInternal();
084: if (renderManagerInternal2 != null) {
085: return renderManagerInternal2.getViewportModelInternal();
086: }
087: return getMap().getViewportModel();
088: }
089:
090: public IProject getProject() {
091: return getMapInternal().getProject();
092: }
093:
094: public IEditManager getEditManager() {
095: return getMapInternal().getEditManager();
096: }
097:
098: public Coordinate getPixelSize() {
099: return getViewportModel().getPixelSize();
100: }
101:
102: public ViewportModel getViewportModelInternal() {
103: return getMapInternal().getViewportModelInternal();
104: }
105:
106: public EditManager getEditManagerInternal() {
107: return getMapInternal().getEditManagerInternal();
108: }
109:
110: public RenderManager getRenderManagerInternal() {
111: return renderManagerInternal;
112: }
113:
114: public void setRenderManagerInternal(
115: RenderManager newRenderManagerInternal) {
116: renderManagerInternal = newRenderManagerInternal;
117: }
118:
119: public IMapDisplay getMapDisplay() {
120: if (getRenderManager() == null)
121: return null;
122: return getRenderManager().getMapDisplay();
123: }
124:
125: public IRenderManager getRenderManager() {
126: return getRenderManagerInternal();
127: }
128:
129: public IMap getMap() {
130: return getMapInternal();
131: }
132:
133: public Map getMapInternal() {
134: return mapInternal;
135: }
136:
137: public void setMapInternal(Map newMapInternal) {
138: mapInternal = newMapInternal;
139: }
140:
141: public Project getProjectInternal() {
142: return getMapInternal().getProjectInternal();
143: }
144:
145: public AffineTransform worldToScreenTransform() {
146: return getViewportModel().worldToScreenTransform();
147: }
148:
149: public Point worldToPixel(Coordinate coord) {
150: return getViewportModel().worldToPixel(coord);
151: }
152:
153: public MathTransform2D worldToScreenMathTransform() {
154: GeneralMatrix matrix = new GeneralMatrix(
155: getViewportModelInternal().worldToScreenTransform());
156: try {
157: return (MathTransform2D) FactoryFinder
158: .getMathTransformFactory(null)
159: .createAffineTransform(matrix);
160: } catch (Exception e) {
161: return null;
162: }
163: }
164:
165: public Coordinate pixelToWorld(int x, int y) {
166: return getViewportModel().pixelToWorld(x, y);
167: }
168:
169: public ReferencedEnvelope worldBounds(Rectangle rectangle) {
170: Envelope start = new Envelope(rectangle.x, rectangle.x
171: + rectangle.width, rectangle.y, rectangle.y
172: + rectangle.height);
173:
174: Envelope end = null;
175: try {
176: end = JTS.transform(start, null,
177: worldToScreenMathTransform().inverse(), 10);
178: } catch (Exception e) {
179: return null;
180: }
181: return new ReferencedEnvelope(end, getViewportModel().getCRS());
182: }
183:
184: public Envelope getPixelBoundingBox(Point screenLocation) {
185: return getBoundingBox(screenLocation, 1);
186: }
187:
188: public ReferencedEnvelope getBoundingBox(Point screenLocation,
189: int scalefactor) {
190: Coordinate center = pixelToWorld(screenLocation.x,
191: screenLocation.y);
192: Coordinate size = getPixelSize();
193: double dw = (size.x / 2) * scalefactor;
194: double dh = (size.y / 2) * scalefactor;
195: Envelope e = new Envelope(center.x - dw, center.x + dw,
196: center.y - dh, center.y + dh);
197: return new ReferencedEnvelope(e, getCRS());
198: }
199:
200: public CoordinateReferenceSystem getCRS() {
201: return getViewportModel().getCRS();
202: }
203:
204: public Shape toShape(ReferencedEnvelope envelope) {
205: return toShape(new GeometryFactory().toGeometry(envelope),
206: envelope.getCoordinateReferenceSystem());
207: }
208:
209: public Shape toShape(Geometry geometry,
210: CoordinateReferenceSystem crs) {
211: try {
212: MathTransform transform = CRS.findMathTransform(crs,
213: getCRS(), true);
214: MathTransformFactory factory = FactoryFinder
215: .getMathTransformFactory(null);
216: MathTransform toScreen = factory
217: .createAffineTransform(new GeneralMatrix(
218: worldToScreenTransform()));
219: transform = factory.createConcatenatedTransform(transform,
220: toScreen);
221: return new LiteShape2(geometry, transform, new Decimator(
222: transform), false);
223: } catch (FactoryException e) {
224: return null;
225: } catch (TransformException e) {
226: return null;
227: }
228: }
229:
230: public Query getQuery(ILayer layer, boolean selection) {
231: return layer.getQuery(selection);
232: }
233:
234: public FeatureCollection getFeaturesInBbox(ILayer layer,
235: Envelope bbox) throws IOException {
236: return layer.getResource(FeatureSource.class, null)
237: .getFeatures(layer.createBBoxFilter(bbox, null));
238: }
239:
240: /**
241: * @see net.refractions.udig.project.tool.ToolContext#getMapLayers()
242: */
243: public List<ILayer> getMapLayers() {
244: return getMap().getMapLayers();
245: }
246:
247: public ILayer getSelectedLayer() {
248: return getEditManager().getSelectedLayer();
249: }
250:
251: @Override
252: public int hashCode() {
253: final int PRIME = 31;
254: int result = 1;
255: result = PRIME * result
256: + ((mapInternal == null) ? 0 : mapInternal.hashCode());
257: result = PRIME
258: * result
259: + ((renderManagerInternal == null) ? 0
260: : renderManagerInternal.hashCode());
261: return result;
262: }
263:
264: @Override
265: public boolean equals(Object obj) {
266: if (this == obj)
267: return true;
268: if (obj == null)
269: return false;
270: if (getClass() != obj.getClass())
271: return false;
272: final AbstractContextImpl other = (AbstractContextImpl) obj;
273: if (mapInternal == null) {
274: if (other.mapInternal != null)
275: return false;
276: } else if (!mapInternal.equals(other.mapInternal))
277: return false;
278: if (renderManagerInternal == null) {
279: if (other.renderManagerInternal != null)
280: return false;
281: } else if (!renderManagerInternal
282: .equals(other.renderManagerInternal))
283: return false;
284: return true;
285: }
286:
287: public Point tranformCoordinate(Envelope bbox,
288: Dimension displaySize, Coordinate coordinate) {
289: AffineTransform at = getViewportModelInternal()
290: .worldToScreenTransform(bbox, displaySize);
291: Point2D w = new Point2D.Double(coordinate.x, coordinate.y);
292: Point2D p = at.transform(w, new Point2D.Double());
293: return new Point((int) p.getX(), (int) p.getY());
294: }
295:
296: } // AbstractContextImpl
|