svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / shp_util / NewSHPDataCollection.java @ 21927
History | View | Annotate | Download (5.11 KB)
1 |
package org.gvsig.data.datastores.vectorial.file.shp_util; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.util.Collection; |
5 |
import java.util.Comparator; |
6 |
import java.util.Iterator; |
7 |
import java.util.TreeSet; |
8 |
|
9 |
import org.apache.log4j.Logger; |
10 |
import org.gvsig.data.DataCollection; |
11 |
import org.gvsig.data.ReadException; |
12 |
import org.gvsig.data.datastores.vectorial.file.shp_mem.simplify.MemoryShpDriver; |
13 |
import org.gvsig.data.vectorial.Feature; |
14 |
import org.gvsig.data.vectorial.FeatureType; |
15 |
import org.gvsig.data.vectorial.expressionevaluator.FeatureComparator; |
16 |
import org.gvsig.fmap.geom.Geometry; |
17 |
import org.gvsig.util.observer.Observable; |
18 |
import org.opengis.filter.Filter; |
19 |
import org.opengis.filter.sort.SortBy; |
20 |
|
21 |
public class NewSHPDataCollection implements DataCollection { |
22 |
|
23 |
private static Logger logger = Logger.getLogger(NewSHPDataCollection.class); |
24 |
private MemoryShpDriver shp_drv = null; |
25 |
private DBFDataSource dbf_ds = null; |
26 |
private FeatureType featureType = null; |
27 |
private boolean onlyGeo = false; |
28 |
private IGeometricDataStore store;
|
29 |
|
30 |
private SortBy[] order; |
31 |
protected Collection featureIDs; |
32 |
|
33 |
|
34 |
private Filter filter; |
35 |
|
36 |
public NewSHPDataCollection(IGeometricDataStore _store, MemoryShpDriver drv, DBFDataSource dbfds,
|
37 |
FeatureType ft, boolean only_geo, Filter _filter, |
38 |
SortBy[] _order) {
|
39 |
|
40 |
shp_drv = drv; |
41 |
dbf_ds = dbfds; |
42 |
featureType = ft; |
43 |
onlyGeo = only_geo; |
44 |
|
45 |
order = _order; |
46 |
filter = _filter; |
47 |
store = _store; |
48 |
|
49 |
if (order != null) { |
50 |
initOrderIDs(); |
51 |
} |
52 |
} |
53 |
|
54 |
private void initOrderIDs() { |
55 |
|
56 |
Comparator comparator = new FeatureComparator(featureType, order); |
57 |
featureIDs = new TreeSet(comparator); |
58 |
|
59 |
Iterator iter = unsortedIterator();
|
60 |
while (iter.hasNext()) {
|
61 |
Feature feature = (Feature) iter.next(); |
62 |
Object obj = feature.getID();
|
63 |
featureIDs.add(obj); |
64 |
} |
65 |
} |
66 |
|
67 |
public void dispose() { |
68 |
// TODO Auto-generated method stub
|
69 |
|
70 |
} |
71 |
|
72 |
public int size() { |
73 |
int count = 0; |
74 |
Iterator iter = new SHPIterator(); |
75 |
while (iter.hasNext()) {
|
76 |
count++; |
77 |
iter.next(); |
78 |
} |
79 |
return count;
|
80 |
} |
81 |
|
82 |
public boolean isEmpty() { |
83 |
return (size() == 0); |
84 |
} |
85 |
|
86 |
public boolean contains(Object arg0) { |
87 |
return false; |
88 |
} |
89 |
|
90 |
private Iterator unsortedIterator() { |
91 |
return new SHPIterator(); |
92 |
} |
93 |
|
94 |
public Iterator iterator() { |
95 |
|
96 |
if (order == null) { |
97 |
return new SHPIterator(); |
98 |
} else {
|
99 |
return new SortedSHPIterator(); |
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
public Object[] toArray() { |
105 |
// TODO Auto-generated method stub
|
106 |
return null; |
107 |
} |
108 |
|
109 |
public Object[] toArray(Object[] arg0) { |
110 |
// TODO Auto-generated method stub
|
111 |
return null; |
112 |
} |
113 |
|
114 |
public boolean add(Object arg0) { |
115 |
// TODO Auto-generated method stub
|
116 |
return false; |
117 |
} |
118 |
|
119 |
public boolean remove(Object arg0) { |
120 |
// TODO Auto-generated method stub
|
121 |
return false; |
122 |
} |
123 |
|
124 |
public boolean containsAll(Collection arg0) { |
125 |
// TODO Auto-generated method stub
|
126 |
return false; |
127 |
} |
128 |
|
129 |
public boolean addAll(Collection arg0) { |
130 |
// TODO Auto-generated method stub
|
131 |
return false; |
132 |
} |
133 |
|
134 |
public boolean removeAll(Collection arg0) { |
135 |
// TODO Auto-generated method stub
|
136 |
return false; |
137 |
} |
138 |
|
139 |
public boolean retainAll(Collection arg0) { |
140 |
// TODO Auto-generated method stub
|
141 |
return false; |
142 |
} |
143 |
|
144 |
public void clear() { |
145 |
// TODO Auto-generated method stub
|
146 |
|
147 |
} |
148 |
|
149 |
public void update(Observable observable, Object notification) { |
150 |
// TODO Auto-generated method stub
|
151 |
|
152 |
} |
153 |
|
154 |
private class SHPIterator implements Iterator { |
155 |
|
156 |
private int index = 0; |
157 |
private int max_size = 0; |
158 |
private NewSHPFeature nextFeat = null; |
159 |
|
160 |
public SHPIterator() {
|
161 |
max_size = shp_drv.getShapeCount(); |
162 |
} |
163 |
|
164 |
public boolean hasNext() { |
165 |
|
166 |
while (index < max_size) {
|
167 |
|
168 |
Geometry ig = null;
|
169 |
|
170 |
try {
|
171 |
ig = shp_drv.getShape(index); |
172 |
} catch (IOException e) { |
173 |
logger.debug("While getting geometry: " + e.getMessage());
|
174 |
} |
175 |
|
176 |
NewSHPFeature item = null;
|
177 |
|
178 |
try {
|
179 |
item = new NewSHPFeature(featureType, index, store);
|
180 |
if (onlyGeo) {
|
181 |
item.load(ig); |
182 |
} else {
|
183 |
item.load(dbf_ds, ig); |
184 |
} |
185 |
} catch (ReadException e) {
|
186 |
logger.error("While loading feature: " + e.getMessage());
|
187 |
} |
188 |
|
189 |
index++; |
190 |
|
191 |
if (filter == null) { |
192 |
nextFeat = item; |
193 |
return true; |
194 |
} else {
|
195 |
if (filter.evaluate(item)) {
|
196 |
nextFeat = item; |
197 |
return true; |
198 |
} |
199 |
} |
200 |
} |
201 |
return false; |
202 |
} |
203 |
|
204 |
public Object next() { |
205 |
return nextFeat;
|
206 |
} |
207 |
|
208 |
public void remove() { |
209 |
} |
210 |
|
211 |
} |
212 |
|
213 |
|
214 |
|
215 |
|
216 |
private class SortedSHPIterator implements Iterator { |
217 |
|
218 |
private Iterator idIterator; |
219 |
|
220 |
public SortedSHPIterator() {
|
221 |
idIterator = featureIDs.iterator(); |
222 |
} |
223 |
|
224 |
public boolean hasNext() { |
225 |
return idIterator.hasNext();
|
226 |
} |
227 |
|
228 |
public Object next() { |
229 |
|
230 |
NewShpFeatureID item = (NewShpFeatureID) idIterator.next(); |
231 |
int ind = item.getIndex();
|
232 |
NewSHPFeature resp = null;
|
233 |
|
234 |
try {
|
235 |
Geometry ig = null;
|
236 |
|
237 |
try {
|
238 |
ig = shp_drv.getShape(ind); |
239 |
} catch (IOException e) { |
240 |
logger.debug("While getting geometry: " + e.getMessage());
|
241 |
} |
242 |
|
243 |
resp = new NewSHPFeature(featureType, ind, store);
|
244 |
if (onlyGeo) {
|
245 |
resp.load(ig); |
246 |
} else {
|
247 |
resp.load(dbf_ds, ig); |
248 |
} |
249 |
|
250 |
} catch (ReadException e) {
|
251 |
logger.error("While loading feature: " + e.getMessage());
|
252 |
} |
253 |
|
254 |
return resp;
|
255 |
} |
256 |
|
257 |
public void remove() { |
258 |
} |
259 |
|
260 |
} |
261 |
|
262 |
} |