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

History | View | Annotate | Download (6.61 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
    ) {
87
        super(plan, factory);
88
        this.actions.add(new DeleteAction());
89
    }
90

    
91
    public PointMustBeProperlyInsidePolygonRule(
92
            TopologyPlan plan,
93
            TopologyRuleFactory factory,
94
            double tolerance,
95
            String dataSet1,
96
            String dataSet2
97
    ) {
98
        super(plan, factory, tolerance, dataSet1, dataSet2);
99

    
100
        this.addAction(new DeleteAction());
101
    }
102

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

    
168
}