gvsig-projects-pool / org.gvsig.topology / trunk / org.gvsig.topology / org.gvsig.topology.lib / org.gvsig.topology.lib.impl / src / main / java / org / gvsig / topology / rule / PolygonContainsOnePointRule.java @ 4565
History | View | Annotate | Download (10.1 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2021 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 3
|
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.topology.rule; |
25 |
|
26 |
import org.gvsig.expressionevaluator.Expression; |
27 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
28 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
29 |
import org.gvsig.expressionevaluator.GeometryExpressionUtils; |
30 |
import org.gvsig.fmap.dal.feature.Feature; |
31 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
32 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.tools.ToolsLocator; |
36 |
import org.gvsig.tools.i18n.I18nManager; |
37 |
import org.gvsig.tools.task.SimpleTaskStatus; |
38 |
import org.gvsig.tools.visitor.VisitCanceledException; |
39 |
import org.gvsig.topology.lib.api.TopologyDataSet; |
40 |
import org.gvsig.topology.lib.api.TopologyReport; |
41 |
import org.gvsig.topology.lib.api.TopologyRuleFactory; |
42 |
import org.gvsig.topology.lib.spi.AbstractTopologyRule; |
43 |
|
44 |
/**
|
45 |
*
|
46 |
* @author jjdelcerro
|
47 |
*/
|
48 |
@SuppressWarnings("UseSpecificCatch") |
49 |
public class PolygonContainsOnePointRule extends AbstractTopologyRule { |
50 |
|
51 |
private String geomName; |
52 |
private Expression expression = null; |
53 |
private GeometryExpressionBuilder expressionBuilder = null; |
54 |
|
55 |
public PolygonContainsOnePointRule() {
|
56 |
// for persistence only
|
57 |
} |
58 |
|
59 |
public PolygonContainsOnePointRule(
|
60 |
TopologyRuleFactory factory, |
61 |
double tolerance,
|
62 |
String dataSet1,
|
63 |
String dataSet2
|
64 |
) { |
65 |
super(factory, tolerance, dataSet1, dataSet2);
|
66 |
} |
67 |
|
68 |
@Override
|
69 |
public long getSteps() { |
70 |
return this.getDataSet1().getSize()+this.getDataSet2().getSize(); |
71 |
} |
72 |
|
73 |
@Override
|
74 |
public void execute(final SimpleTaskStatus taskStatus, final TopologyReport report) { |
75 |
try {
|
76 |
this.getDataSet1().accept((final Object o1) -> { |
77 |
if (taskStatus.isCancellationRequested()) {
|
78 |
throw new VisitCanceledException(); |
79 |
} |
80 |
taskStatus.incrementCurrentValue(); |
81 |
try {
|
82 |
check(taskStatus, report, (Feature) o1); |
83 |
} catch (Exception ex) { |
84 |
throw new RuntimeException(ex); |
85 |
} |
86 |
}); |
87 |
this.getDataSet2().accept((final Object o1) -> { |
88 |
if (taskStatus.isCancellationRequested()) {
|
89 |
throw new VisitCanceledException(); |
90 |
} |
91 |
taskStatus.incrementCurrentValue(); |
92 |
try {
|
93 |
check2(taskStatus, report, (Feature) o1); |
94 |
} catch (Exception ex) { |
95 |
throw new RuntimeException(ex); |
96 |
} |
97 |
}); |
98 |
} catch (VisitCanceledException ex) {
|
99 |
// return;
|
100 |
} |
101 |
} |
102 |
|
103 |
|
104 |
@Override
|
105 |
protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception { |
106 |
FeatureSet set = null;
|
107 |
try {
|
108 |
FeatureStore store2 = this.getDataSet2().getFeatureStore();
|
109 |
if (this.expression == null) { |
110 |
this.expression = ExpressionUtils.createExpression();
|
111 |
this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
|
112 |
this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
|
113 |
} |
114 |
Geometry polygon = feature1.getDefaultGeometry(); |
115 |
double theTolerance = getTolerance();
|
116 |
if(theTolerance > 0){ |
117 |
polygon = polygon.buffer(theTolerance); |
118 |
} |
119 |
|
120 |
TopologyDataSet theDataSet = this.getDataSet2();
|
121 |
int contained = 0; |
122 |
if (theDataSet.getSpatialIndex() != null) { |
123 |
for (FeatureReference featureReference : theDataSet.query(polygon)) {
|
124 |
Feature feature2 = featureReference.getFeature(); |
125 |
Geometry otherPoint = feature2.getDefaultGeometry(); |
126 |
if (otherPoint != null && polygon.contains(otherPoint)) { |
127 |
contained++; |
128 |
break;
|
129 |
} |
130 |
} |
131 |
|
132 |
} else {
|
133 |
this.expression.setPhrase(
|
134 |
this.expressionBuilder.ifnull(
|
135 |
this.expressionBuilder.column(this.geomName), |
136 |
this.expressionBuilder.constant(false), |
137 |
this.expressionBuilder.ST_Contains(
|
138 |
this.expressionBuilder.geometry(polygon),
|
139 |
this.expressionBuilder.column(this.geomName) |
140 |
) |
141 |
).toString() |
142 |
); |
143 |
|
144 |
FeatureSet featSet = theDataSet.getFeatureStore().getFeatureSet(this.expression);
|
145 |
contained = featSet.size(); |
146 |
} |
147 |
if (contained <= 0) { |
148 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
149 |
report.addLine(this,
|
150 |
this.getDataSet1(),
|
151 |
this.getDataSet2(),
|
152 |
polygon, |
153 |
polygon, |
154 |
feature1.getReference(), |
155 |
null,
|
156 |
false,
|
157 |
i18n.getTranslation("_The_polygon_does_not_contain_any_points")
|
158 |
); |
159 |
} else if (contained > 1) { |
160 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
161 |
report.addLine(this,
|
162 |
this.getDataSet1(),
|
163 |
this.getDataSet2(),
|
164 |
polygon, |
165 |
polygon, |
166 |
feature1.getReference(), |
167 |
null,
|
168 |
false,
|
169 |
i18n.getTranslation("_The_polygon_contains_more_than_a_point")
|
170 |
); |
171 |
} |
172 |
} catch (Exception ex) { |
173 |
LOGGER.warn("Can't check feature.", ex);
|
174 |
addCodeException(report, feature1, ex); |
175 |
} finally {
|
176 |
if (set != null) { |
177 |
set.dispose(); |
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
protected void check2(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature2) throws Exception { |
183 |
FeatureSet set = null;
|
184 |
try {
|
185 |
TopologyDataSet theDataSet2 = this.getDataSet2();
|
186 |
TopologyDataSet theDataSet1 = this.getDataSet1();
|
187 |
FeatureStore otherStore = theDataSet1.getFeatureStore(); |
188 |
if (this.expression == null) { |
189 |
this.expression = ExpressionUtils.createExpression();
|
190 |
this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
|
191 |
this.geomName = otherStore.getDefaultFeatureType().getDefaultGeometryAttributeName();
|
192 |
} |
193 |
Geometry point = feature2.getDefaultGeometry(); |
194 |
if (theDataSet1.getSpatialIndex() != null) { |
195 |
boolean contained = false; |
196 |
for (FeatureReference otherFeatureReference : theDataSet1.query(point)) {
|
197 |
Feature otherFeature = otherFeatureReference.getFeature(); |
198 |
Geometry otherPolygon = otherFeature.getDefaultGeometry(); |
199 |
if( otherPolygon!=null && otherPolygon.contains(point) ) { |
200 |
contained = true;
|
201 |
break;
|
202 |
} |
203 |
} |
204 |
if( !contained ) {
|
205 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
206 |
report.addLine(this,
|
207 |
theDataSet1, |
208 |
theDataSet2, |
209 |
point, |
210 |
point, |
211 |
null,
|
212 |
feature2.getReference(), |
213 |
false,
|
214 |
i18n.getTranslation("_Point_is_not_contained_in_a_polygon")
|
215 |
); |
216 |
} |
217 |
} else {
|
218 |
this.expression.setPhrase(
|
219 |
this.expressionBuilder.ifnull(
|
220 |
this.expressionBuilder.column(this.geomName), |
221 |
this.expressionBuilder.constant(false), |
222 |
this.expressionBuilder.ST_Contains(
|
223 |
this.expressionBuilder.column(this.geomName), |
224 |
this.expressionBuilder.geometry(point)
|
225 |
) |
226 |
).toString() |
227 |
); |
228 |
if (theDataSet1.findFirst(this.expression) == null) { |
229 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
230 |
report.addLine(this,
|
231 |
theDataSet1, |
232 |
theDataSet2, |
233 |
point, |
234 |
point, |
235 |
null,
|
236 |
feature2.getReference(), |
237 |
false,
|
238 |
i18n.getTranslation("_Point_is_not_contained_in_a_polygon")
|
239 |
); |
240 |
} |
241 |
} |
242 |
} catch (Exception ex) { |
243 |
LOGGER.warn("Can't check feature.", ex);
|
244 |
addCodeException(report, feature2, ex); |
245 |
} finally {
|
246 |
if (set != null) { |
247 |
set.dispose(); |
248 |
} |
249 |
} |
250 |
} |
251 |
|
252 |
|
253 |
|
254 |
} |