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 / PointMustBeProperlyInsidePolygonRule.java @ 1284

History | View | Annotate | Download (6.65 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.expressionevaluator.ExpressionUtils;
31
import org.gvsig.expressionevaluator.GeometryExpressionBuilder;
32
import org.gvsig.expressionevaluator.GeometryExpressionUtils;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureReference;
35
import org.gvsig.fmap.dal.feature.FeatureSet;
36
import org.gvsig.fmap.dal.feature.FeatureStore;
37
import org.gvsig.fmap.geom.Geometry;
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 PointMustBeProperlyInsidePolygonRule extends AbstractTopologyRule {
56

    
57
    private class DeleteAction extends AbstractTopologyRuleAction {
58

    
59
        public DeleteAction() {
60
            super(
61
                    PointMustBeProperlyInsidePolygonRuleFactory.NAME,
62
                    "Delete",
63
                    "Delete",
64
                    " The Delete fix removes point features that are not properly within polygon features. Note that you can use the Edit tool and move the point inside the polygon feature if you do not want to delete it. This fix can be applied to one or more Must Be Properly Inside errors."
65
            );
66
        }
67

    
68
        @Override
69
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
70
            try {
71
                TopologyDataSet dataSet = rule.getDataSet1();
72

    
73
                dataSet.delete(line.getFeature1());
74

    
75
            } catch (Exception ex) {
76
                throw new ExecuteTopologyRuleActionException(ex);
77
            }
78
        }
79

    
80
    }
81

    
82
    private String geomName;
83
    private Expression expression = null;
84
    private GeometryExpressionBuilder expressionBuilder = null;
85

    
86
    public PointMustBeProperlyInsidePolygonRule(
87
            TopologyPlan plan,
88
            TopologyRuleFactory factory,
89
            double tolerance,
90
            String dataSet1,
91
            String dataSet2
92
    ) {
93
        super(plan, factory, tolerance, dataSet1, dataSet2);
94

    
95
        this.addAction(new DeleteAction());
96
    }
97

    
98
    @Override
99
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
100
        FeatureSet set = null;
101
        try {
102
            FeatureStore store2 = this.getDataSet2().getFeatureStore();
103
            if (this.expression == null) {
104
                this.expression = ExpressionUtils.createExpression();
105
                this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
106
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
107
            }
108
            Geometry point = feature1.getDefaultGeometry();
109
            TopologyDataSet theDataSet = this.getDataSet2();
110
            if (theDataSet.getSpatialIndex() != null) {
111
                boolean contained = false;
112
                for (FeatureReference featureReference : theDataSet.query(point)) {
113
                    Feature feature2 = featureReference.getFeature();
114
                    Geometry otherPolygon = feature2.getDefaultGeometry();
115
                    if( otherPolygon!=null && otherPolygon.contains(point) ) {
116
                        contained = true;
117
                        break;
118
                    }
119
                }
120
                if( !contained ) {
121
                    report.addLine(this,
122
                            this.getDataSet1(),
123
                            this.getDataSet2(),
124
                            point,
125
                            point,
126
                            feature1.getReference(),
127
                            null,
128
                            false,
129
                            "The point are error where are not inside a polygon."
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.column(this.geomName),
139
                                        this.expressionBuilder.geometry(point)
140
                                )
141
                        ).toString()
142
                );
143
                if (theDataSet.findFirst(this.expression) == null) {
144
                    report.addLine(this,
145
                            this.getDataSet1(),
146
                            this.getDataSet2(),
147
                            point,
148
                            point,
149
                            feature1.getReference(),
150
                            null,
151
                            false,
152
                            "The point are error where are not inside a polygon."
153
                    );
154
                }
155
            }
156
        } catch (Exception ex) {
157
            LOGGER.warn("Can't check feature.", ex);
158
        } finally {
159
            if (set != null) {
160
                set.dispose();
161
            }
162
        }
163
    }
164

    
165
}