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

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

    
45
/**
46
 *
47
 * @author jjdelcerro
48
 */
49
@SuppressWarnings("UseSpecificCatch")
50
public class MustNotOverlapPolygonRule extends AbstractTopologyRule {
51

    
52
    private class CreateFetureAction extends AbstractTopologyRuleAction {
53

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

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

    
77
    }
78

    
79
    private class SubtractAction extends AbstractTopologyRuleAction {
80

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

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

    
103
    }
104

    
105
    private class MergeAction extends AbstractTopologyRuleAction {
106

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

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

    
131
    private String geomName;
132
    private Expression expression = null;
133
    private ExpressionBuilder expressionBuilder = null;
134

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

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

    
219
}