Statistics
| Revision:

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

History | View | Annotate | Download (20.8 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;
42

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

    
54
import javax.imageio.ImageIO;
55

    
56
import junit.framework.TestCase;
57

    
58
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
59
import com.iver.cit.gvsig.fmap.AllTests;
60
import com.iver.cit.gvsig.fmap.MapContext;
61
import com.iver.cit.gvsig.fmap.ViewPort;
62
import com.iver.cit.gvsig.fmap.core.CartographicSupport.CartographicSupportToolkit;
63
import com.iver.cit.gvsig.fmap.core.symbols.ILineSymbol;
64
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
65
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
66
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
67
import com.iver.cit.gvsig.fmap.core.symbols.TestISymbol;
68
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
69
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
70
import com.iver.utiles.IPersistance;
71
import com.iver.utiles.NotExistInXMLEntity;
72
import com.iver.utiles.XMLEntity;
73
import com.iver.utiles.swing.threads.Cancellable;
74

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

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

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

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

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

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

    
115
        );
116
    }
117

    
118
    public static final Rectangle2D TEST_EXTENT = new Rectangle2D.Double(
119
            4191037.369934333,
120
            626674.7454557443,
121

    
122
            4519266.460824658 - 4191037.369934333,
123
            797903.2656794232 -  626674.7454557443
124
        );
125

    
126

    
127

    
128

    
129
    private CartographicSupport[] symbols;
130
    private long[] scales = new long[] {
131

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

    
141

    
142

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

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

    
156
        this.symbols = (CartographicSupport[]) symbolsToTest
157
            .toArray(new CartographicSupport[symbolsToTest.size()]);
158

    
159
    }
160

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

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

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

    
189

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

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

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

    
225
    public void testCartographySupportForMarkerSymbols() {
226
        MapContext mc = AllTests.newMapContext(AllTests.TEST_DEFAULT_PROJECTION);
227
        ViewPort viewPort = mc.getViewPort();
228
        viewPort.setMapUnits(8);
229
        double dist1Pixel = viewPort.getDist1pixel();
230
        viewPort.setImageSize(new Dimension(400, 400));
231
        viewPort.setExtent(new Rectangle2D.Double(-30, -30, 60, 60));
232
        FPoint2D p = new FPoint2D(viewPort.getAdjustedExtent().getCenterX(), viewPort.getAdjustedExtent().getCenterY());
233
        final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(p);
234
        int i = -1;
235
        for (int k = 0; k < symbols.length; k++) {
236
            if (symbols[k] instanceof IMarkerSymbol) {
237

    
238

    
239
                IMarkerSymbol sym = (IMarkerSymbol) symbols[k];
240
                sym.setSize(3*111317.09969444444444444444444444);
241
                sym.setColor(Color.RED);
242
                sym.setUnit(1);
243

    
244
                String name = sym.getClassName().substring(
245
                      sym.getClassName().lastIndexOf('.')+1,
246
                      sym.getClassName().length());
247

    
248
                GraphicLayer gl = mc.getGraphicsLayer();
249
                FGraphic graphicVLC = new FGraphic(dummyPointGeom, gl.addSymbol(sym));
250
                gl.addGraphic(graphicVLC);
251
                BufferedImage bi = new BufferedImage(viewPort.getImageWidth(), viewPort.getImageHeight(), BufferedImage.TYPE_INT_RGB);
252
                    try {
253
                        mc.drawGraphics(bi, bi.createGraphics(), new Cancellable() {
254
                            public boolean isCanceled() { return false;        }
255
                            public void setCanceled(boolean canceled) {}
256
                        }, viewPort.getScale());
257
                        ImageIO.write(bi, "png",
258
                              new File("E:/prova-imatges/"+
259
                                      name+"_"/*+MapContext.NAMES[i]+"_1_"+scales[j]*/+".png"));
260
                    } catch (ReadDriverException e) {
261
                        System.err.println("The test for "+name+" symbol couldn't be finished.\n" +
262
                                "This does not necessarily means that the test fails, but\n" +
263
                        "a check should be made");
264
                        e.printStackTrace();
265
                    } catch (IOException e) {
266
                        e.printStackTrace();
267
                        fail();
268
                    }
269

    
270
            }
271
        }
272
    }
273

    
274
//    public void testCartographySupportForMarkerSymbols() {
275
//        final IGeometry dummyPointGeom = ShapeFactory.createPoint2D(new FPoint2D(726344, 4372688)); // Valencia in EPSG:23030
276
//        GeneralPathX testLine = new GeneralPathX();
277
//        testLine.moveTo(726344, 4372688); // Valencia in EPSG:23030
278
//        testLine.lineTo(719633, 4247336); // Alacant in EPSG:23030
279
//        final IGeometry testEPSG23030LineGeom = ShapeFactory.createPolyline2D(testLine);
280
//
281
//        for (int i = 0; i < MapContext.CHANGEM.length; i++) {
282
//            for (int j = 0; j < scales.length; j++) {
283
//                for (int k = 0; k < symbols.length; k++) {
284
//                    if (symbols[k] instanceof IMarkerSymbol) {
285
//                        IMarkerSymbol sym = (IMarkerSymbol) symbols[k];
286
//                        sym.setColor(Color.RED);
287
//                        sym.setUnit(i);
288
//                        double length = 100;
289
//                        sym.setSize(length);
290
//
291
//                        // setup MapContext and ViewPort
292
//                        MapContext mc = AllTests.newMapContext(AllTests.TEST_DEFAULT_MERCATOR_PROJECTION);
293
//                        ViewPort viewPort = mc.getViewPort();
294
//
295
//                        // A thin extent from Valencia to Alacant (EPSG:23030)
296
//                        Rectangle2D extent = new Rectangle2D.Double(
297
//                                696852.4848577793,
298
//                                4233679.785765858,
299
//                                47071.474971979726,
300
//                                155028.87952728104);
301
//
302
//                        viewPort.setExtent(extent);
303
//
304
//
305
//                        sym.setCartographicScaleX(CartographicSupportToolkit.toScreenUnitXAxis(sym, 1, viewPort));
306
//                        sym.setCartographicScaleY(CartographicSupportToolkit.toScreenUnitYAxis(sym, 1, viewPort));
307
//
308
//
309
//                        // A thin dimension more or less like the extent above
310
//                        viewPort.setImageSize(new Dimension(300, 1000));
311
//
312
//
313
//                        // con este extent y tama?o
314
//
315
//                        // will draw the symbol in the mapcontext's graphic layer
316
//                        GraphicLayer gl = mc.getGraphicsLayer();
317
//
318
//                        // add the symbol to the graphic layer
319
//
320
//
321
//                        // create a graphic
322
//                        FGraphic graphicVLC = new FGraphic(dummyPointGeom, gl.addSymbol(sym));
323
//                        gl.addGraphic(graphicVLC);
324
//
325
//                        SimpleLineSymbol sym2 = new SimpleLineSymbol();
326
//                        FGraphic graphicLine = new FGraphic(testEPSG23030LineGeom, gl.addSymbol(sym2));
327
//                        gl.addGraphic(graphicLine);
328
//
329
//                        // new black buffered image
330
//                        BufferedImage bi = new BufferedImage(viewPort.getImageWidth(), viewPort.getImageHeight(), BufferedImage.TYPE_INT_RGB);
331
//
332
//                        // the graphics for the image, so we can draw onto the buffered image
333
//                        Graphics2D g = bi.createGraphics();
334
//                        String name = sym.getClassName().substring(
335
//                                sym.getClassName().lastIndexOf('.')+1,
336
//                                sym.getClassName().length());
337
//
338
//
339
//                        try {
340
//
341
//                                                        mc.drawGraphics(bi, g, new Cancellable() {
342
//                                                                public boolean isCanceled() { return false;        }
343
//                                                                public void setCanceled(boolean canceled) {}
344
//                                                        }, scales[j]);
345
//
346
//                            g.setColor(Color.YELLOW);
347
//
348
//
349
////                            g.drawLine( 10, bi.getHeight() - 10, (int) (length / viewPort.getDist1pixel()*MapContext.CHANGEM[i])+10, bi.getHeight() - 10);
350
////                            g.drawLine( 10,  bi.getHeight() - 15, 10, bi.getHeight() - 5);
351
////                            g.drawLine( (int) (length / viewPort.getDist1pixel()*MapContext.CHANGEM[i])+10,  bi.getHeight() - 15,  (int) (length / viewPort.getDist1pixel()*MapContext.CHANGEM[i])+10, bi.getHeight() - 5);
352
//
353
//                            int length100Units = (int) ((length*MapContext.CHANGEM[i])/ viewPort.getDist1pixel());
354
//                            g.drawLine( 10, bi.getHeight() - 10, 10, length100Units+10);
355
//                            g.drawLine( 5,  bi.getHeight() - 10, 15, bi.getHeight() - 10);
356
//                            g.drawLine( 5,  length100Units+10, 15, length100Units+10);
357
//
358
//
359
//                            g.setFont(new Font("Arial", Font.PLAIN, 20));
360
//                            g.drawString(length+" "+MapContext.NAMES[i], 15,  bi.getHeight() - 40);
361
//                            g.drawString("1:"+scales[j], 15,  bi.getHeight() - 20);
362
//
363
//                            ImageIO.write(bi, "png",
364
//                                    new File("E:/prova-imatges/"+
365
//                                            name+"_"+MapContext.NAMES[i]+"_1_"+scales[j]
366
//                                                                                      +".png"));
367
//
368
//                                                } catch (ReadDriverException e) {
369
//                                                        System.err.println("The test for "+name+" symbol couldn't be finished.\n" +
370
//                                                                        "This does not necessarily means that the test fails, but\n" +
371
//                                                        "a check should be made");
372
//                                                        e.printStackTrace();
373
//                        } catch (IOException e) {
374
//                            e.printStackTrace();
375
//                            fail();
376
//                        }
377
//                    }
378
//                }
379
//            }
380
//
381
//        }
382
//    }
383
//
384
//    public void _testCartographySupportForLineSymbols() {
385
//        GeneralPathX testLine = new GeneralPathX();
386
//        testLine.moveTo(726344, 4372688); // Valencia in EPSG:23030
387
//        testLine.lineTo(719633, 4247336); // Alacant in EPSG:23030
388
//        final IGeometry testEPSG23030LineGeom = ShapeFactory.createPolyline2D(testLine);
389
//
390
//        // all units will be tested
391
//        for (int i = 0; i < MapContext.CHANGEM.length; i++) {
392
//
393
//            // several scales defined for the test
394
//            for (int j = 0; j < scales.length; j++) {
395
//
396
//                // line CartographySupport symbols will
397
//                for (int k = 0; k < symbols.length; k++) {
398
//                    if (symbols[k] instanceof ILineSymbol) {
399
//                        ILineSymbol sym = (ILineSymbol) symbols[k];
400
//                        sym.setLineColor(Color.RED);
401
//
402
//                        // setup MapContext and ViewPort
403
//                        MapContext mc = AllTests.newMapContext(AllTests.TEST_DEFAULT_MERCATOR_PROJECTION);
404
//                        ViewPort viewPort = mc.getViewPort();
405
//
406
//                        // A thin extent from Valencia to Alacant (EPSG:23030)
407
//                        Rectangle2D extent = new Rectangle2D.Double(
408
//                                696852.4848577793,
409
//                                4233679.785765858,
410
//                                47071.474971979726,
411
//                                155028.87952728104);
412
//
413
//                        viewPort.setExtent(extent);
414
//
415
//                        // A thin dimension more or less like the extent above
416
//                        viewPort.setImageSize(new Dimension(90, 300));
417
//
418
//                        // con este extent y tama?o
419
//
420
//                        // will draw the symbol in the mapcontext's graphic layer
421
//                        GraphicLayer gl = mc.getGraphicsLayer();
422
//
423
//                        // add the symbol to the graphic layer
424
//                        int idSymbol = gl.addSymbol(sym);
425
//
426
//                        // create a graphic
427
//                        FGraphic graphicLine = new FGraphic(testEPSG23030LineGeom, idSymbol);
428
//                        gl.addGraphic(graphicLine);
429
//
430
//                        // new black buffered image
431
//                        BufferedImage bi = new BufferedImage(viewPort.getImageWidth(), viewPort.getImageHeight(), BufferedImage.TYPE_INT_RGB);
432
//
433
//                        // the graphics for the image, so we can draw onto the buffered image
434
//                        Graphics2D g = bi.createGraphics();
435
//                        String name = sym.getClassName().substring(
436
//                                sym.getClassName().lastIndexOf('.')+1,
437
//                                sym.getClassName().length());
438
//
439
//
440
//                        try {
441
//
442
//                            mc.drawGraphics(bi, g, new Cancellable() {
443
//                                public boolean isCanceled() { return false;        }
444
//                                public void setCanceled(boolean canceled) {}
445
//                            }, scales[j]);
446
//
447
//
448
//                            ImageIO.write(bi, "png",
449
//                                    new File("E:/prova-imatges/"+
450
//                                            name+"_"+MapContext.NAMES[i]+"_1_"+scales[j]
451
//                                            +".png"));
452
//                        } catch (ReadDriverException e) {
453
//                            System.err.println("The test for "+name+" symbol couldn't be finished.\n" +
454
//                                    "This does not necessarily means that the test fails, but\n" +
455
//                                    "a check should be made");
456
//                            e.printStackTrace();
457
//                        } catch (IOException e) {
458
//                            e.printStackTrace();
459
//                            fail();
460
//                        }
461
//                    }
462
//                }
463
//            }
464
//        }
465
//    }
466

    
467
}