Statistics
| Revision:

root / trunk / libraries / libTopology / src / org / gvsig / topology / errorfixes / MergeOverlapPolygonFix.java @ 24086

History | View | Annotate | Download (4.89 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.errorfixes;
50

    
51
import java.util.ArrayList;
52
import java.util.List;
53

    
54
import org.gvsig.exceptions.BaseException;
55
import org.gvsig.fmap.core.FeatureUtil;
56
import org.gvsig.fmap.core.NewFConverter;
57
import org.gvsig.topology.Messages;
58
import org.gvsig.topology.TopologyError;
59
import org.gvsig.util.GFeatureParameter;
60
import org.gvsig.util.GParameter;
61

    
62
import com.iver.cit.gvsig.fmap.core.IFeature;
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
65
import com.vividsolutions.jts.geom.Geometry;
66

    
67
/**
68
 * Automatic fix to 'Polygon must not overlaps' topology error.
69
 * 
70
 * It removes from one polygon the common area.
71
 * 
72
 * @author Alvaro Zabala
73
 *
74
 */
75

    
76
public class MergeOverlapPolygonFix extends AbstractTopologyErrorFix implements ITopologyErrorFixWithParameters {
77

    
78
        /**
79
         * One of the topology error's causing features, which preserve
80
         * its geometry.
81
         */
82
        public IFeature featureToPreserve;
83
        
84
        /**
85
         * Parameters needed for the fix
86
         */
87
        GParameter[] parameters = null;
88
        
89
        public MergeOverlapPolygonFix() {
90
        }
91
        
92
        public List<IFeature>[] fixAlgorithm(TopologyError error) throws BaseException {
93
                
94
                List<IFeature>[] solution = null;
95
                
96
                IGeometry errorGeometry = error.getGeometry();
97
                Geometry errorGeoJts = NewFConverter.toJtsGeometry(errorGeometry);
98
                
99
                IFeature firstFeature = error.getFeature1();
100
                Geometry firstJts =NewFConverter.toJtsGeometry(firstFeature.getGeometry());
101
                
102
                IFeature secondFeature = error.getFeature2();
103
                Geometry secondJts = NewFConverter.toJtsGeometry(secondFeature.getGeometry());
104
                
105
                List<IFeature> editedFeatures = new ArrayList<IFeature>();
106
                if(featureToPreserve.getID().equals(firstFeature.getID())){
107
                        //we remove the overlapping area of first feature
108
                        firstFeature = FeatureUtil.removeOverlappingArea(firstFeature, 
109
                                                                                                        firstJts, 
110
                                                                                                        errorGeoJts);
111
                        //and adds to second feature
112
                        secondJts = secondJts.union(errorGeoJts);
113
                        secondFeature.setGeometry(NewFConverter.toFMap(secondJts));
114
                }else{
115
                        secondFeature = FeatureUtil.removeOverlappingArea(secondFeature, 
116
                                        secondJts, 
117
                                        errorGeoJts);
118
                        //FIXME USAR JTSUTIL PARA CONTEMPLAR LA POSIBILIDAD DE QUE LLEGUEN MULTIGEOMETRIAS
119
                        firstJts = firstJts.union(errorGeoJts);
120
                        firstFeature.setGeometry(NewFConverter.toFMap(firstJts));
121
                }
122
                editedFeatures.add(firstFeature);
123
                editedFeatures.add(secondFeature);
124
                
125
                solution = (List<IFeature>[]) new List[]{editedFeatures};
126
                return solution;
127
        }
128
        
129
        
130
        public String getEditionDescription() {
131
                return Messages.getText("MERGE_OVERLAP_AREA_FIX");
132
        }
133

    
134

    
135
        public IFeature getFeatureToPreserve() {
136
                return featureToPreserve;
137
        }
138

    
139

    
140
        public void setFeatureToPreserve(IFeature featureToPreserve) {
141
                this.featureToPreserve = featureToPreserve;
142
        }
143

    
144

    
145
        public GParameter[] getParameters() {
146
                return parameters;
147
        }
148

    
149

    
150
        public void setParameterValue(String paramName, Object value) {
151
                if(paramName.equals("featureToPreserve")){
152
                        if(value.getClass().isAssignableFrom(IFeature.class)){
153
                                this.featureToPreserve = (IFeature) value; 
154
                        }
155
                }        
156
                
157
        }
158

    
159
        public void initialize(TopologyError error) {
160
                List<FLyrVect> featureContainers = new ArrayList<FLyrVect>();
161
                featureContainers.add(error.getOriginLayer());
162
                if(error.getDestinationLayer() != null){
163
                        featureContainers.add(error.getDestinationLayer());
164
                }
165
                        
166
                parameters = new GParameter[]{
167
                                new GFeatureParameter("featureToPreserve",  
168
                                                        GParameter.FEATURE_PARAM_TYPES, 
169
                                                         error.getFeature1(), 
170
                                                         this,
171
                                                         featureContainers)
172
                };
173
        }
174

    
175
}