Statistics
| Revision:

root / trunk / libraries / libTopology / src / org / gvsig / topology / topologyrules / MustBeLargerThanClusterTolerance.java @ 18065

History | View | Annotate | Download (4.84 KB)

1
/*
2
 * Created on 07-sep-2007
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
 */
50
package org.gvsig.topology.topologyrules;
51

    
52
import org.gvsig.jts.GeometryCollapsedException;
53
import org.gvsig.jts.GeometrySnapper;
54
import org.gvsig.topology.AbstractTopologyRule;
55
import org.gvsig.topology.IOneLyrRule;
56
import org.gvsig.topology.IRuleWithClusterTolerance;
57
import org.gvsig.topology.Messages;
58
import org.gvsig.topology.Topology;
59
import org.gvsig.topology.TopologyError;
60
import org.gvsig.topology.TopologyRuleDefinitionException;
61

    
62
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
63
import com.iver.cit.gvsig.fmap.core.FShape;
64
import com.iver.cit.gvsig.fmap.core.IFeature;
65
import com.iver.cit.gvsig.fmap.core.IGeometry;
66
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
67
import com.iver.utiles.XMLEntity;
68
import com.vividsolutions.jts.geom.Geometry;
69

    
70

    
71
/**
72
 * For lines o polygons. The lenght of a line or the perimeter
73
 * of a polygon must be larger than the cluster tolerance.
74
 * 
75
 * Two consecutives points at a distance lower than cluster tolerance must be
76
 * deleted.
77
 *
78
 *
79
 * @author azabala
80
 */
81
public class MustBeLargerThanClusterTolerance extends AbstractTopologyRule implements IOneLyrRule, IRuleWithClusterTolerance {
82
 
83
        //TODO Test that internazionalization are running well
84
         static  final String ruleName = Messages.getText("must_be_larger_than_cluster_tolerance");
85
        
86
         /**
87
          * Cluster tolerance to check.
88
          */
89
        private double clusterTolerance;
90
        
91
        /**
92
         * Snapper to check coords proximity of a given geometry
93
         */
94
        private GeometrySnapper geometrySnapper;
95
        
96
        
97
        
98
        public MustBeLargerThanClusterTolerance(Topology topology, FLyrVect originLyr, double clusterTolerance){
99
                super(topology, originLyr);
100
                setClusterTolerance(clusterTolerance);
101
        }
102
        
103
        public MustBeLargerThanClusterTolerance(FLyrVect originLyr, double clusterTolerance){
104
                this(null, originLyr, clusterTolerance);
105
        }
106
        
107
        public MustBeLargerThanClusterTolerance(){}
108
        
109
        
110
        public String getDescription() {
111
                return ruleName;
112
        }
113
        
114
        
115
        public void checkPreconditions() throws TopologyRuleDefinitionException {
116
                //This rule doesnt apply to Point or MultiPoint vectorial layers.
117
                try {
118
                        int shapeType = this.originLyr.getShapeType();
119
                        if( (shapeType == FShape.POINT) || (shapeType == FShape.MULTIPOINT) || (shapeType == FShape.TEXT))
120
                                throw new TopologyRuleDefinitionException();
121
                } catch (ReadDriverException e) {
122
                        e.printStackTrace();
123
                        throw new TopologyRuleDefinitionException("Error leyendo el tipo de geometria del driver",e);
124
                }        
125
        }
126

    
127
        /**
128
         * Validates this rule with a feature of the origin layer
129
         * @param feature feature of the origin layer this rule is checking
130
         */
131
        public void validateFeature(IFeature feature) {
132
                IGeometry geometry = feature.getGeometry();
133
                Geometry jtsGeometry = geometry.toJTSGeometry();
134
                try{
135
                        geometrySnapper.snap(jtsGeometry);
136
                }catch(GeometryCollapsedException e){
137
                        //use an ErrorFactory
138
                        TopologyError topologyError = new TopologyError(geometry, this, feature, topology);
139
                        topologyError.setID(errorContainer.getErrorFid());
140
                        addTopologyError(topologyError);
141
                }
142
        }
143

    
144
        public double getClusterTolerance() {
145
                return clusterTolerance;
146
        }
147

    
148
        public void setClusterTolerance(double clusterTolerance) {
149
                this.clusterTolerance = clusterTolerance;
150
                this.geometrySnapper = new GeometrySnapper(clusterTolerance);
151
        } 
152
        
153
        public XMLEntity getXMLEntity(){
154
                XMLEntity xml = super.getXMLEntity();
155
                xml.putProperty("className", getClassName());
156
                xml.putProperty("clusterTolerance", this.clusterTolerance);
157
                return xml;
158
        }
159
            
160
        public void setXMLEntity(XMLEntity xml){
161
                super.setXMLEntity(xml);
162
                if(xml.contains("clusterTolerance")){
163
                        setClusterTolerance(xml.getDoubleProperty("clusterTolerance"));
164
                }
165
        }
166
}
167