svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / CartographicSupport.java @ 11741
History | View | Annotate | Download (7.48 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.core; |
42 |
|
43 |
import java.awt.geom.AffineTransform; |
44 |
|
45 |
import com.iver.cit.gvsig.fmap.MapContext; |
46 |
import com.iver.cit.gvsig.fmap.ViewPort; |
47 |
|
48 |
|
49 |
/**
|
50 |
* <p>This interface enables Cartographic support for those graphical elements that require
|
51 |
* additional runtime information rather than the feature itself geometric definitions.<br></p>
|
52 |
*
|
53 |
* <p>It allows to realworld's measure units dimensioning.<br></p>
|
54 |
*
|
55 |
* <p>It also supplies a toolkit to perform operations with centralized static methods.
|
56 |
* @see CartographicSupportToolkit inner class' methods<br></p>
|
57 |
*
|
58 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
59 |
*/
|
60 |
public interface CartographicSupport { |
61 |
public static final int WORLD = 0; |
62 |
public static final int PAPER = 0; |
63 |
/**
|
64 |
* Defines the unit used to express measures. It is the position of the unit in the <b>MapContext.NAMES</b> array.
|
65 |
* @param unitIndex, the index of the unit in the MapContext.NAMES array
|
66 |
*/
|
67 |
public abstract void setUnit(int unitIndex); |
68 |
/**
|
69 |
* Returns the unit used to express measures. It is the position of the unit in the <b>MapContext.NAMES</b> array.
|
70 |
* @returns an <b>int</b> with the index of the unit in the MapContext.NAMES array
|
71 |
*/
|
72 |
public abstract int getUnit(); |
73 |
|
74 |
/**
|
75 |
* Returns the <b>Reference System</b> used to draw the elements of the image.<br>
|
76 |
* <p>
|
77 |
* The elements have to be scaled to <b>pixel</b> when the image is bein drawn in
|
78 |
* order to compose the map. The elements of the map may define its size in
|
79 |
* other units than pixel.<br>
|
80 |
* </p>
|
81 |
* <p><b>CartographicSupport</b> enables the elements to define the size in
|
82 |
* measure units but these units may refer to different reference system. Two
|
83 |
* kinds of Reference Systems are allowed in this context: <b>CartographicSupport.WORLD</b>,
|
84 |
* and <b>CartographicSupport.PAPER</b>.
|
85 |
* <br></p>
|
86 |
* <p>
|
87 |
* Depending on which <b>Reference System</b> is used the unit used by this element
|
88 |
* refers to distances in the real world (then they are map's CRS-dependant)
|
89 |
* or screen or printer output (then they are output DPI-dependant)<br>
|
90 |
* </p>
|
91 |
* <p>
|
92 |
* In case the unit used is <b>pixel</b> then the reference system does not
|
93 |
* have any effect since the source unit is the same than the target unit.
|
94 |
* <br>
|
95 |
* </p>
|
96 |
* @return
|
97 |
*/
|
98 |
public abstract int getReferenceSystem(); |
99 |
|
100 |
/**
|
101 |
* Sets the <b>Reference System</b> that defines how this units have to be
|
102 |
* handled. Possible values are:
|
103 |
* <ol>
|
104 |
* <li><b>CartographySupport.WORLD</b>: Defines that the unit values refer
|
105 |
* to distances in the world. So, the size of the element displayed <b>depends
|
106 |
* directly on the scale and CRS</b> used by the map. The size of the elements
|
107 |
* defined to use CartographicSupport.WORLD will match exactly to their
|
108 |
* actual size in the real world.</li>
|
109 |
* <li><b>CartographySupport.PAPER</b>: Defines that the unit values refer
|
110 |
* to the length that the element will have regardless where it appears. If
|
111 |
* ReferenceSystem is <b>CartographySupport.PAPER</b>, and the length is (e.g.)
|
112 |
* millimeters the element will be displayed with the <b>same</b> amount of
|
113 |
* millimeters of length whether in the <b>screen</b> or the <b>printer</b>
|
114 |
* (screen DPI and printer DPI must be correctly configured).</li>
|
115 |
* </ol>
|
116 |
*/
|
117 |
public void setReferenceSystem(int referenceSystem); |
118 |
|
119 |
class CartographicSupportToolkit { |
120 |
private static final int CONVERSION_ARRAY_LENGTH = MapContext.CHANGEM.length; |
121 |
|
122 |
/**
|
123 |
* <p><b>Computes the screen size (pixels)</b> of a given <b>length</b> according to the current's rendering
|
124 |
* context defined by an <b>affine transform</b> in the <b>X axis</b>. The value of the length is expressed in the
|
125 |
* units specified by <b>unitIndex</b>-th element of the <b>MapContext.NAMES</b> array.</p>
|
126 |
*
|
127 |
* <p>If the unitIndex is out of the MapContext.NAMES array range, (it is assumed that the
|
128 |
* distance units are already in pixel) then the input and output length values are
|
129 |
* equal.</p>
|
130 |
* @param length, the length to be transformed to screen units
|
131 |
* @param at, an affine transform with the current rendering context
|
132 |
* @param unitIndex, the unit in which the length is given
|
133 |
* @return
|
134 |
*/
|
135 |
public static double toScreenUnitXAxis(CartographicSupport graphicElement, double length, ViewPort viewPort) { |
136 |
final int unitIndex = graphicElement.getUnit(); |
137 |
if (unitIndex >= CONVERSION_ARRAY_LENGTH || unitIndex < 0) return length; |
138 |
return Math.abs(viewPort.getAffineTransform().getScaleX()* |
139 |
toViewPortUnits(length, unitIndex, viewPort)); |
140 |
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* <p><b>Computes the screen size (pixels)</b> of a given <b>length</b> according to the current's rendering
|
145 |
* context defined by an <b>affine transform</b> in the <b>Y axis</b>. The value of the length is expressed in the
|
146 |
* units specified by <b>unitIndex</b>-th element of the <b>MapContext.NAMES</b> array.</p>
|
147 |
*
|
148 |
* <p>If the unitIndex is out of the MapContext.NAMES array range, (it is assumed that the
|
149 |
* distance units are already in pixel) then the input and output length values are
|
150 |
* equal.</p>
|
151 |
* @param length, the length to be transformed to screen units
|
152 |
* @param at, an affine transform with the current rendering context
|
153 |
* @param unitIndex, the unit in which the length is given
|
154 |
* @return
|
155 |
*/
|
156 |
public static double toScreenUnitYAxis(CartographicSupport graphicElement, double length, ViewPort viewPort) { |
157 |
final int unitIndex = graphicElement.getUnit(); |
158 |
if (unitIndex >= CONVERSION_ARRAY_LENGTH || unitIndex < 0) return length; |
159 |
return Math.abs(viewPort.getAffineTransform().getScaleY()* |
160 |
toViewPortUnits(length, unitIndex, viewPort)); |
161 |
} |
162 |
|
163 |
public static double toViewPortUnits(double length, int unit, ViewPort viewPort) { |
164 |
int viewPortUnit = viewPort.getMapUnits();
|
165 |
if (viewPortUnit == unit) return length; |
166 |
|
167 |
// for speed, only change if length is not in meters
|
168 |
if (unit != 1) length = length*MapContext.CHANGEM[unit]; |
169 |
|
170 |
// now length is in meters, translate it to viewport's units
|
171 |
if (viewPortUnit != 1) return length/MapContext.CHANGEM[viewPortUnit]; |
172 |
|
173 |
// viewport's units are meters too, no tranlation needed.
|
174 |
return length;
|
175 |
} |
176 |
|
177 |
public static double toPaperUnits(double length, int unit, int dpi) { |
178 |
// inch units used as canonical.
|
179 |
if (unit != 7) length = length*MapContext.CHANGE[unit]; |
180 |
|
181 |
// length is now in inches
|
182 |
return length*dpi;
|
183 |
|
184 |
} |
185 |
|
186 |
} |
187 |
} |