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 688 jjdelcerro
/**
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 726 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureReference;
32 688 jjdelcerro
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 721 jjdelcerro
import org.gvsig.tools.task.SimpleTaskStatus;
37 688 jjdelcerro
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 726 jjdelcerro
public class PointMustBeProperlyInsidePolygonRule extends AbstractTopologyRule {
53 688 jjdelcerro
54 726 jjdelcerro
    private class DeleteAction extends AbstractTopologyRuleAction {
55 688 jjdelcerro
56 726 jjdelcerro
        public DeleteAction() {
57 688 jjdelcerro
            super(
58 726 jjdelcerro
                    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 688 jjdelcerro
            );
63
        }
64
65
        @Override
66 726 jjdelcerro
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
67 688 jjdelcerro
            try {
68 726 jjdelcerro
                TopologyDataSet dataSet = rule.getDataSet1();
69 688 jjdelcerro
70 726 jjdelcerro
                dataSet.delete(line.getFeature1());
71
72 688 jjdelcerro
            } 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 726 jjdelcerro
83
    public PointMustBeProperlyInsidePolygonRule(
84 688 jjdelcerro
            TopologyPlan plan,
85
            TopologyRuleFactory factory,
86
            double tolerance,
87
            String dataSet1,
88
            String dataSet2
89
    ) {
90
        super(plan, factory, tolerance, dataSet1, dataSet2);
91 726 jjdelcerro
92
        this.addAction(new DeleteAction());
93 688 jjdelcerro
    }
94
95
    @Override
96 721 jjdelcerro
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
97 688 jjdelcerro
        FeatureSet set = null;
98
        try {
99 727 jjdelcerro
            FeatureStore store2 = this.getDataSet2().getFeatureStore();
100 726 jjdelcerro
            if (this.expression == null) {
101 688 jjdelcerro
                ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
102
                this.expression = manager.createExpression();
103
                this.expressionBuilder = manager.createExpressionBuilder();
104
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
105 726 jjdelcerro
            }
106
            Geometry point = feature1.getDefaultGeometry();
107
            TopologyDataSet theDataSet = this.getDataSet2();
108 727 jjdelcerro
            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 726 jjdelcerro
                this.expression.setPhrase(
132
                        this.expressionBuilder.ifnull(
133
                                this.expressionBuilder.column(this.geomName),
134
                                this.expressionBuilder.constant(false),
135 727 jjdelcerro
                                this.expressionBuilder.ST_Contains(
136 726 jjdelcerro
                                        this.expressionBuilder.column(this.geomName),
137
                                        this.expressionBuilder.geometry(point)
138
                                )
139
                        ).toString()
140 688 jjdelcerro
                );
141 726 jjdelcerro
                if (theDataSet.findFirst(this.expression) == null) {
142
                    report.addLine(this,
143
                            this.getDataSet1(),
144
                            this.getDataSet2(),
145
                            point,
146 727 jjdelcerro
                            point,
147 726 jjdelcerro
                            feature1.getReference(),
148
                            null,
149
                            false,
150
                            "The point are error where are not inside a polygon."
151
                    );
152
                }
153 727 jjdelcerro
            }
154 726 jjdelcerro
        } catch (Exception ex) {
155 688 jjdelcerro
            LOGGER.warn("Can't check feature.", ex);
156
        } finally {
157 726 jjdelcerro
            if (set != null) {
158 688 jjdelcerro
                set.dispose();
159
            }
160
        }
161
    }
162
163
}