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

History | View | Annotate | Download (6.82 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.ExpressionUtils;
28
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
29
import org.gvsig.expressionevaluator.GeometryExpressionUtils;
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 GeometryExpressionBuilder 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
                this.expression = ExpressionUtils.createExpression();
111
                this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
112
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
113
            }
114
            Geometry polygon = feature1.getDefaultGeometry();
115
            TopologyDataSet theDataSet = this.getDataSet2();
116
            if (theDataSet.getSpatialIndex() != null) {
117
                boolean contains = false;
118
                for (FeatureReference featureReference : theDataSet.query(polygon)) {
119
                    Feature feature2 = featureReference.getFeature();
120
                    Geometry otherPoint = feature2.getDefaultGeometry();
121
                    if( otherPoint!=null && polygon.contains(otherPoint) ) {
122
                        contains = true;
123
                        break;
124
                    }
125
                }
126
                if( !contains ) {
127
                    report.addLine(this,
128
                            this.getDataSet1(),
129
                            this.getDataSet2(),
130
                            polygon,
131
                            polygon,
132
                            feature1.getReference(),
133
                            null,
134
                            false,
135
                            "The polygon is an error because it does not contain a point."
136
                    );
137
                }
138
            } else {
139
                this.expression.setPhrase(
140
                        this.expressionBuilder.ifnull(
141
                                this.expressionBuilder.column(this.geomName),
142
                                this.expressionBuilder.constant(false),
143
                                this.expressionBuilder.ST_Contains(
144
                                        this.expressionBuilder.geometry(polygon),
145
                                        this.expressionBuilder.column(this.geomName)
146
                                )
147
                        ).toString()
148
                );
149
                if (theDataSet.findFirst(this.expression) == null) {
150
                    report.addLine(this,
151
                            this.getDataSet1(),
152
                            this.getDataSet2(),
153
                            polygon,
154
                            polygon,
155
                            feature1.getReference(),
156
                            null,
157
                            false,
158
                            "The polygon is an error because it does not contain a point."
159
                    );
160
                }
161
            }
162
        } catch (Exception ex) {
163
            LOGGER.warn("Can't check feature.", ex);
164
        } finally {
165
            if (set != null) {
166
                set.dispose();
167
            }
168
        }
169
    }
170

    
171
}