Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src-file / org / gvsig / data / datastores / vectorial / file / shp_util / NewSHPFeature.java @ 21606

History | View | Annotate | Download (4.47 KB)

1
package org.gvsig.data.datastores.vectorial.file.shp_util;
2

    
3
import java.text.DateFormat;
4
import java.text.ParseException;
5
import java.util.Date;
6
import java.util.List;
7
import java.util.Locale;
8

    
9
import org.apache.log4j.Logger;
10
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile;
11
import org.gvsig.data.exception.ReadException;
12
import org.gvsig.data.spatialprovisional.IExtent;
13
import org.gvsig.data.vectorial.Feature;
14
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
15
import org.gvsig.data.vectorial.IFeatureID;
16
import org.gvsig.data.vectorial.IFeatureType;
17

    
18
import com.hardcode.gdbms.engine.data.DataSource;
19
import com.hardcode.gdbms.engine.data.driver.DriverException;
20

    
21
import es.prodevelop.gvsig.mobile.fmap.core.IGeometry;
22
import es.prodevelop.gvsig.mobile.fmap.driver.vect.dbf.DbfMemoryDataSource;
23

    
24
public class NewSHPFeature extends Feature {
25

    
26
        private static Logger logger = Logger.getLogger(NewSHPFeature.class);
27
        
28
        protected static Locale ukLocale = new Locale("en", "UK");
29
        private int featureIndex = 0;
30
        
31
        public NewSHPFeature(IFeatureType ftype, int feat_ind) {
32
                super(ftype);
33
                featureIndex = feat_ind;
34
        }
35
        
36
        public IFeatureID getID() {
37
                return null;
38
        }
39

    
40
        public IExtent getExtent() {
41
                // TODO Auto-generated method stub
42
                return null;
43
        }
44

    
45
        public List getAllSRS() {
46
                // TODO Auto-generated method stub
47
                return null;
48
        }
49

    
50
        public String getDefaultSRS() {
51
                // TODO Auto-generated method stub
52
                return null;
53
        }
54
        
55
        
56
        
57
        public void load(DataSource dbf_ds,IGeometry geometry) throws ReadException {
58
                
59
                for (int i=0;i<featureType.size();i++) {
60
                        
61
                        IFeatureAttributeDescriptor descriptor= (IFeatureAttributeDescriptor)featureType.get(i);
62
                        String fieldType=descriptor.getDataType();
63
                        if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){
64
                                this.setGeometry(i,geometry);
65
                                continue;
66
                        }
67
                        try {
68
                                if (dbf_ds.getFieldCount()<i) return ;
69
                        } catch (DriverException e1) {
70
                                throw new ReadException(dbf_ds.getName(), e1.getMessage());
71
                        }
72
                        
73
                        String desc_fname = descriptor.getName();
74
                        int ind  = 0;
75
                        try {
76
                                ind = dbf_ds.getFieldIndexByName(desc_fname);
77
                        } catch (DriverException e1) {
78
                                throw new ReadException(dbf_ds.getName(), e1.getMessage());
79
                        }
80
                        
81
                        if (ind == -1) {
82
                                logger.error("Attribute not found: " + desc_fname);
83
                                return;
84
                        }
85
                        String value;
86
                        try {
87
                                value = dbf_ds.getFieldValue((int) featureIndex, ind).toString();
88
                        } catch (DriverException e1) {
89
                                throw new ReadException(dbf_ds.getName(), e1.getMessage());
90
                        }
91
                        
92
                        value=value.trim();
93

    
94
                        if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_STRING)){
95
                                this.set(i,value);
96
                        }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){
97
                                try{
98
                                        Double.parseDouble(value);
99
                                }catch (NumberFormatException e) {
100
                                        value="0";
101
                                }
102
                                this.set(i,Double.parseDouble(value));
103
                        }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_INT)){
104
                                this.set(i,Integer.parseInt(value));
105
                        }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){
106
                                this.set(i,Float.parseFloat(value));
107
                        }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_LONG)){
108
                                this.set(i,Long.parseLong(value));
109
                        }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)){
110
                                this.set(i,Boolean.valueOf(value));
111
                        }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){
112
                                this.set(i,Byte.parseByte(value));
113
                        }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_DATE)){
114
                                String year = value.substring(0, 4);
115
                                String month = value.substring(4, 6);
116
                                String day = value.substring(6, 8);
117
                                DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, ukLocale);
118
                                /* Calendar c = Calendar.getInstance();
119
                            c.clear();
120
                            c.set(Integer.parseInt(year), Integer.parseInt(month),
121
                                Integer.parseInt(day));
122
                            c.set(Calendar.MILLISECOND, 0); */
123
                                String strAux = month + "/" + day + "/" + year;
124
                                Date dat=null;
125
                                try {
126
                                        dat = df.parse(strAux);
127
                                } catch (ParseException e) {
128
                                        throw new ReadException(dbf_ds.getName(), e);
129
                                }
130
                                this.set(i,dat);
131
                        }else{
132
                                this.set(i,null);
133
                        }
134
                }
135
        }
136
                
137
                                
138
        public void load(IGeometry geometry)
139
                        throws ReadException {
140

    
141
                for (int i = 0; i < featureType.size(); i++) {
142
                        IFeatureAttributeDescriptor descriptor = (IFeatureAttributeDescriptor) featureType
143
                                        .get(i);
144
                        String fieldType = descriptor.getDataType();
145
                        if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)) {
146
                                setGeometry(i, geometry);
147
                        }
148
                }
149

    
150
        }
151
        
152

    
153
}