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 @ 270

History | View | Annotate | Download (4.25 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

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

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

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

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

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

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

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

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

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