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

History | View | Annotate | Download (7.92 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.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.ToolsLocator;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.i18n.I18nManager;
38
import org.gvsig.tools.task.SimpleTaskStatus;
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
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
47
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
48

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

    
56
    private class DeleteAction extends AbstractTopologyRuleAction {
57

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

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

    
72
                dataSet.delete(line.getFeature1());
73
                return EXECUTE_OK;
74
            } catch (Exception ex) {
75
                throw new ExecuteTopologyRuleActionException(ex);
76
            }
77
        }
78

    
79
    }
80

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

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

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

    
97
    @Override
98
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
99
        FeatureSet set = null;
100
        try {
101
            FeatureStore store2 = this.getDataSet2().getFeatureStore();
102
            if (this.expression == null) {
103
                this.expression = ExpressionUtils.createExpression();
104
                this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
105
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
106
            }
107
            double theTolerance = getTolerance();
108
            Geometry point = feature1.getDefaultGeometry();
109
            TopologyDataSet theDataSet2 = this.getDataSet2();
110
            if (theDataSet2.getSpatialIndex() != null) {
111
                boolean contained = false;
112
                for (FeatureReference featureReference : theDataSet2.query(point)) {
113
                    Feature feature2 = featureReference.getFeature();
114
                    Geometry otherPolygon = feature2.getDefaultGeometry();
115
                    if(theTolerance > 0){
116
                        otherPolygon = otherPolygon.buffer(theTolerance);
117
                    }
118
                    if( otherPolygon!=null && otherPolygon.contains(point) ) {
119
                        contained = true;
120
                        break;
121
                    }
122
                }
123
                if( !contained ) {
124
                    I18nManager i18n = ToolsLocator.getI18nManager();
125
                    report.addLine(this,
126
                            this.getDataSet1(),
127
                            this.getDataSet2(),
128
                            point,
129
                            point,
130
                            feature1.getReference(),
131
                            null,
132
                            false,
133
                            i18n.getTranslation("_Point_is_not_contained_in_a_polygon")
134
                    );
135
                }
136
            } else {
137
                
138
                if (theTolerance > 0) {
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.ST_Buffer(
145
                                                    this.expressionBuilder.column(this.geomName),
146
                                                    this.expressionBuilder.constant(theTolerance)
147
                                            ),
148
                                            this.expressionBuilder.geometry(point)
149
                                    )
150
                            ).toString()
151
                    );
152
                } else {
153
                    this.expression.setPhrase(
154
                            this.expressionBuilder.ifnull(
155
                                    this.expressionBuilder.column(this.geomName),
156
                                    this.expressionBuilder.constant(false),
157
                                    this.expressionBuilder.ST_Contains(
158
                                            this.expressionBuilder.column(this.geomName),
159
                                            this.expressionBuilder.geometry(point)
160
                                    )
161
                            ).toString()
162
                    );
163
                }
164
                if (theDataSet2.findFirst(this.expression) == null) {
165
                    I18nManager i18n = ToolsLocator.getI18nManager();
166
                    report.addLine(this,
167
                            this.getDataSet1(),
168
                            this.getDataSet2(),
169
                            point,
170
                            point,
171
                            feature1.getReference(),
172
                            null,
173
                            false,
174
                            i18n.getTranslation("_Point_is_not_contained_in_a_polygon")
175
                    );
176
                }
177
            }
178
        } catch (Exception ex) {
179
            LOGGER.warn("Can't check feature.", ex);
180
            addCodeException(report, feature1, ex);
181
        } finally {
182
            if (set != null) {
183
                set.dispose();
184
            }
185
        }
186
    }
187

    
188
}