Statistics
| Revision:

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 / ContainsPointRule.java @ 727

History | View | Annotate | Download (6.89 KB)

1 688 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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.ExpressionBuilder;
28
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
29
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
30
import org.gvsig.fmap.dal.feature.EditableFeature;
31
import org.gvsig.fmap.dal.feature.Feature;
32 726 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureReference;
33 688 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureSet;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.tools.dynobject.DynObject;
38 721 jjdelcerro
import org.gvsig.tools.task.SimpleTaskStatus;
39 688 jjdelcerro
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
40
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
41
import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException;
42
import org.gvsig.topology.lib.api.TopologyDataSet;
43
import org.gvsig.topology.lib.api.TopologyPlan;
44
import org.gvsig.topology.lib.api.TopologyReport;
45
import org.gvsig.topology.lib.api.TopologyReportLine;
46
import org.gvsig.topology.lib.api.TopologyRule;
47
import org.gvsig.topology.lib.api.TopologyRuleFactory;
48
49
/**
50
 *
51
 * @author jjdelcerro
52
 */
53
@SuppressWarnings("UseSpecificCatch")
54
public class ContainsPointRule extends AbstractTopologyRule {
55
56
    private class CreateFetureAction extends AbstractTopologyRuleAction {
57
58
        public CreateFetureAction() {
59
            super(
60 712 jjdelcerro
                    ContainsPointRuleFactory.NAME,
61 688 jjdelcerro
                    "CreateFeature",
62
                    "Create Feature",
63
                    "The Create Feature fix creates a new point feature at the centroid of the polygon feature that is causing the error. The point feature that is created is guaranteed to be within the polygon feature."
64
            );
65
        }
66
67
        @Override
68 726 jjdelcerro
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
69 688 jjdelcerro
            try {
70 726 jjdelcerro
                Geometry polygon = line.getGeometry();
71
                Point point = polygon.centroid();
72
                if( !polygon.contains(point) ) {
73
                    point = polygon.getInteriorPoint();
74
                }
75 688 jjdelcerro
                TopologyDataSet dataSet = rule.getDataSet2();
76
77
                EditableFeature feature = dataSet.createNewFeature();
78
                feature.setDefaultGeometry(point);
79
                dataSet.insert(feature);
80 726 jjdelcerro
81 688 jjdelcerro
            } catch (Exception ex) {
82
                throw new ExecuteTopologyRuleActionException(ex);
83
            }
84
        }
85
86
    }
87
88
    private String geomName;
89
    private Expression expression = null;
90
    private ExpressionBuilder expressionBuilder = null;
91 726 jjdelcerro
92 688 jjdelcerro
    public ContainsPointRule(
93
            TopologyPlan plan,
94
            TopologyRuleFactory factory,
95
            double tolerance,
96
            String dataSet1,
97
            String dataSet2
98
    ) {
99
        super(plan, factory, tolerance, dataSet1, dataSet2);
100 726 jjdelcerro
101 722 jjdelcerro
        this.addAction(new CreateFetureAction());
102 688 jjdelcerro
    }
103
104
    @Override
105 721 jjdelcerro
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
106 688 jjdelcerro
        FeatureSet set = null;
107
        try {
108 727 jjdelcerro
            FeatureStore store2 = this.getDataSet2().getFeatureStore();
109 726 jjdelcerro
            if (this.expression == null) {
110 688 jjdelcerro
                ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
111
                this.expression = manager.createExpression();
112
                this.expressionBuilder = manager.createExpressionBuilder();
113
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
114 726 jjdelcerro
            }
115 688 jjdelcerro
            Geometry polygon = feature1.getDefaultGeometry();
116 726 jjdelcerro
            TopologyDataSet theDataSet = this.getDataSet2();
117
            if (theDataSet.getSpatialIndex() != null) {
118 727 jjdelcerro
                boolean contains = false;
119 726 jjdelcerro
                for (FeatureReference featureReference : theDataSet.query(polygon)) {
120
                    Feature feature2 = featureReference.getFeature();
121 727 jjdelcerro
                    Geometry otherPoint = feature2.getDefaultGeometry();
122
                    if( otherPoint!=null && polygon.contains(otherPoint) ) {
123
                        contains = true;
124 726 jjdelcerro
                        break;
125
                    }
126
                }
127 727 jjdelcerro
                if( !contains ) {
128 726 jjdelcerro
                    report.addLine(this,
129
                            this.getDataSet1(),
130
                            this.getDataSet2(),
131
                            polygon,
132 727 jjdelcerro
                            polygon,
133 726 jjdelcerro
                            feature1.getReference(),
134
                            null,
135
                            false,
136
                            "The polygon is an error because it does not contain a point."
137
                    );
138
                }
139
            } else {
140
                this.expression.setPhrase(
141
                        this.expressionBuilder.ifnull(
142
                                this.expressionBuilder.column(this.geomName),
143
                                this.expressionBuilder.constant(false),
144
                                this.expressionBuilder.ST_Contains(
145
                                        this.expressionBuilder.geometry(polygon),
146
                                        this.expressionBuilder.column(this.geomName)
147
                                )
148
                        ).toString()
149 688 jjdelcerro
                );
150 726 jjdelcerro
                if (theDataSet.findFirst(this.expression) == null) {
151
                    report.addLine(this,
152
                            this.getDataSet1(),
153
                            this.getDataSet2(),
154
                            polygon,
155 727 jjdelcerro
                            polygon,
156 726 jjdelcerro
                            feature1.getReference(),
157
                            null,
158
                            false,
159
                            "The polygon is an error because it does not contain a point."
160
                    );
161
                }
162 688 jjdelcerro
            }
163 726 jjdelcerro
        } catch (Exception ex) {
164 688 jjdelcerro
            LOGGER.warn("Can't check feature.", ex);
165
        } finally {
166 726 jjdelcerro
            if (set != null) {
167 688 jjdelcerro
                set.dispose();
168
            }
169
        }
170
    }
171
172
}