gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / DALIFeatureIterator.java @ 245
History | View | Annotate | Download (5.53 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.geoprocess.lib.sextante.dataObjects; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
import java.util.NoSuchElementException; |
30 |
|
31 |
import com.vividsolutions.jts.geom.Geometry; |
32 |
|
33 |
import es.unex.sextante.core.Sextante; |
34 |
import es.unex.sextante.dataObjects.FeatureImpl; |
35 |
import es.unex.sextante.dataObjects.IFeature; |
36 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
37 |
import es.unex.sextante.dataObjects.vectorFilters.IVectorLayerFilter; |
38 |
|
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.feature.Feature; |
41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
43 |
import org.gvsig.fmap.dal.feature.FeatureType; |
44 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
45 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
46 |
import org.gvsig.tools.dispose.DisposableIterator; |
47 |
|
48 |
public class DALIFeatureIterator implements IFeatureIterator { |
49 |
|
50 |
private final List<IVectorLayerFilter> m_Filters; |
51 |
private boolean m_bIsNextConsumed; |
52 |
private IFeature m_CurrentValue;
|
53 |
private boolean m_bFinished; |
54 |
private DisposableIterator m_FeatureIterator;
|
55 |
|
56 |
public DALIFeatureIterator(final FLyrVect layer, |
57 |
final List<IVectorLayerFilter> filters) { |
58 |
|
59 |
m_bIsNextConsumed = true;
|
60 |
m_Filters = filters; |
61 |
|
62 |
FeatureSet set = null;
|
63 |
try {
|
64 |
set = layer.getFeatureStore().getFeatureSet(); |
65 |
m_FeatureIterator = set.fastIterator(); |
66 |
} catch (final DataException e) { |
67 |
if (m_FeatureIterator != null) { |
68 |
m_FeatureIterator.dispose(); |
69 |
} |
70 |
if (set != null) { |
71 |
set.dispose(); |
72 |
} |
73 |
} |
74 |
} |
75 |
|
76 |
public DALIFeatureIterator() {
|
77 |
|
78 |
m_Filters = null;
|
79 |
|
80 |
} |
81 |
|
82 |
private boolean accept(final IFeature value) { |
83 |
|
84 |
for (final IVectorLayerFilter filter : m_Filters) { |
85 |
if (!filter.accept(value, 0)) { |
86 |
return false; |
87 |
} |
88 |
} |
89 |
return true; |
90 |
} |
91 |
|
92 |
public boolean moveToNextValid() { |
93 |
|
94 |
if (m_FeatureIterator == null) { |
95 |
return false; |
96 |
} |
97 |
|
98 |
boolean bFound = false; |
99 |
; |
100 |
while (!bFound && m_FeatureIterator.hasNext()) {
|
101 |
try {
|
102 |
final Feature f = (Feature) m_FeatureIterator.next();
|
103 |
final org.gvsig.fmap.geom.Geometry geometry =
|
104 |
f.getDefaultGeometry(); |
105 |
final Geometry geom =
|
106 |
(Geometry) geometry.invokeOperation(ToJTS.CODE, null);
|
107 |
final FeatureType type = f.getType();
|
108 |
@SuppressWarnings("unchecked") |
109 |
final Iterator<FeatureAttributeDescriptor> iter = |
110 |
type.iterator(); |
111 |
final List<Object> objects = new ArrayList<Object>(); |
112 |
while (iter.hasNext()) {
|
113 |
final FeatureAttributeDescriptor descriptor = iter.next();
|
114 |
final String name = descriptor.getName(); |
115 |
// Comment this as if the geometry field is in the middle of
|
116 |
// the fields it will be problematic elsewhere
|
117 |
if (!type.getDefaultGeometryAttributeName().equals(name)) {
|
118 |
objects.add(f.get(name)); |
119 |
} |
120 |
} |
121 |
final FeatureImpl feature =
|
122 |
new FeatureImpl(geom, objects.toArray(new Object[objects |
123 |
.size()])); |
124 |
if (accept(feature)) {
|
125 |
bFound = true;
|
126 |
m_CurrentValue = feature; |
127 |
m_bIsNextConsumed = false;
|
128 |
} |
129 |
} catch (final Exception e) { |
130 |
Sextante.addErrorToLog(e); |
131 |
return false; |
132 |
} |
133 |
} |
134 |
if (!bFound) {
|
135 |
m_bFinished = true;
|
136 |
} |
137 |
return bFound;
|
138 |
} |
139 |
|
140 |
public IFeature next() {
|
141 |
|
142 |
if (!m_bIsNextConsumed) {
|
143 |
m_bIsNextConsumed = true;
|
144 |
return m_CurrentValue;
|
145 |
} |
146 |
|
147 |
if (!m_bFinished) {
|
148 |
if (moveToNextValid()) {
|
149 |
m_bIsNextConsumed = true;
|
150 |
return m_CurrentValue;
|
151 |
} |
152 |
} |
153 |
|
154 |
throw new NoSuchElementException(); |
155 |
} |
156 |
|
157 |
public boolean hasNext() { |
158 |
|
159 |
if (m_bFinished) {
|
160 |
return false; |
161 |
} |
162 |
|
163 |
if (!m_bIsNextConsumed) {
|
164 |
return true; |
165 |
} |
166 |
|
167 |
return moveToNextValid();
|
168 |
|
169 |
} |
170 |
|
171 |
public void close() { |
172 |
} |
173 |
|
174 |
} |