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