Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout.app / org.gvsig.app.document.layout.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / FLayoutUtilities.java @ 88

History | View | Annotate | Download (9.7 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout;
23

    
24
import java.awt.Point;
25
import java.awt.geom.AffineTransform;
26
import java.awt.geom.NoninvertibleTransformException;
27
import java.awt.geom.Point2D;
28
import java.awt.geom.Rectangle2D;
29
import java.util.ArrayList;
30

    
31
import org.cresques.cts.IProjection;
32

    
33
import org.gvsig.fmap.mapcontext.MapContext;
34
import org.gvsig.fmap.mapcontext.ViewPort;
35

    
36
/**
37
 * Clase que recoge m?todos est?ticos sobre el Layout.
38
 * 
39
 * @author Vicente Caballero Navarro
40
 */
41
public class FLayoutUtilities {
42

    
43
    /**
44
     * Devuelve true si las dos ArrayList que se le pasan como parametro son
45
     * iguales.
46
     * 
47
     * @param n
48
     *            lista anterior
49
     * @param l
50
     *            lista actual
51
     * 
52
     * @return true si los ArrayList son iguales.
53
     */
54
    public static boolean isEqualList(ArrayList n, ArrayList l) {
55
        if (n.size() != l.size()) {
56
            return false;
57
        }
58

    
59
        for (int i = 0; i < n.size(); i++) {
60
            if (l.get(i) != n.get(i)) {
61
                return false;
62
            }
63
        }
64

    
65
        return true;
66
    }
67

    
68
    /**
69
     * Pasa una distancia en pixels a unidades del folio.
70
     * 
71
     * @param d
72
     *            distancia en pixels.
73
     * @param at
74
     *            Matriz de transformaci?n.
75
     * 
76
     * @return distancia en unidades de folio.
77
     */
78
    public static double toSheetDistance(double d, AffineTransform at) {
79
        double dist = d / at.getScaleX(); // pProv.x;
80

    
81
        return dist;
82
    }
83

    
84
    /**
85
     * Pasa una distancia de coordenadas del folio a pixels.
86
     * 
87
     * @param d
88
     *            distancia en coordenadas de folio.
89
     * @param at
90
     *            Matriz de transformaci?n.
91
     * 
92
     * @return double en pixels.
93
     */
94
    public static double fromSheetDistance(double d, AffineTransform at) {
95
        Point2D.Double pSheet1 = new Point2D.Double(0, 0);
96
        Point2D.Double pSheet2 = new Point2D.Double(1, 0);
97
        Point2D.Double pScreen1 = new Point2D.Double();
98
        Point2D.Double pScreen2 = new Point2D.Double();
99

    
100
        try {
101
            at.transform(pSheet1, pScreen1);
102
            at.transform(pSheet2, pScreen2);
103
        } catch (Exception e) {
104
            System.err.print(e.getMessage());
105
        }
106

    
107
        return pScreen1.distance(pScreen2) * d;
108
    }
109

    
110
    /**
111
     * Pasa un punto en pixels a coordenadas del folio.
112
     * 
113
     * @param pScreen
114
     *            pixels.
115
     * @param at
116
     *            Matriz de transformaci?n.
117
     * 
118
     * @return Point2D en coordenadas de folio.
119
     */
120
    public static Point2D.Double toSheetPoint(Point2D pScreen,
121
        AffineTransform at) {
122
        Point2D.Double pWorld = new Point2D.Double();
123
        AffineTransform at1;
124

    
125
        try {
126
            at1 = at.createInverse();
127
            at1.transform(pScreen, pWorld);
128
        } catch (NoninvertibleTransformException e) {
129
        }
130

    
131
        return pWorld;
132
    }
133

    
134
    /**
135
     * Pasa un ret?ngulo de pixels a coordenadas del folio.
136
     * 
137
     * @param r
138
     *            rect?ngulo en coordenadas de pixels a coordenadas de folio.
139
     * @param at
140
     *            Matriz de transformaci?n.
141
     * 
142
     * @return Rectangle2D en coordenadas de folio.
143
     */
144
    public static Rectangle2D.Double toSheetRect(Rectangle2D r,
145
        AffineTransform at) {
146
        Point2D.Double pSheet =
147
            toSheetPoint(new Point2D.Double(r.getX(), r.getY()), at);
148
        Point2D.Double pSheetX =
149
            toSheetPoint(new Point2D.Double(r.getMaxX(), r.getMinY()), at);
150
        Point2D.Double pSheetY =
151
            toSheetPoint(new Point2D.Double(r.getMinX(), r.getMaxY()), at);
152
        Rectangle2D.Double res = new Rectangle2D.Double();
153
        res.setRect(pSheet.getX(), pSheet.getY(), pSheet.distance(pSheetX),
154
            pSheet.distance(pSheetY));
155

    
156
        return res;
157
    }
158

    
159
    /**
160
     * Pasa de un punto en coordenadas del folio a pixels.
161
     * 
162
     * @param pSheet
163
     *            punto en coordenadas de folio.
164
     * @param at
165
     *            Matriz de transformaci?n.
166
     * 
167
     * @return Point2D en pixels.
168
     */
169
    public static Point2D.Double fromSheetPoint(Point2D pSheet,
170
        AffineTransform at) {
171
        Point2D.Double pScreen = new Point2D.Double();
172

    
173
        try {
174
            at.transform(pSheet, pScreen);
175
        } catch (Exception e) {
176
            System.err.print(e.getMessage());
177
        }
178

    
179
        return pScreen;
180
    }
181

    
182
    /**
183
     * Pasa un rect?ngulo en coordenadas del folio a pixels.
184
     * 
185
     * @param r
186
     *            rect?ngulo en coordenadas de folio.
187
     * @param at
188
     *            Matriz de transformaci?n.
189
     * 
190
     * @return Rectangle2D en pixels.
191
     */
192
    public static Rectangle2D.Double fromSheetRect(Rectangle2D r,
193
        AffineTransform at) {
194
        Point2D.Double pSheet = new Point2D.Double(r.getX(), r.getY());
195
        Point2D.Double pSX = new Point2D.Double(r.getMaxX(), r.getMinY());
196
        Point2D.Double pSY = new Point2D.Double(r.getMinX(), r.getMaxY());
197
        Point2D.Double pScreen = new Point2D.Double();
198
        Point2D.Double pScreenX = new Point2D.Double();
199
        Point2D.Double pScreenY = new Point2D.Double();
200

    
201
        try {
202
            at.transform(pSheet, pScreen);
203
            at.transform(pSX, pScreenX);
204
            at.transform(pSY, pScreenY);
205
        } catch (Exception e) {
206
            System.err.print(e.getMessage());
207
        }
208

    
209
        Rectangle2D.Double res = new Rectangle2D.Double();
210
        res.setRect(pScreen.getX(), pScreen.getY(), pScreen.distance(pScreenX),
211
            pScreen.distance(pScreenY));
212

    
213
        return res;
214
    }
215

    
216
    /**
217
     * Obtiene el punto ajustado al grid del layout.
218
     * 
219
     * @param p
220
     *            Punto a ajustar.
221
     * @param distX
222
     *            Distancia m?nima en pixels de X.
223
     * @param distY
224
     *            Distancia m?nima en pixels de Y.
225
     * @param at
226
     *            Matriz de transformaci?n.
227
     */
228
    public static Point getPointGrid(Point p, double distX, double distY,
229
        AffineTransform at) {
230

    
231
        Point2D.Double auxp =
232
            FLayoutUtilities.fromSheetPoint(new Point2D.Double(0, 0), at);
233
        int x = (int) (((p.getX()) % distX) - ((auxp.x) % distX));
234
        int y = (int) (((p.getY()) % distY) - ((auxp.y) % distY));
235
        if (x < distX / 2) {
236
            x = -x;
237
        } else {
238
            x = (int) (distX - x);
239
        }
240
        if (y < distY / 2) {
241
            y = -y;
242
        } else {
243
            y = (int) (distY - y);
244
        }
245
        return new Point((int) (p.getX() + x), (int) (p.getY() + y));
246
    }
247

    
248
    /**
249
     * Cuando se dibuja sobre el graphics todo se tiene que situar en enteros y
250
     * aqu? lo que se comprueba es que si los valores que contiene el
251
     * Rectangle2D, que toma como par?metro, supera los valores soportados por
252
     * un entero.
253
     * 
254
     * @param r
255
     *            Rectangle2D a comprobar si los valores que contiene no superan
256
     *            a los que puede tener un entero.
257
     * 
258
     * @return true si no se han superado los l?mites.
259
     */
260
    public static boolean isPosible(Rectangle2D.Double r) {
261
        if ((r.getMaxX() > Integer.MAX_VALUE)
262
            || (r.getMaxY() > Integer.MAX_VALUE)
263
            || (r.getMinX() < Integer.MIN_VALUE)
264
            || (r.getMinY() < Integer.MIN_VALUE)
265
            || (r.getWidth() > Integer.MAX_VALUE)
266
            || (r.getHeight() > Integer.MAX_VALUE)) {
267
            return false;
268
        }
269

    
270
        return true;
271
    }
272

    
273
    /**
274
     * Devuelve un long representando a la escala en funci?n de que unidad de
275
     * medida se pase como par?metro.
276
     * 
277
     * @param map
278
     *            FMap
279
     * @param h
280
     *            Rect?ngulo.
281
     * 
282
     * @return escala.
283
     */
284
    /*
285
     * public static long getScaleView(FMap map, double h) {
286
     * if (map == null) {
287
     * return 0;
288
     * }
289
     * 
290
     * long scaleView = 1;
291
     * double hextent = map.getViewPort().getExtent().getHeight();
292
     * double hview = h;
293
     * scaleView = (long) (Attributes.CHANGE[map.getViewPort().getMapUnits()] *
294
     * (hextent / hview));
295
     * 
296
     * return scaleView;
297
     * }
298
     */
299
    public static long getScaleView(
300
        ViewPort viewPort,
301
        double wcm,
302
        double wpixels) {
303
        
304
        double dpi = wpixels / wcm * 2.54;
305
        IProjection proj = viewPort.getProjection();
306

    
307
        if (viewPort.getAdjustedEnvelope() == null) {
308
            return 0;
309
        }
310

    
311
        if (proj == null || viewPort.getImageSize() == null) {
312
            return (long) (viewPort.getAdjustedEnvelope().getLength(1)
313
                / wcm * Attributes.CHANGE[viewPort.getMapUnits()]);
314
        }
315

    
316
        double[] trans2Meter = MapContext.getDistanceTrans2Meter();
317
        int mUnits = viewPort.getMapUnits();
318
        
319
        return (long) proj.getScale(
320
            viewPort.getAdjustedEnvelope().getMinimum(0) * trans2Meter[mUnits],
321
            viewPort.getAdjustedEnvelope().getMaximum(0) * trans2Meter[mUnits],
322
            wpixels, dpi);
323
    }
324

    
325
}