Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src-test / com / iver / cit / gvsig / fmap / core / symbols / TestCartographySupport.java @ 12043

History | View | Annotate | Download (11.1 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 com.iver.cit.gvsig.fmap.core.symbols;
42

    
43
import java.awt.Color;
44
import java.awt.Dimension;
45
import java.awt.Graphics2D;
46
import java.awt.geom.Rectangle2D;
47
import java.awt.image.BufferedImage;
48
import java.io.File;
49
import java.io.IOException;
50
import java.util.ArrayList;
51

    
52
import javax.imageio.ImageIO;
53

    
54
import junit.framework.TestCase;
55

    
56
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
57
import com.iver.cit.gvsig.fmap.AllTests;
58
import com.iver.cit.gvsig.fmap.MapContext;
59
import com.iver.cit.gvsig.fmap.ViewPort;
60
import com.iver.cit.gvsig.fmap.core.CartographicSupport;
61
import com.iver.cit.gvsig.fmap.core.FPoint2D;
62
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
65
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
66
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
67
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
68
import com.iver.utiles.IPersistance;
69
import com.iver.utiles.NotExistInXMLEntity;
70
import com.iver.utiles.XMLEntity;
71
import com.iver.utiles.swing.threads.Cancellable;
72

    
73
/**
74
 * @author jaume dominguez faus - jaume.dominguez@iver.es
75
 */
76
public class TestCartographySupport extends TestCase {
77
        /**
78
         * Geodesic vertex of the Micalet in Valencia (Spain) in <b>EPSG:23030</b>
79
         */
80
        public static final FPoint2D valenciaUTM30 = new FPoint2D(725846.080, 4373022.720);
81

    
82
        /**
83
         * Geodesic vertex of the Micalet in Valencia (Spain) in <b>EPSG:4326</b>
84
         */
85
        public static final FPoint2D valenciaGeo = new FPoint2D(- (0+ 22/60 +  27.919/3600), 39 + 28/60 + 35.4276/3600);
86
        /**
87
         * Extent that covers the whole Comunitat Valenciana in  <b>EPSG:23030</b>
88
         */
89
        public static final Rectangle2D TEST_UTM30_EXTENT = new Rectangle2D.Double();
90
        {
91
                TEST_UTM30_EXTENT.setFrameFromDiagonal(
92
                        4191037.369934333,
93
                        626674.7454557443,
94

    
95
                        4519266.460824658,
96
                        797903.2656794232
97
                );
98
        }
99
        /**
100
         * Extent that covers the whole Comunitat Valenciana in  <b>EPSG:4326</b>
101
         */
102

    
103
        public static final Rectangle2D TEST_GEO_EXTENT = new Rectangle2D.Double();
104
        {
105
                TEST_GEO_EXTENT.setFrameFromDiagonal(
106

    
107
                        -  (1 + (31/60) + (28.09/3600)),        // Western egde
108
                           37 + (50/60) + (48.05/3600),                // Southern edge
109

    
110
                            0 + (31/60) + (1.85/3600),                // Eastern edge
111
                           40 + (47/60) + (21.36/3600)                // Northern edge
112

    
113
                );
114
        }
115

    
116
        public static final Rectangle2D TEST_EXTENT = new Rectangle2D.Double(
117
                        4191037.369934333,
118
                        626674.7454557443,
119

    
120
                        4519266.460824658 - 4191037.369934333,
121
                        797903.2656794232 -  626674.7454557443
122
                );
123

    
124

    
125

    
126

    
127
        private CartographicSupport[] symbols;
128
        private long[] scales = new long[] {
129

    
130
                100000,        // 1:100.000
131
                50000,  //  1:50.000
132
                25000,  //  1:25.000
133
                10000,        //         1:10.000
134
                1000,        //   1:1.000
135
                100,        //           1:100
136
                10,                //                   10
137
        };
138

    
139

    
140

    
141
        public TestCartographySupport() {
142
                // take the symbols added to the TestISymbol test
143
                ISymbol[] symbols = TestISymbol.getNewSymbolInstances();
144

    
145
                // take only those implementing CartographySupport interface
146
                // for this test
147
                ArrayList symbolsToTest = new ArrayList();
148
                for (int i = 0; i < symbols.length; i++) {
149
                        if (symbols[i] instanceof CartographicSupport) {
150
                                symbolsToTest.add((CartographicSupport) symbols[i]);
151
                        }
152
                }
153

    
154
                this.symbols = (CartographicSupport[]) symbolsToTest
155
                        .toArray(new CartographicSupport[symbolsToTest.size()]);
156

    
157
        }
158

    
159
        public void testCartographicSupportInSymbolUnitDefinition() {
160
                for (int i = 0; i < symbols.length; i++) {
161
                        XMLEntity xml = ((IPersistance) symbols[i]).getXMLEntity();
162
                        String name = symbols[i].getClass().getName().substring(
163
                                        symbols[i].getClass().getName().lastIndexOf('.')+1,
164
                                        symbols[i].getClass().getName().length());
165

    
166
                        try {
167
                                assertTrue( ((ISymbol) symbols[i]).getClassName()+" does not declare units correctly",
168
                                                symbols[i].getUnit() == xml.getIntProperty("unit"));
169
                        } catch (NotExistInXMLEntity neiXMLEx) {
170
                                fail(((ISymbol) symbols[i]).getClassName()+ " does not declare field attribute in its XMLEntity");
171
                        }
172
                        symbols[i].setUnit(5);
173
                        try {
174
                                assertTrue( ((ISymbol) symbols[i]).getClassName()+" does not apply changes to symbol",symbols[i].getUnit()== 5);
175
                                assertTrue( ((ISymbol) symbols[i]).getClassName()+" does not declare units correctly",
176
                                                5 == ((IPersistance) symbols[i]).getXMLEntity().getIntProperty("unit"));
177
                        } catch (NotExistInXMLEntity neiXMLEx) {
178
                                fail(((ISymbol) symbols[i]).getClassName()+ " does not declare field attribute in its XMLEntity");
179
                        }
180

    
181
                        xml.putProperty("unit", 3);
182
                        ISymbol ts = SymbologyFactory.createSymbolFromXML(xml, xml.getStringProperty("desc"));
183
                        assertTrue("The application of the UNIT value to the XMLEntity doesn't had any effect ("+name+")", ((CartographicSupport) ts).getUnit() == 3);
184
                }
185
        }
186

    
187

    
188
        public void testCartographicSupportInSymbolReferenceSystemDefinition() {
189
                for (int i = 0; i < symbols.length; i++) {
190
                        XMLEntity xml = ((IPersistance) symbols[i]).getXMLEntity();
191
                        String name = symbols[i].getClass().getName().substring(
192
                                        symbols[i].getClass().getName().lastIndexOf('.')+1,
193
                                        symbols[i].getClass().getName().length());
194
                        try {
195
                        assertTrue( ((ISymbol) symbols[i]).getClassName()+" does not declare units correctly",
196
                                        symbols[i].getReferenceSystem() == xml.getIntProperty("referenceSystem"));
197
                        } catch (NotExistInXMLEntity neiXMLEx) {
198
                                fail(((ISymbol) symbols[i]).getClassName()+ " does not declare referenceSystem field attribute in its XMLEntity");
199
                        }
200
                        symbols[i].setReferenceSystem(CartographicSupport.PAPER);
201
                        try {
202
                                assertTrue( ((ISymbol) symbols[i]).getClassName()+" does not apply changes to symbol",symbols[i].getReferenceSystem()== CartographicSupport.PAPER);
203
                                assertTrue( ((ISymbol) symbols[i]).getClassName()+" does not declare referenceSystem correctly",
204
                                                CartographicSupport.PAPER == ((IPersistance) symbols[i]).getXMLEntity().getIntProperty("referenceSystem"));
205
                        } catch (NotExistInXMLEntity neiXMLEx) {
206
                                fail(((ISymbol) symbols[i]).getClassName()+ " does not declare referenceSystem field attribute in its XMLEntity ("+name+")");
207
                        }
208

    
209
                        symbols[i].setReferenceSystem(CartographicSupport.WORLD);
210
                        try {
211
                                assertTrue( ((ISymbol) symbols[i]).getClassName()+" does not apply changes to symbol",symbols[i].getReferenceSystem()== CartographicSupport.WORLD);
212
                                assertTrue( ((ISymbol) symbols[i]).getClassName()+" does not declare referenceSystem correctly",
213
                                                CartographicSupport.WORLD == ((IPersistance) symbols[i]).getXMLEntity().getIntProperty("referenceSystem"));
214
                        } catch (NotExistInXMLEntity neiXMLEx) {
215
                                fail(((ISymbol) symbols[i]).getClassName()+ " does not declare referenceSystem field attribute in its XMLEntity");
216
                        }
217

    
218
                        xml.putProperty("referenceSystem", CartographicSupport.PAPER);
219
                        assertTrue("The application of the REFERENCE SYSTEM value to the XMLEntity doesn't had any effect ("+name+")", symbols[i].getReferenceSystem() == CartographicSupport.PAPER);
220
                }
221
        }
222

    
223
//        public void testCartographySupportForMarkerSymbols() {
224
//                final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D());
225
//
226
//                MapContext mc = AllTests.newMapContext(AllTests.TEST_PROJECTION_DEFAULT);
227
//                for (int i = 0; i < MapContext.CHANGEM.length; i++) {
228
//                        for (int j = 0; j < scales.length; j++) {
229
//                                for (int k = 0; k < symbols.length; k++) {
230
//                                        if (symbols[k] instanceof IMarkerSymbol) {
231
//                                                IMarkerSymbol sym = (IMarkerSymbol) symbols[k];
232
//                                        }
233
//
234
//                                }
235
//                        }
236
//                }
237
//        }
238

    
239
        public void testCartographySupportForLineSymbols() {
240
                GeneralPathX testLine = new GeneralPathX();
241
                testLine.moveTo(726344, 4372688); // Valencia in EPSG:23030
242
                testLine.lineTo(719633, 4247336); // Alacant in EPSG:23030
243
                final IGeometry testEPSG23030LineGeom = ShapeFactory.createPolyline2D(testLine);
244

    
245
                // all units will be tested
246
                for (int i = 0; i < MapContext.CHANGEM.length; i++) {
247

    
248
                        // several scales defined for the test
249
                        for (int j = 0; j < scales.length; j++) {
250

    
251
                                // line CartographySupport symbols will
252
                                for (int k = 0; k < symbols.length; k++) {
253
                                        if (symbols[k] instanceof ILineSymbol) {
254
                                                ILineSymbol sym = (ILineSymbol) symbols[k];
255
                                                sym.setLineColor(Color.RED);
256

    
257
                                                // setup MapContext and ViewPort
258
                                                MapContext mc = AllTests.newMapContext(AllTests.TEST_DEFAULT_MERCATOR_PROJECTION);
259
                                                ViewPort viewPort = mc.getViewPort();
260

    
261
                                                // A thin extent from Valencia to Alacant (EPSG:23030)
262
                                                Rectangle2D extent = new Rectangle2D.Double(
263
                                                                696852.4848577793,
264
                                                                4233679.785765858,
265
                                                                47071.474971979726,
266
                                                                155028.87952728104);
267

    
268
                                                viewPort.setExtent(extent);
269

    
270
                                                // A thin dimension more or less like the extent above
271
                                                viewPort.setImageSize(new Dimension(90, 300));
272

    
273
                                                // con este extent y tama?o
274

    
275
                                                // will draw the symbol in the mapcontext's graphic layer
276
                                                GraphicLayer gl = mc.getGraphicsLayer();
277

    
278
                                                // add the symbol to the graphic layer
279
                                                int idSymbol = gl.addSymbol(sym);
280

    
281
                                                // create a graphic
282
                                                FGraphic graphicLine = new FGraphic(testEPSG23030LineGeom, idSymbol);
283
                                                gl.addGraphic(graphicLine);
284

    
285
                                                // new black buffered image
286
                                                BufferedImage bi = new BufferedImage(90, 300, BufferedImage.TYPE_INT_RGB);
287

    
288
                                                // the graphics for the image, so we can draw onto the buffered image
289
                                                Graphics2D g = bi.createGraphics();
290
                                                String name = sym.getClassName().substring(
291
                                                                sym.getClassName().lastIndexOf('.')+1,
292
                                                                sym.getClassName().length());
293

    
294

    
295
                                                try {
296

    
297
                                                        mc.drawGraphics(bi, g, new Cancellable() {
298
                                                                public boolean isCanceled() { return false;        }
299
                                                                public void setCanceled(boolean canceled) {}
300
                                                        }, scales[j]);
301

    
302

    
303
                                                        ImageIO.write(bi, "png",
304
                                                                        new File("E:/prova-imatges/"+
305
                                                                                        name+"_"+MapContext.NAMES[i]+"_1_"+scales[j]
306
                                                                                        +".png"));
307
                                                } catch (ReadDriverException e) {
308
                                                        System.err.println("The test for "+name+" symbol couldn't be finished.\n" +
309
                                                                        "This does not necessarily means that the test fails, but\n" +
310
                                                                        "a check should be made");
311
                                                        e.printStackTrace();
312
                                                } catch (IOException e) {
313
                                                        e.printStackTrace();
314
                                                        fail();
315
                                                }
316
                                        }
317
                                }
318
                        }
319
                }
320
        }
321

    
322
}