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