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 @ 726

History | View | Annotate | Download (6.96 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.fmap.geom.primitive.Polygon;
38
import org.gvsig.tools.dynobject.DynObject;
39
import org.gvsig.tools.task.SimpleTaskStatus;
40
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
41
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
42
import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException;
43
import org.gvsig.topology.lib.api.TopologyDataSet;
44
import org.gvsig.topology.lib.api.TopologyPlan;
45
import org.gvsig.topology.lib.api.TopologyReport;
46
import org.gvsig.topology.lib.api.TopologyReportLine;
47
import org.gvsig.topology.lib.api.TopologyRule;
48
import org.gvsig.topology.lib.api.TopologyRuleFactory;
49

    
50
/**
51
 *
52
 * @author jjdelcerro
53
 */
54
@SuppressWarnings("UseSpecificCatch")
55
public class ContainsPointRule extends AbstractTopologyRule {
56

    
57
    private class CreateFetureAction extends AbstractTopologyRuleAction {
58

    
59
        public CreateFetureAction() {
60
            super(
61
                    ContainsPointRuleFactory.NAME,
62
                    "CreateFeature",
63
                    "Create Feature",
64
                    "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."
65
            );
66
        }
67

    
68
        @Override
69
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
70
            try {
71
                Geometry polygon = line.getGeometry();
72
                Point point = polygon.centroid();
73
                if( !polygon.contains(point) ) {
74
                    point = polygon.getInteriorPoint();
75
                }
76
                TopologyDataSet dataSet = rule.getDataSet2();
77

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

    
82
            } catch (Exception ex) {
83
                throw new ExecuteTopologyRuleActionException(ex);
84
            }
85
        }
86

    
87
    }
88

    
89
    private String geomName;
90
    private Expression expression = null;
91
    private ExpressionBuilder expressionBuilder = null;
92

    
93
    public ContainsPointRule(
94
            TopologyPlan plan,
95
            TopologyRuleFactory factory
96
    ) {
97
        super(plan, factory);
98
        this.actions.add(new CreateFetureAction());
99
    }
100

    
101
    public ContainsPointRule(
102
            TopologyPlan plan,
103
            TopologyRuleFactory factory,
104
            double tolerance,
105
            String dataSet1,
106
            String dataSet2
107
    ) {
108
        super(plan, factory, tolerance, dataSet1, dataSet2);
109

    
110
        this.addAction(new CreateFetureAction());
111
    }
112

    
113
    @Override
114
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
115
        FeatureSet set = null;
116
        try {
117
            FeatureStore store2 = this.getDataSet2().getStore();
118
            if (this.expression == null) {
119
                ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
120
                this.expression = manager.createExpression();
121
                this.expressionBuilder = manager.createExpressionBuilder();
122
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
123
            }
124
            Geometry polygon = feature1.getDefaultGeometry();
125
            TopologyDataSet theDataSet = this.getDataSet2();
126
            if (theDataSet.getSpatialIndex() != null) {
127
                boolean ok = false;
128
                for (FeatureReference featureReference : theDataSet.query(polygon)) {
129
                    Feature feature2 = featureReference.getFeature();
130
                    if( polygon.contains(feature2.getDefaultGeometry()) ) {
131
                        ok = true;
132
                        break;
133
                    }
134
                }
135
                if( !ok ) {
136
                    report.addLine(this,
137
                            this.getDataSet1(),
138
                            this.getDataSet2(),
139
                            polygon,
140
                            feature1.getReference(),
141
                            null,
142
                            false,
143
                            "The polygon is an error because it does not contain a point."
144
                    );
145
                }
146
            } else {
147
                this.expression.setPhrase(
148
                        this.expressionBuilder.ifnull(
149
                                this.expressionBuilder.column(this.geomName),
150
                                this.expressionBuilder.constant(false),
151
                                this.expressionBuilder.ST_Contains(
152
                                        this.expressionBuilder.geometry(polygon),
153
                                        this.expressionBuilder.column(this.geomName)
154
                                )
155
                        ).toString()
156
                );
157
                if (theDataSet.findFirst(this.expression) == null) {
158
                    report.addLine(this,
159
                            this.getDataSet1(),
160
                            this.getDataSet2(),
161
                            polygon,
162
                            feature1.getReference(),
163
                            null,
164
                            false,
165
                            "The polygon is an error because it does not contain a point."
166
                    );
167
                }
168
            }
169
        } catch (Exception ex) {
170
            LOGGER.warn("Can't check feature.", ex);
171
        } finally {
172
            if (set != null) {
173
                set.dispose();
174
            }
175
        }
176
    }
177

    
178
}