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

History | View | Annotate | Download (6.53 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.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureReference;
32
import org.gvsig.fmap.dal.feature.FeatureSet;
33
import org.gvsig.fmap.dal.feature.FeatureStore;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.tools.dynobject.DynObject;
36
import org.gvsig.tools.task.SimpleTaskStatus;
37
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
38
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
39
import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException;
40
import org.gvsig.topology.lib.api.TopologyDataSet;
41
import org.gvsig.topology.lib.api.TopologyPlan;
42
import org.gvsig.topology.lib.api.TopologyReport;
43
import org.gvsig.topology.lib.api.TopologyReportLine;
44
import org.gvsig.topology.lib.api.TopologyRule;
45
import org.gvsig.topology.lib.api.TopologyRuleFactory;
46

    
47
/**
48
 *
49
 * @author jjdelcerro
50
 */
51
@SuppressWarnings("UseSpecificCatch")
52
public class PointMustBeProperlyInsidePolygonRule extends AbstractTopologyRule {
53

    
54
    private class DeleteAction extends AbstractTopologyRuleAction {
55

    
56
        public DeleteAction() {
57
            super(
58
                    PointMustBeProperlyInsidePolygonRuleFactory.NAME,
59
                    "Delete",
60
                    "Delete",
61
                    " 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."
62
            );
63
        }
64

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

    
70
                dataSet.delete(line.getFeature1());
71

    
72
            } catch (Exception ex) {
73
                throw new ExecuteTopologyRuleActionException(ex);
74
            }
75
        }
76

    
77
    }
78

    
79
    private String geomName;
80
    private Expression expression = null;
81
    private ExpressionBuilder expressionBuilder = null;
82

    
83
    public PointMustBeProperlyInsidePolygonRule(
84
            TopologyPlan plan,
85
            TopologyRuleFactory factory,
86
            double tolerance,
87
            String dataSet1,
88
            String dataSet2
89
    ) {
90
        super(plan, factory, tolerance, dataSet1, dataSet2);
91

    
92
        this.addAction(new DeleteAction());
93
    }
94

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

    
163
}