root / org.gvsig.legend.dotdensity.app.mainplugin / trunk / org.gvsig.legend.dotdensity.app.mainplugin / src / main / java / org / gvsig / symbology / fmap / rendering / DotDensityLegend.java @ 9511
History | View | Annotate | Download (9.61 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 |
*/
|
20 |
|
21 |
package org.gvsig.symbology.fmap.rendering; |
22 |
|
23 |
import java.awt.Color; |
24 |
|
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
|
28 |
import org.gvsig.fmap.dal.feature.Feature; |
29 |
import org.gvsig.fmap.geom.Geometry; |
30 |
import org.gvsig.fmap.geom.GeometryLocator; |
31 |
import org.gvsig.fmap.geom.GeometryManager; |
32 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
33 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
34 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
35 |
import org.gvsig.symbology.fmap.mapcontext.rendering.legend.impl.VectorialUniqueValueLegend; |
36 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol; |
37 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.MultiLayerFillSymbol; |
38 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
39 |
import org.gvsig.symbology.fmap.symbols.DotDensityFillSymbol; |
40 |
import org.gvsig.tools.ToolsLocator; |
41 |
import org.gvsig.tools.dynobject.DynStruct; |
42 |
import org.gvsig.tools.persistence.PersistenceManager; |
43 |
import org.gvsig.tools.persistence.PersistentState; |
44 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
45 |
import org.gvsig.tools.util.Callable; |
46 |
|
47 |
|
48 |
/**
|
49 |
*
|
50 |
* Implements a legend where the magnitudes of a specific area of the
|
51 |
* map are represented by the density of the points that are distributed
|
52 |
* in the surface.
|
53 |
*
|
54 |
*
|
55 |
*/
|
56 |
public class DotDensityLegend extends VectorialUniqueValueLegend { |
57 |
|
58 |
private static Logger logger = LoggerFactory.getLogger( |
59 |
DotDensityLegend.class); |
60 |
|
61 |
public static final String |
62 |
DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME = |
63 |
"DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME";
|
64 |
public static final String |
65 |
DOT_DENSITY_LEGEND_NAME = "DOT_DENSITY_LEGEND_NAME";
|
66 |
|
67 |
private double dotValue; |
68 |
private Color dotColor = Color.BLACK; |
69 |
private Color backgroundColor = Color.WHITE; |
70 |
private static final int SIMPLE_FILL_LAYER_INDEX = 0; |
71 |
private static final int DOT_DENSITY_LAYER_INDEX = 1; |
72 |
|
73 |
|
74 |
public ISymbol getSymbolByValue(Object val) { |
75 |
MultiLayerFillSymbol sym = (MultiLayerFillSymbol) getDefaultSymbol(); |
76 |
DotDensityFillSymbol densitySym = (DotDensityFillSymbol) sym.getLayer(DOT_DENSITY_LAYER_INDEX); |
77 |
|
78 |
if (val instanceof Number) { |
79 |
Number dou = (Number) val; |
80 |
densitySym.setDotCount( |
81 |
(int) (dou.doubleValue()/dotValue));
|
82 |
return sym;
|
83 |
} else {
|
84 |
logger.info("Error: Unexpected value in dot density legend: " + val);
|
85 |
return null; |
86 |
} |
87 |
} |
88 |
|
89 |
public ISymbol getSymbolByFeature(Feature feat) {
|
90 |
|
91 |
String[] ff = this.getClassifyingFieldNames(); |
92 |
Object val = feat.get(ff[0]); |
93 |
return this.getSymbolByValue(val); |
94 |
} |
95 |
|
96 |
/**
|
97 |
* Establishes the value for the dot used in the dot density legend
|
98 |
*
|
99 |
* @param dotValue
|
100 |
*/
|
101 |
public void setDotValue(double dotValue) { |
102 |
this.dotValue = dotValue;
|
103 |
} |
104 |
|
105 |
public void saveToState(PersistentState state) throws PersistenceException { |
106 |
|
107 |
super.saveToState(state);
|
108 |
state.set("dotValue", dotValue);
|
109 |
state.set("dotColor", getDotColor());
|
110 |
state.set("bgColor", getBGColor());
|
111 |
} |
112 |
|
113 |
public void loadFromState(PersistentState state) |
114 |
throws PersistenceException {
|
115 |
|
116 |
super.loadFromState(state);
|
117 |
|
118 |
dotValue = state.getDouble("dotValue");
|
119 |
Object obj_col = state.get("dotColor"); |
120 |
this.setDotColor((Color) obj_col); |
121 |
obj_col = state.get("bgColor");
|
122 |
this.setBGColor((Color) obj_col); |
123 |
} |
124 |
|
125 |
|
126 |
|
127 |
|
128 |
/**
|
129 |
* Returns the outline
|
130 |
*
|
131 |
* @return
|
132 |
*/
|
133 |
public ILineSymbol getOutline() {
|
134 |
// defined by the SimpleFillSymbol layer
|
135 |
ISymbol symbol = getDefaultSymbol(); |
136 |
if (!(symbol instanceof IFillSymbol)){ |
137 |
return null; |
138 |
} |
139 |
IFillSymbol fillsym = (IFillSymbol) symbol; |
140 |
if (fillsym instanceof MultiLayerFillSymbol){ |
141 |
fillsym = (IFillSymbol) ((MultiLayerFillSymbol) fillsym). |
142 |
getLayer(SIMPLE_FILL_LAYER_INDEX); |
143 |
} |
144 |
if (fillsym == null){ |
145 |
return null; |
146 |
} |
147 |
|
148 |
return fillsym.getOutline();
|
149 |
} |
150 |
|
151 |
/**
|
152 |
* Returns the color for the dot used in the dot density legend.
|
153 |
* @return
|
154 |
*/
|
155 |
public Color getDotColor() { |
156 |
// try {
|
157 |
// // defined by the DotDensitySymbol layer
|
158 |
// DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
|
159 |
// getLayer(DOT_DENSITY_LAYER_INDEX);
|
160 |
// return sym.getDotColor();
|
161 |
// } catch (NullPointerException npE) {
|
162 |
// return null;
|
163 |
// }
|
164 |
return dotColor;
|
165 |
} |
166 |
|
167 |
|
168 |
/**
|
169 |
* Sets the color for the dot used in the dot density legend.
|
170 |
* @return
|
171 |
*/
|
172 |
public void setDotColor(Color color){ |
173 |
|
174 |
// DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
|
175 |
// getLayer(DOT_DENSITY_LAYER_INDEX);
|
176 |
// sym.setDotColor(color);
|
177 |
|
178 |
this.dotColor = color;
|
179 |
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Obtains the background color for the dot density legend
|
184 |
* @return
|
185 |
*/
|
186 |
public Color getBGColor() { |
187 |
// try {
|
188 |
// // defined by the SimpleFillSymbol layer
|
189 |
// IFillSymbol symbol = (IFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()).
|
190 |
// getLayer(SIMPLE_FILL_LAYER_INDEX);
|
191 |
// return symbol.getFillColor();
|
192 |
// } catch (NullPointerException npE) {
|
193 |
// return null;
|
194 |
// }
|
195 |
return backgroundColor;
|
196 |
} |
197 |
|
198 |
/**
|
199 |
* Sets the background color for the dot density legend
|
200 |
* @return
|
201 |
*/
|
202 |
public void setBGColor(Color color) { |
203 |
this.backgroundColor = color;
|
204 |
} |
205 |
/**
|
206 |
* Returns the value for the dot that is used in the dot density legend
|
207 |
* @return
|
208 |
*/
|
209 |
public double getDotValue() { |
210 |
return this.dotValue; |
211 |
} |
212 |
/**
|
213 |
* Obtains the size of the dot that is used in the dot density legend
|
214 |
* @return
|
215 |
*/
|
216 |
public double getDotSize() { |
217 |
|
218 |
// defined by the SimpleFillSymbol layer
|
219 |
ISymbol symbol = getDefaultSymbol(); |
220 |
if (symbol == null){ |
221 |
return -1; |
222 |
} |
223 |
if (symbol instanceof DotDensityFillSymbol){ |
224 |
return ((DotDensityFillSymbol)symbol).getDotSize();
|
225 |
} |
226 |
if (!(symbol instanceof IFillSymbol)){ |
227 |
return -1; |
228 |
} |
229 |
IFillSymbol fillsym = (IFillSymbol) symbol; |
230 |
if (fillsym instanceof MultiLayerFillSymbol){ |
231 |
fillsym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) fillsym). |
232 |
getLayer(DOT_DENSITY_LAYER_INDEX); |
233 |
} |
234 |
if (fillsym instanceof DotDensityFillSymbol){ |
235 |
return ((DotDensityFillSymbol)fillsym).getDotSize();
|
236 |
} |
237 |
return -1; |
238 |
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* Sets the size of the dot that is used in the dot density legend
|
243 |
*/
|
244 |
public void setDotSize(double value) { |
245 |
DotDensityFillSymbol sym = (DotDensityFillSymbol) ((MultiLayerFillSymbol) getDefaultSymbol()). |
246 |
getLayer(DOT_DENSITY_LAYER_INDEX); |
247 |
sym.setDotSize(value); |
248 |
} |
249 |
|
250 |
|
251 |
public static boolean isPolygonal(int ty) { |
252 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
253 |
return geomManager.isSubtype(Geometry.TYPES.MULTISURFACE, ty) ||
|
254 |
geomManager.isSubtype(Geometry.TYPES.SURFACE, ty); |
255 |
} |
256 |
|
257 |
// =============================
|
258 |
|
259 |
public static class RegisterPersistence implements Callable { |
260 |
|
261 |
public Object call() throws Exception { |
262 |
|
263 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
264 |
if (manager.getDefinition(
|
265 |
DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME) == null) {
|
266 |
DynStruct definition = manager |
267 |
.addDefinition(DotDensityLegend.class, |
268 |
DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME, |
269 |
DOT_DENSITY_LEGEND_PERSISTENCE_DEFINITION_NAME |
270 |
+ " Persistence definition", null, null); |
271 |
|
272 |
definition.extend(manager.getDefinition( |
273 |
VectorialUniqueValueLegend |
274 |
.VECTORIAL_UNIQUE_VALUE_LEGEND_PERSISTENCE_DEFINITION_NAME)); |
275 |
|
276 |
definition.addDynFieldDouble("dotValue").setMandatory(true); |
277 |
definition.addDynFieldObject("dotColor")
|
278 |
.setClassOfValue(Color.class).setMandatory(true); |
279 |
definition.addDynFieldObject("bgColor")
|
280 |
.setClassOfValue(Color.class).setMandatory(true); |
281 |
} |
282 |
return Boolean.TRUE; |
283 |
} |
284 |
|
285 |
} |
286 |
|
287 |
public static class RegisterLegend implements Callable { |
288 |
|
289 |
public Object call() throws Exception { |
290 |
MapContextManager manager = |
291 |
MapContextLocator.getMapContextManager(); |
292 |
|
293 |
manager.registerLegend( |
294 |
DOT_DENSITY_LEGEND_NAME, |
295 |
DotDensityLegend.class); |
296 |
|
297 |
return Boolean.TRUE; |
298 |
} |
299 |
|
300 |
} |
301 |
|
302 |
public Object clone() throws CloneNotSupportedException { |
303 |
|
304 |
DotDensityLegend resp = (DotDensityLegend) super.clone();
|
305 |
resp.setDotColor(this.getDotColor());
|
306 |
resp.setDotSize(this.getDotSize());
|
307 |
resp.setDotValue(this.getDotValue());
|
308 |
return resp;
|
309 |
} |
310 |
|
311 |
public String[] getDescriptions() { |
312 |
try {
|
313 |
return new String[]{String.format("%s, %.3f",getClassifyingFieldNames()[0], getDotValue())}; |
314 |
}catch (Exception ex){ |
315 |
return new String[]{"Dot density"}; |
316 |
} |
317 |
} |
318 |
|
319 |
} |