/
FilteringMappingFeatureIterator.java
174 lines (158 loc) · 6.52 KB
/
FilteringMappingFeatureIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2010-2011, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.data.complex;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.geotools.data.Query;
import org.geotools.data.store.ReprojectingIterator;
import org.geotools.factory.FactoryRegistryException;
import org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.OperationNotFoundException;
/**
* An extension to {@linkplain org.geotools.data.complex.DataAccessMappingFeatureIterator} where
* filter is present. Since join query between 2 or more tables isn't supported, the only way we can
* query nested features is by applying the filter per simple feature (database row). This is done
* in hasNext().
*
* @author Rini Angreani (CSIRO Earth Science and Resource Engineering)
*
*
*
* @source $URL$
*/
public class FilteringMappingFeatureIterator extends DataAccessMappingFeatureIterator {
private Filter filter;
private List<Feature> sources;
private String currentFeatureId;
private CoordinateReferenceSystem targetCrs;
public FilteringMappingFeatureIterator(AppSchemaDataAccess store, FeatureTypeMapping mapping,
Query query, Query unrolledQuery, Filter filter) throws IOException {
super(store, mapping, query, unrolledQuery);
this.filter = filter;
// don't reproject if there's no geometry, therefore no source CRS (will cause exception)
this.targetCrs = (sourceFeatures.getSchema().getGeometryDescriptor() != null) ? super.reprojection
: null;
}
public FilteringMappingFeatureIterator(AppSchemaDataAccess store, FeatureTypeMapping mapping,
Query query, Query unrolledQuery, Filter filter, CoordinateReferenceSystem crs)
throws IOException {
super(store, mapping, query, unrolledQuery);
this.filter = filter;
this.targetCrs = crs;
}
@Override
protected void initialiseSourceFeatures(FeatureTypeMapping mapping, Query query,
CoordinateReferenceSystem crs) throws IOException {
super.initialiseSourceFeatures(mapping, query, crs);
}
@Override
protected void closeSourceFeatures() {
super.closeSourceFeatures();
this.sources = null;
}
@Override
public boolean hasNext() {
if (sources != null) {
// this is called in the beginning of next()
// we don't want to actually check the source iterator again
return true;
}
List<Feature> groupedFeatures;
// check that the feature exists
boolean matches = false;
while (!matches && super.hasNext()) {
sources = null;
// get all rows with same id from denormalised views
// and evaluate each row
currentFeatureId = extractIdForFeature(curSrcFeature);
try {
groupedFeatures = super.getSources(currentFeatureId);
Iterator<Feature> srcFeatures = groupedFeatures.iterator();
while (!matches && srcFeatures.hasNext()) {
// apply filter
if (filter == null || filter.evaluate(srcFeatures.next())) {
sources = reprojectFeatures(groupedFeatures);
matches = true;
}
}
} catch (IOException e) {
close();
throw new RuntimeException(e);
}
// reset so next time we call hasNext, it will get the next row
setHasNextCalled(false);
}
return matches;
}
/**
* Reproject source features if reprojection is set in the query. This has to be done after filtering, so
* it's consistent with JDBCFeatureSource way of filtering and reprojection.
* @param srcFeatures
* @return
* @throws OperationNotFoundException
* @throws FactoryRegistryException
* @throws FactoryException
*/
@SuppressWarnings("unchecked")
private List<Feature> reprojectFeatures(List<Feature> srcFeatures) {
if (targetCrs != null) {
List<Feature> features = new ArrayList<Feature>();
GeometryCoordinateSequenceTransformer transformer = new GeometryCoordinateSequenceTransformer();
Iterator<SimpleFeature> reprojectedFeatures;
try {
// this is really bad, app-schema really needs general feature utility classes
Iterator iterator = srcFeatures.iterator();
reprojectedFeatures = new ReprojectingIterator(iterator, mappedSource
.getSchema().getCoordinateReferenceSystem(), targetCrs,
(SimpleFeatureType) this.sourceFeatures.getSchema(), transformer);
while (reprojectedFeatures.hasNext()) {
features.add(reprojectedFeatures.next());
}
} catch (Exception e) {
close();
throw new RuntimeException ("Failed to reproject features in app-schema!", e);
}
return features;
}
return srcFeatures;
}
@Override
protected String getNextFeatureId() {
return currentFeatureId;
}
@Override
protected List<Feature> getSources(String id) throws IOException {
// return grouped source features
List<Feature> features;
if (sources != null) {
features = sources;
// reset for the next hasNext call
sources = null;
} else {
features = super.getSources(id);
}
return features;
}
}