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
/**
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
import org.gvsig.fmap.dal.feature.FeatureReference;
33
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
import org.gvsig.tools.task.SimpleTaskStatus;
39
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
                    ContainsPointRuleFactory.NAME,
61
                    "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
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
69
            try {
70
                Geometry polygon = line.getGeometry();
71
                Point point = polygon.centroid();
72
                if( !polygon.contains(point) ) {
73
                    point = polygon.getInteriorPoint();
74
                }
75
                TopologyDataSet dataSet = rule.getDataSet2();
76

    
77
                EditableFeature feature = dataSet.createNewFeature();
78
                feature.setDefaultGeometry(point);
79
                dataSet.insert(feature);
80

    
81
            } 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

    
92
    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

    
101
        this.addAction(new CreateFetureAction());
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
                ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
111
                this.expression = manager.createExpression();
112
                this.expressionBuilder = manager.createExpressionBuilder();
113
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
114
            }
115
            Geometry polygon = feature1.getDefaultGeometry();
116
            TopologyDataSet theDataSet = this.getDataSet2();
117
            if (theDataSet.getSpatialIndex() != null) {
118
                boolean contains = false;
119
                for (FeatureReference featureReference : theDataSet.query(polygon)) {
120
                    Feature feature2 = featureReference.getFeature();
121
                    Geometry otherPoint = feature2.getDefaultGeometry();
122
                    if( otherPoint!=null && polygon.contains(otherPoint) ) {
123
                        contains = true;
124
                        break;
125
                    }
126
                }
127
                if( !contains ) {
128
                    report.addLine(this,
129
                            this.getDataSet1(),
130
                            this.getDataSet2(),
131
                            polygon,
132
                            polygon,
133
                            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
                );
150
                if (theDataSet.findFirst(this.expression) == null) {
151
                    report.addLine(this,
152
                            this.getDataSet1(),
153
                            this.getDataSet2(),
154
                            polygon,
155
                            polygon,
156
                            feature1.getReference(),
157
                            null,
158
                            false,
159
                            "The polygon is an error because it does not contain a point."
160
                    );
161
                }
162
            }
163
        } catch (Exception ex) {
164
            LOGGER.warn("Can't check feature.", ex);
165
        } finally {
166
            if (set != null) {
167
                set.dispose();
168
            }
169
        }
170
    }
171

    
172
}