001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.internal.command.navigation;
010:
011: import java.text.MessageFormat;
012:
013: import net.refractions.udig.project.command.MapCommand;
014: import net.refractions.udig.project.internal.Messages;
015: import net.refractions.udig.project.internal.ProjectPlugin;
016: import net.refractions.udig.project.internal.render.ViewportModel;
017:
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.geotools.geometry.jts.JTS;
020: import org.geotools.geometry.jts.ReferencedEnvelope;
021: import org.geotools.referencing.CRS;
022: import org.opengis.referencing.crs.CoordinateReferenceSystem;
023: import org.opengis.referencing.operation.MathTransform;
024:
025: import com.vividsolutions.jts.geom.Envelope;
026:
027: /**
028: * Sets the viewport's bounding box. The bbox have a positive width and height
029: * and must have a aspect ratio within 0.0000001 units of the value returned by
030: * {@linkplain ViewportModel#getViewportAspectRatio()}.
031: *
032: * @author jeichar
033: * @since 0.3
034: */
035: public class SetViewportBBoxCommand extends AbstractNavCommand {
036:
037: private Envelope newbbox = null;
038:
039: private CoordinateReferenceSystem crs;
040:
041: /**
042: * Creates a new instance of SetViewportBBoxCommand. The bbox is expected to be the same as the viewport model.
043: *
044: * @param bbox
045: * the new bounding box. The new bbox must have a positive width
046: * and height and must have a aspect ratio within 0.0000001 units
047: * of the value returned by
048: * {@linkplain ViewportModel#getViewportAspectRatio()}.
049: * @deprecated
050: */
051: public SetViewportBBoxCommand(Envelope bbox) {
052: this .newbbox = bbox;
053: if (bbox instanceof ReferencedEnvelope)
054: crs = ((ReferencedEnvelope) bbox)
055: .getCoordinateReferenceSystem();
056: }
057:
058: /**
059: * Creates a new instance of SetViewportBBoxCommand. The bbox is expected to be the same as the viewport model.
060: *
061: * @param bbox
062: * the new bounding box. The new bbox must have a positive width
063: * and height and must have a aspect ratio within 0.0000001 units
064: * of the value returned by
065: * {@linkplain ViewportModel#getViewportAspectRatio()}.
066: */
067: public SetViewportBBoxCommand(ReferencedEnvelope bbox) {
068: this .newbbox = bbox;
069: crs = ((ReferencedEnvelope) bbox)
070: .getCoordinateReferenceSystem();
071: }
072:
073: /**
074: * Sets the bounds of the viewport model to the bounds. The crs parameter indications the crs of
075: * the provided bounds. The appropriate transformation will take place.
076: *
077: * @param bounds the bounds to apply to the viewport model
078: * @param crs The crs of the provided bounds.
079: */
080: public SetViewportBBoxCommand(Envelope bounds,
081: CoordinateReferenceSystem crs) {
082: this (bounds);
083: this .crs = crs;
084: }
085:
086: /**
087: * @see net.refractions.udig.project.internal.command.MapCommand#copy()
088: */
089: public MapCommand copy() {
090: return new SetViewportBBoxCommand(newbbox, crs);
091: }
092:
093: /**
094: * @see net.refractions.udig.project.internal.command.navigation.AbstractNavCommand#runImpl()
095: */
096: protected void runImpl(IProgressMonitor monitor) {
097: try {
098: if (crs != null) {
099: MathTransform mt = CRS.transform(crs, model.getCRS(),
100: true);
101: if (!mt.isIdentity()) {
102: Envelope transformedBounds = JTS.transform(newbbox,
103: null, mt, 5);
104: crs = null;
105: newbbox = transformedBounds;
106: }
107: }
108: } catch (Exception e) {
109: ProjectPlugin
110: .log(
111: "Error transforming from " + crs.getName() + " to " + model.getCRS().getName(), e); //$NON-NLS-1$//$NON-NLS-2$
112: }
113: model.setBounds(newbbox);
114: }
115:
116: /**
117: * @see net.refractions.udig.project.command.MapCommand#getName()
118: */
119: public String getName() {
120: return MessageFormat.format(
121: Messages.SetViewportBBoxCommand_setViewArea,
122: new Object[] { newbbox });
123: }
124:
125: }
|