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 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 725 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureReference;
32 688 jjdelcerro
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.tools.dynobject.DynObject;
34 721 jjdelcerro
import org.gvsig.tools.task.SimpleTaskStatus;
35 688 jjdelcerro
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 712 jjdelcerro
public class MustNotOverlapPolygonRule extends AbstractTopologyRule {
51 688 jjdelcerro
52
    private class CreateFetureAction extends AbstractTopologyRuleAction {
53
54
        public CreateFetureAction() {
55
            super(
56 712 jjdelcerro
                    MustNotOverlapPolygonRuleFactory.NAME,
57 688 jjdelcerro
                    "CreateFeature",
58
                    "Create Feature",
59 712 jjdelcerro
                    "The Create Feature fix creates a new polygon feature out "
60 725 jjdelcerro
                    + "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 688 jjdelcerro
            );
66
        }
67
68
        @Override
69 725 jjdelcerro
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
70 688 jjdelcerro
            try {
71 712 jjdelcerro
                // TODO
72
            } catch (Exception ex) {
73
                throw new ExecuteTopologyRuleActionException(ex);
74
            }
75
        }
76 688 jjdelcerro
77 712 jjdelcerro
    }
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 725 jjdelcerro
                    + "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 712 jjdelcerro
            );
92
        }
93
94
        @Override
95 725 jjdelcerro
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
96 712 jjdelcerro
            try {
97
                // TODO
98 688 jjdelcerro
            } catch (Exception ex) {
99
                throw new ExecuteTopologyRuleActionException(ex);
100
            }
101
        }
102
103
    }
104
105 712 jjdelcerro
    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 725 jjdelcerro
                    + "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 712 jjdelcerro
            );
119
        }
120
121
        @Override
122 725 jjdelcerro
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
123 712 jjdelcerro
            try {
124
                // TODO
125
            } catch (Exception ex) {
126
                throw new ExecuteTopologyRuleActionException(ex);
127
            }
128
        }
129
    }
130 725 jjdelcerro
131 688 jjdelcerro
    private String geomName;
132
    private Expression expression = null;
133
    private ExpressionBuilder expressionBuilder = null;
134 725 jjdelcerro
135 712 jjdelcerro
    public MustNotOverlapPolygonRule(
136 688 jjdelcerro
            TopologyPlan plan,
137
            TopologyRuleFactory factory,
138
            double tolerance,
139 712 jjdelcerro
            String dataSet1
140 688 jjdelcerro
    ) {
141 712 jjdelcerro
        super(plan, factory, tolerance, dataSet1);
142 722 jjdelcerro
        this.addAction(new CreateFetureAction());
143
        this.addAction(new MergeAction());
144
        this.addAction(new SubtractAction());
145 688 jjdelcerro
    }
146
147
    @Override
148 721 jjdelcerro
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
149 688 jjdelcerro
        try {
150 725 jjdelcerro
            if (this.expression == null) {
151 688 jjdelcerro
                ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
152
                this.expression = manager.createExpression();
153
                this.expressionBuilder = manager.createExpressionBuilder();
154 712 jjdelcerro
                this.geomName = feature1.getType().getDefaultGeometryAttributeName();
155 725 jjdelcerro
            }
156 688 jjdelcerro
            Geometry polygon = feature1.getDefaultGeometry();
157 725 jjdelcerro
            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 727 jjdelcerro
                    Geometry otherPolygon = feature.getDefaultGeometry();
168
                    if (otherPolygon!=null && polygon.overlaps(otherPolygon)) {;
169
                        Geometry error = otherPolygon.difference(polygon);
170 725 jjdelcerro
                        report.addLine(this,
171
                                theDataSet,
172
                                null,
173
                                polygon,
174 727 jjdelcerro
                                error,
175 725 jjdelcerro
                                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 727 jjdelcerro
                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 722 jjdelcerro
                    report.addLine(this,
202 725 jjdelcerro
                            theDataSet,
203 722 jjdelcerro
                            null,
204
                            polygon,
205 727 jjdelcerro
                            error,
206 722 jjdelcerro
                            feature1.getReference(),
207
                            null,
208
                            false,
209
                            "The polygon overlay with others."
210
                    );
211
                }
212 688 jjdelcerro
            }
213 725 jjdelcerro
        } catch (Exception ex) {
214 688 jjdelcerro
            LOGGER.warn("Can't check feature.", ex);
215
        } finally {
216
        }
217
    }
218
219
}