svn-gvsig-desktop / tags / v2_0_0_Build_2021 / extensions / org.gvsig.symbology / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / line / impl / AbstractLineSymbol.java @ 34111
History | View | Annotate | Download (12 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.symbology.fmap.mapcontext.rendering.symbol.line.impl; |
23 |
|
24 |
import java.awt.Color; |
25 |
import java.awt.Graphics2D; |
26 |
import java.awt.Rectangle; |
27 |
import java.awt.geom.AffineTransform; |
28 |
import java.awt.geom.PathIterator; |
29 |
import java.awt.geom.Point2D; |
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
|
33 |
import org.gvsig.compat.print.PrintAttributes; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
36 |
import org.gvsig.fmap.geom.GeometryLocator; |
37 |
import org.gvsig.fmap.geom.GeometryManager; |
38 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
39 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
40 |
import org.gvsig.fmap.mapcontext.ViewPort; |
41 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
42 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.AbstractSymbol; |
43 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.impl.CartographicSupportToolkit; |
44 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
45 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.ILineStyle; |
46 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.SimpleLineStyle; |
47 |
import org.gvsig.tools.ToolsLocator; |
48 |
import org.gvsig.tools.dynobject.DynStruct; |
49 |
import org.gvsig.tools.persistence.PersistenceManager; |
50 |
import org.gvsig.tools.persistence.PersistentState; |
51 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
52 |
import org.gvsig.tools.util.Callable; |
53 |
|
54 |
|
55 |
/**
|
56 |
* AbstractLineSymbol is the class that implements the interface for line symbols.
|
57 |
* It is considered as the father of all the XXXLineSymbols and will implement all the
|
58 |
* methods that these classes had not developed (and correspond with one of the methods
|
59 |
* of AbstractLineSymbol class).
|
60 |
*
|
61 |
*
|
62 |
* @author 2005-2009 jaume dominguez faus - jaume.dominguez@iver.es
|
63 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
64 |
*/
|
65 |
public abstract class AbstractLineSymbol extends AbstractSymbol implements ILineSymbol { |
66 |
|
67 |
private static final String FIELD_LINESTYLE = "lineStyle"; |
68 |
|
69 |
private static final String FIELD_COLOR = "color"; |
70 |
|
71 |
public static final String LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME = "LineSymbol"; |
72 |
|
73 |
|
74 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
75 |
private Color color; |
76 |
private ILineStyle lineStyle = new SimpleLineStyle(); |
77 |
|
78 |
public AbstractLineSymbol() {
|
79 |
super();
|
80 |
color = |
81 |
MapContextLocator.getSymbolManager() |
82 |
.getSymbolPreferences() |
83 |
.getDefaultSymbolColor(); |
84 |
} |
85 |
|
86 |
public Color getColor() { |
87 |
return color;
|
88 |
} |
89 |
|
90 |
public void setLineColor(Color color) { |
91 |
this.color = color;
|
92 |
} |
93 |
|
94 |
public void setColor(Color color) { |
95 |
setLineColor(color); |
96 |
} |
97 |
|
98 |
public int getOnePointRgb() { |
99 |
return color.getRGB();
|
100 |
} |
101 |
|
102 |
public final int getSymbolType() { |
103 |
return Geometry.TYPES.CURVE;
|
104 |
} |
105 |
|
106 |
public void getPixExtentPlus(Geometry geom, float[] distances, ViewPort viewPort, int dpi) { |
107 |
float cs = (float) getCartographicSize(viewPort, dpi, geom); |
108 |
// TODO and add the line offset
|
109 |
distances[0] = cs;
|
110 |
distances[1] = cs;
|
111 |
} |
112 |
|
113 |
public boolean isSuitableFor(Geometry geom) { |
114 |
switch(geom.getType()) {
|
115 |
case Geometry.TYPES.CURVE:
|
116 |
case Geometry.TYPES.ARC:
|
117 |
return true; |
118 |
} |
119 |
return false; |
120 |
} |
121 |
|
122 |
public ILineStyle getLineStyle() {
|
123 |
return lineStyle;
|
124 |
} |
125 |
|
126 |
public void setLineStyle(ILineStyle lineStyle) { |
127 |
this.lineStyle = lineStyle;
|
128 |
} |
129 |
|
130 |
public int getAlpha() { |
131 |
return color.getAlpha();
|
132 |
} |
133 |
|
134 |
public void setAlpha(int outlineAlpha) { |
135 |
color = new Color(color.getRed(), color.getGreen(), color.getBlue(), outlineAlpha); |
136 |
} |
137 |
|
138 |
public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException { |
139 |
final int hGap = (int) (r.getWidth() * 0.1); // the left and right margins |
140 |
final int vPos = 1; // the top and bottom margins |
141 |
final int splitCount = 3; // number of lines |
142 |
final int splitHSize = (r.width - hGap - hGap) / splitCount; |
143 |
int hPos = hGap;
|
144 |
boolean swap = false; |
145 |
|
146 |
GeneralPathX gpx = new GeneralPathX();
|
147 |
gpx.moveTo(r.x + hPos, r.y + r.height-vPos); |
148 |
|
149 |
for (int i = 0; i < splitCount; i++) { |
150 |
swap = !swap; |
151 |
gpx.lineTo(r.x + hPos + splitHSize, (swap ? vPos : r.height-vPos) + r.y); |
152 |
hPos += splitHSize; |
153 |
} |
154 |
|
155 |
try {
|
156 |
if (properties==null) |
157 |
draw(g, new AffineTransform(), geomManager.createSurface(gpx, SUBTYPES.GEOM2D), null, null); |
158 |
else
|
159 |
print(g, new AffineTransform(), geomManager.createSurface(gpx, SUBTYPES.GEOM2D), properties); |
160 |
} catch (Exception e) { |
161 |
throw new SymbolDrawingException(SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS); |
162 |
} |
163 |
|
164 |
} |
165 |
|
166 |
|
167 |
|
168 |
public void setCartographicSize(double cartographicSize, Geometry geom) { |
169 |
getLineStyle().setLineWidth((float) cartographicSize);
|
170 |
} |
171 |
|
172 |
public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
173 |
double oldSize = getLineWidth();
|
174 |
setCartographicSize(getCartographicSize( |
175 |
viewPort, |
176 |
dpi, |
177 |
geom), |
178 |
geom); |
179 |
return oldSize;
|
180 |
} |
181 |
|
182 |
public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) { |
183 |
return CartographicSupportToolkit.
|
184 |
getCartographicLength(this,
|
185 |
getLineWidth(), |
186 |
viewPort, |
187 |
dpi); |
188 |
} |
189 |
|
190 |
public static Geometry offsetFShape(Geometry shp, double offset) { |
191 |
Geometry offsetFShape = null;
|
192 |
if (shp != null) { |
193 |
if (offset == 0) |
194 |
return shp;
|
195 |
|
196 |
List<Point2D[]> segments = new ArrayList<Point2D[]>(); |
197 |
GeneralPathX gpx = new GeneralPathX(shp.getPathIterator(null)); |
198 |
PathIterator it = gpx.getPathIterator(null); |
199 |
double[] data = new double[6]; |
200 |
Point2D segmentIni = null; |
201 |
Point2D segmentEnd = null; |
202 |
while (!it.isDone()) {
|
203 |
switch (it.currentSegment(data)) {
|
204 |
case PathIterator.SEG_MOVETO: |
205 |
segmentEnd = new Point2D.Double( |
206 |
data[0], data[1]); |
207 |
break;
|
208 |
|
209 |
case PathIterator.SEG_LINETO: |
210 |
segmentEnd = segmentIni; |
211 |
segmentIni = new Point2D.Double( |
212 |
data[0], data[1]); |
213 |
|
214 |
segments.add(getParallel(segmentIni, segmentEnd, offset)); |
215 |
break;
|
216 |
|
217 |
case PathIterator.SEG_QUADTO: |
218 |
break;
|
219 |
|
220 |
case PathIterator.SEG_CUBICTO: |
221 |
break;
|
222 |
|
223 |
case PathIterator.SEG_CLOSE: |
224 |
break;
|
225 |
} |
226 |
} |
227 |
|
228 |
} |
229 |
|
230 |
|
231 |
return offsetFShape;
|
232 |
} |
233 |
|
234 |
private static Point2D[] getParallel(Point2D p1, Point2D p2, double distance) { |
235 |
Point2D[] pParallel=new Point2D[2]; |
236 |
pParallel[0]=getPerpendicularPoint(p1,p2,p1,distance);
|
237 |
pParallel[1]=getPerpendicularPoint(p1,p2,p2,distance);
|
238 |
return pParallel;
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* Obtiene el punto que se encuentra a una distancia 'dist' de la recta
|
243 |
* p1-p2 y se encuentra en la recta perpendicular que pasa por perpPoint
|
244 |
*
|
245 |
* @param p1 Punto de la recta p1-p2
|
246 |
* @param p2 Punto de la recta p1-p2
|
247 |
* @param perpPoint Punto de la recta perpendicular
|
248 |
* @param dist Distancia del punto que se quiere obtener a la recta p1-p2
|
249 |
*
|
250 |
* @return DOCUMENT ME!
|
251 |
*/
|
252 |
private static Point2D getPerpendicularPoint(Point2D p1, Point2D p2, |
253 |
Point2D perpPoint, double dist) { |
254 |
Point2D[] p = getPerpendicular(p1, p2, perpPoint); |
255 |
Point2D unit = getUnitVector(p[0], p[1]); |
256 |
|
257 |
return new Point2D.Double(perpPoint.getX() + (unit.getX() * dist), |
258 |
perpPoint.getY() + (unit.getY() * dist)); |
259 |
} |
260 |
|
261 |
|
262 |
/**
|
263 |
* Obtiene un par de puntos que definen la recta perpendicular a p1-p2 que
|
264 |
* pasa por el punto perp
|
265 |
*
|
266 |
* @param p1 punto de la recta p1-p2
|
267 |
* @param p2 punto de la recta p1-p2
|
268 |
* @param perp Punto por el que pasa la recta perpendicular, debe ser
|
269 |
* distinto a p2
|
270 |
*
|
271 |
* @return Array con dos puntos que definen la recta resultante
|
272 |
*/
|
273 |
private static Point2D[] getPerpendicular(Point2D p1, Point2D p2, |
274 |
Point2D perp) {
|
275 |
if ((p2.getY() - p1.getY()) == 0) { |
276 |
return new Point2D[] { |
277 |
new Point2D.Double(perp.getX(), 0), |
278 |
new Point2D.Double(perp.getX(), 1) |
279 |
}; |
280 |
} |
281 |
|
282 |
//Pendiente de la recta perpendicular
|
283 |
double m = (p1.getX() - p2.getX()) / (p2.getY() - p1.getY());
|
284 |
|
285 |
//b de la funcion de la recta perpendicular
|
286 |
double b = perp.getY() - (m * perp.getX());
|
287 |
|
288 |
//Obtenemos un par de puntos
|
289 |
Point2D[] res = new Point2D[2]; |
290 |
|
291 |
res[0] = new Point2D.Double(0, (m * 0) + b); |
292 |
res[1] = new Point2D.Double(1000, (m * 1000) + b); |
293 |
|
294 |
return res;
|
295 |
} |
296 |
|
297 |
/**
|
298 |
* Devuelve un vector unitario en forma de punto a partir de dos puntos.
|
299 |
*
|
300 |
* @param p1 punto origen.
|
301 |
* @param p2 punto destino.
|
302 |
*
|
303 |
* @return vector unitario.
|
304 |
*/
|
305 |
private static Point2D getUnitVector(Point2D p1, Point2D p2) { |
306 |
Point2D paux = new Point2D.Double(p2.getX() - p1.getX(), |
307 |
p2.getY() - p1.getY()); |
308 |
double v = Math.sqrt(Math.pow(paux.getX(), 2d) + |
309 |
Math.pow(paux.getY(), 2d)); |
310 |
paux = new Point2D.Double(paux.getX() / v, paux.getY() / v); |
311 |
|
312 |
return paux;
|
313 |
} |
314 |
public void print(Graphics2D g, AffineTransform at, Geometry geom, PrintAttributes properties) { |
315 |
double originalSize = getLineWidth();
|
316 |
double size=originalSize;
|
317 |
// scale it to size
|
318 |
int pq = properties.getPrintQuality();
|
319 |
if (pq == PrintAttributes.PRINT_QUALITY_NORMAL){
|
320 |
size *= (double) 300/72; |
321 |
}else if (pq == PrintAttributes.PRINT_QUALITY_HIGH){ |
322 |
size *= (double) 600/72; |
323 |
}else if (pq == PrintAttributes.PRINT_QUALITY_DRAFT){ |
324 |
// size *= 72/72; // (which is the same than doing nothing)
|
325 |
} |
326 |
setLineWidth(size); |
327 |
draw(g,at,geom,null, null); |
328 |
setLineWidth(originalSize); |
329 |
} |
330 |
|
331 |
public Object clone() throws CloneNotSupportedException { |
332 |
AbstractLineSymbol copy = (AbstractLineSymbol) super.clone();
|
333 |
|
334 |
// Clone the line style
|
335 |
if (lineStyle != null) { |
336 |
copy.lineStyle = (ILineStyle) lineStyle.clone(); |
337 |
} |
338 |
|
339 |
return copy;
|
340 |
} |
341 |
|
342 |
|
343 |
public void loadFromState(PersistentState state) |
344 |
throws PersistenceException {
|
345 |
// Set parent symbol properties
|
346 |
super.loadFromState(state);
|
347 |
// Set own properties
|
348 |
setColor((Color) state.get(FIELD_COLOR));
|
349 |
setLineStyle((ILineStyle) state.get(FIELD_LINESTYLE)); |
350 |
} |
351 |
|
352 |
public void saveToState(PersistentState state) throws PersistenceException { |
353 |
// Save parent symbol properties
|
354 |
super.saveToState(state);
|
355 |
// Save own properties
|
356 |
state.set(FIELD_COLOR, getColor()); |
357 |
state.set(FIELD_LINESTYLE, getLineStyle()); |
358 |
} |
359 |
|
360 |
public static class RegisterPersistence implements Callable { |
361 |
|
362 |
public Object call() throws Exception { |
363 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
364 |
if( manager.getDefinition(LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME)==null ) { |
365 |
DynStruct definition = manager.addDefinition( |
366 |
AbstractLineSymbol.class, |
367 |
LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME, |
368 |
LINE_SYMBOL_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
369 |
null,
|
370 |
null
|
371 |
); |
372 |
|
373 |
// Extend the Symbol base definition
|
374 |
definition.extend(manager.getDefinition(SYMBOL_PERSISTENCE_DEFINITION_NAME)); |
375 |
|
376 |
// Color
|
377 |
definition.addDynFieldObject(FIELD_COLOR).setMandatory(false).setClassOfValue(Color.class); |
378 |
// LineStyle
|
379 |
definition.addDynFieldObject(FIELD_LINESTYLE).setMandatory(false).setClassOfValue(ILineStyle.class);
|
380 |
} |
381 |
return Boolean.TRUE; |
382 |
} |
383 |
|
384 |
} |
385 |
|
386 |
|
387 |
} |