Revision 8415

View differences:

org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/GeometryMustNotBeNullRule.java
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.fmap.dal.feature.Feature;
27
import org.gvsig.fmap.dal.feature.FeatureSet;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.i18n.I18nManager;
30
import org.gvsig.tools.task.SimpleTaskStatus;
31
import org.gvsig.topology.lib.api.TopologyReport;
32
import org.gvsig.topology.lib.api.TopologyRuleFactory;
33
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
34

  
35
/**
36
 *
37
 * @author jjdelcerro
38
 */
39
@SuppressWarnings("UseSpecificCatch")
40
public class GeometryMustNotBeNullRule extends AbstractTopologyRule {
41

  
42
    public GeometryMustNotBeNullRule() {
43
        // for persistence only
44
    }
45
    
46

  
47
    public GeometryMustNotBeNullRule(
48
            TopologyRuleFactory factory,
49
            double tolerance,
50
            String dataSet1,
51
            String dataSet2
52
    ) {
53
        super(factory, tolerance, dataSet1, dataSet2);
54
    }
55

  
56
    @Override
57
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
58
        FeatureSet set = null;
59
        try {
60
            if ( feature1.getDefaultGeometry()==null ) {
61
                I18nManager i18n = ToolsLocator.getI18nManager();
62
                report.addLine(this,
63
                        this.getDataSet1(),
64
                        this.getDataSet2(),
65
                        feature1.getDefaultGeometry(),
66
                        null,
67
                        feature1.getReference(),
68
                        null,
69
                        false,
70
                        i18n.getTranslation("_The_geometry_is_null")
71
                );
72
            }
73
        } catch(Exception ex) {
74
            LOGGER.warn("Can't check feature.", ex);
75
            addCodeException(report, feature1, ex);
76
        } finally {
77
            if( set!=null ) {
78
                set.dispose();
79
            }
80
        }
81
    }
82

  
83
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/PolygonContainsPointRuleFactory.java
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.fmap.geom.Geometry;
27
import org.gvsig.json.Json;
28
import org.gvsig.tools.util.ListBuilder;
29
import org.gvsig.topology.lib.api.TopologyLocator;
30
import org.gvsig.topology.lib.api.TopologyManager;
31
import org.gvsig.topology.lib.api.TopologyRule;
32
import org.gvsig.topology.lib.spi.AbstractTopologyRuleFactory;
33

  
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
@SuppressWarnings("UseSpecificCatch")
39
public class PolygonContainsPointRuleFactory extends AbstractTopologyRuleFactory {
40
    
41
    public static final String NAME = "PolygonContainsPoint";
42
    
43
    public PolygonContainsPointRuleFactory() {
44
        super(
45
                NAME, 
46
                "Contains Point", 
47
                "Requires that a polygon in one dataset contain at least one point from another dataset. Points must be within the polygon, not on the boundary. ", 
48
                new ListBuilder<Integer>()
49
                        .add(Geometry.TYPES.SURFACE)
50
                        .add(Geometry.TYPES.MULTISURFACE)
51
                        .asList(),
52
                new ListBuilder<Integer>()
53
                        .add(Geometry.TYPES.POINT)
54
                        .asList()
55
        );
56
    }
57
    
58
    @Override
59
    public TopologyRule createRule(String dataSet1, String dataSet2, double tolerance) {
60
        TopologyRule rule = new PolygonContainsPointRule(this, tolerance, dataSet1, dataSet2);
61
        return rule;
62
    }    
63

  
64
    public static void selfRegister() {
65
        try {
66
            TopologyManager manager = TopologyLocator.getTopologyManager();
67
            manager.addRuleFactories(new PolygonContainsPointRuleFactory());
68
            Json.registerSerializer(PolygonContainsPointRule.class);
69
        } catch(Exception ex) {
70
            LOGGER.warn("Can't register topology rule from "+PolygonContainsPointRuleFactory.class.getSimpleName(), ex);
71
        }
72
    }
73
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/PolygonContainsOnePointRuleFactory.java
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.fmap.geom.Geometry;
27
import org.gvsig.json.Json;
28
import org.gvsig.tools.util.ListBuilder;
29
import org.gvsig.topology.lib.api.TopologyLocator;
30
import org.gvsig.topology.lib.api.TopologyManager;
31
import org.gvsig.topology.lib.api.TopologyRule;
32
import org.gvsig.topology.lib.spi.AbstractTopologyRuleFactory;
33

  
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
@SuppressWarnings("UseSpecificCatch")
39
public class PolygonContainsOnePointRuleFactory extends AbstractTopologyRuleFactory {
40
    
41
    public static final String NAME = "PolygonContainsOnePoint";
42
    
43
    public PolygonContainsOnePointRuleFactory() {
44
        super(
45
                NAME, 
46
                "Contains One Point", 
47
                "Requires that a polygon in one dataset contain at least one point from another dataset. Points must be within the polygon, not on the boundary. ", 
48
                new ListBuilder<Integer>()
49
                        .add(Geometry.TYPES.SURFACE)
50
                        .add(Geometry.TYPES.MULTISURFACE)
51
                        .asList(),
52
                new ListBuilder<Integer>()
53
                        .add(Geometry.TYPES.POINT)
54
                        .asList()
55
        );
56
    }
57
    
58
    @Override
59
    public TopologyRule createRule(String dataSet1, String dataSet2, double tolerance) {
60
        TopologyRule rule = new PolygonContainsOnePointRule(this, tolerance, dataSet1, dataSet2);
61
        return rule;
62
    }    
63

  
64
    public static void selfRegister() {
65
        try {
66
            TopologyManager manager = TopologyLocator.getTopologyManager();
67
            manager.addRuleFactories(new PolygonContainsOnePointRuleFactory());
68
            Json.registerSerializer(PolygonContainsOnePointRule.class);
69
        } catch(Exception ex) {
70
            LOGGER.warn("Can't register topology rule from "+PolygonContainsOnePointRuleFactory.class.getSimpleName(), ex);
71
        }
72
    }
73
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/PolygonContainsPolygonRule.java
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.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureReference;
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.tools.ToolsLocator;
36
import org.gvsig.tools.i18n.I18nManager;
37
import org.gvsig.tools.task.SimpleTaskStatus;
38
import org.gvsig.topology.lib.api.TopologyDataSet;
39
import org.gvsig.topology.lib.api.TopologyReport;
40
import org.gvsig.topology.lib.api.TopologyRuleFactory;
41
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
42

  
43
/**
44
 *
45
 * @author jjdelcerro
46
 */
47
@SuppressWarnings("UseSpecificCatch")
48
public class PolygonContainsPolygonRule extends AbstractTopologyRule {
49

  
50
    private String geomName;
51
    private Expression expression = null;
52
    private GeometryExpressionBuilder expressionBuilder = null;
53

  
54
    public PolygonContainsPolygonRule() {
55
        // for persistence only
56
    }
57

  
58
    public PolygonContainsPolygonRule(
59
            TopologyRuleFactory factory,
60
            double tolerance,
61
            String dataSet1,
62
            String dataSet2
63
    ) {
64
        super(factory, tolerance, dataSet1, dataSet2);
65

  
66
    }
67

  
68
    @Override
69
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
70
        FeatureSet set = null;
71
        try {
72
            FeatureStore store2 = this.getDataSet2().getFeatureStore();
73
            if (this.expression == null) {
74
                this.expression = ExpressionUtils.createExpression();
75
                this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
76
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
77
            }
78
            double theTolerance = getTolerance();
79
            Geometry polygon = feature1.getDefaultGeometry();
80
            if(theTolerance > 0){
81
                polygon = polygon.buffer(theTolerance);
82
            }
83
            TopologyDataSet theDataSet = this.getDataSet2();
84
            if (theDataSet.getSpatialIndex() != null) {
85
                boolean contains = false;
86
                for (FeatureReference featureReference : theDataSet.query(polygon)) {
87
                    Feature feature2 = featureReference.getFeature();
88
                    Geometry otherPolygon = feature2.getDefaultGeometry();
89
                    if( otherPolygon!=null && polygon.contains(otherPolygon) ) {
90
                        contains = true;
91
                        break;
92
                    }
93
                }
94
                if( !contains ) {
95
                    I18nManager i18n = ToolsLocator.getI18nManager();
96
                    report.addLine(this,
97
                            this.getDataSet1(),
98
                            this.getDataSet2(),
99
                            polygon,
100
                            polygon,
101
                            feature1.getReference(),
102
                            null,
103
                            false,
104
                            i18n.getTranslation("_The_polygon_does_not_contain_any_polygons")
105
                    );
106
                }
107
            } else {
108
                this.expression.setPhrase(
109
                        this.expressionBuilder.ifnull(
110
                                this.expressionBuilder.column(this.geomName),
111
                                this.expressionBuilder.constant(false),
112
                                this.expressionBuilder.ST_Contains(
113
                                        this.expressionBuilder.geometry(polygon),
114
                                        this.expressionBuilder.column(this.geomName)
115
                                )
116
                        ).toString()
117
                );
118
                if (theDataSet.findFirst(this.expression) == null) {
119
                    polygon = feature1.getDefaultGeometry();
120
                    I18nManager i18n = ToolsLocator.getI18nManager();
121
                    report.addLine(this,
122
                            this.getDataSet1(),
123
                            this.getDataSet2(),
124
                            polygon,
125
                            polygon,
126
                            feature1.getReference(),
127
                            null,
128
                            false,
129
                            i18n.getTranslation("_The_polygon_does_not_contain_any_polygons")
130
                    );
131
                }
132
            }
133
        } catch (Exception ex) {
134
            LOGGER.warn("Can't check feature.", ex);
135
            addCodeException(report, feature1, ex);
136
        } finally {
137
            if (set != null) {
138
                set.dispose();
139
            }
140
        }
141
    }
142

  
143
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/PolygonContainsPointRule.java
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.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.TopologyReport;
44
import org.gvsig.topology.lib.api.TopologyReportLine;
45
import org.gvsig.topology.lib.api.TopologyRule;
46
import org.gvsig.topology.lib.api.TopologyRuleFactory;
47
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
48
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
49

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

  
57
    private class CreateFetureAction extends AbstractTopologyRuleAction {
58

  
59
        public CreateFetureAction() {
60
            super(
61
                    PolygonContainsPointRuleFactory.NAME,
62
                    "CreateFeature",
63
                    "Create Feature",
64
                    "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."
65
            );
66
        }
67

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

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

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

  
88
    }
89

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

  
94
    public PolygonContainsPointRule() {
95
        // for persistence only
96
    }
97

  
98
    public PolygonContainsPointRule(
99
            TopologyRuleFactory factory,
100
            double tolerance,
101
            String dataSet1,
102
            String dataSet2
103
    ) {
104
        super(factory, tolerance, dataSet1, dataSet2);
105

  
106
        this.addAction(new CreateFetureAction());
107
    }
108

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

  
185
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/PointMustBeProperlyInsidePolygonRuleFactory.java
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.fmap.geom.Geometry;
27
import org.gvsig.json.Json;
28
import org.gvsig.tools.util.ListBuilder;
29
import org.gvsig.topology.lib.api.TopologyLocator;
30
import org.gvsig.topology.lib.api.TopologyManager;
31
import org.gvsig.topology.lib.spi.AbstractTopologyRuleFactory;
32
import org.gvsig.topology.lib.api.TopologyRule;
33

  
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
@SuppressWarnings("UseSpecificCatch")
39
public class PointMustBeProperlyInsidePolygonRuleFactory extends AbstractTopologyRuleFactory {
40
    
41
    public static final String NAME = "PointMustBeProperlyInsidePolygon";
42
    
43
    public PointMustBeProperlyInsidePolygonRuleFactory() {
44
        super(
45
                NAME, 
46
                "Must Be Properly Inside", 
47
                "Requires that points fall within area features. This is useful when the point features are related to polygons, such as wells and well pads or address points and parcels.", 
48
                new ListBuilder<Integer>()
49
                        .add(Geometry.TYPES.POINT)
50
                        .asList(),
51
                new ListBuilder<Integer>()
52
                        .add(Geometry.TYPES.SURFACE)
53
                        .add(Geometry.TYPES.MULTISURFACE)
54
                        .asList()
55
        );
56
    }
57
    
58
    @Override
59
    public TopologyRule createRule(String dataSet1, String dataSet2, double tolerance) {
60
        TopologyRule rule = new PointMustBeProperlyInsidePolygonRule(this, tolerance, dataSet1, dataSet2);
61
        return rule;
62
    }    
63

  
64
    public static void selfRegister() {
65
        try {
66
            TopologyManager manager = TopologyLocator.getTopologyManager();
67
            manager.addRuleFactories(new PointMustBeProperlyInsidePolygonRuleFactory());
68
            Json.registerSerializer(PointMustBeProperlyInsidePolygonRule.class);
69
        } catch(Exception ex) {
70
            LOGGER.warn("Can't register topology rule from PointMustBeProperlyInsidePolygonRuleFactory.", ex);
71
        }
72
    }
73
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/PolygonContainsOnePointRule.java
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.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureReference;
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.tools.ToolsLocator;
36
import org.gvsig.tools.i18n.I18nManager;
37
import org.gvsig.tools.task.SimpleTaskStatus;
38
import org.gvsig.tools.visitor.VisitCanceledException;
39
import org.gvsig.topology.lib.api.TopologyDataSet;
40
import org.gvsig.topology.lib.api.TopologyReport;
41
import org.gvsig.topology.lib.api.TopologyRuleFactory;
42
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
43

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

  
51
    private String geomName;
52
    private Expression expression = null;
53
    private GeometryExpressionBuilder expressionBuilder = null;
54

  
55
    public PolygonContainsOnePointRule() {
56
        // for persistence only
57
    }
58

  
59
    public PolygonContainsOnePointRule(
60
            TopologyRuleFactory factory,
61
            double tolerance,
62
            String dataSet1,
63
            String dataSet2
64
    ) {
65
        super(factory, tolerance, dataSet1, dataSet2);
66
    }
67
    
68
    @Override
69
    public long getSteps() {
70
        return this.getDataSet1().getSize()+this.getDataSet2().getSize();
71
    }
72

  
73
    @Override
74
    public void execute(final SimpleTaskStatus taskStatus, final TopologyReport report) {
75
        try {
76
            this.getDataSet1().accept((final Object o1) -> {
77
                if (taskStatus.isCancellationRequested()) {
78
                    throw new VisitCanceledException();
79
                }
80
                taskStatus.incrementCurrentValue();
81
                try {
82
                    check(taskStatus, report, (Feature) o1);
83
                } catch (Exception ex) {
84
                    throw new RuntimeException(ex);
85
                }
86
            });
87
            this.getDataSet2().accept((final Object o1) -> {
88
                if (taskStatus.isCancellationRequested()) {
89
                    throw new VisitCanceledException();
90
                }
91
                taskStatus.incrementCurrentValue();
92
                try {
93
                    check2(taskStatus, report, (Feature) o1);
94
                } catch (Exception ex) {
95
                    throw new RuntimeException(ex);
96
                }
97
            });
98
        } catch (VisitCanceledException ex) {
99
            // return;
100
        }
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
            double theTolerance = getTolerance();
116
            if(theTolerance > 0){
117
                polygon = polygon.buffer(theTolerance);
118
            }
119

  
120
            TopologyDataSet theDataSet = this.getDataSet2();
121
            int contained = 0;
122
            if (theDataSet.getSpatialIndex() != null) {
123
                for (FeatureReference featureReference : theDataSet.query(polygon)) {
124
                    Feature feature2 = featureReference.getFeature();
125
                    Geometry otherPoint = feature2.getDefaultGeometry();
126
                    if (otherPoint != null && polygon.contains(otherPoint)) {
127
                        contained++;
128
                        break;
129
                    }
130
                }
131

  
132
            } else {
133
                this.expression.setPhrase(
134
                        this.expressionBuilder.ifnull(
135
                                this.expressionBuilder.column(this.geomName),
136
                                this.expressionBuilder.constant(false),
137
                                this.expressionBuilder.ST_Contains(
138
                                        this.expressionBuilder.geometry(polygon),
139
                                        this.expressionBuilder.column(this.geomName)
140
                                )
141
                        ).toString()
142
                );
143

  
144
                FeatureSet featSet = theDataSet.getFeatureStore().getFeatureSet(this.expression);
145
                contained = featSet.size();
146
            }
147
            if (contained <= 0) {
148
                I18nManager i18n = ToolsLocator.getI18nManager();
149
                report.addLine(this,
150
                        this.getDataSet1(),
151
                        this.getDataSet2(),
152
                        polygon,
153
                        polygon,
154
                        feature1.getReference(),
155
                        null,
156
                        false,
157
                        i18n.getTranslation("_The_polygon_does_not_contain_any_points")
158
                );
159
            } else if (contained > 1) {
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_contains_more_than_a_point")
170
                );
171
            }
172
        } catch (Exception ex) {
173
            LOGGER.warn("Can't check feature.", ex);
174
            addCodeException(report, feature1, ex);
175
        } finally {
176
            if (set != null) {
177
                set.dispose();
178
            }
179
        }
180
    }
181

  
182
    protected void check2(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature2) throws Exception {
183
        FeatureSet set = null;
184
        try {
185
            TopologyDataSet theDataSet2 = this.getDataSet2();
186
            TopologyDataSet theDataSet1 = this.getDataSet1();
187
            FeatureStore otherStore = theDataSet1.getFeatureStore();
188
            if (this.expression == null) {
189
                this.expression = ExpressionUtils.createExpression();
190
                this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
191
                this.geomName = otherStore.getDefaultFeatureType().getDefaultGeometryAttributeName();
192
            }
193
            Geometry point = feature2.getDefaultGeometry();
194
            if (theDataSet1.getSpatialIndex() != null) {
195
                boolean contained = false;
196
                for (FeatureReference otherFeatureReference : theDataSet1.query(point)) {
197
                    Feature otherFeature = otherFeatureReference.getFeature();
198
                    Geometry otherPolygon = otherFeature.getDefaultGeometry();
199
                    if( otherPolygon!=null && otherPolygon.contains(point) ) {
200
                        contained = true;
201
                        break;
202
                    }
203
                }
204
                if( !contained ) {
205
                    I18nManager i18n = ToolsLocator.getI18nManager();
206
                    report.addLine(this,
207
                            theDataSet1,
208
                            theDataSet2,
209
                            point,
210
                            point,
211
                            null,
212
                            feature2.getReference(),
213
                            false,
214
                            i18n.getTranslation("_Point_is_not_contained_in_a_polygon")
215
                    );
216
                }
217
            } else {
218
                this.expression.setPhrase(
219
                        this.expressionBuilder.ifnull(
220
                                this.expressionBuilder.column(this.geomName),
221
                                this.expressionBuilder.constant(false),
222
                                this.expressionBuilder.ST_Contains(
223
                                        this.expressionBuilder.column(this.geomName),
224
                                        this.expressionBuilder.geometry(point)
225
                                )
226
                        ).toString()
227
                );
228
                if (theDataSet1.findFirst(this.expression) == null) {
229
                    I18nManager i18n = ToolsLocator.getI18nManager();
230
                    report.addLine(this,
231
                            theDataSet1,
232
                            theDataSet2,
233
                            point,
234
                            point,
235
                            null,
236
                            feature2.getReference(),
237
                            false,
238
                            i18n.getTranslation("_Point_is_not_contained_in_a_polygon")
239
                    );
240
                }
241
            }
242
        } catch (Exception ex) {
243
            LOGGER.warn("Can't check feature.", ex);
244
            addCodeException(report, feature2, ex);
245
        } finally {
246
            if (set != null) {
247
                set.dispose();
248
            }
249
        }
250
    }
251
    
252
    
253

  
254
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/LineMustBeCoveredByBoundaryOfPolygonRuleFactory.java
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.fmap.geom.Geometry;
27
import org.gvsig.json.Json;
28
import org.gvsig.tools.util.ListBuilder;
29
import org.gvsig.topology.lib.api.TopologyLocator;
30
import org.gvsig.topology.lib.api.TopologyManager;
31
import org.gvsig.topology.lib.api.TopologyRule;
32
import org.gvsig.topology.lib.spi.AbstractTopologyRuleFactory;
33

  
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
@SuppressWarnings("UseSpecificCatch")
39
public class LineMustBeCoveredByBoundaryOfPolygonRuleFactory extends AbstractTopologyRuleFactory {
40
    
41
    public static final String NAME = "LineMustBeCoveredByBoundaryOfPolygon";
42
    
43
    public LineMustBeCoveredByBoundaryOfPolygonRuleFactory() {
44
        super(
45
                NAME, 
46
                "Must Be Covered By Boundary Of", 
47
                "Requires that lines be covered by the boundaries of area features. This is useful for modeling lines, such as lot lines, that must coincide with the edge of polygon features, such as lots.", 
48
                new ListBuilder<Integer>()
49
                        .add(Geometry.TYPES.CURVE)
50
                        .add(Geometry.TYPES.MULTICURVE)
51
                        .asList(),
52
                new ListBuilder<Integer>()
53
                        .add(Geometry.TYPES.SURFACE)
54
                        .add(Geometry.TYPES.MULTISURFACE)
55
                        .asList()
56
        );
57
    }
58
    
59
    @Override
60
    public TopologyRule createRule(String dataSet1, String dataSet2, double tolerance) {
61
        TopologyRule rule = new LineMustBeCoveredByBoundaryOfPolygonRule(this, tolerance, dataSet1, dataSet2);
62
        return rule;
63
    }    
64

  
65
    public static void selfRegister() {
66
        try {
67
            TopologyManager manager = TopologyLocator.getTopologyManager();
68
            manager.addRuleFactories(new LineMustBeCoveredByBoundaryOfPolygonRuleFactory());
69
            Json.registerSerializer(LineMustBeCoveredByBoundaryOfPolygonRule.class);
70
        } catch(Exception ex) {
71
            LOGGER.warn("Can't register topology rule from "+LineMustBeCoveredByBoundaryOfPolygonRuleFactory.class.getSimpleName(), ex);
72
        }
73
    }
74
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/LineMustBeLargerThanToleranceRuleFactory.java
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.fmap.geom.Geometry;
27
import org.gvsig.json.Json;
28
import org.gvsig.tools.util.ListBuilder;
29
import org.gvsig.topology.lib.api.TopologyLocator;
30
import org.gvsig.topology.lib.api.TopologyManager;
31
import org.gvsig.topology.lib.api.TopologyRule;
32
import org.gvsig.topology.lib.spi.AbstractTopologyRuleFactory;
33

  
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
@SuppressWarnings("UseSpecificCatch")
39
public class LineMustBeLargerThanToleranceRuleFactory extends AbstractTopologyRuleFactory {
40

  
41
    public static final String NAME = "LineMustBeLargerThanTolerance";
42
    
43
    public LineMustBeLargerThanToleranceRuleFactory() {
44
        super(
45
                NAME, 
46
                "Must Be Larger Than Tolerance", 
47
                "Requires that a feature does not collapse during a validate process. This rule is mandatory for a topology and applies to all line feature classes. In instances where this rule is violated, the original geometry is left unchanged", 
48
                new ListBuilder<Integer>()
49
                        .add(Geometry.TYPES.CURVE)
50
                        .add(Geometry.TYPES.MULTICURVE)
51
                        .asList()
52
        );
53
    }
54
    
55
    @Override
56
    public TopologyRule createRule(String dataSet1, String dataSet2, double tolerance) {
57
        TopologyRule rule = new LineMustBeLargerThanToleranceRule( this, tolerance, dataSet1, dataSet2);
58
        return rule;
59
    }
60
    
61
    public static void selfRegister() {
62
        try {
63
            TopologyManager manager = TopologyLocator.getTopologyManager();
64
            manager.addRuleFactories(new LineMustBeLargerThanToleranceRuleFactory());
65
            Json.registerSerializer(LineMustBeLargerThanToleranceRule.class);
66
        } catch(Exception ex) {
67
            LOGGER.warn("Can't register topology rule from "+LineMustBeLargerThanToleranceRuleFactory.class.getSimpleName(), ex);
68
        }
69
    }
70
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/PolygonMustNotOverlapWithPolygonRuleFactory.java
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.fmap.geom.Geometry;
27
import org.gvsig.json.Json;
28
import org.gvsig.tools.util.ListBuilder;
29
import org.gvsig.topology.lib.api.TopologyLocator;
30
import org.gvsig.topology.lib.api.TopologyManager;
31
import org.gvsig.topology.lib.api.TopologyRule;
32
import org.gvsig.topology.lib.spi.AbstractTopologyRuleFactory;
33

  
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
@SuppressWarnings("UseSpecificCatch")
39
public class PolygonMustNotOverlapWithPolygonRuleFactory extends AbstractTopologyRuleFactory {
40

  
41
    public static final String NAME = "PolygonMustNotOverlapWithPolygon";
42
    
43
    public PolygonMustNotOverlapWithPolygonRuleFactory() {
44
        super(
45
                NAME, 
46
                "Must Not Overlap With", 
47
                "Requires that the interior of polygons in one feature class (or subtype) must not overlap with the interior of polygons in another feature class (or subtype). Polygons of the two feature classes can share edges or vertices or be completely disjointed. This rule is used when an area cannot belong to two separate feature classes. It is useful for combining two mutually exclusive systems of area classification, such as zoning and water body type, where areas defined within the zoning class cannot also be defined in the water body class and vice versa.", 
48
                new ListBuilder<Integer>()
49
                        .add(Geometry.TYPES.SURFACE)
50
                        .add(Geometry.TYPES.MULTISURFACE)
51
                        .asList(),
52
                new ListBuilder<Integer>()
53
                        .add(Geometry.TYPES.SURFACE)
54
                        .add(Geometry.TYPES.MULTISURFACE)
55
                        .asList()
56

  
57
                
58
        );
59
    }
60
    
61
    @Override
62
    public TopologyRule createRule(String dataSet1, String dataSet2, double tolerance) {
63
        TopologyRule rule = new PolygonMustNotOverlapWithPolygonRule(this, tolerance, dataSet1, dataSet2);
64
        return rule;
65
    }    
66

  
67
    public static void selfRegister() {
68
        try {
69
            TopologyManager manager = TopologyLocator.getTopologyManager();
70
            manager.addRuleFactories(new PolygonMustNotOverlapWithPolygonRuleFactory());
71
            Json.registerSerializer(PolygonMustNotOverlapWithPolygonRule.class);
72
        } catch(Exception ex) {
73
            LOGGER.warn("Can't register topology rule from "+PolygonMustNotOverlapWithPolygonRuleFactory.class.getSimpleName(), ex);
74
        }
75
    }
76
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/GeometryIsValidFactory.java
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.fmap.geom.Geometry;
27
import org.gvsig.json.Json;
28
import org.gvsig.tools.util.ListBuilder;
29
import org.gvsig.topology.lib.api.TopologyLocator;
30
import org.gvsig.topology.lib.api.TopologyManager;
31
import org.gvsig.topology.lib.api.TopologyRule;
32
import org.gvsig.topology.lib.spi.AbstractTopologyRuleFactory;
33

  
34
/**
35
 *
36
 * @author jjdelcerro
37
 */
38
@SuppressWarnings("UseSpecificCatch")
39
public class GeometryIsValidFactory extends AbstractTopologyRuleFactory {
40
    
41
    public static final String NAME = "GeometryIsValid";
42
    
43
    public GeometryIsValidFactory() {
44
        super(
45
                NAME, 
46
                "Geometry must be valid", 
47
                "Requires that geometries are valid", 
48
                new ListBuilder<Integer>()
49
                        .add(Geometry.TYPES.GEOMETRY)
50
                        .asList(),
51
                null
52
        );
53
    }
54
    
55
    @Override
56
    public TopologyRule createRule(String dataSet1, String dataSet2, double tolerance) {
57
        TopologyRule rule = new GeometryIsValidRule(this, tolerance, dataSet1, dataSet2);
58
        return rule;
59
    }    
60

  
61

  
62
    public static void selfRegister() {
63
        try {
64
            TopologyManager manager = TopologyLocator.getTopologyManager();
65
            manager.addRuleFactories(new GeometryIsValidFactory());
66
            Json.registerSerializer(GeometryIsValidRule.class);
67
        } catch(Exception ex) {
68
            LOGGER.warn("Can't register topology rule from GeometryIsValidRuleFactory.", ex);
69
        }
70
    }
71
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/GeometryIsValidRule.java
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.fmap.dal.feature.Feature;
27
import org.gvsig.fmap.dal.feature.FeatureSet;
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.tools.ToolsLocator;
30
import org.gvsig.tools.i18n.I18nManager;
31
import org.gvsig.tools.task.SimpleTaskStatus;
32
import org.gvsig.topology.lib.api.TopologyReport;
33
import org.gvsig.topology.lib.api.TopologyRuleFactory;
34
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
35

  
36
/**
37
 *
38
 * @author jjdelcerro
39
 */
40
@SuppressWarnings("UseSpecificCatch")
41
public class GeometryIsValidRule extends AbstractTopologyRule {
42

  
43
    public GeometryIsValidRule() {
44
        // for persistence only
45
    }
46
    
47

  
48
    public GeometryIsValidRule(
49
            TopologyRuleFactory factory,
50
            double tolerance,
51
            String dataSet1,
52
            String dataSet2
53
    ) {
54
        super(factory, tolerance, dataSet1, dataSet2);
55
    }
56

  
57
    @Override
58
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
59
        FeatureSet set = null;
60
        try {
61
            Geometry geom = feature1.getDefaultGeometry();
62
            Geometry.ValidationStatus status = geom.getValidationStatus();
63
            if ( !status.isValid() ) {
64
                I18nManager i18n = ToolsLocator.getI18nManager();
65
                report.addLine(this,
66
                        this.getDataSet1(),
67
                        this.getDataSet2(),
68
                        feature1.getDefaultGeometry(),
69
                        status.getProblemLocation(),
70
                        feature1.getReference(),
71
                        null,
72
                        false,
73
                        i18n.getTranslation("_The_geometry_is_not_valid")+". "+status.getMessage()
74
                );
75
            }
76
        } catch(Exception ex) {
77
            LOGGER.warn("Can't check feature.", ex);
78
            addCodeException(report, feature1, ex);
79
        } finally {
80
            if( set!=null ) {
81
                set.dispose();
82
            }
83
        }
84
    }
85

  
86
}
org.gvsig.topology/tags/org.gvsig.topology-1.0.146/org.gvsig.topology.lib/org.gvsig.topology.lib.impl/src/main/java/org/gvsig/topology/rule/PointMustBeProperlyInsidePolygonRule.java
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.Feature;
31
import org.gvsig.fmap.dal.feature.FeatureReference;
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.tools.ToolsLocator;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.i18n.I18nManager;
38
import org.gvsig.tools.task.SimpleTaskStatus;
39
import org.gvsig.topology.lib.api.ExecuteTopologyRuleActionException;
40
import org.gvsig.topology.lib.api.TopologyDataSet;
41
import org.gvsig.topology.lib.api.TopologyReport;
42
import org.gvsig.topology.lib.api.TopologyReportLine;
43
import org.gvsig.topology.lib.api.TopologyRule;
44
import org.gvsig.topology.lib.api.TopologyRuleFactory;
45
import org.gvsig.topology.lib.spi.AbstractTopologyRule;
46
import org.gvsig.topology.lib.spi.AbstractTopologyRuleAction;
47

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

  
55
    private class DeleteAction extends AbstractTopologyRuleAction {
56

  
57
        public DeleteAction() {
58
            super(
59
                    PointMustBeProperlyInsidePolygonRuleFactory.NAME,
60
                    "Delete",
61
                    "Delete",
62
                    " The Delete fix removes point features that are not properly within polygon features. Note that you can use the Edit tool and move the point inside the polygon feature if you do not want to delete it. This fix can be applied to one or more Must Be Properly Inside errors."
63
            );
64
        }
65

  
66
        @Override
67
        public int execute(TopologyRule rule, TopologyReportLine line, DynObject parameters) {
68
            try {
69
                TopologyDataSet dataSet = rule.getDataSet1();
70

  
71
                dataSet.delete(line.getFeature1());
72
                return EXECUTE_OK;
73
            } catch (Exception ex) {
74
                throw new ExecuteTopologyRuleActionException(ex);
75
            }
76
        }
77

  
78
    }
79

  
80
    private String geomName;
81
    private Expression expression = null;
82
    private GeometryExpressionBuilder expressionBuilder = null;
83

  
84
    public PointMustBeProperlyInsidePolygonRule() {
85
        // for persistence only
86
    }
87
    
88
    public PointMustBeProperlyInsidePolygonRule(
89
            TopologyRuleFactory factory,
90
            double tolerance,
91
            String dataSet1,
92
            String dataSet2
93
    ) {
94
        super(factory, tolerance, dataSet1, dataSet2);
95

  
96
        this.addAction(new DeleteAction());
97
    }
98

  
99
    @Override
100
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature1) throws Exception {
101
        FeatureSet set = null;
102
        try {
103
            FeatureStore store2 = this.getDataSet2().getFeatureStore();
104
            if (this.expression == null) {
105
                this.expression = ExpressionUtils.createExpression();
106
                this.expressionBuilder = GeometryExpressionUtils.createExpressionBuilder();
107
                this.geomName = store2.getDefaultFeatureType().getDefaultGeometryAttributeName();
108
            }
109
            double theTolerance = getTolerance();
110
            Geometry point = feature1.getDefaultGeometry();
111
            TopologyDataSet theDataSet2 = this.getDataSet2();
112
            if (theDataSet2.getSpatialIndex() != null) {
113
                boolean contained = false;
114
                for (FeatureReference featureReference : theDataSet2.query(point)) {
115
                    Feature feature2 = featureReference.getFeature();
116
                    Geometry otherPolygon = feature2.getDefaultGeometry();
117
                    if(theTolerance > 0){
118
                        otherPolygon = otherPolygon.buffer(theTolerance);
119
                    }
120
                    if( otherPolygon!=null && otherPolygon.contains(point) ) {
121
                        contained = true;
122
                        break;
123
                    }
124
                }
125
                if( !contained ) {
126
                    I18nManager i18n = ToolsLocator.getI18nManager();
127
                    report.addLine(this,
128
                            this.getDataSet1(),
129
                            this.getDataSet2(),
130
                            point,
131
                            point,
132
                            feature1.getReference(),
133
                            null,
134
                            false,
135
                            i18n.getTranslation("_Point_is_not_contained_in_a_polygon")
136
                    );
137
                }
138
            } else {
139
                
140
                if (theTolerance > 0) {
141
                    this.expression.setPhrase(
142
                            this.expressionBuilder.ifnull(
143
                                    this.expressionBuilder.column(this.geomName),
144
                                    this.expressionBuilder.constant(false),
145
                                    this.expressionBuilder.ST_Contains(
146
                                            this.expressionBuilder.ST_Buffer(
147
                                                    this.expressionBuilder.column(this.geomName),
148
                                                    this.expressionBuilder.constant(theTolerance)
149
                                            ),
150
                                            this.expressionBuilder.geometry(point)
151
                                    )
152
                            ).toString()
153
                    );
154
                } else {
155
                    this.expression.setPhrase(
156
                            this.expressionBuilder.ifnull(
157
                                    this.expressionBuilder.column(this.geomName),
158
                                    this.expressionBuilder.constant(false),
159
                                    this.expressionBuilder.ST_Contains(
160
                                            this.expressionBuilder.column(this.geomName),
161
                                            this.expressionBuilder.geometry(point)
162
                                    )
163
                            ).toString()
164
                    );
165
                }
166
                if (theDataSet2.findFirst(this.expression) == null) {
167
                    I18nManager i18n = ToolsLocator.getI18nManager();
168
                    report.addLine(this,
169
                            this.getDataSet1(),
170
                            this.getDataSet2(),
171
                            point,
172
                            point,
173
                            feature1.getReference(),
174
                            null,
175
                            false,
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff