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 / ContainsPointRule.java @ 712

History | View | Annotate | Download (5.38 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.EditableFeature;
31
import org.gvsig.fmap.dal.feature.Feature;
32
import org.gvsig.fmap.dal.feature.FeatureSet;
33
import org.gvsig.fmap.dal.feature.FeatureStore;
34
import org.gvsig.fmap.geom.Geometry;
35
import org.gvsig.fmap.geom.primitive.Point;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
38
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
39
import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException;
40
import org.gvsig.topology.lib.api.TopologyDataSet;
41
import org.gvsig.topology.lib.api.TopologyPlan;
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

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

    
54
    private class CreateFetureAction extends AbstractTopologyRuleAction {
55

    
56
        public CreateFetureAction() {
57
            super(
58
                    ContainsPointRuleFactory.NAME,
59
                    "CreateFeature",
60
                    "Create Feature",
61
                    "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."
62
            );
63
        }
64

    
65
        @Override
66
        public void execute(TopologyRule rule, TopologyReportLine line, DynObject parameters)  {
67
            try {
68
                Point point = line.getGeometry().centroid();
69
                TopologyDataSet dataSet = rule.getDataSet2();
70

    
71
                EditableFeature feature = dataSet.createNewFeature();
72
                feature.setDefaultGeometry(point);
73
                dataSet.insert(feature);
74
                
75
            } catch (Exception ex) {
76
                throw new ExecuteTopologyRuleActionException(ex);
77
            }
78
        }
79

    
80
    }
81

    
82
    private String geomName;
83
    private Expression expression = null;
84
    private ExpressionBuilder expressionBuilder = null;
85
    
86
    public ContainsPointRule(
87
            TopologyPlan plan,
88
            TopologyRuleFactory factory
89
    ) {
90
        super(plan, factory);
91
        this.actions.add(new CreateFetureAction());
92
    }
93
    
94
    public ContainsPointRule(
95
            TopologyPlan plan,
96
            TopologyRuleFactory factory,
97
            double tolerance,
98
            String dataSet1,
99
            String dataSet2
100
    ) {
101
        super(plan, factory, tolerance, dataSet1, dataSet2);
102
        this.actions.add(new CreateFetureAction());
103
    }
104

    
105
    @Override
106
    protected void check(TopologyReport report, Feature feature1, TopologyDataSet dataSet2) throws Exception {
107
        FeatureSet set = null;
108
        try {
109
            FeatureStore store2 = dataSet2.getStore();
110
            if( this.expression == null ) {
111
                ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
112
                this.expression = manager.createExpression();
113
                this.expressionBuilder = manager.createExpressionBuilder();
114
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
115
             }
116
            Geometry polygon = feature1.getDefaultGeometry();
117
            this.expression.setPhrase(
118
                this.expressionBuilder.ST_Intersects(
119
                    this.expressionBuilder.column(this.geomName),
120
                    this.expressionBuilder.geometry(polygon)
121
                ).toString()
122
            );
123
            Feature f = store2.findFirst(this.expression);
124
            if ( f==null ) {
125
                report.addLine(this,
126
                        this.getDataSet1(),
127
                        this.getDataSet2(),
128
                        polygon,
129
                        feature1.getReference(),
130
                        null,
131
                        false,
132
                        "The polygon is an error because it does not contain a point."
133
                );
134
            }
135
        } catch(Exception ex) {
136
            LOGGER.warn("Can't check feature.", ex);
137
        } finally {
138
            if( set!=null ) {
139
                set.dispose();
140
            }
141
        }
142
    }
143

    
144
}