Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / MarkerFillSymbol.java @ 29244

History | View | Annotate | Download (16.5 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.FPoint2D;
162
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
163
import com.iver.cit.gvsig.fmap.core.FShape;
164
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
165
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
166
import com.iver.cit.gvsig.fmap.core.styles.IMarkerFillPropertiesStyle;
167
import com.iver.cit.gvsig.fmap.core.symbols.AbstractFillSymbol;
168
import com.iver.cit.gvsig.fmap.core.symbols.AbstractMarkerSymbol;
169
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
170
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
171
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
172
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
173
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
174
import com.iver.utiles.StringUtilities;
175
import com.iver.utiles.XMLEntity;
176
import com.iver.utiles.swing.threads.Cancellable;
177
import com.vividsolutions.jts.geom.Envelope;
178
import com.vividsolutions.jts.geom.Geometry;
179

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

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

    
202
                return selectionSymbol;
203
        }
204

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

    
239
                        BufferedImage sample = null;
240
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
241
                        Graphics2D gAux = sample.createGraphics();
242

    
243
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
244
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
245
                        double xOffset = markerFillProperties.getXOffset();
246
                        double yOffset = markerFillProperties.getYOffset();
247

    
248

    
249
                        try {
250
                                markerSymbol.drawInsideRectangle(gAux, gAux.getTransform(), rProv, null);
251
                        } catch (SymbolDrawingException e) {
252
                                if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
253
                                        try {
254
                                                SymbologyFactory.getWarningSymbol(
255
                                                                SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
256
                                                                "",
257
                                                                SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS).drawInsideRectangle(gAux, gAux.getTransform(), rProv, null);
258
                                        } catch (SymbolDrawingException e1) {
259
                                                // IMPOSSIBLE TO REACH THIS
260
                                        }
261
                                } else {
262
                                        // should be unreachable code
263
                                        throw new Error(Messages.getString("symbol_shapetype_mismatch"));
264
                                }
265
                        }
266
                        rProv.setRect(0, 0,
267
                                        rProv.getWidth() + xSeparation,
268
                                        rProv.getHeight() + ySeparation);
269

    
270
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
271
                        gAux = bi.createGraphics();
272
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
273

    
274
                        resulPatternFill = new TexturePaint(bi,rProv);
275

    
276
                        g.setColor(null);
277
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
278
                                RenderingHints.VALUE_ANTIALIAS_ON);
279

    
280
                        g.translate(xOffset, rProv.getHeight()-yOffset);
281
                        g.setPaint(resulPatternFill);
282
                        g.fill(shp);
283
                        g.translate(-xOffset, -rProv.getHeight()+yOffset);
284
                        g.setClip(null);
285

    
286
                        }
287
                        break;
288
                case RANDOM_FILL:
289
                        {
290

    
291
                        double s = markerSymbol.getSize();
292
                        Rectangle r = shp.getBounds();
293
                        int drawCount = (int) (Math.min(r.getWidth(), r.getHeight())/s);
294
                        Random random = new Random();
295

    
296
                        int minx = r.x;
297
                        int miny = r.y;
298
                        int width = r.width;
299
                        int height = r.height;
300

    
301
                        r = new Rectangle();
302
                        g.setClip(shp);
303

    
304
                        for (int i = 0; (cancel==null || !cancel.isCanceled()) && i < drawCount; i++) {
305
                                int x = (int) Math.abs(random.nextDouble() * width);
306
                                int y = (int) Math.abs(random.nextDouble() * height);
307
                                x = x + minx;
308
                                y = y + miny;
309
                                markerSymbol.draw(g, new AffineTransform(), new FPoint2D(x, y), cancel);
310

    
311
                        }
312
                        g.setClip(null);
313
                        }
314
                        break;
315
                }
316
                if(getOutline()!= null){
317
                        getOutline().draw(g, affineTransform, shp, cancel);
318
                }
319

    
320
        }
321

    
322
        public XMLEntity getXMLEntity() {
323
                XMLEntity xml = new XMLEntity();
324
                xml.putProperty("className", getClassName());
325
                xml.putProperty("isShapeVisible", isShapeVisible());
326
                // color (necessite)
327
                if (getFillColor() !=null)
328
                        xml.putProperty("color", StringUtilities.color2String(getFillColor()));
329
                xml.putProperty("desc", getDescription());
330
                xml.putProperty("referenceSystem", getReferenceSystem());
331
                xml.putProperty("unit", getUnit());
332
                xml.addChild(markerSymbol.getXMLEntity());
333
                xml.addChild(markerFillProperties.getXMLEntity());
334

    
335
                if (getOutline()!=null) {
336
                        XMLEntity outlineXML = getOutline().getXMLEntity();
337
                        outlineXML.putProperty("id", "outline symbol");
338
                        xml.addChild(outlineXML);
339
                }
340
                xml.putProperty("hasOutline", hasOutline());
341
                return xml;
342
        }
343

    
344
        public int getSymbolType() {
345
                return FShape.POLYGON;
346
        }
347

    
348
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintRequestAttributeSet properties) throws SymbolDrawingException {
349
                markerFillProperties.setSampleSymbol(markerSymbol);
350
                switch (markerFillProperties.getFillStyle()) {
351
                case SINGLE_CENTERED_SYMBOL:
352
                        FPoint2D p = new FPoint2D(r.getCenterX(), r.getCenterY());
353
                        markerSymbol.draw(g, null, p, null);
354
                        break;
355
                case GRID_FILL:
356
                {
357
                        g.setClip(r);
358
                        int size = (int) markerSymbol.getSize();
359
                        if (size <= 0 ) size = 1;
360
                        Rectangle rProv = new Rectangle();
361
                        rProv.setFrame(0, 0, size, size);
362
                        Paint resulPatternFill = null;
363

    
364
                        BufferedImage sample = null;
365
                        sample = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
366
                        Graphics2D gAux = sample.createGraphics();
367

    
368
                        double xSeparation = markerFillProperties.getXSeparation(); // TODO apply CartographicSupport
369
                        double ySeparation = markerFillProperties.getYSeparation(); // TODO apply CartographicSupport
370
                        double xOffset = markerFillProperties.getXOffset();
371
                        double yOffset = markerFillProperties.getYOffset();
372

    
373
                        markerSymbol.drawInsideRectangle(gAux, new AffineTransform(), rProv, properties);
374

    
375
                        rProv.setRect(0, 0,
376
                                        rProv.getWidth() + xSeparation,
377
                                        rProv.getHeight() + ySeparation);
378

    
379
                        BufferedImage bi = new BufferedImage(rProv.width, rProv.height, BufferedImage.TYPE_INT_ARGB);
380
                        gAux = bi.createGraphics();
381
                        gAux.drawImage(sample, null, (int) (xSeparation*0.5), (int) (ySeparation*0.5));
382

    
383

    
384
                        resulPatternFill = new TexturePaint(bi,rProv);
385
                        g.setColor(null);
386
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
387
                                        RenderingHints.VALUE_ANTIALIAS_ON);
388

    
389
                        g.translate(xOffset, rProv.getHeight()-yOffset);
390
                        g.setPaint(resulPatternFill);
391
                        g.fill(r);
392
                        g.translate(-xOffset, -rProv.getHeight()+yOffset);
393
                        g.setClip(null);
394
                }
395
                        break;
396
                case RANDOM_FILL:
397
                        g.setClip(r);
398
                        int x = r.x;
399
                        int y = r.y;
400
                        int width = r.width;
401
                        int height= r.height;
402
                        g.setBackground(null);
403

    
404
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.2), (y+height*0.8)), null);
405
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.634), (y+height*0.3)), null);
406
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.26), (y+height*0.35)), null);
407
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.45), (y+height*0.98)), null);
408
                        markerSymbol.draw(g, null, new FPoint2D((x+width*0.9), (y+height*0.54)), null);
409
                        markerSymbol.draw(g, null, new FPoint2D((x+width*1.1), (y+height*0.7)), null);
410
                        g.setClip(null);
411
                        break;
412
                }
413
                if(getOutline()!= null && hasOutline()){
414
                        if (properties==null)
415
                                getOutline().draw(g, scaleInstance, new FPolyline2D(new GeneralPathX(r)), null);
416
                        else
417
                                getOutline().print(g, scaleInstance, new FPolyline2D(new GeneralPathX(r)), properties);
418
                }
419
        }
420

    
421

    
422
        public String getClassName() {
423
                return getClass().getName();
424
        }
425

    
426
        public void setXMLEntity(XMLEntity xml) {
427
                setDescription(xml.getStringProperty("desc"));
428
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
429

    
430
                markerSymbol = (AbstractMarkerSymbol) SymbologyFactory.
431
                                                        createSymbolFromXML(xml.getChild(0), null);
432
                markerFillProperties = (SimpleMarkerFillPropertiesStyle) SymbologyFactory.
433
                                                        createStyleFromXML(xml.getChild(1), null);
434

    
435
                if (xml.contains("unit")) { // remove this line when done
436
                // measure unit (for outline)
437
                setUnit(xml.getIntProperty("unit"));
438

    
439
                // reference system (for outline)
440
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
441
                }
442

    
443
                if(xml.contains("hasOutline")) {
444
                        XMLEntity outlineXML = xml.firstChild("id", "outline symbol");
445
                        if (outlineXML != null) {
446
                                setOutline((ILineSymbol) SymbologyFactory.createSymbolFromXML(outlineXML, "outline"));
447
                        }
448
                }
449
        }
450

    
451
        public void setMarker(IMarkerSymbol marker) {
452
                this.markerSymbol = marker;
453
        }
454

    
455
        public IMarkerSymbol getMarker() {
456
                return markerSymbol;
457
        }
458

    
459
        public Color getFillColor(){
460
                return markerSymbol.getColor();
461
        }
462

    
463
        public void setFillColor (Color color) {
464
                markerSymbol.setColor(color);
465
        }
466

    
467
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties) {
468
                this.properties=properties;
469
        draw(g, at, shape, null);
470
        this.properties=null;
471

    
472
        }
473
        /**
474
         * Sets the markerfillproperties to be used by the class
475
         *
476
         * @param markerFillStyle,IMarkerFillPropertiesStyle
477
         */
478
        public void setMarkerFillProperties(IMarkerFillPropertiesStyle markerFillStyle) {
479
                this.markerFillProperties = markerFillStyle;
480
        }
481

    
482
        /**
483
         * Returns the markerfillproperties that are used by the class
484
         *
485
         * @return markerFillProperties,IMarkerFillPropertiesStyle
486
         */
487
        public IMarkerFillPropertiesStyle getMarkerFillProperties() {
488
                return markerFillProperties;
489
        }
490

    
491

    
492
        @Override
493
        public void setUnit(int unitIndex) {
494
                super.setUnit(unitIndex);
495
                if (getMarker()!=null) {
496
                        getMarker().setUnit(unitIndex);
497
                }
498
        }
499

    
500
        @Override
501
        public void setReferenceSystem(int system) {
502
                super.setReferenceSystem(system);
503
                if (getMarker()!=null) {
504
                        getMarker().setReferenceSystem(system);
505
                }
506
        }
507

    
508
        public void setCartographicSize(double cartographicSize, FShape shp) {
509
                super.setCartographicSize(cartographicSize, shp);
510
                if (getMarker()!=null) {
511
                        getMarker().setCartographicSize(cartographicSize, shp);
512
                }
513
        }
514

    
515
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
516
                double s = super.toCartographicSize(viewPort, dpi, shp);
517
                if (getMarker()!=null) {
518
                        s = getMarker().toCartographicSize(viewPort, dpi, shp);
519
                }
520
                return s;
521
        }
522

    
523
}