Statistics
| Revision:

root / trunk / libraries / libTopology / src / org / gvsig / topology / errorfixes / CreateFeatureOverlapPolygonFix.java @ 23039

History | View | Annotate | Download (6.04 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.NewFConverter;
56
import org.gvsig.jts.JtsUtil;
57
import org.gvsig.topology.Messages;
58
import org.gvsig.topology.TopologyError;
59

    
60
import com.hardcode.gdbms.engine.values.Value;
61
import com.hardcode.gdbms.engine.values.ValueFactory;
62
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
63
import com.iver.cit.gvsig.fmap.core.IFeature;
64
import com.iver.cit.gvsig.fmap.core.IGeometry;
65
import com.iver.cit.gvsig.fmap.edition.EditableAdapter;
66
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
67
import com.vividsolutions.jts.geom.Geometry;
68
import com.vividsolutions.jts.geom.GeometryCollection;
69
import com.vividsolutions.jts.precision.EnhancedPrecisionOp;
70

    
71
public class CreateFeatureOverlapPolygonFix extends AbstractTopologyErrorFix {
72

    
73
        
74
        public List<IFeature>[] fixAlgorithm(TopologyError error) throws BaseException {
75
                IGeometry errorGeometry = error.getGeometry();
76
                Geometry errorGeoJts = NewFConverter.toJtsGeometry(errorGeometry);
77
                
78
                
79
                IFeature firstFeature = error.getFeature1();
80
                Geometry firstJts = NewFConverter.toJtsGeometry(firstFeature.getGeometry());
81
                Geometry[] first = null;
82
                if(firstJts instanceof GeometryCollection){
83
                        first = JtsUtil.extractGeometries((GeometryCollection) firstJts);
84
                }else
85
                        first = new Geometry[]{firstJts} ;
86
                
87
                
88
                Geometry[] second = null;
89
                if(errorGeoJts instanceof GeometryCollection){
90
                        second = JtsUtil.extractGeometries((GeometryCollection) errorGeoJts);
91
                }else
92
                {
93
                        second = new Geometry[]{errorGeoJts};
94
                }
95
                
96
                
97
                for (int i = 0; i < first.length; i++) {
98
                        Geometry geom = first[i];
99
                        Geometry partialSolution = null;
100
                        for (int j = 0; j < second.length; j++) {
101
                                Geometry aux = EnhancedPrecisionOp.difference(geom, second[j]);
102
                                if(partialSolution == null)
103
                                        partialSolution = aux;
104
                                else
105
                                        partialSolution = EnhancedPrecisionOp.union(partialSolution, aux);
106
                        }//for
107
                        first[i] = partialSolution;
108
                }//for i
109
                
110
                Geometry newFirstJts = JtsUtil.GEOMETRY_FACTORY.createGeometryCollection(first);
111
                IGeometry newFirst = NewFConverter.toFMap(newFirstJts);
112
                
113
                IFeature secondFeature = error.getFeature2();
114
                Geometry secondJts = NewFConverter.toJtsGeometry(secondFeature.getGeometry());
115
                Geometry[] third = null;
116
                if(secondJts instanceof GeometryCollection){
117
                        third = JtsUtil.extractGeometries((GeometryCollection) errorGeoJts);
118
                }else
119
                {
120
                        third = new Geometry[]{errorGeoJts};
121
                }
122
                
123
                for (int i = 0; i < third.length; i++) {
124
                        Geometry geom = third[i];
125
                        Geometry partialSolution = null;
126
                        for (int j = 0; j < second.length; j++) {
127
                                Geometry aux = EnhancedPrecisionOp.difference(geom, second[j]);
128
                                if(partialSolution == null)
129
                                        partialSolution = aux;
130
                                else
131
                                        partialSolution = EnhancedPrecisionOp.union(partialSolution, aux);
132
                        }//for
133
                        third[i] = partialSolution;
134
                }//for i
135
                Geometry newThirdJts = JtsUtil.GEOMETRY_FACTORY.createGeometryCollection(third);
136
                IGeometry newThird = NewFConverter.toFMap(newThirdJts);
137
                
138
                
139
                firstFeature.setGeometry(newFirst);
140
                secondFeature.setGeometry(newThird);
141
                
142
                int valueLenght = firstFeature.getAttributes().length;
143
                Value[] newValues = new Value[valueLenght];
144
                for(int i = 0; i < valueLenght; i++){
145
                        newValues[i] = ValueFactory.createNullValue();
146
                }
147
                
148
                String newId = error.getOriginLayer().getSource().getShapeCount()+"";
149
                DefaultFeature newFeature = new DefaultFeature(errorGeometry, newValues, newId );
150
                
151
                List<IFeature> firstLyrFeatures = new ArrayList<IFeature>();
152
                firstLyrFeatures.add(firstFeature);
153
                firstLyrFeatures.add(secondFeature);
154
                firstLyrFeatures.add(newFeature);
155
                return (List<IFeature>[]) new List[]{firstLyrFeatures};
156
        }
157

    
158
        public void fix(TopologyError error) throws BaseException {
159
                EditableAdapter[] adapters = prepareEdition(error);
160
                List<IFeature>[] correctedFeatures = fixAlgorithm(error);
161
                if (correctedFeatures != null) {
162
                        List<IFeature> firstLyr = correctedFeatures[0];
163
                        IFeature firstFeature = firstLyr.get(0);
164
                        adapters[0].modifyRow(Integer.parseInt(firstFeature.getID()), 
165
                                        firstFeature, 
166
                                        getEditionDescription(), 
167
                                        EditionEvent.GRAPHIC);
168
                        
169
                        IFeature secondFeature = firstLyr.get(1);
170
                        adapters[0].modifyRow(Integer.parseInt(secondFeature.getID()), 
171
                                        secondFeature, 
172
                                        getEditionDescription(), 
173
                                        EditionEvent.GRAPHIC);
174
                        
175

    
176
                        
177
                        adapters[0].doAddRow(firstLyr.get(2), EditionEvent.GRAPHIC);
178
                
179
                        
180
                        adapters[0].endComplexRow(getEditionDescription());
181
                        error.getTopology().removeError(error);
182
                }
183
        }
184

    
185
        public String getEditionDescription() {
186
                return Messages.getText("CREATE_FEATURE_OVERLAP_AREA_FIX");
187
        }
188

    
189
}