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 @ 218
History | View | Annotate | Download (6.22 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2010 Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*
|
20 |
*/
|
21 |
package org.gvsig.geoprocess.algorithm.buffer; |
22 |
|
23 |
import java.util.ArrayList; |
24 |
import java.util.List; |
25 |
import java.util.Stack; |
26 |
|
27 |
import com.vividsolutions.jts.geom.Geometry; |
28 |
import com.vividsolutions.jts.geom.GeometryCollection; |
29 |
import com.vividsolutions.jts.geom.GeometryFactory; |
30 |
import com.vividsolutions.jts.geom.MultiPolygon; |
31 |
import com.vividsolutions.jts.geom.Polygon; |
32 |
import com.vividsolutions.jts.operation.buffer.BufferOp; |
33 |
import com.vividsolutions.jts.operation.buffer.BufferParameters; |
34 |
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier; |
35 |
|
36 |
import es.unex.sextante.core.Sextante; |
37 |
import es.unex.sextante.dataObjects.IVectorLayer; |
38 |
|
39 |
import org.gvsig.fmap.dal.exception.DataException; |
40 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
41 |
import org.gvsig.fmap.dal.feature.Feature; |
42 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
43 |
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil; |
44 |
import org.gvsig.geoprocess.algorithm.base.util.JTSFacade; |
45 |
|
46 |
/**
|
47 |
* Buffer operation
|
48 |
*
|
49 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
50 |
*/
|
51 |
public class InOutBufferOperation extends BufferOperation { |
52 |
|
53 |
/**
|
54 |
* Builds an instance of this operation.
|
55 |
*
|
56 |
* @param distance
|
57 |
* @param layer
|
58 |
* @param userDistance
|
59 |
*/
|
60 |
public InOutBufferOperation(IDistance distance, IVectorLayer layer,
|
61 |
double userDistance) {
|
62 |
super(distance, layer, userDistance);
|
63 |
} |
64 |
|
65 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g,
|
66 |
Feature feature) { |
67 |
GeometryFactory geomFact = new GeometryFactory();
|
68 |
Geometry newGeom = null;
|
69 |
Geometry previousExteriorRing = null;
|
70 |
Geometry previousInteriorRing = null;
|
71 |
Geometry originalGeometry = GeometryUtil.geomToJTS(g); |
72 |
Geometry inputParam = originalGeometry; |
73 |
distance.setFeature(feature); |
74 |
double bufferDistance =
|
75 |
distance.getBufferDistance(userDistance, projection, |
76 |
getDistanceUnits(), getMapUnits()); |
77 |
|
78 |
if (originalGeometry.getDimension() != 0) |
79 |
inputParam = |
80 |
TopologyPreservingSimplifier.simplify(originalGeometry, |
81 |
bufferDistance / 10d);
|
82 |
|
83 |
for (int i = 1; i <= numberOfRadialBuffers; i++) { |
84 |
double distRing = i * bufferDistance;
|
85 |
BufferOp bufOp = new BufferOp(inputParam);
|
86 |
bufOp.setEndCapStyle(capBuffer == CAP_ROUND |
87 |
? BufferParameters.CAP_ROUND : BufferParameters.CAP_SQUARE); |
88 |
Geometry out = bufOp.getResultGeometry(distRing); |
89 |
Geometry in = bufOp.getResultGeometry(-1 * distRing);
|
90 |
boolean collapsedInterior = verifyNilGeometry(in);
|
91 |
if (previousExteriorRing == null || previousInteriorRing == null) { |
92 |
if (collapsedInterior)
|
93 |
newGeom = out; |
94 |
else
|
95 |
newGeom = JTSFacade.difference(out, in); |
96 |
} else {
|
97 |
if (collapsedInterior)
|
98 |
newGeom = JTSFacade.difference(out, previousExteriorRing); |
99 |
else {
|
100 |
Geometry outRing = |
101 |
JTSFacade.difference(out, previousExteriorRing); |
102 |
Geometry inRing = |
103 |
JTSFacade.difference(previousInteriorRing, in); |
104 |
|
105 |
Geometry[] geomArray = new Geometry[] { outRing, inRing }; |
106 |
newGeom = geomFact.createGeometryCollection(geomArray); |
107 |
|
108 |
// FMap doesnt work with GeometryCollection, so we try to
|
109 |
// pass to a MultiPolygon.
|
110 |
List<Geometry> polygons = new ArrayList<Geometry>(); |
111 |
Stack<Geometry> stack = new Stack<Geometry>(); |
112 |
stack.push(newGeom); |
113 |
while (stack.size() != 0) { |
114 |
GeometryCollection geCol = |
115 |
(GeometryCollection) stack.pop(); |
116 |
|
117 |
for (int j = 0; j < geCol.getNumGeometries(); j++) { |
118 |
Geometry geometry = geCol.getGeometryN(j); |
119 |
if (geometry instanceof GeometryCollection) |
120 |
stack.push(geometry); |
121 |
if (geometry instanceof Polygon) |
122 |
polygons.add(geometry); |
123 |
} |
124 |
} |
125 |
|
126 |
Polygon[] pols = new Polygon[polygons.size()]; |
127 |
pols = polygons.toArray(pols); |
128 |
MultiPolygon newSolution = |
129 |
geomFact.createMultiPolygon(pols); |
130 |
newGeom = newSolution; |
131 |
} |
132 |
} |
133 |
try {
|
134 |
lastEditFeature = |
135 |
persister.addFeature(newGeom, id, -1 * distRing, distRing);
|
136 |
id++; |
137 |
} catch (CreateGeometryException e) {
|
138 |
Sextante.addErrorToLog(e); |
139 |
} catch (DataException e) {
|
140 |
Sextante.addErrorToLog(e); |
141 |
} |
142 |
previousExteriorRing = out; |
143 |
if (!collapsedInterior)
|
144 |
previousInteriorRing = in; |
145 |
} |
146 |
return lastEditFeature;
|
147 |
} |
148 |
|
149 |
/*
|
150 |
* (non-Javadoc)
|
151 |
*
|
152 |
* @see
|
153 |
* org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org
|
154 |
* .gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
155 |
*/
|
156 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
157 |
invoke(g, (Feature) feature); |
158 |
} |
159 |
} |