Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / MarkerFillSymbol.java @ 37958

History | View | Annotate | Download (17.3 KB)

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

    
42
/* CVS MESSAGES:
43
*
44
* $Id: MarkerFillSymbol.java 16176 2007-11-08 16:07:26Z jdominguez $
45
* $Log$
46
* Revision 1.19  2007-09-21 12:25:32  jaume
47
* cancellation support extended down to the IGeometry and ISymbol level
48
*
49
* Revision 1.18  2007/09/20 11:53:11  jvidal
50
* bug solved
51
*
52
* Revision 1.16  2007/08/09 10:39:41  jaume
53
* first round of found bugs fixed
54
*
55
* Revision 1.15  2007/08/08 12:04:05  jvidal
56
* javadoc
57
*
58
* Revision 1.14  2007/08/03 09:22:09  jaume
59
* refactored class names
60
*
61
* Revision 1.13  2007/08/02 11:13:50  jaume
62
* char encoding fix
63
*
64
* Revision 1.12  2007/08/01 11:45:59  jaume
65
* passing general tests (drawing test yet missing)
66
*
67
* Revision 1.11  2007/07/23 06:52:25  jaume
68
* default selection color refactored, moved to MapContext
69
*
70
* Revision 1.10  2007/05/28 15:36:42  jaume
71
* *** empty log message ***
72
*
73
* Revision 1.9  2007/05/08 08:47:40  jaume
74
* *** empty log message ***
75
*
76
* Revision 1.8  2007/03/28 16:48:14  jaume
77
* *** empty log message ***
78
*
79
* Revision 1.7  2007/03/26 14:25:17  jaume
80
* implements IPrintable
81
*
82
* Revision 1.6  2007/03/21 17:36:22  jaume
83
* *** empty log message ***
84
*
85
* Revision 1.5  2007/03/13 16:58:36  jaume
86
* Added QuantityByCategory (Multivariable legend) and some bugfixes in symbols
87
*
88
* Revision 1.4  2007/03/09 11:20:57  jaume
89
* Advanced symbology (start committing)
90
*
91
* Revision 1.2.2.4  2007/02/16 10:54:12  jaume
92
* multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
93
*
94
* Revision 1.2.2.3  2007/02/15 16:23:44  jaume
95
* *** empty log message ***
96
*
97
* Revision 1.2.2.2  2007/02/12 15:15:20  jaume
98
* refactored interval legend and added graduated symbol legend
99
*
100
* Revision 1.2.2.1  2007/02/09 07:47:05  jaume
101
* Isymbol moved
102
*
103
* Revision 1.2  2007/01/10 16:39:41  jaume
104
* ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
105
*
106
* Revision 1.1  2007/01/10 16:31:36  jaume
107
* *** empty log message ***
108
*
109
* Revision 1.10  2006/11/09 18:39:05  jaume
110
* *** empty log message ***
111
*
112
* Revision 1.9  2006/11/09 10:22:50  jaume
113
* *** empty log message ***
114
*
115
* Revision 1.8  2006/11/08 13:05:51  jaume
116
* *** empty log message ***
117
*
118
* Revision 1.7  2006/11/08 10:56:47  jaume
119
* *** empty log message ***
120
*
121
* Revision 1.6  2006/11/07 08:52:30  jaume
122
* *** empty log message ***
123
*
124
* Revision 1.5  2006/11/06 17:08:45  jaume
125
* *** empty log message ***
126
*
127
* Revision 1.4  2006/11/06 16:06:52  jaume
128
* *** empty log message ***
129
*
130
* Revision 1.3  2006/11/06 07:33:54  jaume
131
* javadoc, source style
132
*
133
* Revision 1.2  2006/10/31 16:16:34  jaume
134
* *** empty log message ***
135
*
136
* Revision 1.1  2006/10/30 19:30:35  jaume
137
* *** empty log message ***
138
*
139
*
140
*/
141
package org.gvsig.symbology.fmap.symbols;
142

    
143
import java.awt.Color;
144
import java.awt.Graphics2D;
145
import java.awt.Paint;
146
import java.awt.Rectangle;
147
import java.awt.RenderingHints;
148
import java.awt.TexturePaint;
149
import java.awt.geom.AffineTransform;
150
import java.awt.geom.Point2D;
151
import java.awt.image.BufferedImage;
152
import java.util.Random;
153

    
154
import javax.print.attribute.PrintRequestAttributeSet;
155

    
156
import org.gvsig.symbology.fmap.styles.SimpleMarkerFillPropertiesStyle;
157

    
158
import com.iver.cit.gvsig.fmap.MapContext;
159
import com.iver.cit.gvsig.fmap.Messages;
160
import com.iver.cit.gvsig.fmap.ViewPort;
161
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
162
import com.iver.cit.gvsig.fmap.core.FPoint2D;
163
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
164
import com.iver.cit.gvsig.fmap.core.FShape;
165
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
166
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
167
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
168
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol;
169
import com.iver.cit.gvsig.fmap.core.symbols.AbstractMarkerSymbol;
170
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
171
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
172
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
173
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
174
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
175
import com.iver.utiles.StringUtilities;
176
import com.iver.utiles.XMLEntity;
177
import com.iver.utiles.swing.threads.Cancellable;
178
import com.vividsolutions.jts.geom.Envelope;
179
import com.vividsolutions.jts.geom.Geometry;
180

    
181
/**
182
 * Allows to define a marker symbol of any type as a path image to be used for a filled of a
183
 * polygon's padding
184
 *
185
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
186
 */
187
public class MarkerFillSymbol extends AbstractFillSymbol {
188
        public static final int RANDOM_FILL = 3;
189
        public static final int GRID_FILL = 1;
190
        public static final int SINGLE_CENTERED_SYMBOL = 2;
191
        public static int DefaultFillStyle = GRID_FILL;
192
        private MarkerFillSymbol selectionSymbol;
193
        private IMarkerFillPropertiesStyle markerFillProperties = new SimpleMarkerFillPropertiesStyle();
194
        private IMarkerSymbol markerSymbol = SymbologyFactory.createDefaultMarkerSymbol();
195
        private double previousMarkerSize = markerSymbol.getSize();
196
        private PrintRequestAttributeSet properties;
197

    
198
        public ISymbol getSymbolForSelection() {
199
                if (selectionSymbol == null) {
200
                        selectionSymbol = (MarkerFillSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), null);
201
                        selectionSymbol.setFillColor(MapContext.getSelectionColor());
202
                }
203

    
204
                return selectionSymbol;
205
        }
206

    
207
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
208
                switch (markerFillProperties.getFillStyle()) {
209
                case SINGLE_CENTERED_SYMBOL:
210
                        // case a single marker is used into a polygon shapetype
211
                        Geometry geom = FConverter.java2d_to_jts(shp);
212
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
213
                        /*
214
                         * Hay ocasiones en que jts no puede calcular un centroide y devuelve NaN
215
                         * (por ejemplo con geometr?as poligonales cuyos puntos tienen todos la misma
216
                         * abscisa y distinta ordenada con tan solo una diferencia de 1 ? 2 unidades)
217
                         * entonces, en lugar de utilizar este centroide tomamos el centro del
218
                         * bounds del shp (la geometr?a es tan peque?a que consideramos que deben coincidir).
219
                         */
220
                        if(!(Double.isNaN(centroid.getX()) ||Double.isNaN(centroid.getY()))){
221
                                double centroidX = centroid.getX()+markerFillProperties.getXOffset();
222
                                double centroidY = centroid.getY()+markerFillProperties.getYOffset();
223
                                FPoint2D p = new FPoint2D(new Point2D.Double(centroidX,centroidY));
224
                                markerSymbol.draw(g, affineTransform, p, null);
225
                        } else {
226
                                double centroidX = shp.getBounds().getCenterX();
227
                                double centroidY = shp.getBounds().getCenterY();
228
                                FPoint2D p = new FPoint2D(new Point2D.Double(centroidX,centroidY));
229
                                markerSymbol.draw(g, affineTransform, p, null);
230
                        }
231
                        break;
232
                case GRID_FILL:
233
                        // case a grid fill is used
234
                        {
235
                        Rectangle rClip = null;
236
                        if (g.getClipBounds()!=null){
237
                                rClip=(Rectangle)g.getClipBounds().clone();
238
                                g.setClip(rClip.x, rClip.y, rClip.width, rClip.height);
239
                        }
240
                        g.clip(shp);
241

    
242
                        int size = (int) markerSymbol.getSize();
243
                        Rectangle rProv = new Rectangle();
244
                        rProv.setFrame(0, 0, size, size);
245
                        Paint resulPatternFill = null;
246

    
247
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
248
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
249
                        double xOffset = markerFillProperties.getXOffset();
250
                        double yOffset = markerFillProperties.getYOffset();
251

    
252
                        BufferedImage sample = null;
253
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
254
                        Graphics2D gAux = sample.createGraphics();
255
                        gAux.setRenderingHint(RenderingHints.KEY_ANTIALIASING,        RenderingHints.VALUE_ANTIALIAS_ON);
256

    
257
                        try {
258
                                markerSymbol.drawInsideRectangle(gAux, gAux.getTransform(), rProv, null);
259
                        } catch (SymbolDrawingException e) {
260
                                if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
261
                                        try {
262
                                                SymbologyFactory.getWarningSymbol(
263
                                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
264
                                                                "",
265
                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS).drawInsideRectangle(gAux, gAux.getTransform(), rProv, null);
266
                                        } catch (SymbolDrawingException e1) {
267
                                                // IMPOSSIBLE TO REACH THIS
268
                                        }
269
                                } else {
270
                                        // should be unreachable code
271
                                        throw new Error(Messages.getString("symbol_shapetype_mismatch"));
272
                                }
273
                        }
274
                        rProv.setRect(0, 0,
275
                                        rProv.getWidth() + xSeparation,
276
                                        rProv.getHeight() + ySeparation);
277

    
278
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
279
                        gAux = bi.createGraphics();
280
                        gAux.setRenderingHint(RenderingHints.KEY_ANTIALIASING,        RenderingHints.VALUE_ANTIALIAS_ON);
281

    
282
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
283
                
284
                        resulPatternFill = new TexturePaint(bi,rProv);
285
                        sample = null;
286
                        gAux.dispose();
287

    
288
                        g.setColor(null);
289
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
290
                                RenderingHints.VALUE_ANTIALIAS_ON);
291

    
292
                        g.translate(xOffset, -yOffset);
293
                        g.setPaint(resulPatternFill);
294
                        g.fill(shp);
295
                        g.translate(-xOffset, +yOffset);
296
                        g.setClip(rClip);
297
                        bi = null;
298
                        }
299
                        break;
300
                case RANDOM_FILL:
301
                        {
302

    
303
                        double s = markerSymbol.getSize();
304
                        Rectangle r = shp.getBounds();
305
                        int drawCount = (int) (Math.min(r.getWidth(), r.getHeight())/s);
306
                        Random random = new Random();
307

    
308
                        int minx = r.x;
309
                        int miny = r.y;
310
                        int width = r.width;
311
                        int height = r.height;
312

    
313
                        r = new Rectangle();
314
                        g.setClip(shp);
315

    
316
                        for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < drawCount; i++) {
317
                                int x = (int) Math.abs(random.nextDouble() * width);
318
                                int y = (int) Math.abs(random.nextDouble() * height);
319
                                x = x + minx;
320
                                y = y + miny;
321
                                markerSymbol.draw(g, new AffineTransform(), new FPoint2D(x, y), cancel);
322

    
323
                        }
324
                        g.setClip(null);
325
                        }
326
                        break;
327
                }
328
                if(getOutline()!= null){
329
                        getOutline().draw(g, affineTransform, shp, cancel);
330
                }
331

    
332
        }
333

    
334
        public XMLEntity getXMLEntity() {
335
                XMLEntity xml = new XMLEntity();
336
                xml.putProperty("className", getClassName());
337
                xml.putProperty("isShapeVisible", isShapeVisible());
338
                // color (necessite)
339
                if (getFillColor() !=null)
340
                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
341
                xml.putProperty("desc", getDescription());
342
                xml.putProperty("referenceSystem", getReferenceSystem());
343
                xml.putProperty("unit", getUnit());
344
                xml.addChild(markerSymbol.getXMLEntity());
345
                xml.addChild(markerFillProperties.getXMLEntity());
346

    
347
                if (getOutline()!=null) {
348
                        XMLEntity outlineXML = getOutline().getXMLEntity();
349
                        outlineXML.putProperty("id", "outline symbol");
350
                        xml.addChild(outlineXML);
351
                }
352
                xml.putProperty("hasOutline", hasOutline());
353
                return xml;
354
        }
355

    
356
        public int getSymbolType() {
357
                return FShape.POLYGON;
358
        }
359

    
360
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException {
361
                markerFillProperties.setSampleSymbol(markerSymbol);
362
                switch (markerFillProperties.getFillStyle()) {
363
                case SINGLE_CENTERED_SYMBOL:
364
                        FPoint2D p = new FPoint2D(r.getCenterX(), r.getCenterY());
365
                        markerSymbol.draw(g, null, p, null);
366
                        break;
367
                case GRID_FILL:
368
                {
369
                        g.setClip(r);
370
                        int size = (int) markerSymbol.getSize();
371
                        if (size <= 0 ) size = 1;
372
                        Rectangle rProv = new Rectangle();
373
                        rProv.setFrame(0, 0, size, size);
374
                        Paint resulPatternFill = null;
375

    
376
                        BufferedImage sample = null;
377
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
378
                        Graphics2D gAux = sample.createGraphics();
379

    
380
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
381
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
382
                        double xOffset = markerFillProperties.getXOffset();
383
                        double yOffset = markerFillProperties.getYOffset();
384

    
385
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv, properties);
386

    
387
                        rProv.setRect(0, 0,
388
                                        rProv.getWidth() + xSeparation,
389
                                        rProv.getHeight() + ySeparation);
390

    
391
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
392
                        gAux = bi.createGraphics();
393
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
394

    
395

    
396
                        resulPatternFill = new TexturePaint(bi,rProv);
397
                        g.setColor(null);
398
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
399
                                        RenderingHints.VALUE_ANTIALIAS_ON);
400

    
401
//                        g.translate(xOffset, rProv.getHeight()-yOffset);
402
                        g.translate(xOffset, -yOffset);
403
                        g.setPaint(resulPatternFill);
404
                        g.fill(r);
405
//                        g.translate(-xOffset, -rProv.getHeight()+yOffset);
406
                        g.translate(-xOffset, yOffset);
407
                        g.setClip(null);
408
                }
409
                        break;
410
                case RANDOM_FILL:
411
                        g.setClip(r);
412
                        int x = r.x;
413
                        int y = r.y;
414
                        int width = r.width;
415
                        int height= r.height;
416
                        g.setBackground(null);
417

    
418
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.2), (y+height*0.8)), null);
419
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.634), (y+height*0.3)), null);
420
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.26), (y+height*0.35)), null);
421
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.45), (y+height*0.98)), null);
422
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.9), (y+height*0.54)), null);
423
                        markerSymbol.draw(g, null, new FPoint2D((x+width*1.1), (y+height*0.7)), null);
424
                        g.setClip(null);
425
                        break;
426
                }
427
                if(getOutline()!= null && hasOutline()){
428
                        if (properties==null)
429
                                getOutline().draw(g, scaleInstance, new FPolyline2D(new GeneralPathX(r)), null);
430
                        else
431
                                getOutline().print(g, scaleInstance, new FPolyline2D(new GeneralPathX(r)), properties);
432
                }
433
        }
434

    
435

    
436
        public String getClassName() {
437
                return getClass().getName();
438
        }
439

    
440
        public void setXMLEntity(XMLEntity xml) {
441
                setDescription(xml.getStringProperty("desc"));
442
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
443

    
444
                markerSymbol = (AbstractMarkerSymbol) SymbologyFactory.
445
                                                        createSymbolFromXML(xml.getChild(0), null);
446
                markerFillProperties = (SimpleMarkerFillPropertiesStyle) SymbologyFactory.
447
                                                        createStyleFromXML(xml.getChild(1), null);
448

    
449
                if (xml.contains("unit")) { // remove this line when done
450
                // measure unit (for outline)
451
                setUnit(xml.getIntProperty("unit"));
452

    
453
                // reference system (for outline)
454
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
455
                }
456

    
457
                if(xml.contains("hasOutline")) {
458
                        XMLEntity outlineXML = xml.firstChild("id", "outline symbol");
459
                        if (outlineXML != null) {
460
                                setOutline((ILineSymbol) SymbologyFactory.createSymbolFromXML(outlineXML, "outline"));
461
                        }
462
                }
463
        }
464

    
465
        public void setMarker(IMarkerSymbol marker) {
466
                this.markerSymbol = marker;
467
        }
468

    
469
        public IMarkerSymbol getMarker() {
470
                return markerSymbol;
471
        }
472

    
473
        public Color getFillColor(){
474
                return markerSymbol.getColor();
475
        }
476

    
477
        public void setFillColor (Color color) {
478
                markerSymbol.setColor(color);
479
        }
480

    
481
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) {
482
                this.properties=properties;
483
        draw(g, at, shape, null);
484
        this.properties=null;
485

    
486
        }
487
        /**
488
         * Sets the markerfillproperties to be used by the class
489
         *
490
         * @param markerFillStyle,IMarkerFillPropertiesStyle
491
         */
492
        public void setMarkerFillProperties(IMarkerFillPropertiesStyle markerFillStyle) {
493
                this.markerFillProperties = markerFillStyle;
494
        }
495

    
496
        /**
497
         * Returns the markerfillproperties that are used by the class
498
         *
499
         * @return markerFillProperties,IMarkerFillPropertiesStyle
500
         */
501
        public IMarkerFillPropertiesStyle getMarkerFillProperties() {
502
                return markerFillProperties;
503
        }
504

    
505

    
506
        @Override
507
        public void setUnit(int unitIndex) {
508
                super.setUnit(unitIndex);
509
                if (getMarker()!=null) {
510
                        getMarker().setUnit(unitIndex);
511
                }
512
        }
513

    
514
        @Override
515
        public void setReferenceSystem(int system) {
516
                super.setReferenceSystem(system);
517
                if (getMarker()!=null) {
518
                        getMarker().setReferenceSystem(system);
519
                }
520
        }
521

    
522
        public void setCartographicSize(double cartographicSize, FShape shp) {
523
                
524
                super.setCartographicSize(cartographicSize, shp);
525
                IMarkerSymbol marker = getMarker();
526
                if (marker!=null) {
527
                                marker.setCartographicSize(previousMarkerSize, shp);
528
                        }
529
                
530
                super.setCartographicSize(cartographicSize, shp);
531

    
532
        }
533

    
534
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
535
                IMarkerSymbol marker = getMarker();
536
                if (marker!=null) {
537
                        previousMarkerSize = marker.getSize();
538
                        double size = CartographicSupportToolkit.getCartographicLength(this, previousMarkerSize, viewPort, dpi);
539
                        marker.setSize(size);
540
                }
541
                double s = super.toCartographicSize(viewPort, dpi, shp);
542
                return s;
543
                
544
        }
545
}