Statistics
| Revision:

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

History | View | Annotate | Download (4.38 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 java.util.Collection;
53
import java.util.Iterator;
54

    
55
import org.gvsig.topology.AbstractTopologyRule;
56
import org.gvsig.topology.IOneLyrRule;
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.FMultiPoint2D;
64
import com.iver.cit.gvsig.fmap.core.FShape;
65
import com.iver.cit.gvsig.fmap.core.IFeature;
66
import com.iver.cit.gvsig.fmap.core.IGeometry;
67
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
68
import com.vividsolutions.jts.geom.Coordinate;
69
import com.vividsolutions.jts.geom.Geometry;
70
import org.gvsig.jts.RepeatedPointTester;
71

    
72

    
73
/**
74
 * This rule checks that a geometry (line or polygon)
75
 * dont have consecutive repeated points)
76
 * 
77
 * @author azabala
78
 *
79
 */
80
public class MustNotHaveRepeatedPoints extends AbstractTopologyRule implements IOneLyrRule {
81

    
82
        //TODO Test that internazionalization are running well
83
        String ruleName = Messages.getText("must_not_have_repeated_points");
84
        /**
85
         * tests if a geometry has repeated points
86
         */
87
        RepeatedPointTester tester = new RepeatedPointTester();
88
        
89
        /**
90
         * Default constructor
91
         *
92
         */
93
        public MustNotHaveRepeatedPoints(Topology topology, FLyrVect originLyr){
94
                super(topology, originLyr);
95
        }
96
        
97
        public MustNotHaveRepeatedPoints(FLyrVect originLyr){
98
                super(originLyr);
99
        }
100
        
101
        public MustNotHaveRepeatedPoints(){}
102
        
103
        public String getDescription() {
104
                return ruleName;
105
        }
106

    
107
        
108
        public void checkPreconditions() throws TopologyRuleDefinitionException {
109
                //This rule doesnt apply to Point vectorial layers.
110
                try {
111
                        int shapeType = this.originLyr.getShapeType();
112
                        if(shapeType == FShape.POINT)
113
                                throw new TopologyRuleDefinitionException();
114
                } catch (ReadDriverException e) {
115
                        e.printStackTrace();
116
                        throw new TopologyRuleDefinitionException("Error leyendo el tipo de geometria del driver",e);
117
                }        
118
        }
119

    
120
        /**
121
         * Validates this rule with a feature of the origin layer
122
         * @param feature feature of the origin layer this rule is checking
123
         */
124
        public void validateFeature(IFeature feature) {
125
                IGeometry geometry = feature.getGeometry();
126
                Geometry jtsGeometry = geometry.toJTSGeometry();
127
                if(tester.hasRepeatedPoint(jtsGeometry)){
128
                        Collection<Coordinate> repeatedCoords = tester.getRepeatedCoordinates();
129
                    int numRepeatedPoints = repeatedCoords.size();
130
                    double[] x = new double[numRepeatedPoints];
131
                    double[] y = new double[numRepeatedPoints];
132
                    Iterator<Coordinate> coordsIt = repeatedCoords.iterator();
133
                    int count = 0;
134
                    while(coordsIt.hasNext()){
135
                            Coordinate coord = (Coordinate) coordsIt.next();
136
                            x[count] = coord.x;
137
                            y[count] = coord.y;
138
                            count++;
139
                    }
140
                    FMultiPoint2D multiPoint = new FMultiPoint2D(x, y);
141
                        
142
                    //FIXME Create a TopologyErrorFactory
143
                    TopologyError topologyError = 
144
                                        new TopologyError(multiPoint, 
145
                                                                                this, 
146
                                                                         feature,
147
                                                                        topology);
148
                        topologyError.setID(errorContainer.getErrorFid());
149
                        addTopologyError(topologyError);
150
                }//if
151
                tester.clear();
152
        } 
153
}
154