Statistics
| Revision:

root / trunk / extensions / extSymbology / src / org / gvsig / symbology / Utils.java @ 22501

History | View | Annotate | Download (9.59 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
package org.gvsig.symbology;
42

    
43
import java.awt.Color;
44
import java.io.File;
45
import java.io.IOException;
46

    
47
import org.gvsig.symbology.fmap.symbols.LineFillSymbol;
48
import org.gvsig.symbology.fmap.symbols.PictureMarkerSymbol;
49

    
50
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
51
import com.iver.cit.gvsig.fmap.core.FShape;
52
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
53
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle;
54
import com.iver.cit.gvsig.fmap.core.symbols.AbstractSymbol;
55
import com.iver.cit.gvsig.fmap.core.symbols.IFillSymbol;
56
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
57
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
58
import com.iver.cit.gvsig.fmap.core.symbols.IMultiLayerSymbol;
59
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
60
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
61
import com.iver.cit.gvsig.fmap.core.symbols.MultiShapeSymbol;
62
import com.iver.cit.gvsig.fmap.core.symbols.SimpleMarkerSymbol;
63
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
64
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
65

    
66
public class Utils {
67
        public static ISymbol deriveFSymbol(FSymbol fSymbol) {
68
                ISymbol derivedSymbol;
69
                
70
                int symbolType = fSymbol.getSymbolType();
71
                Color color = fSymbol.getColor();
72
                double size = fSymbol.getSize();
73
                int unit = fSymbol.isSizeInPixels() ? -1 : 1; // only meters or pixels
74
                                                                                                                // were supported in
75
                                                                                                                // FSymbol
76
                if (symbolType == FShape.LINE) {
77
                        ILineSymbol line = SymbologyFactory.createDefaultLineSymbol();
78
                        line.setLineColor(color);
79

    
80
                        SimpleLineStyle lineStyle = new SimpleLineStyle();
81
                        lineStyle.setUnit(unit); 
82
                        lineStyle.setStroke(fSymbol.getStroke());
83
                        lineStyle.setOffset(0);
84
                        lineStyle.setReferenceSystem(CartographicSupport.WORLD);
85
                        lineStyle.setLineWidth((float) size);
86
                        line.setLineStyle(lineStyle);
87
                        derivedSymbol = line;
88
                        
89
                } else if (symbolType == FShape.POINT) {
90
                        int style = fSymbol.getStyle();
91
                        IMarkerSymbol marker; 
92
                        if (style == FConstant.SYMBOL_STYLE_MARKER_IMAGEN) {
93
                                marker = new PictureMarkerSymbol();
94
                                PictureMarkerSymbol pic = (PictureMarkerSymbol) marker;
95
                                try {
96
                                        pic.setImage(new File(fSymbol.getIconURI().getPath()).toURL());
97
                                } catch (IOException e) {
98
                                        // image could not be restored,
99
                                        // will use a regular point as symbol
100
                                        fSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_CIRCLE);
101
                                        return deriveFSymbol(fSymbol);
102
                                }
103
                        } else {
104
                                marker = new SimpleMarkerSymbol();
105
                                SimpleMarkerSymbol sms = (SimpleMarkerSymbol) marker;
106
                                if (style == FConstant.SYMBOL_STYLE_MARKER_CIRCLE) {
107
                                        sms.setStyle(SimpleMarkerSymbol.CIRCLE_STYLE);
108
                                } else if (style == FConstant.SYMBOL_STYLE_MARKER_CROSS) {
109
                                        sms.setStyle(SimpleMarkerSymbol.CROSS_STYLE);
110
                                } else if (style == FConstant.SYMBOL_STYLE_MARKER_SQUARE) {
111
                                        sms.setStyle(SimpleMarkerSymbol.SQUARE_STYLE);
112
                                } else if (style == FConstant.SYMBOL_STYLE_MARKER_TRIANGLE) {
113
                                        sms.setStyle(SimpleMarkerSymbol.TRIANGLE_STYLE);
114
                                }
115
                                Color outlineColor = fSymbol.getOutlineColor();
116
                                if (outlineColor != null) {
117
                                        sms.setOutlined(true);
118
                                        sms.setOutlineColor(outlineColor);
119
                                }
120
                        }
121
                        marker.setColor(color);
122
                        marker.setSize(size);
123
                        marker.setRotation(fSymbol.getRotation());
124
                        derivedSymbol = marker;
125
                        
126
                } else if (symbolType == FShape.POLYGON) {
127
                        IFillSymbol fill;
128
                        int fSymbolStyle = fSymbol.getStyle();
129
                        color = null;
130
                        if (fSymbolStyle == FConstant.SYMBOL_STYLE_FILL_SOLID) {
131
                                fill = SymbologyFactory.createDefaultFillSymbol();
132
                                color = fSymbol.getColor();
133
                        } else if (fSymbolStyle == FConstant.SYMBOL_STYLE_FILL_TRANSPARENT ||
134
                                           fSymbolStyle == FConstant.SYMBOL_STYLE_DGNSPECIAL) {
135
                                fill = SymbologyFactory.createDefaultFillSymbol();
136
                        } else {
137
                                // lets see how to derive FSymbol with fill patterns
138
                                if (fSymbolStyle == FConstant.SYMBOL_STYLE_FILL_CROSS) {
139
                                        // the cross will be substituted by two line fill symbols
140
                                        // with
141
                                        // perpendicular line angles mixed into a multilayer symbol
142
                                        IMultiLayerSymbol mfs = SymbologyFactory.createEmptyMultiLayerSymbol(FShape.POLYGON);
143
                                        fSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_VERTICAL);
144
                                        LineFillSymbol firstLayer = (LineFillSymbol) deriveFSymbol(fSymbol);
145
                                        fSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_HORIZONTAL);
146
                                        LineFillSymbol secondLayer = (LineFillSymbol) deriveFSymbol(fSymbol);
147
                                        mfs.addLayer(firstLayer);
148
                                        mfs.addLayer(secondLayer);
149
                                        fill = (IFillSymbol) mfs;
150
                                        fSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_CROSS); // restore
151
                                                                                                                                                        // old
152
                                                                                                                                                        // style
153
                                                                                                                                                        // (just
154
                                                                                                                                                        // in
155
                                                                                                                                                        // case)
156
                                } else if (fSymbolStyle == FConstant.SYMBOL_STYLE_FILL_CROSS_DIAGONAL ) {
157
                                        // the cross will be substituted by two line fill symbols
158
                                        // with
159
                                        // perpendicular line angles mixed into a multilayer symbol
160
                                        IMultiLayerSymbol mfs = SymbologyFactory.createEmptyMultiLayerSymbol(FShape.POLYGON);
161
                                        fSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_UPWARD_DIAGONAL);
162
                                        LineFillSymbol firstLayer = (LineFillSymbol) deriveFSymbol(fSymbol);
163
                                        fSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_DOWNWARD_DIAGONAL);
164
                                        LineFillSymbol secondLayer = (LineFillSymbol) deriveFSymbol(fSymbol);
165
                                        mfs.addLayer(firstLayer);
166
                                        mfs.addLayer(secondLayer);
167
                                        fill = (IFillSymbol) mfs;
168
                                        fSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_CROSS_DIAGONAL); // restore
169
                                                                                                                                                        // old
170
                                                                                                                                                        // style
171
                                                                                                                                                        // (just
172
                                                                                                                                                        // in
173
                                                                                                                                                        // case)
174
                                } else {
175
                                        LineFillSymbol lfs = new LineFillSymbol();
176
                                        // Let's create the filling line symbol
177
                                        fSymbol.setSymbolType(FShape.LINE);
178
                                        ILineSymbol lineSymbol = (ILineSymbol) deriveFSymbol(fSymbol);
179
                                        SimpleLineStyle lineStyle = new SimpleLineStyle();
180
                                        lineStyle.setLineWidth(1f);
181
                                        lineSymbol.setLineStyle(lineStyle);
182
                                        
183
                                        // restore the old value for symbol type (should be always
184
                                        // FShape.POLYGON)
185
                                        assert symbolType == FShape.POLYGON;
186
                                        fSymbol.setSymbolType(symbolType);
187
                                        double angle = 0;
188
                                        switch (fSymbolStyle) {
189
                                        case FConstant.SYMBOL_STYLE_FILL_UPWARD_DIAGONAL:
190
                                                angle = 45;
191
                                                break;
192
                                        case FConstant.SYMBOL_STYLE_FILL_VERTICAL:
193
                                                angle = 90;
194
                                                break;
195
                                        case FConstant.SYMBOL_STYLE_FILL_HORIZONTAL:
196
                                                angle = 0;
197
                                                break;
198
                                        case FConstant.SYMBOL_STYLE_FILL_DOWNWARD_DIAGONAL:
199
                                                angle = -45;
200
                                                break;
201
                                        }
202
                                        lfs.setSeparation(10);
203
                                        lfs.setAngle(angle*FConstant.DEGREE_TO_RADIANS);
204
                                        lfs.setLineSymbol(lineSymbol);
205
                                        fill = lfs;
206
                                }
207
                        }
208
                        
209
                        fill.setFillColor(color);
210

    
211
                        if (fSymbol.isOutlined()) {
212
                                // Let's create the outline line symbol
213
                                fSymbol.setSymbolType(FShape.LINE);
214
                                ILineSymbol outline = (ILineSymbol) deriveFSymbol(fSymbol);
215
                                
216
                                // restore the old value for symbol type (should be always
217
                                // FShape.POLYGON)
218
                                assert symbolType == FShape.POLYGON;
219
                                fSymbol.setSymbolType(symbolType);
220
                                outline.setLineColor(fSymbol.getOutlineColor());
221
                                fill.setOutline(outline);
222
                        }
223
                        derivedSymbol = fill;
224
                } else if (symbolType == FShape.MULTI) {
225
                        fSymbol.setSymbolType(FShape.LINE);
226
                        ILineSymbol line = (ILineSymbol) deriveFSymbol(fSymbol);
227
                        fSymbol.setSymbolType(FShape.POINT);
228
                        IMarkerSymbol marker = (IMarkerSymbol) deriveFSymbol(fSymbol);
229
                        fSymbol.setSymbolType(FShape.POLYGON);
230
                        IFillSymbol fill = (IFillSymbol) deriveFSymbol(fSymbol);
231
                        assert symbolType == FShape.MULTI;
232
                        fSymbol.setSymbolType(symbolType);
233
                        MultiShapeSymbol multiShapeSymbol = new MultiShapeSymbol();
234
                        multiShapeSymbol.setMarkerSymbol(marker);
235
                        multiShapeSymbol.setLineSymbol(line);
236
                        multiShapeSymbol.setFillSymbol(fill);
237
                        
238
                        derivedSymbol = multiShapeSymbol;
239
                } else if (symbolType == FShape.TEXT) {
240
                        ITextSymbol textSym = SymbologyFactory.createDefaultTextSymbol();
241
                        textSym.setTextColor(color);
242
                        derivedSymbol = textSym;
243
                } else {
244
                        throw new Error("FSymbol of type "+symbolType+" cannot be imported yet!");
245
                }
246
                
247
                // establish the general description;
248
                if (derivedSymbol instanceof AbstractSymbol) {
249
                        AbstractSymbol symbol = (AbstractSymbol) derivedSymbol;
250
                        symbol.setIsShapeVisible(true);
251
                        symbol.setDescription(fSymbol.getDescription());
252
                }
253
                
254
                
255
                
256
                if (derivedSymbol instanceof CartographicSupport) {
257
                        CartographicSupport cs = (CartographicSupport) derivedSymbol;
258
                        // the only options allowed by the old FSymbol class
259
                        cs.setUnit(unit);
260
                        cs.setReferenceSystem(CartographicSupport.WORLD);
261
                }
262
                
263
                return derivedSymbol;
264
        }
265
}