root / org.gvsig.dgn / trunk / org.gvsig.dgn / org.gvsig.dgn.provider / src / main / java / org / gvsig / fmap / dal / store / dgn / PostProcessFilterOperation.java @ 332
History | View | Annotate | Download (12 KB)
1 |
package org.gvsig.fmap.dal.store.dgn; |
---|---|
2 |
|
3 |
import java.util.regex.Pattern; |
4 |
import org.apache.commons.lang3.StringUtils; |
5 |
import org.gvsig.fmap.dal.feature.FeatureType; |
6 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
7 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_CONTAINS; |
8 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_COVEREDBY; |
9 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_COVERS; |
10 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_CROSSES; |
11 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_DISJOINT; |
12 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_INTERSECT; |
13 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_NONE; |
14 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_OVERLAPS; |
15 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_TOUCHES; |
16 |
import static org.gvsig.fmap.dal.store.dgn.DGNStoreProvider.CROP_OPERATION_WITHIN; |
17 |
import org.gvsig.fmap.geom.Geometry; |
18 |
import org.gvsig.fmap.geom.GeometryLocator; |
19 |
import org.gvsig.fmap.geom.GeometryManager; |
20 |
import org.gvsig.fmap.geom.primitive.Point; |
21 |
import org.gvsig.fmap.geom.type.GeometryType; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
|
25 |
public class PostProcessFilterOperation { |
26 |
|
27 |
private static final Logger logger = LoggerFactory.getLogger(PostProcessFilterOperation.class); |
28 |
|
29 |
private Pattern levelFilter = null; |
30 |
private Pattern colorFilter = null; |
31 |
private Pattern styleFilter = null; |
32 |
private Pattern weighFilter = null; |
33 |
private Pattern typeFilter = null; |
34 |
private Pattern stypeFilter = null; |
35 |
private Pattern idFilter = null; |
36 |
private Pattern groupFilter = null; |
37 |
private Pattern textFilter = null; |
38 |
private Point pointForCheckGeometries = null; |
39 |
private boolean hasfilter = false; |
40 |
private boolean skipCurruptGeometries = true; |
41 |
private int geometryTypeFilter = Geometry.TYPES.GEOMETRY; |
42 |
private int counter_exceptions = 0; |
43 |
private int cropOperationFilter; |
44 |
private Geometry cropFilter = null; |
45 |
private int ID_FIELD_LEVEL = -1; |
46 |
private int ID_FIELD_COLOR = -1; |
47 |
private int ID_FIELD_STYLE = -1; |
48 |
private int ID_FIELD_WEIGHT = -1; |
49 |
private int ID_FIELD_TYPE = -1; |
50 |
private int ID_FIELD_STYPE = -1; |
51 |
private int ID_FIELD_GROUP = -1; |
52 |
private int ID_FIELD_ID = -1; |
53 |
private int ID_FIELD_TEXT = -1; |
54 |
|
55 |
PostProcessFilterOperation(DGNOpenStoreParameters parameters, FeatureType featureType) { |
56 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
57 |
|
58 |
geometryTypeFilter = parameters.getGeometryTypeFilter(); |
59 |
skipCurruptGeometries = parameters.skipCorruptGeometries(); |
60 |
if (!StringUtils.isBlank(parameters.getLevelFilter())) {
|
61 |
this.levelFilter = Pattern.compile(parameters.getLevelFilter()); |
62 |
this.ID_FIELD_LEVEL = featureType.getIndex(DGNStoreProvider.NAME_FIELD_LEVEL);
|
63 |
this.hasfilter = true; |
64 |
} |
65 |
if (!StringUtils.isBlank(parameters.getColorFilter())) {
|
66 |
colorFilter = Pattern.compile(parameters.getColorFilter());
|
67 |
this.ID_FIELD_COLOR = featureType.getIndex(DGNStoreProvider.NAME_FIELD_COLOR);
|
68 |
this.hasfilter = true; |
69 |
} |
70 |
if (!StringUtils.isBlank(parameters.getStyleFilter())) {
|
71 |
styleFilter = Pattern.compile(parameters.getStyleFilter());
|
72 |
this.ID_FIELD_STYLE = featureType.getIndex(DGNStoreProvider.NAME_FIELD_STYLE);
|
73 |
this.hasfilter = true; |
74 |
} |
75 |
if (!StringUtils.isBlank(parameters.getTypeFilter())) {
|
76 |
this.typeFilter = Pattern.compile(parameters.getTypeFilter()); |
77 |
this.ID_FIELD_TYPE = featureType.getIndex(DGNStoreProvider.NAME_FIELD_TYPE);
|
78 |
this.hasfilter = true; |
79 |
} |
80 |
if (!StringUtils.isBlank(parameters.getSTypeFilter())) {
|
81 |
this.stypeFilter = Pattern.compile(parameters.getSTypeFilter()); |
82 |
this.ID_FIELD_STYPE = featureType.getIndex(DGNStoreProvider.NAME_FIELD_STYPE);
|
83 |
this.hasfilter = true; |
84 |
} |
85 |
if (!StringUtils.isBlank(parameters.getWeightFilter())) {
|
86 |
weighFilter = Pattern.compile(parameters.getWeightFilter());
|
87 |
this.ID_FIELD_WEIGHT = featureType.getIndex(DGNStoreProvider.NAME_FIELD_WEIGHT);
|
88 |
this.hasfilter = true; |
89 |
} |
90 |
if (!StringUtils.isBlank(parameters.getGroupFilter())) {
|
91 |
groupFilter = Pattern.compile(parameters.getGroupFilter());
|
92 |
this.ID_FIELD_GROUP = featureType.getIndex(DGNStoreProvider.NAME_FIELD_GROUP);
|
93 |
this.hasfilter = true; |
94 |
} |
95 |
if (!StringUtils.isBlank(parameters.getIDFilter())) {
|
96 |
idFilter = Pattern.compile(parameters.getIDFilter());
|
97 |
this.ID_FIELD_ID = featureType.getIndex(DGNStoreProvider.NAME_FIELD_ID);
|
98 |
this.hasfilter = true; |
99 |
} |
100 |
if (!StringUtils.isBlank(parameters.getTextFilter())) {
|
101 |
textFilter = Pattern.compile(parameters.getTextFilter());
|
102 |
this.ID_FIELD_TEXT = featureType.getIndex(DGNStoreProvider.NAME_FIELD_TEXT);
|
103 |
this.hasfilter = true; |
104 |
} |
105 |
if (this.geometryTypeFilter != Geometry.TYPES.GEOMETRY) { |
106 |
this.hasfilter = true; |
107 |
} |
108 |
this.cropFilter = parameters.getCropFilter();
|
109 |
if (this.cropFilter != null) { |
110 |
this.hasfilter = true; |
111 |
} |
112 |
this.cropOperationFilter = parameters.getCropOperationFilter();
|
113 |
if (this.cropOperationFilter == CROP_OPERATION_NONE) { |
114 |
this.cropFilter = null; |
115 |
} |
116 |
this.pointForCheckGeometries = null; |
117 |
if (skipCurruptGeometries) {
|
118 |
try {
|
119 |
int geomSubtype = featureType.getDefaultGeometryAttribute().getGeomType().getSubType();
|
120 |
this.pointForCheckGeometries = geomManager.createPoint(1, 1, geomSubtype); |
121 |
if (geomSubtype == Geometry.SUBTYPES.GEOM3D) {
|
122 |
this.pointForCheckGeometries.setCoordinateAt(Geometry.DIMENSIONS.Z, 1); |
123 |
} |
124 |
} catch (Exception ex) { |
125 |
this.pointForCheckGeometries = null; |
126 |
} |
127 |
} |
128 |
} |
129 |
|
130 |
public boolean hasFilter() { |
131 |
return this.hasfilter; |
132 |
} |
133 |
|
134 |
private boolean match(Pattern exp, Object value) { |
135 |
if (value == null) { |
136 |
return false; |
137 |
} |
138 |
return exp.matcher(value.toString()).matches();
|
139 |
} |
140 |
|
141 |
public boolean accept(FeatureProvider data) { |
142 |
|
143 |
if (!this.hasfilter) { |
144 |
return true; |
145 |
} |
146 |
|
147 |
Geometry geometry = data.getDefaultGeometry(); |
148 |
if (this.pointForCheckGeometries != null) { |
149 |
if (geometry != null) { |
150 |
try {
|
151 |
this.pointForCheckGeometries.intersects(geometry);
|
152 |
} catch (Exception ex) { |
153 |
// Skip geometries with problems
|
154 |
data.setDefaultGeometry(null);
|
155 |
geometry = null;
|
156 |
} |
157 |
} |
158 |
} |
159 |
|
160 |
try {
|
161 |
if (levelFilter!=null && !match(levelFilter, data.get(ID_FIELD_LEVEL))) { |
162 |
return false; |
163 |
} |
164 |
if (colorFilter!=null && !match(colorFilter, data.get(ID_FIELD_COLOR))) { |
165 |
return false; |
166 |
} |
167 |
if (styleFilter!=null && !match(styleFilter, data.get(ID_FIELD_STYLE))) { |
168 |
return false; |
169 |
} |
170 |
if (weighFilter!=null && !match(weighFilter, data.get(ID_FIELD_WEIGHT))) { |
171 |
return false; |
172 |
} |
173 |
if (typeFilter!=null && !match(typeFilter, data.get(ID_FIELD_TYPE))) { |
174 |
return false; |
175 |
} |
176 |
if (stypeFilter!=null && !match(stypeFilter, data.get(ID_FIELD_STYPE))) { |
177 |
return false; |
178 |
} |
179 |
if (groupFilter!=null && !match(groupFilter, data.get(ID_FIELD_GROUP))) { |
180 |
return false; |
181 |
} |
182 |
if (idFilter!=null && !match(idFilter, data.get(ID_FIELD_ID))) { |
183 |
return false; |
184 |
} |
185 |
if (textFilter!=null && !match(textFilter, data.get(ID_FIELD_TEXT))) { |
186 |
return false; |
187 |
} |
188 |
|
189 |
if (geometry != null) { |
190 |
if (geometryTypeFilter != Geometry.TYPES.GEOMETRY) {
|
191 |
GeometryType geomtype = geometry.getGeometryType(); |
192 |
switch (geometryTypeFilter) {
|
193 |
case Geometry.TYPES.POINT:
|
194 |
if (!geomtype.isTypeOf(Geometry.TYPES.POINT)
|
195 |
&& !geomtype.isTypeOf(Geometry.TYPES.MULTIPOINT)) { |
196 |
return false; |
197 |
} |
198 |
break;
|
199 |
case Geometry.TYPES.CURVE:
|
200 |
if (!geomtype.isTypeOf(Geometry.TYPES.CURVE)
|
201 |
&& !geomtype.isTypeOf(Geometry.TYPES.MULTICURVE)) { |
202 |
return false; |
203 |
} |
204 |
break;
|
205 |
case Geometry.TYPES.SURFACE:
|
206 |
if (!geomtype.isTypeOf(Geometry.TYPES.SURFACE)
|
207 |
&& !geomtype.isTypeOf(Geometry.TYPES.MULTISURFACE)) { |
208 |
return false; |
209 |
} |
210 |
break;
|
211 |
default:
|
212 |
break;
|
213 |
} |
214 |
} |
215 |
if (this.cropFilter != null) { |
216 |
switch (this.cropOperationFilter) { |
217 |
case CROP_OPERATION_CONTAINS:
|
218 |
if (!this.cropFilter.contains(geometry)) { |
219 |
return false; |
220 |
} |
221 |
break;
|
222 |
case CROP_OPERATION_COVEREDBY:
|
223 |
if (!this.cropFilter.coveredBy(geometry)) { |
224 |
return false; |
225 |
} |
226 |
break;
|
227 |
case CROP_OPERATION_COVERS:
|
228 |
if (!this.cropFilter.covers(geometry)) { |
229 |
return false; |
230 |
} |
231 |
break;
|
232 |
case CROP_OPERATION_CROSSES:
|
233 |
if (!this.cropFilter.crosses(geometry)) { |
234 |
return false; |
235 |
} |
236 |
break;
|
237 |
case CROP_OPERATION_DISJOINT:
|
238 |
if (!this.cropFilter.disjoint(geometry)) { |
239 |
return false; |
240 |
} |
241 |
break;
|
242 |
case CROP_OPERATION_INTERSECT:
|
243 |
if (!this.cropFilter.intersects(geometry)) { |
244 |
return false; |
245 |
} |
246 |
break;
|
247 |
case CROP_OPERATION_OVERLAPS:
|
248 |
if (!this.cropFilter.overlaps(geometry)) { |
249 |
return false; |
250 |
} |
251 |
break;
|
252 |
case CROP_OPERATION_TOUCHES:
|
253 |
if (!this.cropFilter.touches(geometry)) { |
254 |
return false; |
255 |
} |
256 |
break;
|
257 |
case CROP_OPERATION_WITHIN:
|
258 |
if (!this.cropFilter.within(geometry)) { |
259 |
return false; |
260 |
} |
261 |
break;
|
262 |
} |
263 |
} |
264 |
} |
265 |
} catch (Exception ex) { |
266 |
if (++counter_exceptions < 10) { |
267 |
logger.warn("Can't filter data, accept it.", ex);
|
268 |
} else if (counter_exceptions == 10) { |
269 |
logger.warn("Can't filter data, accept it.", ex);
|
270 |
logger.warn("Too many exceptions, dont show more.");
|
271 |
} |
272 |
return true; |
273 |
} |
274 |
return true; |
275 |
} |
276 |
|
277 |
} |