Statistics
| Revision:

root / org.gvsig.proj / trunk / org.gvsig.proj / org.gvsig.proj.lib / org.gvsig.proj.lib.api / src / main / java / org / gvsig / proj / cts / DefaultIProjection.java @ 627

History | View | Annotate | Download (6.76 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2012 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.proj.cts;
24

    
25
import java.awt.Color;
26
import java.awt.Graphics2D;
27
import java.awt.geom.Point2D;
28
import java.awt.geom.Rectangle2D;
29

    
30
import org.cresques.cts.ICoordTrans;
31
import org.cresques.cts.IDatum;
32
import org.cresques.cts.IProjection;
33
import org.cresques.geo.ViewPortData;
34

    
35
import org.gvsig.proj.CoordinateReferenceSystem;
36
import org.gvsig.proj.CoordinateReferenceSystemLocator;
37
import org.gvsig.proj.CoordinateReferenceSystemNotFoundException;
38
import org.gvsig.proj.CoordinateTransformation;
39

    
40
/**
41
 * Java Map CoordinateReferenceSystem Library based implementation. It works as
42
 * a facade over a {@link CoordinateReferenceSystem} object.
43
 *
44
 * @author gvSIG Team
45
 */
46
public class DefaultIProjection implements IProjection {
47

    
48
    private CoordinateReferenceSystem crs;
49

    
50
    /**
51
     * Creates a {@link DefaultIProjection} with an authority name and CRS code.
52
     *
53
     * @param authorityName name of the authority which defined the CRS.
54
     * @param code of the defined CRS represented by this object
55
     * @throws CoordinateReferenceSystemNotFoundException if a CRS with the
56
     * given authority name and code could not be found in the system
57
     */
58
    public DefaultIProjection(String authorityName, String code)
59
            throws CoordinateReferenceSystemNotFoundException {
60
        this(CoordinateReferenceSystemLocator.getManager()
61
                .getCoordinateReferenceSystem(authorityName, code));
62
    }
63

    
64
    /**
65
     *
66
     * @param crs
67
     */
68
    public DefaultIProjection(CoordinateReferenceSystem crs) {
69
        this.crs = crs;
70
    }
71

    
72
    @Override
73
    public IDatum getDatum() {
74
        return new IDatum() {
75

    
76
            @Override
77
            public double getEIFlattening() {
78
                return getCoordinateReferenceSystem().getDatum().getEllipsoid()
79
                        .getReciprocalFlattening();
80
            }
81

    
82
            @Override
83
            public double getESemiMajorAxis() {
84
                return getCoordinateReferenceSystem().getDatum().getEllipsoid()
85
                        .getSemiMajorAxis();
86
            }
87

    
88
        };
89
    }
90

    
91
    @Override
92
    public Point2D createPoint(double x, double y) {
93
        return new Point2D.Double(x, y);
94
    }
95

    
96
    @Override
97
    public String getAbrev() {
98
        return getCoordinateReferenceSystem().getReference();
99
    }
100

    
101
    @Override
102
    public String getFullCode() {
103
        return getCoordinateReferenceSystem().getReference();
104
    }
105

    
106
    @Override
107
    public void drawGrid(Graphics2D g, ViewPortData vp) {
108
        throw new UnsupportedOperationException("Not implemented");
109
    }
110

    
111
    @Override
112
    public void setGridColor(Color gridColor) {
113
        throw new UnsupportedOperationException("Not implemented");
114
    }
115

    
116
    @Override
117
    public Color getGridColor() {
118
        throw new UnsupportedOperationException("Not implemented");
119
    }
120

    
121
    @Override
122
    public ICoordTrans getCT(IProjection dest) {
123
        return new DefaultICoordTrans(this, (DefaultIProjection) dest);
124
    }
125

    
126
    @Override
127
    public Point2D toGeo(Point2D pt) {
128
        CoordinateReferenceSystem geographic = crs.createGeographic();
129

    
130
        CoordinateTransformation transfToGeo
131
                = crs.getTransformation(geographic);
132

    
133
        double[] coords = new double[]{pt.getX(), pt.getY()};
134
        transfToGeo.convert(coords);
135
        return new Point2D.Double(coords[0], coords[1]);
136
    }
137

    
138
    @Override
139
    public Point2D fromGeo(Point2D gPt, Point2D mPt) {
140
        CoordinateReferenceSystem geographic = crs.createGeographic();
141

    
142
        CoordinateTransformation transfFromGeo
143
                = geographic.getTransformation(crs);
144

    
145
        double[] coords = new double[]{gPt.getX(), gPt.getY()};
146
        transfFromGeo.convert(coords);
147
        mPt.setLocation(coords[0], coords[1]);
148
        return mPt;
149
    }
150

    
151
    @Override
152
    public double getScale(double minX, double maxX, double width, double dpi) {
153
        double scale = ((maxX - minX) * // metros
154
                (dpi / 2.54 * 100.0))
155
                / // px / metro
156
                width; // pixels
157
        return scale;
158
    }
159

    
160
    @Override
161
    public Rectangle2D getExtent(Rectangle2D extent, double scale,
162
            double wImage, double hImage, double mapUnits, double distanceUnits,
163
            double dpi) {
164

    
165
        // Code copied from the org.gvsig.crs.Crs class.
166
        // I think this calculation might be moved outside the projection
167
        // implementation classes, it seems to be common.
168
        double w = ((wImage / dpi) * 2.54);
169
        double h = ((hImage / dpi) * 2.54);
170
        double wExtent = w * scale / mapUnits;
171
        double hExtent = h * scale / mapUnits;
172

    
173
        double xExtent = extent.getCenterX() - wExtent / 2;
174
        double yExtent = extent.getCenterY() - hExtent / 2;
175
        Rectangle2D rec
176
                = new Rectangle2D.Double(xExtent, yExtent, wExtent, hExtent);
177
        return rec;
178
    }
179

    
180
    @Override
181
    public boolean isProjected() {
182
        return getCoordinateReferenceSystem().isProjected();
183
    }
184

    
185
    @Override
186
    public Object clone() throws CloneNotSupportedException {
187
        DefaultIProjection cloned = (DefaultIProjection) super.clone();
188
        cloned.crs
189
                = (CoordinateReferenceSystem) this.getCoordinateReferenceSystem()
190
                .clone();
191
        return cloned;
192
    }
193

    
194
    public CoordinateReferenceSystem getCoordinateReferenceSystem() {
195
        return crs;
196
    }
197

    
198
    @Override
199
    public String toString() {
200
        return crs.toString();
201
    }
202

    
203
    @Override
204
    public String export(String format) {
205
        return null;
206
    }
207

    
208
    @Override
209
    public boolean equals(Object obj) {
210
        if (this == obj) {
211
            return true;
212
        }
213
        if (!(obj instanceof DefaultIProjection)) {
214
            return false;
215
        }
216
        return this.getFullCode().equalsIgnoreCase(((DefaultIProjection) obj).getFullCode());
217
    }
218

    
219
    @Override
220
    public int hashCode() {
221
        return this.getFullCode().hashCode();
222
    }
223
}