Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libCq CMS for java.old / src / org / cresques / cts / gt2 / CoordSys.java @ 7310

History | View | Annotate | Download (5.54 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
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.cts.ProjectionPool;
35
import org.cresques.geo.ViewPortData;
36
import org.geotools.cs.CoordinateSystem;
37
import org.geotools.cs.CoordinateSystemFactory;
38
import org.geotools.cs.GeographicCoordinateSystem;
39
import org.geotools.cs.ProjectedCoordinateSystem;
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
    }
58

    
59
    /**
60
     * Crea un nuevo CoordSys geogr?fico a partir de uno proyectado.
61
     * Si el actual es geogr?fico retorna el mismo.
62
     * @return
63
     */
64
    public CoordSys toGeo() {
65
        CoordSys coordSys = new CoordSys((CSDatum) getDatum());
66

    
67
        if (geogCS != null) {
68
            coordSys.geogCS = this.geogCS;
69
        } else {
70
            coordSys.geogCS = projCS.getGeographicCoordinateSystem();
71
        }
72

    
73
        return coordSys;
74
    }
75

    
76
    public IDatum getDatum() {
77
        return datum;
78
    }
79

    
80
    public CoordinateSystem getCS() {
81
        if (projCS != null) {
82
            return projCS;
83
        }
84

    
85
        return geogCS;
86
    }
87

    
88
    public CoordinateSystem getGeogCS() {
89
        if (geogCS != null) {
90
            return geogCS;
91
        }
92

    
93
        return projCS.getGeographicCoordinateSystem();
94
    }
95

    
96
    /* (no Javadoc)
97
     * @see org.cresques.cts.IProjection#createPoint(double, double)
98
     */
99
    public Point2D createPoint(double x, double y) {
100
        return new Point2D.Double(x, y);
101
    }
102

    
103
    public String toString() {
104
        if (projCS != null) {
105
            return projCS.toString();
106
        }
107

    
108
        return geogCS.toString();
109
    }
110

    
111
    public void setAbrev(String abrev) {
112
        this.abrev = abrev;
113
    }
114

    
115
    public String getAbrev() {
116
        return abrev;
117
    }
118

    
119
    /* (no Javadoc)
120
     * @see org.cresques.cts.IProjection#drawGrid(java.awt.Graphics2D, org.cresques.geo.ViewPortData)
121
     */
122
    public void drawGrid(Graphics2D g, ViewPortData vp) {
123
        // TODO Ap?ndice de m?todo generado autom?ticamente
124
    }
125

    
126
    public void setGridColor(Color c) {
127
        gridColor = c;
128
    }
129

    
130
    public Color getGridColor() {
131
        return gridColor;
132
    }
133

    
134
    /* (no Javadoc)
135
     * @see org.cresques.cts.IProjection#toGeo(java.awt.geom.Point2D)
136
     */
137
    public Point2D toGeo(Point2D pt) {
138
        if (getGeogCS() == geogCS) {
139
            return pt;
140
        } else {
141
            CoordTrans ct = new CoordTrans(this, toGeo());
142

    
143
            return ct.convert(pt, null);
144
        }
145
    }
146

    
147
    /* (no Javadoc)
148
     * @see org.cresques.cts.IProjection#fromGeo(java.awt.geom.Point2D, java.awt.geom.Point2D)
149
     */
150
    public Point2D fromGeo(Point2D gPt, Point2D mPt) {
151
        // TODO Ap?ndice de m?todo generado autom?ticamente
152
        return null;
153
    }
154

    
155
    public double getScale(double minX, double maxX, double w, double dpi) {
156
        double scale = 0D;
157

    
158
        if (projCS == null) { // Es geogr?fico; calcula la escala.
159
            scale = ((maxX - minX) * // grados
160

    
161
            // 1852.0 metros x minuto de meridiano
162
            (dpi / 2.54 * 100.0 * 1852.0 * 60.0)) / // px / metro
163
                    w; // pixels
164
        }
165

    
166
        return scale;
167
    }
168
    public Rectangle2D getExtent(Rectangle2D extent,double scale,double wImage,double hImage,double changeUnits,double dpi) {
169
            double w =0;
170
                double h =0;
171
                double wExtent =0;
172
                double hExtent =0;
173
            if (projCS!=null) {
174
                        w = ((wImage / dpi) * 2.54);
175
                        h = ((hImage / dpi) * 2.54);
176
                        wExtent =w * scale/ changeUnits;
177
                        hExtent =h * scale/ changeUnits;
178

    
179
                }else {
180
                        w = ((wImage / dpi) * 2.54);
181
                        h = ((hImage / dpi) * 2.54);
182
                        wExtent =(w*scale)/ (changeUnits*1852.0*60.0);
183
                        hExtent =(h*scale)/ (changeUnits*1852.0*60.0);
184
                }
185
            double xExtent = extent.getCenterX() - wExtent/2;
186
                double yExtent = extent.getCenterY() - hExtent/2;
187
                Rectangle2D rec=new Rectangle2D.Double(xExtent,yExtent,wExtent,hExtent);
188
            return  rec;
189
    }
190
    public boolean isProjected() {
191
            return projCS != null;
192
    }
193

    
194
        public ICoordTrans getCT(IProjection dest) {
195
                // TODO Auto-generated method stub
196
                return new CoordTrans(this, (CoordSys) dest);
197
        }
198
}