svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / shp_mem / MemSHPStore.java @ 21865
History | View | Annotate | Download (7.02 KB)
1 |
package org.gvsig.data.datastores.vectorial.file.shp_mem; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.sql.Types; |
6 |
import java.util.List; |
7 |
|
8 |
import org.apache.log4j.Logger; |
9 |
import org.gvsig.data.CloseException; |
10 |
import org.gvsig.data.DataCollection; |
11 |
import org.gvsig.data.DataExplorer; |
12 |
import org.gvsig.data.DataStoreParameters; |
13 |
import org.gvsig.data.InitializeException; |
14 |
import org.gvsig.data.OpenException; |
15 |
import org.gvsig.data.ReadException; |
16 |
import org.gvsig.data.WriteException; |
17 |
import org.gvsig.data.datastores.vectorial.file.Utils; |
18 |
import org.gvsig.data.datastores.vectorial.file.shp.SHPStoreParameters; |
19 |
import org.gvsig.data.datastores.vectorial.file.shp_mem.simplify.DbfMemoryDataSource; |
20 |
import org.gvsig.data.datastores.vectorial.file.shp_mem.simplify.MemoryShpDriver; |
21 |
import org.gvsig.data.datastores.vectorial.file.shp_util.DBFDataSource; |
22 |
import org.gvsig.data.datastores.vectorial.file.shp_util.IGeometricDataStore; |
23 |
import org.gvsig.data.datastores.vectorial.file.shp_util.NewSHPDataCollection; |
24 |
import org.gvsig.data.datastores.vectorial.file.shp_util.NewSHPFeature; |
25 |
import org.gvsig.data.datastores.vectorial.file.shp_util.NewShpFeatureID; |
26 |
import org.gvsig.data.spatialprovisionalold.Extent; |
27 |
import org.gvsig.data.spatialprovisionalold.IExtent; |
28 |
import org.gvsig.data.vectorial.AbstractFeatureStore; |
29 |
import org.gvsig.data.vectorial.AttributeDescriptor; |
30 |
import org.gvsig.data.vectorial.DefaultAttributeDescriptor; |
31 |
import org.gvsig.data.vectorial.DefaultFeatureType; |
32 |
import org.gvsig.data.vectorial.Feature; |
33 |
import org.gvsig.data.vectorial.FeatureAttributeDescriptor; |
34 |
import org.gvsig.data.vectorial.FeatureID; |
35 |
import org.gvsig.data.vectorial.FeatureType; |
36 |
import org.gvsig.data.vectorial.IsNotAttributeSettingException; |
37 |
import org.gvsig.data.vectorial.NewFeatureStoreParameters; |
38 |
import org.gvsig.exceptions.BaseException; |
39 |
import org.gvsig.exceptions.DriverException; |
40 |
import org.gvsig.fmap.geom.Geometry; |
41 |
import org.gvsig.metadata.IMetadata; |
42 |
import org.opengis.filter.Filter; |
43 |
import org.opengis.filter.sort.SortBy; |
44 |
|
45 |
public class MemSHPStore extends AbstractFeatureStore implements |
46 |
IGeometricDataStore { |
47 |
|
48 |
private static Logger logger = Logger.getLogger(MemSHPStore.class); |
49 |
|
50 |
public static final String DATASTORE_NAME = "MemSHPStore"; |
51 |
|
52 |
static {
|
53 |
MemSHPRegister.selfRegister(); |
54 |
} |
55 |
|
56 |
protected IMetadata metadata;
|
57 |
|
58 |
private MemoryShpDriver shp_drv = null; |
59 |
|
60 |
private DbfMemoryDataSource dbf_drv = null; |
61 |
|
62 |
private File shp_file = null; |
63 |
|
64 |
protected FeatureType featureType = null; |
65 |
|
66 |
private IExtent extent = null; |
67 |
|
68 |
private boolean onlyGeometry = false; |
69 |
|
70 |
public void init(DataStoreParameters p) throws InitializeException { |
71 |
super.init(p);
|
72 |
shp_drv = new MemoryShpDriver();
|
73 |
shp_file = ((SHPStoreParameters) p).getSHPFile(); |
74 |
File dbff = ((SHPStoreParameters) p).getDBFFile();
|
75 |
dbf_drv = new DbfMemoryDataSource(dbff);
|
76 |
featureType = new DefaultFeatureType();
|
77 |
|
78 |
AttributeDescriptor dad = (AttributeDescriptor) featureType.createNewAttributeDescriptor(); |
79 |
dad.setOrdinal(featureType.size()); |
80 |
dad.loading(); |
81 |
|
82 |
try {
|
83 |
dad.setName("GEOMETRY");
|
84 |
dad.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY); |
85 |
} catch (IsNotAttributeSettingException e) {
|
86 |
logger.error("While setting att descriptor: " + e.getMessage());
|
87 |
} |
88 |
|
89 |
dad.stopLoading(); |
90 |
featureType.add(dad); |
91 |
featureType.setDefaultGeometry("GEOMETRY");
|
92 |
} |
93 |
|
94 |
protected void doFinishEdition() throws WriteException, ReadException { |
95 |
} |
96 |
|
97 |
public DataCollection getDataCollection(FeatureType type, Filter filter, |
98 |
SortBy[] order) throws ReadException { |
99 |
|
100 |
return new NewSHPDataCollection(this, shp_drv, dbf_drv, featureType, |
101 |
onlyGeometry, filter, order); |
102 |
} |
103 |
|
104 |
public Feature getFeatureByID(FeatureID id, FeatureType ft) throws ReadException { |
105 |
NewShpFeatureID newid = (NewShpFeatureID) id; |
106 |
|
107 |
int ind = newid.getIndex();
|
108 |
return getFeatureByPosition(ft, ind);
|
109 |
} |
110 |
|
111 |
private Feature getFeatureByPosition(FeatureType ft, int ind) { |
112 |
|
113 |
Geometry ig = null;
|
114 |
|
115 |
try {
|
116 |
ig = shp_drv.getShape(ind); |
117 |
} catch (IOException e) { |
118 |
logger.debug("While getting geometry: " + e.getMessage());
|
119 |
} |
120 |
|
121 |
NewSHPFeature item = null;
|
122 |
|
123 |
try {
|
124 |
item = new NewSHPFeature(ft, ind, this); |
125 |
item.load(dbf_drv, ig); |
126 |
} catch (ReadException e) {
|
127 |
logger.error("While loading feature: " + e.getMessage());
|
128 |
} |
129 |
return item;
|
130 |
} |
131 |
|
132 |
public List getFeatureTypes() { |
133 |
return null; |
134 |
} |
135 |
|
136 |
public FeatureType getDefaultFeatureType() {
|
137 |
return featureType;
|
138 |
} |
139 |
|
140 |
public boolean isWithDefaultLegend() { |
141 |
return false; |
142 |
} |
143 |
|
144 |
public Object getDefaultLegend() { |
145 |
return null; |
146 |
} |
147 |
|
148 |
public Object getDefaultLabelingStrategy() { |
149 |
return null; |
150 |
} |
151 |
|
152 |
public boolean canAlterFeatureType() { |
153 |
return false; |
154 |
} |
155 |
|
156 |
public String getName() { |
157 |
return DATASTORE_NAME;
|
158 |
} |
159 |
|
160 |
public void doOpen() throws OpenException { |
161 |
try {
|
162 |
shp_drv.open(shp_file); |
163 |
shp_drv.initialize(); |
164 |
extent = new Extent(shp_drv.getFullExtent());
|
165 |
dbf_drv.start(); |
166 |
for (int i = 0; i < dbf_drv.getFieldCount(); i++) { |
167 |
featureType.add(createFeatureAttribute(dbf_drv, i, (DefaultFeatureType) featureType)); |
168 |
} |
169 |
|
170 |
} catch (Exception e) { |
171 |
throw new OpenException(e.getMessage(), DATASTORE_NAME); |
172 |
} |
173 |
} |
174 |
|
175 |
private FeatureAttributeDescriptor createFeatureAttribute(
|
176 |
DBFDataSource dbf, int i, DefaultFeatureType ft_type) throws Exception { |
177 |
|
178 |
AttributeDescriptor dad = (AttributeDescriptor) ft_type.createNewAttributeDescriptor(); |
179 |
|
180 |
dad.loading(); |
181 |
|
182 |
int fieldType = dbf.getFieldType(i);
|
183 |
// DefaultAttributeDescriptor dad = new DefaultAttributeDescriptor();
|
184 |
dad.setOrdinal(i); |
185 |
dad.setName(dbf.getFieldName(i)); |
186 |
dad.setSize(dbf.getFieldWidth(i)); |
187 |
|
188 |
switch (fieldType) {
|
189 |
case Types.DOUBLE: |
190 |
|
191 |
if (dbf instanceof DbfMemoryDataSource) { |
192 |
|
193 |
DbfMemoryDataSource new_name = (DbfMemoryDataSource) dbf; |
194 |
int prec = new_name.getFieldDecimalPrecision(i);
|
195 |
if (prec > 0) { |
196 |
dad.setType(FeatureAttributeDescriptor.TYPE_DOUBLE); |
197 |
dad.setPrecision(prec); |
198 |
} else {
|
199 |
dad.setType(FeatureAttributeDescriptor.TYPE_INT); |
200 |
} |
201 |
} else {
|
202 |
dad.setType(FeatureAttributeDescriptor.TYPE_INT); |
203 |
} |
204 |
break;
|
205 |
|
206 |
case Types.VARCHAR: |
207 |
dad.setType(FeatureAttributeDescriptor.TYPE_STRING); |
208 |
break;
|
209 |
case Types.DATE: |
210 |
dad.setType(FeatureAttributeDescriptor.TYPE_DATE); |
211 |
break;
|
212 |
case Types.BOOLEAN: |
213 |
dad.setType(FeatureAttributeDescriptor.TYPE_BOOLEAN); |
214 |
break;
|
215 |
default:
|
216 |
dad.setType(FeatureAttributeDescriptor.TYPE_STRING); |
217 |
} |
218 |
|
219 |
dad.stopLoading(); |
220 |
return dad;
|
221 |
} |
222 |
|
223 |
public void doClose() throws CloseException { |
224 |
|
225 |
try {
|
226 |
shp_drv.close(); |
227 |
dbf_drv.stop(); |
228 |
} catch (Exception e) { |
229 |
throw new CloseException(e.getMessage(), DATASTORE_NAME); |
230 |
} |
231 |
} |
232 |
|
233 |
public void doDispose() { |
234 |
shp_drv = null;
|
235 |
} |
236 |
|
237 |
public boolean isEditable() { |
238 |
return false; |
239 |
} |
240 |
|
241 |
public IMetadata getMetadata() throws BaseException { |
242 |
return metadata;
|
243 |
} |
244 |
|
245 |
public IExtent getFullExtent() {
|
246 |
return extent;
|
247 |
} |
248 |
|
249 |
public void setOnlyGeometry(boolean b) { |
250 |
onlyGeometry = b; |
251 |
} |
252 |
|
253 |
public DataExplorer getExplorer() throws ReadException { |
254 |
// TODO Auto-generated method stub
|
255 |
return null; |
256 |
} |
257 |
|
258 |
@Override
|
259 |
protected void doRefresh() throws OpenException, InitializeException { |
260 |
// TODO Auto-generated method stub
|
261 |
|
262 |
} |
263 |
|
264 |
} |