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

History | View | Annotate | Download (8.68 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.geom.Geometry;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.task.SimpleTaskStatus;
38
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
39
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
40
import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException;
41
import org.gvsig.topology.lib.api.TopologyDataSet;
42
import org.gvsig.topology.lib.api.TopologyPlan;
43
import org.gvsig.topology.lib.api.TopologyReport;
44
import org.gvsig.topology.lib.api.TopologyReportLine;
45
import org.gvsig.topology.lib.api.TopologyRule;
46
import org.gvsig.topology.lib.api.TopologyRuleFactory;
47

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

    
55
    private class CreateFetureAction extends AbstractTopologyRuleAction {
56

    
57
        public CreateFetureAction() {
58
            super(
59
                    MustNotOverlapPolygonRuleFactory.NAME,
60
                    "CreateFeature",
61
                    "Create Feature",
62
                    "The Create Feature fix creates a new polygon feature out "
63
                    + "of the error shape and removes the portion of "
64
                    + "overlap from each of the features, causing the "
65
                    + "error to create a planar representation of the "
66
                    + "feature geometry. This fix can be applied to "
67
                    + "one or more selected Must Not Overlap errors."
68
            );
69
        }
70

    
71
        @Override
72
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
73
            try {
74
                // TODO
75
            } catch (Exception ex) {
76
                throw new ExecuteTopologyRuleActionException(ex);
77
            }
78
        }
79

    
80
    }
81

    
82
    private class SubtractAction extends AbstractTopologyRuleAction {
83

    
84
        public SubtractAction() {
85
            super(
86
                    MustNotOverlapPolygonRuleFactory.NAME,
87
                    "Subtract",
88
                    "Subtract",
89
                    "The Subtract fix removes the overlapping portion of "
90
                    + "geometry from each feature that is causing the "
91
                    + "error and leaves a gap or void in its place. "
92
                    + "This fix can be applied to one or more selected "
93
                    + "Must Not Overlap errors."
94
            );
95
        }
96

    
97
        @Override
98
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
99
            try {
100
                // TODO
101
            } catch (Exception ex) {
102
                throw new ExecuteTopologyRuleActionException(ex);
103
            }
104
        }
105

    
106
    }
107

    
108
    private class MergeAction extends AbstractTopologyRuleAction {
109

    
110
        public MergeAction() {
111
            super(
112
                    MustNotOverlapPolygonRuleFactory.NAME,
113
                    "Merge",
114
                    "Merge",
115
                    "The Merge fix adds the portion of overlap from one feature "
116
                    + "and subtracts it from the others that are "
117
                    + "violating the rule. You need to pick the feature "
118
                    + "that receives the portion of overlap using the "
119
                    + "Merge dialog box. This fix can be applied to one "
120
                    + "Must Not Overlap error only."
121
            );
122
        }
123

    
124
        @Override
125
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
126
            try {
127
                // TODO
128
            } catch (Exception ex) {
129
                throw new ExecuteTopologyRuleActionException(ex);
130
            }
131
        }
132
    }
133

    
134
    private String geomName;
135
    private Expression expression = null;
136
    private GeometryExpressionBuilder expressionBuilder = null;
137

    
138
    public MustNotOverlapPolygonRule(
139
            TopologyPlan plan,
140
            TopologyRuleFactory factory,
141
            double tolerance,
142
            String dataSet1
143
    ) {
144
        super(plan, factory, tolerance, dataSet1);
145
        this.addAction(new CreateFetureAction());
146
        this.addAction(new MergeAction());
147
        this.addAction(new SubtractAction());
148
    }
149

    
150
    @Override
151
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
152
        try {
153
            if (this.expression == null) {
154
                this.expression = ExpressionUtils.createExpression();
155
                this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
156
                this.geomName = feature1.getType().getDefaultGeometryAttributeName();
157
            }
158
            Geometry polygon = feature1.getDefaultGeometry();
159
            if( polygon==null ) {
160
                return;
161
            }
162
            TopologyDataSet theDataSet = this.getDataSet1();
163
            if (theDataSet.getSpatialIndex() != null) {
164
                for (FeatureReference reference : theDataSet.query(polygon)) {
165
                    if (reference.equals(feature1.getReference())) {
166
                        continue;
167
                    }
168
                    Feature feature = reference.getFeature();
169
                    Geometry otherPolygon = feature.getDefaultGeometry();
170
                    if (otherPolygon!=null && polygon.overlaps(otherPolygon)) {;
171
                        Geometry error = polygon.intersection(otherPolygon);
172
                        report.addLine(this,
173
                                theDataSet,
174
                                null,
175
                                polygon,
176
                                error,
177
                                feature1.getReference(),
178
                                null,
179
                                false,
180
                                "The polygon overlay with others."
181
                        );
182
                        break;
183
                    }
184
                }
185
            } else {
186
                this.expression.setPhrase(
187
                        this.expressionBuilder.ifnull(
188
                                this.expressionBuilder.column(this.geomName),
189
                                this.expressionBuilder.constant(false),
190
                                this.expressionBuilder.ST_Overlaps(
191
                                        this.expressionBuilder.column(this.geomName),
192
                                        this.expressionBuilder.geometry(polygon)
193
                                )
194
                        ).toString()
195
                );
196
                Feature feature = theDataSet.findFirst(this.expression);
197
                if ( feature != null) {
198
                    Geometry otherPolygon = feature.getDefaultGeometry();
199
                    Geometry error = null;
200
                    if( otherPolygon!=null ) {
201
                        error = polygon.difference(otherPolygon);
202
                    }
203
                    report.addLine(this,
204
                            theDataSet,
205
                            null,
206
                            polygon,
207
                            error,
208
                            feature1.getReference(),
209
                            null,
210
                            false,
211
                            "The polygon overlay with others."
212
                    );
213
                }
214
            }
215
        } catch (Exception ex) {
216
            LOGGER.warn("Can't check feature.", ex);
217
        } finally {
218
        }
219
    }
220

    
221
}