Statistics
| Revision:

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 / InBufferOperation.java @ 321

History | View | Annotate | Download (4.35 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 com.vividsolutions.jts.geom.Geometry;
27
import com.vividsolutions.jts.operation.buffer.BufferOp;
28
import com.vividsolutions.jts.operation.buffer.BufferParameters;
29
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier;
30

    
31
import es.unex.sextante.core.Sextante;
32
import es.unex.sextante.dataObjects.IVectorLayer;
33

    
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.EditableFeature;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.geom.exception.CreateGeometryException;
38
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
39
import org.gvsig.geoprocess.algorithm.base.util.JTSFacade;
40
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
41

    
42
/**
43
 * Buffer operation
44
 * 
45
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
46
 * @author gvSIG Team
47
 */
48
public class InBufferOperation extends BufferOperation {
49

    
50
    /**
51
     * Builds an instance of this operation.
52
     * 
53
     * @param distance
54
     * @param layer
55
     * @param userDistance
56
     */
57
    public InBufferOperation(IDistance distance, IVectorLayer layer,
58
        double userDistance, AbstractSextanteGeoProcess p) {
59
        super(distance, layer, userDistance, p);
60
    }
61

    
62
    public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g,
63
        Feature feature) {
64
        // if we have radial internal buffers, we start by most interior buffer
65
        Geometry newGeom = null;
66
        Geometry previousInteriorRing = null;
67
        Geometry originalGeometry = GeometryUtil.geomToJTS(g);
68
        Geometry inputParam = originalGeometry;
69
        distance.setFeature(feature);
70
        double bufferDistance =
71
            distance.getBufferDistance(userDistance, projection,
72
                getDistanceUnits(), getMapUnits());
73

    
74
        if (originalGeometry.getDimension() != 0)
75
            inputParam =
76
                TopologyPreservingSimplifier.simplify(originalGeometry,
77
                    bufferDistance / 10d);
78

    
79
        for (int i = numberOfRadialBuffers; i >= 1; i--) {
80
            double distRing = i * bufferDistance;
81
            BufferOp bufOp = new BufferOp(inputParam);
82
            bufOp.setEndCapStyle(capBuffer == CAP_ROUND
83
                ? BufferParameters.CAP_ROUND : BufferParameters.CAP_SQUARE);
84
            Geometry newGeometry = bufOp.getResultGeometry(-1 * distRing);
85

    
86
            if (verifyNilGeometry(newGeometry))
87
                return lastEditFeature;
88

    
89
            if (previousInteriorRing != null)
90
                newGeom =
91
                    JTSFacade.difference(newGeometry, previousInteriorRing);
92
            else
93
                newGeom = newGeometry;
94

    
95
            try {
96
                    if (newGeom != null && !newGeom.isEmpty()) {
97
                            lastEditFeature = persister.addFeature(newGeom, id, distRing);
98
                            id++;
99
                            previousInteriorRing = newGeom;
100
                    }
101
            } catch (CreateGeometryException e) {
102
                Sextante.addErrorToLog(e);
103
            } catch (DataException e) {
104
                Sextante.addErrorToLog(e);
105
            }
106
        }
107
        return lastEditFeature;
108
    }
109

    
110
    /*
111
     * (non-Javadoc)
112
     * 
113
     * @see
114
     * org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org
115
     * .gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
116
     */
117
    public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
118
        invoke(g, (Feature) feature);
119
    }
120
}