gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.buffer / src / main / java / org / gvsig / geoprocess / algorithm / buffer / InOutBufferOperation.java @ 270
History | View | Annotate | Download (6.39 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.geoprocess.algorithm.buffer; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.List; |
28 |
import java.util.Stack; |
29 |
|
30 |
import com.vividsolutions.jts.geom.Geometry; |
31 |
import com.vividsolutions.jts.geom.GeometryCollection; |
32 |
import com.vividsolutions.jts.geom.GeometryFactory; |
33 |
import com.vividsolutions.jts.geom.MultiPolygon; |
34 |
import com.vividsolutions.jts.geom.Polygon; |
35 |
import com.vividsolutions.jts.operation.buffer.BufferOp; |
36 |
import com.vividsolutions.jts.operation.buffer.BufferParameters; |
37 |
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier; |
38 |
|
39 |
import es.unex.sextante.core.Sextante; |
40 |
import es.unex.sextante.dataObjects.IVectorLayer; |
41 |
|
42 |
import org.gvsig.fmap.dal.exception.DataException; |
43 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
44 |
import org.gvsig.fmap.dal.feature.Feature; |
45 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
46 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
47 |
import org.gvsig.geoprocess.algorithm.base.util.JTSFacade; |
48 |
|
49 |
/**
|
50 |
* Buffer operation
|
51 |
*
|
52 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
53 |
*/
|
54 |
public class InOutBufferOperation extends BufferOperation { |
55 |
|
56 |
/**
|
57 |
* Builds an instance of this operation.
|
58 |
*
|
59 |
* @param distance
|
60 |
* @param layer
|
61 |
* @param userDistance
|
62 |
*/
|
63 |
public InOutBufferOperation(IDistance distance, IVectorLayer layer,
|
64 |
double userDistance) {
|
65 |
super(distance, layer, userDistance);
|
66 |
} |
67 |
|
68 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g,
|
69 |
Feature feature) { |
70 |
GeometryFactory geomFact = new GeometryFactory();
|
71 |
Geometry newGeom = null;
|
72 |
Geometry previousExteriorRing = null;
|
73 |
Geometry previousInteriorRing = null;
|
74 |
Geometry originalGeometry = GeometryUtil.geomToJTS(g); |
75 |
Geometry inputParam = originalGeometry; |
76 |
distance.setFeature(feature); |
77 |
double bufferDistance =
|
78 |
distance.getBufferDistance(userDistance, projection, |
79 |
getDistanceUnits(), getMapUnits()); |
80 |
|
81 |
if (originalGeometry.getDimension() != 0) |
82 |
inputParam = |
83 |
TopologyPreservingSimplifier.simplify(originalGeometry, |
84 |
bufferDistance / 10d);
|
85 |
|
86 |
for (int i = 1; i <= numberOfRadialBuffers; i++) { |
87 |
double distRing = i * bufferDistance;
|
88 |
BufferOp bufOp = new BufferOp(inputParam);
|
89 |
bufOp.setEndCapStyle(capBuffer == CAP_ROUND |
90 |
? BufferParameters.CAP_ROUND : BufferParameters.CAP_SQUARE); |
91 |
Geometry out = bufOp.getResultGeometry(distRing); |
92 |
Geometry in = bufOp.getResultGeometry(-1 * distRing);
|
93 |
boolean collapsedInterior = verifyNilGeometry(in);
|
94 |
if (previousExteriorRing == null || previousInteriorRing == null) { |
95 |
if (collapsedInterior)
|
96 |
newGeom = out; |
97 |
else
|
98 |
newGeom = JTSFacade.difference(out, in); |
99 |
} else {
|
100 |
if (collapsedInterior)
|
101 |
newGeom = JTSFacade.difference(out, previousExteriorRing); |
102 |
else {
|
103 |
Geometry outRing = |
104 |
JTSFacade.difference(out, previousExteriorRing); |
105 |
Geometry inRing = |
106 |
JTSFacade.difference(previousInteriorRing, in); |
107 |
|
108 |
Geometry[] geomArray = new Geometry[] { outRing, inRing }; |
109 |
newGeom = geomFact.createGeometryCollection(geomArray); |
110 |
|
111 |
// FMap doesnt work with GeometryCollection, so we try to
|
112 |
// pass to a MultiPolygon.
|
113 |
List<Geometry> polygons = new ArrayList<Geometry>(); |
114 |
Stack<Geometry> stack = new Stack<Geometry>(); |
115 |
stack.push(newGeom); |
116 |
while (stack.size() != 0) { |
117 |
GeometryCollection geCol = |
118 |
(GeometryCollection) stack.pop(); |
119 |
|
120 |
for (int j = 0; j < geCol.getNumGeometries(); j++) { |
121 |
Geometry geometry = geCol.getGeometryN(j); |
122 |
if (geometry instanceof GeometryCollection) |
123 |
stack.push(geometry); |
124 |
if (geometry instanceof Polygon) |
125 |
polygons.add(geometry); |
126 |
} |
127 |
} |
128 |
|
129 |
Polygon[] pols = new Polygon[polygons.size()]; |
130 |
pols = polygons.toArray(pols); |
131 |
MultiPolygon newSolution = |
132 |
geomFact.createMultiPolygon(pols); |
133 |
newGeom = newSolution; |
134 |
} |
135 |
} |
136 |
try {
|
137 |
if (newGeom != null && !newGeom.isEmpty()) { |
138 |
lastEditFeature = |
139 |
persister.addFeature(newGeom, id, -1 * distRing, distRing);
|
140 |
id++; |
141 |
} |
142 |
} catch (CreateGeometryException e) {
|
143 |
Sextante.addErrorToLog(e); |
144 |
} catch (DataException e) {
|
145 |
Sextante.addErrorToLog(e); |
146 |
} |
147 |
previousExteriorRing = out; |
148 |
if (!collapsedInterior)
|
149 |
previousInteriorRing = in; |
150 |
} |
151 |
return lastEditFeature;
|
152 |
} |
153 |
|
154 |
/*
|
155 |
* (non-Javadoc)
|
156 |
*
|
157 |
* @see
|
158 |
* org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org
|
159 |
* .gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
160 |
*/
|
161 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
162 |
invoke(g, (Feature) feature); |
163 |
} |
164 |
} |