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