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 / PolygonContainsPointRule.java @ 4483

History | View | Annotate | Download (7.36 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.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.fmap.geom.primitive.Point;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.dynobject.DynObject;
39
import org.gvsig.tools.i18n.I18nManager;
40
import org.gvsig.tools.task.SimpleTaskStatus;
41
import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException;
42
import org.gvsig.topology.lib.api.TopologyDataSet;
43
import org.gvsig.topology.lib.api.TopologyPlan;
44
import org.gvsig.topology.lib.api.TopologyReport;
45
import org.gvsig.topology.lib.api.TopologyReportLine;
46
import org.gvsig.topology.lib.api.TopologyRule;
47
import org.gvsig.topology.lib.api.TopologyRuleFactory;
48
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
49
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
50

    
51
/**
52
 *
53
 * @author jjdelcerro
54
 */
55
@SuppressWarnings("UseSpecificCatch")
56
public class PolygonContainsPointRule extends AbstractTopologyRule {
57

    
58
    private class CreateFetureAction extends AbstractTopologyRuleAction {
59

    
60
        public CreateFetureAction() {
61
            super(
62
                    PolygonContainsPointRuleFactory.NAME,
63
                    "CreateFeature",
64
                    "Create Feature",
65
                    "The Create Feature fix creates a new point feature at the centroid of the polygon feature that is causing the error. The point feature that is created is guaranteed to be within the polygon feature."
66
            );
67
        }
68

    
69
        @Override
70
        public int execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
71
            try {
72
                Geometry polygon = line.getGeometry();
73
                Point point = polygon.centroid();
74
                if( !polygon.contains(point) ) {
75
                    point = polygon.getInteriorPoint();
76
                }
77
                TopologyDataSet dataSet = rule.getDataSet2();
78

    
79
                EditableFeature feature = dataSet.createNewFeature();
80
                feature.setDefaultGeometry(point);
81
                dataSet.insert(feature);
82
                return EXECUTE_OK;
83

    
84
            } catch (Exception ex) {
85
                throw new ExecuteTopologyRuleActionException(ex);
86
            }
87
        }
88

    
89
    }
90

    
91
    private String geomName;
92
    private Expression expression = null;
93
    private GeometryExpressionBuilder expressionBuilder = null;
94

    
95
    public PolygonContainsPointRule(
96
            TopologyPlan plan,
97
            TopologyRuleFactory factory,
98
            double tolerance,
99
            String dataSet1,
100
            String dataSet2
101
    ) {
102
        super(plan, factory, tolerance, dataSet1, dataSet2);
103

    
104
        this.addAction(new CreateFetureAction());
105
    }
106

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

    
182
}