Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src-test / org / gvsig / fmap / data / feature / file / shp / SHPTest.java @ 22958

History | View | Annotate | Download (4.37 KB)

1
package org.gvsig.fmap.data.feature.file.shp;
2

    
3
import java.io.File;
4
import java.util.Iterator;
5

    
6
import junit.framework.TestCase;
7

    
8
import org.gvsig.fmap.data.CloseException;
9
import org.gvsig.fmap.data.DataManager;
10
import org.gvsig.fmap.data.InitializeException;
11
import org.gvsig.fmap.data.ReadException;
12
import org.gvsig.fmap.data.feature.Feature;
13
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
14
import org.gvsig.fmap.data.feature.FeatureCollection;
15
import org.gvsig.fmap.data.feature.FeatureStore;
16
import org.gvsig.fmap.data.feature.FeatureType;
17
import org.gvsig.fmap.data.feature.file.DataStoreTest;
18

    
19
public class SHPTest extends TestCase {
20

    
21
        public static final File fileNormal = new File(DataStoreTest.class
22
                        .getResource(
23
                        "data/prueba.shp").getFile());
24
        public static final File fileGeometryNull = new File(DataStoreTest.class
25
                        .getResource("data/pruebaNull.shp").getFile());
26

    
27

    
28
        public static void main(String[] args) {
29
                junit.textui.TestRunner.run(SHPTest.class);
30
        }
31

    
32
        protected void setUp() throws Exception {
33
                super.setUp();
34
        }
35

    
36
        public void testInitialize() {
37
                Register.selfRegister();
38
                shp(fileNormal);
39
                shp(fileGeometryNull);
40
        }
41

    
42
        public static SHPStoreParameters getParams(File file)
43
                        throws InitializeException {
44
                DataManager manager = DataManager.getManager();
45
                SHPStoreParameters shpParameters = null;
46
                shpParameters = (SHPStoreParameters) manager
47
                                .createDataStoreParameters(SHPStore.DATASTORE_NAME);
48
                shpParameters.setFile(file);
49
                return shpParameters;
50
        }
51

    
52
        private void shp(File file){
53
                DataManager manager = DataManager.getManager();
54
                FeatureStore store = null;
55
                SHPStoreParameters shpParameters=null;
56
                try {
57
                        shpParameters = getParams(file);
58
                } catch (InitializeException e3) {
59
                        e3.printStackTrace();
60
                        fail();
61
                }
62

    
63
                try {
64
                        store= (FeatureStore)manager.createDataStore(shpParameters);
65
                } catch (InitializeException e) {
66
                        // TODO Auto-generated catch block
67
                        e.printStackTrace();
68
                }
69

    
70
                FeatureType ft=store.getDefaultFeatureType();
71
                FeatureCollection featureCollection=null;
72
                try {
73
                        featureCollection = (FeatureCollection)store.getDataCollection(ft,null,null);
74
                } catch (ReadException e1) {
75
                        // TODO Auto-generated catch block
76
                        e1.printStackTrace();
77
                }
78
                Iterator iter = featureCollection.iterator();
79
                while (iter.hasNext()) {
80
                        Feature feature = (Feature)iter.next();
81
                        System.out.println("SHP Feature ------------------- ");
82
                        Iterator iterator=ft.iterator();
83
                        while (iterator.hasNext()) {
84
                                FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next();
85
                                int i=descriptor.ordinal();
86
                                String type=descriptor.getDataType();
87
                                if (type.equals(FeatureAttributeDescriptor.TYPE_BOOLEAN)){
88
                                        System.out.println("Boolean ----- "+ feature.getBoolean(i));
89
                                }else if (type.equals(FeatureAttributeDescriptor.TYPE_BYTE)){
90
                                        System.out.println("Byte ----- "+ feature.getByte(i));
91
                                }else if (type.equals(FeatureAttributeDescriptor.TYPE_DATE)){
92
                                        System.out.println("Date ----- "+ feature.getDate(i));
93
                                }else if (type.equals(FeatureAttributeDescriptor.TYPE_DOUBLE)){
94
                                        System.out.println("Double ----- "+ feature.getDouble(i));
95
                                }else if (type.equals(FeatureAttributeDescriptor.TYPE_FLOAT)){
96
                                        System.out.println("Float ----- "+ feature.getFloat(i));
97
                                }else if (type.equals(FeatureAttributeDescriptor.TYPE_INT)){
98
                                        System.out.println("Integer ----- "+ feature.getInt(i));
99
                                }else if (type.equals(FeatureAttributeDescriptor.TYPE_LONG)){
100
                                        System.out.println("Long ----- "+ feature.getLong(i));
101
                                }else if (type.equals(FeatureAttributeDescriptor.TYPE_STRING)){
102
                                        System.out.println("String ----- "+ feature.getString(i));
103
                                }else if (type.equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)){
104

    
105
                                        System.out.println("Geometry ----- "+ feature.getGeometry(i));
106
                                }
107
                        }
108
                }
109
                iter=null;
110
                featureCollection.dispose();
111

    
112
                try {
113
                        store.dispose();
114
                } catch (CloseException e) {
115
                        // TODO Auto-generated catch block
116
                        e.printStackTrace();
117
                }
118

    
119
        }
120

    
121
        public void test_Resources(){
122

    
123
                DataManager manager = DataManager.getManager();
124
                Register.selfRegister();
125

    
126
                SHPStoreParameters shpParameters=null;
127

    
128
                try {
129
                        shpParameters = getParams(fileNormal);
130
                } catch (InitializeException e) {
131
                        e.printStackTrace();
132
                        fail();
133
                }
134

    
135

    
136
                DataStoreTest.doFileResourceTest(shpParameters);
137
        }
138

    
139
}