Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / layout / FLayoutUtilities.java @ 25996

History | View | Annotate | Download (8.21 KB)

1
/*
2
 * Created on 16-jul-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.project.documents.layout;
46

    
47
import java.awt.Point;
48
import java.awt.geom.AffineTransform;
49
import java.awt.geom.NoninvertibleTransformException;
50
import java.awt.geom.Point2D;
51
import java.awt.geom.Rectangle2D;
52
import java.util.ArrayList;
53

    
54
import org.cresques.cts.IProjection;
55

    
56
import com.iver.cit.gvsig.fmap.ViewPort;
57

    
58

    
59
/**
60
 * Clase que recoge m?todos est?ticos sobre el Layout.
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class FLayoutUtilities {
65
        /**
66
         * Devuelve true si las dos ArrayList que se le pasan como parametro son
67
         * iguales.
68
         *
69
         * @param n lista anterior
70
         * @param l lista actual
71
         *
72
         * @return true si los ArrayList son iguales.
73
         */
74
        public static boolean isEqualList(ArrayList n, ArrayList l) {
75
                if (n.size() != l.size()) {
76
                        return false;
77
                }
78

    
79
                for (int i = 0; i < n.size(); i++) {
80
                        if (l.get(i) != n.get(i)) {
81
                                return false;
82
                        }
83
                }
84

    
85
                return true;
86
        }
87

    
88
        /**
89
         * Pasa una distancia en pixels a unidades del folio.
90
         *
91
         * @param d distancia en pixels.
92
         * @param at Matriz de transformaci?n.
93
         *
94
         * @return distancia en unidades de folio.
95
         */
96
        public static double toSheetDistance(double d, AffineTransform at) {
97
                double dist = d / at.getScaleX(); // pProv.x;
98

    
99
                return dist;
100
        }
101

    
102
        /**
103
         * Pasa una distancia de coordenadas del folio a pixels.
104
         *
105
         * @param d distancia en coordenadas de folio.
106
         * @param at Matriz de transformaci?n.
107
         *
108
         * @return double en pixels.
109
         */
110
        public static double fromSheetDistance(double d, AffineTransform at) {
111
                Point2D.Double pSheet1 = new Point2D.Double(0, 0);
112
                Point2D.Double pSheet2 = new Point2D.Double(1, 0);
113
                Point2D.Double pScreen1 = new Point2D.Double();
114
                Point2D.Double pScreen2 = new Point2D.Double();
115

    
116
                try {
117
                        at.transform(pSheet1, pScreen1);
118
                        at.transform(pSheet2, pScreen2);
119
                } catch (Exception e) {
120
                        System.err.print(e.getMessage());
121
                }
122

    
123
                return pScreen1.distance(pScreen2) * d;
124
        }
125

    
126
        /**
127
         * Pasa un punto en pixels a coordenadas del folio.
128
         *
129
         * @param pScreen pixels.
130
         * @param at Matriz de transformaci?n.
131
         *
132
         * @return Point2D en coordenadas de folio.
133
         */
134
        public static Point2D.Double toSheetPoint(Point2D pScreen,
135
                AffineTransform at) {
136
                Point2D.Double pWorld = new Point2D.Double();
137
                AffineTransform at1;
138

    
139
                try {
140
                        at1 = at.createInverse();
141
                        at1.transform(pScreen, pWorld);
142
                } catch (NoninvertibleTransformException e) {
143
                }
144

    
145
                return pWorld;
146
        }
147

    
148
        /**
149
         * Pasa un ret?ngulo de pixels a coordenadas del folio.
150
         *
151
         * @param r rect?ngulo en coordenadas de pixels a coordenadas de folio.
152
         * @param at Matriz de transformaci?n.
153
         *
154
         * @return Rectangle2D en coordenadas de folio.
155
         */
156
        public static Rectangle2D.Double toSheetRect(Rectangle2D r,
157
                AffineTransform at) {
158
                Point2D.Double pSheet = toSheetPoint(new Point2D.Double(r.getX(),
159
                                        r.getY()), at);
160
                Point2D.Double pSheetX = toSheetPoint(new Point2D.Double(r.getMaxX(),
161
                                        r.getMinY()), at);
162
                Point2D.Double pSheetY = toSheetPoint(new Point2D.Double(r.getMinX(),
163
                                        r.getMaxY()), at);
164
                Rectangle2D.Double res = new Rectangle2D.Double();
165
                res.setRect(pSheet.getX(), pSheet.getY(), pSheet.distance(pSheetX),
166
                        pSheet.distance(pSheetY));
167

    
168
                return res;
169
        }
170

    
171
        /**
172
         * Pasa de un punto en coordenadas del folio a pixels.
173
         *
174
         * @param pSheet punto en coordenadas de folio.
175
         * @param at Matriz de transformaci?n.
176
         *
177
         * @return Point2D en pixels.
178
         */
179
        public static Point2D.Double fromSheetPoint(Point2D pSheet,
180
                AffineTransform at) {
181
                Point2D.Double pScreen = new Point2D.Double();
182

    
183
                try {
184
                        at.transform(pSheet, pScreen);
185
                } catch (Exception e) {
186
                        System.err.print(e.getMessage());
187
                }
188

    
189
                return pScreen;
190
        }
191

    
192
        /**
193
         * Pasa un rect?ngulo en coordenadas del folio a pixels.
194
         *
195
         * @param r rect?ngulo en coordenadas de folio.
196
         * @param at Matriz de transformaci?n.
197
         *
198
         * @return Rectangle2D en pixels.
199
         */
200
        public static Rectangle2D.Double fromSheetRect(Rectangle2D r,
201
                AffineTransform at) {
202
                Point2D.Double pSheet = new Point2D.Double(r.getX(), r.getY());
203
                Point2D.Double pSX = new Point2D.Double(r.getMaxX(), r.getMinY());
204
                Point2D.Double pSY = new Point2D.Double(r.getMinX(), r.getMaxY());
205
                Point2D.Double pScreen = new Point2D.Double();
206
                Point2D.Double pScreenX = new Point2D.Double();
207
                Point2D.Double pScreenY = new Point2D.Double();
208

    
209
                try {
210
                        at.transform(pSheet, pScreen);
211
                        at.transform(pSX, pScreenX);
212
                        at.transform(pSY, pScreenY);
213
                } catch (Exception e) {
214
                        System.err.print(e.getMessage());
215
                }
216

    
217
                Rectangle2D.Double res = new Rectangle2D.Double();
218
                res.setRect(pScreen.getX(), pScreen.getY(), pScreen.distance(pScreenX),
219
                        pScreen.distance(pScreenY));
220

    
221
                return res;
222
        }
223

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

    
235
                Point2D.Double auxp = FLayoutUtilities.fromSheetPoint(new Point2D.Double(
236
                                                0, 0), at);
237
                int x = (int)(((p.getX() - distX) % distX) - ((auxp.x) % distX));
238
                int y = (int)(((p.getY() - distY) % distY) - ((auxp.y) % distY));
239
                return new Point((int)(p.getX()-x),(int)(p.getY()-y));
240
        }
241

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

    
263
                return true;
264
        }
265

    
266
        /**
267
         * Devuelve un long representando a la escala en funci?n  de que unidad de
268
         * medida se pase como par?metro.
269
         *
270
         * @param map FMap
271
         * @param h Rect?ngulo.
272
         *
273
         * @return escala.
274
         */
275
/*        public static long getScaleView(FMap map, double h) {
276
                if (map == null) {
277
                        return 0;
278
                }
279

280
                long scaleView = 1;
281
                double hextent = map.getViewPort().getExtent().getHeight();
282
                double hview = h;
283
                scaleView = (long) (Attributes.CHANGE[map.getViewPort().getMapUnits()] * (hextent / hview));
284

285
                return scaleView;
286
        }
287
        */
288
        public static long getScaleView(ViewPort viewPort,double wcm,double wpixels) {
289
                double dpi = wpixels/wcm*2.54;
290
                IProjection proj = viewPort.getProjection();
291

    
292
                //if (viewPort.getImageSize() == null)
293
                //    return -1;
294
                if (viewPort.getAdjustedExtent() == null) {
295
                        return 0;
296
                }
297

    
298
                if (proj == null || viewPort.getImageSize() == null) {
299
                        return (long) (viewPort.getAdjustedExtent().getHeight() / wcm * Attributes.CHANGE[viewPort
300
                                                                                                                                                                        .getMapUnits()]);
301
                }
302

    
303
                return (long) proj.getScale(viewPort.getAdjustedExtent().getMinX(),
304
                        viewPort.getAdjustedExtent().getMaxX(),
305
                        wpixels, dpi);
306
        }
307

    
308
}