svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.operation / src / main / java / org / gvsig / fmap / geom / operation / DrawInts.java @ 42710
History | View | Annotate | Download (5.91 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.geom.operation; |
25 |
|
26 |
import java.awt.Graphics2D; |
27 |
import java.awt.geom.AffineTransform; |
28 |
import java.awt.geom.PathIterator; |
29 |
|
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.GeometryLocator; |
32 |
import org.gvsig.fmap.geom.GeometryManager; |
33 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
34 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
35 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
36 |
import org.gvsig.fmap.geom.type.GeometryType; |
37 |
import org.gvsig.fmap.mapcontext.ViewPort; |
38 |
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport; |
39 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
40 |
import org.gvsig.tools.task.Cancellable; |
41 |
import org.slf4j.Logger; |
42 |
import org.slf4j.LoggerFactory; |
43 |
|
44 |
public class DrawInts extends GeometryOperation { |
45 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
46 |
public static final String NAME = "drawInts"; |
47 |
public static int CODE = Integer.MIN_VALUE; |
48 |
|
49 |
final static private Logger logger = LoggerFactory.getLogger(DrawInts.class); |
50 |
|
51 |
public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException{ |
52 |
DrawOperationContext doc=(DrawOperationContext)ctx; |
53 |
ViewPort viewPort = doc.getViewPort(); |
54 |
ISymbol symbol = doc.getSymbol(); |
55 |
Graphics2D g=doc.getGraphics();
|
56 |
Cancellable cancel=doc.getCancellable(); |
57 |
// make the symbol to resize itself with the current rendering context
|
58 |
try {
|
59 |
if (doc.hasDPI()){
|
60 |
double previousSize = ((CartographicSupport)symbol).
|
61 |
toCartographicSize(viewPort, doc.getDPI(), geom); |
62 |
// draw it as normally
|
63 |
Geometry decimatedShape = transformToInts(geom, viewPort.getAffineTransform()); |
64 |
|
65 |
//the AffineTransform has to be null because the transformToInts method
|
66 |
//reprojects the geometry
|
67 |
symbol.draw(g, null, decimatedShape, doc.getFeature(), cancel);
|
68 |
|
69 |
// restore previous size
|
70 |
((CartographicSupport)symbol).setCartographicSize(previousSize, geom); |
71 |
}else{
|
72 |
Geometry decimatedShape = transformToInts(geom, viewPort.getAffineTransform()); |
73 |
symbol.draw(g, viewPort.getAffineTransform(), decimatedShape, |
74 |
doc.getFeature(), cancel); |
75 |
} |
76 |
} catch (CreateGeometryException e) {
|
77 |
e.printStackTrace(); |
78 |
throw new GeometryOperationException(e); |
79 |
} |
80 |
|
81 |
return null; |
82 |
} |
83 |
|
84 |
public int getOperationIndex() { |
85 |
return CODE;
|
86 |
} |
87 |
|
88 |
private Geometry transformToInts(Geometry gp, AffineTransform at) throws CreateGeometryException { |
89 |
GeneralPathX newGp = new GeneralPathX();
|
90 |
double[] theData = new double[6]; |
91 |
double[] aux = new double[6]; |
92 |
|
93 |
// newGp.reset();
|
94 |
PathIterator theIterator;
|
95 |
int theType;
|
96 |
int numParts = 0; |
97 |
|
98 |
java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
|
99 |
java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
|
100 |
boolean bFirst = true; |
101 |
int xInt, yInt, antX = -1, antY = -1; |
102 |
|
103 |
|
104 |
theIterator = gp.getPathIterator(null); //, flatness); |
105 |
int numSegmentsAdded = 0; |
106 |
while (!theIterator.isDone()) {
|
107 |
theType = theIterator.currentSegment(theData); |
108 |
|
109 |
switch (theType) {
|
110 |
case PathIterator.SEG_MOVETO: |
111 |
numParts++; |
112 |
ptSrc.setLocation(theData[0], theData[1]); |
113 |
at.transform(ptSrc, ptDst); |
114 |
antX = (int) ptDst.getX();
|
115 |
antY = (int) ptDst.getY();
|
116 |
newGp.moveTo(antX, antY); |
117 |
numSegmentsAdded++; |
118 |
bFirst = true;
|
119 |
break;
|
120 |
|
121 |
case PathIterator.SEG_LINETO: |
122 |
ptSrc.setLocation(theData[0], theData[1]); |
123 |
at.transform(ptSrc, ptDst); |
124 |
xInt = (int) ptDst.getX();
|
125 |
yInt = (int) ptDst.getY();
|
126 |
if ((bFirst) || ((xInt != antX) || (yInt != antY)))
|
127 |
{ |
128 |
newGp.lineTo(xInt, yInt); |
129 |
antX = xInt; |
130 |
antY = yInt; |
131 |
bFirst = false;
|
132 |
numSegmentsAdded++; |
133 |
} |
134 |
break;
|
135 |
|
136 |
case PathIterator.SEG_QUADTO: |
137 |
at.transform(theData,0,aux,0,2); |
138 |
newGp.quadTo(aux[0], aux[1], aux[2], aux[3]); |
139 |
numSegmentsAdded++; |
140 |
break;
|
141 |
|
142 |
case PathIterator.SEG_CUBICTO: |
143 |
at.transform(theData,0,aux,0,3); |
144 |
newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]); |
145 |
numSegmentsAdded++; |
146 |
break;
|
147 |
|
148 |
case PathIterator.SEG_CLOSE: |
149 |
if (numSegmentsAdded < 3) { |
150 |
newGp.lineTo(antX, antY); |
151 |
} |
152 |
newGp.closePath(); |
153 |
|
154 |
break;
|
155 |
} //end switch
|
156 |
|
157 |
theIterator.next(); |
158 |
} //end while loop
|
159 |
|
160 |
Geometry geom = null;
|
161 |
GeometryType geometryType = gp.getGeometryType(); |
162 |
|
163 |
if(geometryType.isTypeOf(Geometry.TYPES.POINT)){
|
164 |
geom = geomManager.createPoint(ptDst.getX(), ptDst.getY(), SUBTYPES.GEOM2D); |
165 |
} else if(geometryType.isTypeOf(Geometry.TYPES.CURVE)){ |
166 |
geom = geomManager.createCurve(newGp, SUBTYPES.GEOM2D); |
167 |
} else if(geometryType.isTypeOf(Geometry.TYPES.SURFACE)){ |
168 |
geom = geomManager.createSurface(newGp, SUBTYPES.GEOM2D); |
169 |
} |
170 |
return geom;
|
171 |
} |
172 |
|
173 |
public static void register() { |
174 |
|
175 |
GeometryManager gm = GeometryLocator.getGeometryManager(); |
176 |
CODE = gm.getGeometryOperationCode(NAME); |
177 |
geomManager.registerGeometryOperation(NAME, new DrawInts());
|
178 |
} |
179 |
|
180 |
|
181 |
} |