Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libProjection / src / org / cresques / cts / gt2 / CoordSys.java @ 38898

History | View | Annotate | Download (6.63 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-6.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.cts.gt2;
25

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

    
31
import org.cresques.cts.ICoordTrans;
32
import org.cresques.cts.IDatum;
33
import org.cresques.cts.IProjection;
34
import org.cresques.geo.ViewPortData;
35
import org.geotools.cs.CoordinateSystem;
36
import org.geotools.cs.CoordinateSystemFactory;
37
import org.geotools.cs.GeographicCoordinateSystem;
38
import org.geotools.cs.ProjectedCoordinateSystem;
39
import org.opengis.referencing.FactoryException;
40

    
41

    
42
/**
43
 * Sistema de Coordenadas (Proyecci?n).
44
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
45
 */
46
public class CoordSys implements IProjection {
47
    private static final Color basicGridColor = new Color(64, 64, 64, 128);
48
    protected CoordinateSystemFactory csFactory = CoordinateSystemFactory.getDefault();
49
    protected CSDatum datum = null;
50
    protected GeographicCoordinateSystem geogCS = null;
51
    protected ProjectedCoordinateSystem projCS = null;
52
    protected String abrev = "";
53
    Color gridColor = basicGridColor;
54

    
55
    public CoordSys(CSDatum datum) {
56
        this.datum = datum;
57
        this.geogCS = new GeographicCoordinateSystem(datum.getName(null), datum.getDatum());
58
        }
59

    
60
    public CoordSys(String wkt) {
61
            try {
62
                //((GeographicCoordinateSystem)
63
                    CoordinateSystem cs = CoordinateSystemFactory.getDefault().createFromWKT(wkt);
64
//                         ).getHorizontalDatum();
65
                    if (cs instanceof GeographicCoordinateSystem)
66
                            geogCS = (GeographicCoordinateSystem) cs;
67
                    if (cs instanceof ProjectedCoordinateSystem) {
68
                            projCS = (ProjectedCoordinateSystem) cs;
69
                            geogCS = projCS.getGeographicCoordinateSystem();
70
                    }
71
                    datum = new CSDatum(geogCS.getHorizontalDatum());
72
            } catch (FactoryException e) {
73
                        // TODO Auto-generated catch block
74
                        e.printStackTrace();
75
                }
76
    }
77

    
78
    /**
79
     * Crea un nuevo CoordSys geogr?fico a partir de uno proyectado.
80
     * Si el actual es geogr?fico retorna el mismo.
81
     * @return
82
     */
83
    public CoordSys toGeo() {
84
        CoordSys coordSys = new CoordSys((CSDatum) getDatum());
85

    
86
        if (geogCS != null) {
87
            coordSys.geogCS = this.geogCS;
88
        } else {
89
            coordSys.geogCS = projCS.getGeographicCoordinateSystem();
90
        }
91

    
92
        return coordSys;
93
    }
94

    
95
    public IDatum getDatum() {
96
        return datum;
97
    }
98

    
99
    public CoordinateSystem getCS() {
100
        if (projCS != null) {
101
            return projCS;
102
        }
103

    
104
        return geogCS;
105
    }
106

    
107
    public CoordinateSystem getGeogCS() {
108
        if (geogCS != null) {
109
            return geogCS;
110
        }
111

    
112
        return projCS.getGeographicCoordinateSystem();
113
    }
114

    
115
    /* (no Javadoc)
116
     * @see org.cresques.cts.IProjection#createPoint(double, double)
117
     */
118
    public Point2D createPoint(double x, double y) {
119
        return new Point2D.Double(x, y);
120
    }
121

    
122
    public String toString() {
123
        if (projCS != null) {
124
            return projCS.toString();
125
        }
126

    
127
        return geogCS.toString();
128
    }
129

    
130
    public void setAbrev(String abrev) {
131
        this.abrev = abrev;
132
    }
133

    
134
    public String getAbrev() {
135
        return abrev;
136
    }
137

    
138
    /* (no Javadoc)
139
     * @see org.cresques.cts.IProjection#drawGrid(java.awt.Graphics2D, org.cresques.geo.ViewPortData)
140
     */
141
    public void drawGrid(Graphics2D g, ViewPortData vp) {
142
        // TODO Ap?ndice de m?todo generado autom?ticamente
143
    }
144

    
145
    public void setGridColor(Color c) {
146
        gridColor = c;
147
    }
148

    
149
    public Color getGridColor() {
150
        return gridColor;
151
    }
152

    
153
    /* (no Javadoc)
154
     * @see org.cresques.cts.IProjection#toGeo(java.awt.geom.Point2D)
155
     */
156
    public Point2D toGeo(Point2D pt) {
157
    //TODO VCN Esto si no lo comento no me trasforma el punto en coordenadas geogr?ficas.
158
//        if (getGeogCS() == geogCS) {
159
//            return pt;
160
//        } else {
161
            CoordTrans ct = new CoordTrans(this, toGeo());
162

    
163
            return ct.convert(pt, null);
164
//        }
165
    }
166

    
167
    /* (no Javadoc)
168
     * @see org.cresques.cts.IProjection#fromGeo(java.awt.geom.Point2D, java.awt.geom.Point2D)
169
     */
170
    public Point2D fromGeo(Point2D gPt, Point2D mPt) {
171
        // TODO Ap?ndice de m?todo generado autom?ticamente
172
        return null;
173
    }
174

    
175
    public double getScale(double minX, double maxX, double w, double dpi) {
176
        double scale = 0D;
177

    
178
        if (projCS == null) { // Es geogr?fico; calcula la escala.
179
            scale = ((maxX - minX) * // grados
180

    
181
            // 1852.0 metros x minuto de meridiano
182
            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
183
                    w; // pixels
184
        }
185

    
186
        return scale;
187
    }
188
    public Rectangle2D getExtent(Rectangle2D extent,double scale,double wImage,double hImage,double mapUnits,double distanceUnits,double dpi) {
189
            double w =0;
190
                double h =0;
191
                double wExtent =0;
192
                double hExtent =0;
193
            if (projCS!=null) {
194
                        w = ((wImage / dpi) * 2.54);
195
                        h = ((hImage / dpi) * 2.54);
196
                        wExtent =w * scale*distanceUnits/ mapUnits;
197
                        hExtent =h * scale*distanceUnits/ mapUnits;
198

    
199
                }else {
200
                        w = ((wImage / dpi) * 2.54);
201
                        h = ((hImage / dpi) * 2.54);
202
                        wExtent =(w*scale*distanceUnits)/ (mapUnits*1852.0*60.0);
203
                        hExtent =(h*scale*distanceUnits)/ (mapUnits*1852.0*60.0);
204
                }
205
            double xExtent = extent.getCenterX() - wExtent/2;
206
                double yExtent = extent.getCenterY() - hExtent/2;
207
                Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent);
208
            return  rec;
209
    }
210
    public boolean isProjected() {
211
            return projCS != null;
212
    }
213

    
214
        public ICoordTrans getCT(IProjection dest) {
215
                return new CoordTrans(this, (CoordSys) dest);
216
        }
217

    
218
        public String getFullCode() {
219
                return getAbrev();
220
        }
221

    
222
    /*
223
     * (non-Javadoc)
224
     * @see org.cresques.cts.IProjection#getWKT()
225
     */
226
        public String getWKT() {
227
                if (isProjected()) {
228
                        return projCS.toWKT();
229
                }
230
                else {
231
                        return geogCS.toWKT();        
232
                }
233
        }
234
}