Revision 23039 trunk/libraries/libTopology/src/org/gvsig/topology/errorfixes/CreateFeatureOverlapPolygonFix.java
CreateFeatureOverlapPolygonFix.java | ||
---|---|---|
48 | 48 |
*/ |
49 | 49 |
package org.gvsig.topology.errorfixes; |
50 | 50 |
|
51 |
import java.util.ArrayList; |
|
52 |
import java.util.List; |
|
53 |
|
|
51 | 54 |
import org.gvsig.exceptions.BaseException; |
55 |
import org.gvsig.fmap.core.NewFConverter; |
|
56 |
import org.gvsig.jts.JtsUtil; |
|
52 | 57 |
import org.gvsig.topology.Messages; |
53 | 58 |
import org.gvsig.topology.TopologyError; |
54 | 59 |
|
... | ... | |
57 | 62 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
58 | 63 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
59 | 64 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
60 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
|
61 | 65 |
import com.iver.cit.gvsig.fmap.edition.EditableAdapter; |
62 | 66 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
63 | 67 |
import com.vividsolutions.jts.geom.Geometry; |
68 |
import com.vividsolutions.jts.geom.GeometryCollection; |
|
64 | 69 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
65 | 70 |
|
66 | 71 |
public class CreateFeatureOverlapPolygonFix extends AbstractTopologyErrorFix { |
67 | 72 |
|
68 |
/** |
|
69 |
* Two first elements of returned array will be the edited features. |
|
70 |
* |
|
71 |
* The last element will be the new feature |
|
72 |
*/ |
|
73 |
public IFeature[] fixAlgorithm(TopologyError error) throws BaseException { |
|
73 |
|
|
74 |
public List<IFeature>[] fixAlgorithm(TopologyError error) throws BaseException { |
|
74 | 75 |
IGeometry errorGeometry = error.getGeometry(); |
75 |
Geometry errorGeoJts = errorGeometry.toJTSGeometry();
|
|
76 |
Geometry errorGeoJts = NewFConverter.toJtsGeometry(errorGeometry);
|
|
76 | 77 |
|
78 |
|
|
77 | 79 |
IFeature firstFeature = error.getFeature1(); |
78 |
Geometry firstJts = firstFeature.getGeometry().toJTSGeometry(); |
|
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} ; |
|
79 | 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 |
|
|
80 | 113 |
IFeature secondFeature = error.getFeature2(); |
81 |
Geometry secondJts = secondFeature.getGeometry().toJTSGeometry(); |
|
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 |
} |
|
82 | 122 |
|
83 |
Geometry newFirstJts = EnhancedPrecisionOp.difference(firstJts, errorGeoJts); |
|
84 |
IGeometry newFirst = FConverter.jts_to_igeometry(newFirstJts); |
|
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 |
|
|
85 | 139 |
firstFeature.setGeometry(newFirst); |
140 |
secondFeature.setGeometry(newThird); |
|
86 | 141 |
|
87 |
Geometry newSecondJts = EnhancedPrecisionOp.difference(secondJts, errorGeoJts); |
|
88 |
IGeometry newSecond = FConverter.jts_to_igeometry(newSecondJts); |
|
89 |
secondFeature.setGeometry(newSecond); |
|
90 |
|
|
91 | 142 |
int valueLenght = firstFeature.getAttributes().length; |
92 | 143 |
Value[] newValues = new Value[valueLenght]; |
93 | 144 |
for(int i = 0; i < valueLenght; i++){ |
... | ... | |
97 | 148 |
String newId = error.getOriginLayer().getSource().getShapeCount()+""; |
98 | 149 |
DefaultFeature newFeature = new DefaultFeature(errorGeometry, newValues, newId ); |
99 | 150 |
|
100 |
return new IFeature[]{firstFeature, secondFeature, newFeature}; |
|
101 |
|
|
102 |
|
|
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}; |
|
103 | 156 |
} |
104 | 157 |
|
105 | 158 |
public void fix(TopologyError error) throws BaseException { |
106 | 159 |
EditableAdapter[] adapters = prepareEdition(error); |
107 |
IFeature[] correctedFeatures = fixAlgorithm(error);
|
|
160 |
List<IFeature>[] correctedFeatures = fixAlgorithm(error);
|
|
108 | 161 |
if (correctedFeatures != null) { |
109 |
|
|
110 |
IFeature firstFeature = correctedFeatures[0];
|
|
162 |
List<IFeature> firstLyr = correctedFeatures[0]; |
|
163 |
IFeature firstFeature = firstLyr.get(0);
|
|
111 | 164 |
adapters[0].modifyRow(Integer.parseInt(firstFeature.getID()), |
112 | 165 |
firstFeature, |
113 | 166 |
getEditionDescription(), |
114 | 167 |
EditionEvent.GRAPHIC); |
115 | 168 |
|
116 |
IFeature secondFeature = correctedFeatures[1];
|
|
169 |
IFeature secondFeature = firstLyr.get(1);
|
|
117 | 170 |
adapters[0].modifyRow(Integer.parseInt(secondFeature.getID()), |
118 | 171 |
secondFeature, |
119 | 172 |
getEditionDescription(), |
... | ... | |
121 | 174 |
|
122 | 175 |
|
123 | 176 |
|
124 |
adapters[0].doAddRow(correctedFeatures[2], EditionEvent.GRAPHIC);
|
|
177 |
adapters[0].doAddRow(firstLyr.get(2), EditionEvent.GRAPHIC);
|
|
125 | 178 |
|
126 | 179 |
|
127 | 180 |
adapters[0].endComplexRow(getEditionDescription()); |
Also available in: Unified diff