Statistics
| Revision:

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

History | View | Annotate | Download (4.49 KB)

1
/*
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
 *
46
 * $Id: 
47
 * $Log: 
48
 */
49
package org.gvsig.topology.topologyrules.jtsisvalidrules;
50

    
51
import org.gvsig.fmap.core.FGeometryUtil;
52
import org.gvsig.topology.AbstractTopologyRule;
53
import org.gvsig.topology.Messages;
54
import org.gvsig.topology.Topology;
55
import org.gvsig.topology.TopologyError;
56
import org.gvsig.topology.TopologyRuleDefinitionException;
57
import org.gvsig.topology.topologyrules.JtsValidRule;
58

    
59
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
60
import com.iver.cit.gvsig.fmap.core.FShape;
61
import com.iver.cit.gvsig.fmap.core.IFeature;
62
import com.iver.cit.gvsig.fmap.core.IGeometry;
63
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
64
import com.iver.utiles.XMLEntity;
65

    
66
/**
67
 * This rule checks if an FMap geometry is closed, always applying a 
68
 * given snap tolerance.
69
 * 
70
 * To check multigeometries and multipart geometries, we follow the
71
 * next guideline:
72
 * 
73
 * a) if geometry is multipart or multigeometry, to be closed all parts must be closed.
74
 * b) point geometries are not closed.
75
 *
76
 * @author Alvaro Zabala
77
 *
78
 */
79
public class IGeometryMustBeClosed extends AbstractTopologyRule {
80
                
81
        private double snapTolerance;
82
        
83
        private JtsValidRule parentRule;
84
        
85
        public IGeometryMustBeClosed(Topology topology, FLyrVect originLyr, double snapTolerance) {
86
                super(topology, originLyr);
87
                this.snapTolerance = snapTolerance;
88
        }
89
        
90
        public IGeometryMustBeClosed(){}
91
        
92
        public IGeometryMustBeClosed(FLyrVect originLyr, double snapTolerance) {
93
                this(null, originLyr, snapTolerance);
94
        }
95

    
96
        public String getName() {
97
                return Messages.getText("LINEAR_RING_MUST_BE_CLOSED");
98
        }
99

    
100
        public void checkPreconditions() throws TopologyRuleDefinitionException {
101
                try {
102
                        int shapeType = this.originLyr.getShapeType();
103
                        if(shapeType == FShape.POINT || 
104
                                        shapeType == FShape.MULTIPOINT || 
105
                                        shapeType == FShape.TEXT)
106
                                throw new TopologyRuleDefinitionException();
107
                } catch (ReadDriverException e) {
108
                        e.printStackTrace();
109
                        throw new TopologyRuleDefinitionException("Error leyendo el tipo de geometria del driver",e);
110
                }        
111
        }
112

    
113
        public void validateFeature(IFeature feature) {
114
                IGeometry geometry = feature.getGeometry();
115
                if(! FGeometryUtil.isClosed(geometry, snapTolerance)){
116
                        IGeometry errorGeometry = FGeometryUtil.
117
                                                getGeometryToClose(geometry, snapTolerance);
118
                        addTopologyError(feature, errorGeometry);
119
                }
120
        }
121
        
122

    
123
        private void addTopologyError(IFeature errorFeature, IGeometry errorGeometry) {
124
                AbstractTopologyRule violatedRule = null;
125
                if(this.parentRule != null)
126
                        violatedRule = parentRule;
127
                else
128
                        violatedRule = this;
129
                TopologyError error = 
130
                        new TopologyError(errorGeometry, violatedRule, errorFeature, topology);
131
                addTopologyError(error);
132
        }
133
        
134
        public XMLEntity getXMLEntity(){
135
                XMLEntity xml = super.getXMLEntity();
136
                xml.putProperty("snapTolerance", snapTolerance);
137
                return xml;
138
        }
139
            
140
        public void setXMLEntity(XMLEntity xml){
141
                super.setXMLEntity(xml);
142
                
143
                if(xml.contains("snapTolerance")){
144
                        snapTolerance = xml.getDoubleProperty("snapTolerance");
145
                }
146
        }
147

    
148
        public JtsValidRule getParentRule() {
149
                return parentRule;
150
        }
151

    
152
        public void setParentRule(JtsValidRule parentRule) {
153
                this.parentRule = parentRule;
154
        }
155

    
156
}