Statistics
| Revision:

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 @ 43002

History | View | Annotate | Download (7.67 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.GeometryException;
36
import org.gvsig.fmap.geom.GeometryLocator;
37
import org.gvsig.fmap.geom.GeometryManager;
38
import org.gvsig.fmap.geom.aggregate.MultiLine;
39
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
40
import org.gvsig.fmap.geom.aggregate.MultiSurface;
41
import org.gvsig.fmap.geom.exception.CreateGeometryException;
42
import org.gvsig.fmap.geom.primitive.Curve;
43
import org.gvsig.fmap.geom.primitive.GeneralPathX;
44
import org.gvsig.fmap.geom.primitive.Surface;
45
import org.gvsig.fmap.geom.type.GeometryType;
46
import org.gvsig.fmap.mapcontext.ViewPort;
47
import org.gvsig.fmap.mapcontext.rendering.symbols.CartographicSupport;
48
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
49
import org.gvsig.tools.task.Cancellable;
50

    
51
/**
52
 * @author gvSIG Team
53
 *
54
 */
55
public class DrawInts extends GeometryOperation {
56
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
57
        public static final String NAME = "drawInts";
58
        public static int CODE = Integer.MIN_VALUE;
59

    
60
        final static private Logger logger = LoggerFactory.getLogger(DrawInts.class);
61

    
62
        public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException{
63
                DrawOperationContext doc=(DrawOperationContext)ctx;
64
                ViewPort viewPort = doc.getViewPort();
65
                ISymbol symbol = doc.getSymbol();
66
                Graphics2D g=doc.getGraphics();
67
                Cancellable cancel=doc.getCancellable();
68
        try {
69
            if (doc.hasDPI()) {
70
                double previousSize = ((CartographicSupport) symbol).toCartographicSize(viewPort, doc.getDPI(), geom);
71
                // draw it as normally
72
                Geometry transformedGeometry;
73
                transformedGeometry = transformGeometry(geom.cloneGeometry(), viewPort.getAffineTransform());
74

    
75
                // the AffineTransform has to be null because the transformToInts method reprojects the geometry
76
                symbol.draw(g, null, transformedGeometry, doc.getFeature(), cancel);
77

    
78
                // restore previous size
79
                ((CartographicSupport) symbol).setCartographicSize(previousSize, geom);
80
            } else {
81
                // Geometry decimatedShape = transformToInts(geom, viewPort.getAffineTransform());
82
                Geometry transformedGeometry = transformGeometry(geom.cloneGeometry(), viewPort.getAffineTransform());
83
                symbol.draw(g, viewPort.getAffineTransform(), transformedGeometry, doc.getFeature(), cancel);
84
            }
85
        } catch (GeometryException e) {
86
                    throw new GeometryOperationException(e);
87
                }
88

    
89
                return null;
90
        }
91

    
92
        /**
93
     * @param geom
94
     * @param affineTransform
95
     * @return
96
         * @throws GeometryException
97
     */
98
    private Geometry transformGeometry(Geometry geom, AffineTransform affineTransform) throws GeometryException {
99
        if(geom.canBeTransformed(affineTransform)){
100
            geom.transform(affineTransform);
101
            return geom;
102
        }
103
        if(geom instanceof Curve || geom instanceof Curve){
104
            MultiLine lines = geom.toLines();
105
            lines.transform(affineTransform);
106
            return lines;
107
        }
108
        if(geom instanceof Surface || geom instanceof MultiSurface){
109
            MultiPolygon polygons = geom.toPolygons();
110
            polygons.transform(affineTransform);
111
            return polygons;
112
        }
113
        return null;
114
    }
115

    
116
    public int getOperationIndex() {
117
                return CODE;
118
        }
119

    
120
        private Geometry transformToInts(Geometry gp, AffineTransform at) throws CreateGeometryException {
121
                GeneralPathX newGp = new GeneralPathX();
122
                double[] theData = new double[6];
123
                double[] aux = new double[6];
124

    
125
                // newGp.reset();
126
                PathIterator theIterator;
127
                int theType;
128
                int numParts = 0;
129

    
130
                java.awt.geom.Point2D ptDst = new java.awt.geom.Point2D.Double();
131
                java.awt.geom.Point2D ptSrc = new java.awt.geom.Point2D.Double();
132
                boolean bFirst = true;
133
                int xInt, yInt, antX = -1, antY = -1;
134

    
135

    
136
                theIterator = gp.getPathIterator(null); //, flatness);
137
                int numSegmentsAdded = 0;
138
                while (!theIterator.isDone()) {
139
                        theType = theIterator.currentSegment(theData);
140

    
141
                        switch (theType) {
142
                        case PathIterator.SEG_MOVETO:
143
                                numParts++;
144
                                ptSrc.setLocation(theData[0], theData[1]);
145
                                at.transform(ptSrc, ptDst);
146
                                antX = (int) ptDst.getX();
147
                                antY = (int) ptDst.getY();
148
                                newGp.moveTo(antX, antY);
149
                                numSegmentsAdded++;
150
                                bFirst = true;
151
                                break;
152

    
153
                        case PathIterator.SEG_LINETO:
154
                                ptSrc.setLocation(theData[0], theData[1]);
155
                                at.transform(ptSrc, ptDst);
156
                                xInt = (int) ptDst.getX();
157
                                yInt = (int) ptDst.getY();
158
                                if ((bFirst) || ((xInt != antX) || (yInt != antY)))
159
                                {
160
                                        newGp.lineTo(xInt, yInt);
161
                                        antX = xInt;
162
                                        antY = yInt;
163
                                        bFirst = false;
164
                                        numSegmentsAdded++;
165
                                }
166
                                break;
167

    
168
                        case PathIterator.SEG_QUADTO:
169
                                at.transform(theData,0,aux,0,2);
170
                                newGp.quadTo(aux[0], aux[1], aux[2], aux[3]);
171
                                numSegmentsAdded++;
172
                                break;
173

    
174
                        case PathIterator.SEG_CUBICTO:
175
                                at.transform(theData,0,aux,0,3);
176
                                newGp.curveTo(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5]);
177
                                numSegmentsAdded++;
178
                                break;
179

    
180
                        case PathIterator.SEG_CLOSE:
181
                                if (numSegmentsAdded < 3) {
182
                                        newGp.lineTo(antX, antY);
183
                                }
184
                                newGp.closePath();
185

    
186
                                break;
187
                        } //end switch
188

    
189
                        theIterator.next();
190
                } //end while loop
191

    
192
                Geometry geom = null;
193
                GeometryType geometryType = gp.getGeometryType();
194

    
195
                if(geometryType.isTypeOf(Geometry.TYPES.POINT)){
196
                    geom = geomManager.createPoint(ptDst.getX(), ptDst.getY(), SUBTYPES.GEOM2D);
197
                } else if(geometryType.isTypeOf(Geometry.TYPES.MULTIPOINT)){
198
            geom = geomManager.createMultiPoint(newGp, SUBTYPES.GEOM2D);
199
                } else if(geometryType.isTypeOf(Geometry.TYPES.CURVE)){
200
                    geom = geomManager.createCurve(newGp, SUBTYPES.GEOM2D);
201
        } else if(geometryType.isTypeOf(Geometry.TYPES.MULTICURVE)){
202
            geom = geomManager.createMultiCurve(newGp, SUBTYPES.GEOM2D);
203
                } else if(geometryType.isTypeOf(Geometry.TYPES.SURFACE)){
204
                    geom = geomManager.createSurface(newGp, SUBTYPES.GEOM2D);
205
        } else if(geometryType.isTypeOf(Geometry.TYPES.MULTISURFACE)){
206
            geom = geomManager.createMultiSurface(newGp, SUBTYPES.GEOM2D);
207
                }
208
                return geom;
209
        }
210

    
211
        /**
212
         *
213
         */
214
        public static void register() {
215

    
216
                GeometryManager gm = GeometryLocator.getGeometryManager();
217
                CODE = gm.getGeometryOperationCode(NAME);
218
                geomManager.registerGeometryOperation(NAME, new DrawInts());
219
        }
220

    
221

    
222
}