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 / PolygonMustBeCoveredByPolygonRule.java @ 4565

History | View | Annotate | Download (8.04 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2021 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.EditableFeature;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.dal.feature.FeatureReference;
33
import org.gvsig.fmap.dal.feature.FeatureSet;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynObject;
38
import org.gvsig.tools.i18n.I18nManager;
39
import org.gvsig.tools.task.SimpleTaskStatus;
40
import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException;
41
import org.gvsig.topology.lib.api.TopologyDataSet;
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 PolygonMustBeCoveredByPolygonRule extends AbstractTopologyRule {
55

    
56
    private class CreateFetureAction extends AbstractTopologyRuleAction {
57

    
58
        public CreateFetureAction() {
59
            super(
60
                    PolygonContainsPointRuleFactory.NAME,
61
                    "CreateFeature",
62
                    "Create Feature",
63
                    "The Create Feature fix creates a new polygon feature out of the portion of overlap from the existing polygon so the boundary of each feature from both feature classes is the same. This fix can be applied to one or more selected Must Be Covered By errors."
64
            );
65
        }
66

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

    
73
                EditableFeature feature = dataSet.createNewFeature();
74
                feature.setDefaultGeometry(polygon);
75
                dataSet.insert(feature);
76
                return EXECUTE_OK;
77
            } catch (Exception ex) {
78
                throw new ExecuteTopologyRuleActionException(ex);
79
            }
80
        }
81

    
82
    }
83
    
84
    private String geomName;
85
    private Expression expression = null;
86
    private GeometryExpressionBuilder expressionBuilder = null;
87

    
88
    public PolygonMustBeCoveredByPolygonRule() {
89
        // for persistence only
90
    }
91

    
92
    public PolygonMustBeCoveredByPolygonRule(
93
            TopologyRuleFactory factory,
94
            double tolerance,
95
            String dataSet1,
96
            String dataSet2
97
    ) {
98
        super(factory, tolerance, dataSet1, dataSet2);
99
        
100
        addAction(new CreateFetureAction());
101

    
102
    }
103

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

    
191
}