Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FGeometry.java @ 5878

History | View | Annotate | Download (9.55 KB)

1
/*
2
 * Created on 25-nov-2004
3
 *
4
 * TODO To change the template for this generated file go to
5
 * Window - Preferences - Java - Code Generation - Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.cit.gvsig.fmap.core;
48

    
49
import java.awt.Graphics2D;
50
import java.awt.Rectangle;
51
import java.awt.Shape;
52
import java.awt.geom.AffineTransform;
53
import java.awt.geom.PathIterator;
54
import java.awt.geom.Point2D;
55
import java.awt.geom.Rectangle2D;
56
import java.io.IOException;
57

    
58
import org.cresques.cts.ICoordTrans;
59
import org.geotools.data.postgis.attributeio.WKBEncoder;
60

    
61
import com.iver.cit.gvsig.fmap.ViewPort;
62
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
63
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
64
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
65
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
66
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
67
import com.vividsolutions.jts.geom.Geometry;
68

    
69

    
70
/**
71
 * Geometr?a.
72
 *
73
 * @author FJP
74
 */
75
public class FGeometry implements IGeometry3D {
76
    /**
77
     *
78
     */
79
    private static final long serialVersionUID = 5431865770186523337L;
80
    private FShape shp;
81
    private Rectangle2D bounds2D;
82
    private int type=FShape.LINE;
83

    
84
    /**
85
     * Crea un nuevo FGeometry.
86
     *
87
     * @param shp DOCUMENT ME!
88
     */
89
    FGeometry(FShape shp) {
90
        this.shp = shp;
91
    }
92

    
93
    /* (non-Javadoc)
94
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, org.geotools.renderer.style.Style2D)
95
     */
96
    public void draw(Graphics2D g, ViewPort vp, FStyle2D symbol) {
97
        // OJO CON LA PRECISION. DEBERIAMOS USAR: ((GeneralPathX) shp.m_Polyline).transform(mT);
98
        // O HACER UNA FUNCION DE TRANSFORMACI?N QUE USE LOS DOUBLES DEL
99
        // SHAPE
100
        // Shape shpTrans = vp.getAffineTransform().createTransformedShape(shp);
101
        transform(vp.getAffineTransform());
102

    
103
        switch (shp.getShapeType()) {
104
            case FShape.POINT:
105
                shpPainter.paint(g, shp, symbol.getPointStyle2D(), 0);
106

    
107
                break;
108

    
109
            case FShape.LINE:
110
                shpPainter.paint(g, shp, symbol.getLineStyle2D(), 0);
111

    
112
                break;
113

    
114
            case FShape.POLYGON:
115
                shpPainter.paint(g, shp, symbol.getPolygonStyle2D(), 0);
116

    
117
                break;
118

    
119
            case FShape.TEXT:
120
                shpPainter.paint(g, shp, symbol.getTextStyle2D(), 0);
121
        }
122
    }
123

    
124
    /**
125
     * Dibuja la geometria actual en el graphics que se le pasa como par?metro,
126
     * aplicandole las caracter?sticas del s?mbolo.
127
     *
128
     * @param g Graphics2D.
129
     * @param vp ViewPort.
130
     * @param symbol S?mbolo.
131
     */
132
    public void draw(Graphics2D g, ViewPort vp, FSymbol symbol) {
133
        // OJO CON LA PRECISION. DEBERIAMOS USAR: ((GeneralPathX) shp.m_Polyline).transform(mT);
134
        // O HACER UNA FUNCION DE TRANSFORMACI?N QUE USE LOS DOUBLES DEL
135
        // SHAPE
136
        // Shape shpTrans = vp.getAffineTransform().createTransformedShape(shp);
137
        transform(vp.getAffineTransform());
138
        FGraphicUtilities.DrawShape(g, vp.getAffineTransform(), shp, symbol);
139
    }
140

    
141
    /* (non-Javadoc)
142
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#drawInts(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.core.v02.FSymbol)
143
     */
144
    public void drawInts(Graphics2D g, ViewPort vp, FSymbol symbol) {
145
        FShape decimatedShape = FConverter.transformToInts(this, vp.getAffineTransform());
146
        FGraphicUtilities.DrawShape(g, vp.getAffineTransform(), decimatedShape, symbol);
147
    }
148

    
149
    /**
150
     * Aplica la transformaci?n a la geometr?a de la matriz de transformaci?n
151
     * que se pasa como par?metro.
152
     *
153
     * @param at Matriz de transformaci?n.
154
     */
155
    public void transform(AffineTransform at) {
156
        shp.transform(at);
157
    }
158

    
159
    /* (non-Javadoc)
160
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
161
     */
162
    public boolean intersects(Rectangle2D r) {
163
            if (getGeometryType()==FShape.LINE && shp.getShapeType()==FShape.POLYGON) {
164
                return shp.intersects(r) && !shp.contains(r);
165
        }
166
        return shp.intersects(r);
167
    }
168

    
169
    /* (non-Javadoc)
170
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#getBounds2D()
171
     */
172
    public Rectangle2D getBounds2D() {
173
        if (bounds2D == null)
174
        {
175
            bounds2D = shp.getBounds2D();
176
        }
177
        // return shp.getBounds2D();
178
        return bounds2D;
179
    }
180

    
181
    public Rectangle2D reCalculateBounds2D()
182
    {
183
        return shp.getBounds2D();
184
    }
185

    
186
    /* (non-Javadoc)
187
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#toJTSGeometry()
188
     */
189
    public Geometry toJTSGeometry() {
190
        return FConverter.java2d_to_jts(shp);
191
    }
192

    
193
    /* (non-Javadoc)
194
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#createLabels(int, boolean)
195
     */
196
    public FLabel[] createLabels(int position, boolean duplicates) {
197
        FLabel[] aux = new FLabel[1];
198
        aux[0] = FLabel.createFLabel(shp);
199

    
200
        return aux;
201
    }
202

    
203
    /**
204
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeometryType()
205
     */
206
    public int getGeometryType() {
207
        return type;
208
    }
209

    
210
    /* (non-Javadoc)
211
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#cloneGeometry()
212
     */
213
    public IGeometry cloneGeometry() {
214
        IGeometry geom=new FGeometry(shp.cloneFShape());
215
        geom.setGeometryType(type);
216
        return geom;
217
    }
218

    
219
    /* (non-Javadoc)
220
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#reProject(org.cresques.cts.ICoordTrans)
221
     */
222
    public void reProject(ICoordTrans ct) {
223
        shp.reProject(ct);
224
    }
225

    
226
    /**
227
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
228
     */
229
    public PathIterator getPathIterator(AffineTransform at) {
230
        return (GeneralPathXIterator)shp.getPathIterator(null);
231
    }
232

    
233

    
234

    
235
    /* (non-Javadoc)
236
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#fastIntersects(double, double, double, double)
237
     */
238
    public boolean fastIntersects(double x, double y, double w, double h) {
239
        return shp.intersects(x,y,w,h);
240
    }
241

    
242
    public byte[] toWKB() throws IOException{
243
        return WKBEncoder.encodeGeometry(toJTSGeometry());
244
    }
245
    /**
246
     * Devuelve un array con todos los valores de Z.
247
     *
248
     * @return Array de Zs.
249
     */
250
    public double[] getZs() {
251
        return ((FShape3D) shp).getZs();
252
    }
253
    /**
254
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#getHandlers()
255
     */
256
    public Handler[] getHandlers(int type) {
257
        if (type==STRETCHINGHANDLER){
258
        return shp.getStretchingHandlers();
259
        }else if (type==SELECTHANDLER){
260
            return shp.getSelectHandlers();
261
        }
262
        return null;
263
    }
264

    
265
    /* (non-Javadoc)
266
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeneralPathXIterator(java.awt.geom.AffineTransform, double)
267
     */
268
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
269
        return shp.getPathIterator(at, flatness);
270
    }
271

    
272
    public boolean contains(double x, double y) {
273
        return shp.contains(x,y);
274
    }
275

    
276
    public boolean contains(double x, double y, double w, double h) {
277
        return shp.contains(x,y,w,h);
278
    }
279

    
280
    public boolean intersects(double x, double y, double w, double h) {
281
            if (getGeometryType()==FShape.LINE && shp.getShapeType()==FShape.POLYGON) {
282
                return shp.intersects(x,y,w,h) && !shp.contains(x,y,w,h);
283
        }
284
        return shp.intersects(x,y,w,h);
285
    }
286

    
287
    public Rectangle getBounds() {
288
        return shp.getBounds();
289
    }
290

    
291
    public boolean contains(Point2D p) {
292
        return shp.contains(p);
293
    }
294

    
295
    public boolean contains(Rectangle2D r) {
296
        return shp.contains(r);
297
    }
298

    
299
    public Shape getInternalShape() {
300
        return shp;
301
    }
302

    
303
    public int getOriginalGeometryType() {
304
        return shp.getShapeType();
305
    }
306

    
307
    public void setGeometryType(int type) {
308
        this.type=type;
309

    
310
    }
311

    
312
    /* public void move(double x, double y) {
313
        AffineTransform at = new AffineTransform();
314
        at.translate(x, y);
315
        transform(at);
316
    }
317
    public void rotate(double r, double x, double y) {
318
        AffineTransform at = new AffineTransform();
319
        at.rotate(r, x, y);
320
        transform(at);
321
    }
322
    public void scale(Point2D point,double x, double y) {
323
        AffineTransform at = new AffineTransform();
324
        at.setToTranslation(point.getX(),point.getY());
325
        at.scale(x,y);
326
        at.translate(-point.getX(),-point.getY());
327
        transform(at);
328
    } */
329
}