Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src / org / gvsig / topology / topologyrules / jtsisvalidrules / PolygonMustNotHaveHoles.java @ 18995

History | View | Annotate | Download (4.45 KB)

1 14442 azabala
/*
2
 * Created on 10-abr-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45 16256 azabala
 *
46
 * $Id:
47
 * $Log:
48
 */
49 14442 azabala
package org.gvsig.topology.topologyrules.jtsisvalidrules;
50
51 16256 azabala
import org.gvsig.fmap.core.FGeometryUtil;
52 14473 azabala
import org.gvsig.jts.JtsUtil;
53 14442 azabala
import org.gvsig.topology.AbstractTopologyRule;
54
import org.gvsig.topology.Messages;
55 16256 azabala
import org.gvsig.topology.Topology;
56 14473 azabala
import org.gvsig.topology.TopologyError;
57 14442 azabala
import org.gvsig.topology.TopologyRuleDefinitionException;
58 16256 azabala
import org.gvsig.topology.WrongLyrForTopologyException;
59 18995 azabala
import org.gvsig.topology.topologyrules.JtsValidRule;
60 14442 azabala
61 16256 azabala
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
62 14442 azabala
import com.iver.cit.gvsig.fmap.core.IFeature;
63 16256 azabala
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64 14473 azabala
import com.vividsolutions.jts.geom.Geometry;
65
import com.vividsolutions.jts.geom.GeometryCollection;
66
import com.vividsolutions.jts.geom.MultiPolygon;
67
import com.vividsolutions.jts.geom.Polygon;
68 14442 azabala
69 16256 azabala
public class PolygonMustNotHaveHoles extends AbstractTopologyRule {
70
71 18995 azabala
        JtsValidRule parentRule;
72
73 16256 azabala
        public PolygonMustNotHaveHoles(Topology topology, FLyrVect lyr) {
74
                super(topology, lyr);
75
        }
76
77
        public PolygonMustNotHaveHoles(FLyrVect lyr) {
78
                super(lyr);
79
        }
80
81 16396 azabala
        public PolygonMustNotHaveHoles(){}
82 16256 azabala
83 18253 azabala
        public String getName() {
84 16256 azabala
                return Messages.getText("POLYGON_MUST_NOT_HAVE_DUPLICATED_RINGS");
85
        }
86
87
        public void checkPreconditions() throws TopologyRuleDefinitionException {
88
                int shapeType;
89
                try {
90
                        shapeType = this.originLyr.getShapeType();
91
                        int numDimensions = FGeometryUtil.getDimensions(shapeType);
92
                        if (numDimensions != 2)
93
                                throw new WrongLyrForTopologyException(
94
                                                "MustNotHaveHoles solo aplica sobre capas de dimension 2");
95
                } catch (ReadDriverException e) {
96
                        throw new TopologyRuleDefinitionException(
97
                                        "Error al tratar de verificar el tipo de geometria");
98 14442 azabala
                }
99 16256 azabala
        }
100
101
        public void validateFeature(IFeature feature) {
102
                Geometry jtsGeo = feature.getGeometry().toJTSGeometry();
103
                if (jtsGeo instanceof Polygon) {
104
                        Polygon polygon = (Polygon) jtsGeo;
105
                        if (polygon.getNumInteriorRing() > 0) {
106
                                createTopologyError(feature);
107
                        }
108
                } else if (jtsGeo instanceof MultiPolygon) {
109
                        MultiPolygon multiPoly = (MultiPolygon) jtsGeo;
110
                        for (int i = 0; i < multiPoly.getNumGeometries(); i++) {
111
                                Polygon polygon = (Polygon) multiPoly.getGeometryN(i);
112
                                if (polygon.getNumInteriorRing() > 0) {
113 14473 azabala
                                        createTopologyError(feature);
114
                                }
115 16256 azabala
                        }
116
                } else if (jtsGeo instanceof GeometryCollection) {
117
                        MultiPolygon multiPoly = JtsUtil
118
                                        .convertIfPossible((GeometryCollection) jtsGeo);
119
                        for (int i = 0; i < multiPoly.getNumGeometries(); i++) {
120
                                Polygon polygon = (Polygon) multiPoly.getGeometryN(i);
121
                                if (polygon.getNumInteriorRing() > 0) {
122
                                        createTopologyError(feature);
123 14473 azabala
                                }
124
                        }
125
                }
126 16256 azabala
        }
127
128
        private void createTopologyError(IFeature feature) {
129 18995 azabala
                AbstractTopologyRule violatedRule = null;
130
                if(this.parentRule == null)
131
                        violatedRule = parentRule;
132
                else
133
                        violatedRule = this;
134 16320 azabala
                TopologyError error =
135 18995 azabala
                        new TopologyError(feature.getGeometry(), violatedRule, feature, topology);
136 16256 azabala
                addTopologyError(error);
137
        }
138 18995 azabala
139
        public JtsValidRule getParentRule() {
140
                return parentRule;
141
        }
142
143
        public void setParentRule(JtsValidRule parentRule) {
144
                this.parentRule = parentRule;
145
        }
146 16256 azabala
}