Statistics
| Revision:

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

History | View | Annotate | Download (3.85 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 org.gvsig.fmap.dal.exception.DataException;
24
import org.gvsig.fmap.dal.feature.EditableFeature;
25
import org.gvsig.fmap.dal.feature.Feature;
26
import org.gvsig.fmap.geom.exception.CreateGeometryException;
27
import org.gvsig.sextante.app.algorithm.base.util.GeometryUtil;
28
import org.gvsig.sextante.app.algorithm.base.util.JTSFacade;
29
import org.gvsig.sextante.app.extension.core.MapTools;
30

    
31
import com.vividsolutions.jts.geom.Geometry;
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
/**
40
 * Buffer operation
41
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
42
 */
43
public class InBufferOperation extends BufferOperation {
44
        
45
        /**
46
         * Builds an instance of this operation.
47
         * @param distance
48
         * @param layer
49
         * @param userDistance
50
         */
51
        public InBufferOperation(IDistance distance, IVectorLayer layer, double userDistance) {
52
                super(distance, layer, userDistance);
53
        }
54
        
55
        /*
56
         * (non-Javadoc)
57
         * @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
58
         */
59
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
60
                //if we have radial internal buffers, we start by most interior buffer
61
                Geometry newGeom = null;
62
                Geometry previousInteriorRing = null;
63
                Geometry originalGeometry = GeometryUtil.geomToJTS(g);
64
                Geometry inputParam = originalGeometry;
65
                distance.setFeature(feature);
66
                double bufferDistance = distance.getBufferDistance(userDistance, projection, MapTools.getDistanceUnits(), MapTools.getMapUnits());
67
                
68
                if(originalGeometry.getDimension() != 0)
69
                        inputParam = TopologyPreservingSimplifier.simplify(originalGeometry, bufferDistance / 10d);
70
                
71
                for(int i = numberOfRadialBuffers; i >= 1; i--) {
72
                        double distRing = i * bufferDistance;
73
                        BufferOp bufOp = new BufferOp(inputParam);
74
                        bufOp.setEndCapStyle(capBuffer == CAP_ROUND ? BufferParameters.CAP_ROUND : BufferParameters.CAP_SQUARE);
75
                        Geometry newGeometry = bufOp.getResultGeometry(-1 * distRing);
76
                        
77
                        if(verifyNilGeometry(newGeometry))
78
                                return lastEditFeature;
79
                        
80
                        if(previousInteriorRing != null)
81
                                newGeom = JTSFacade.difference(newGeometry, previousInteriorRing);
82
                        else
83
                                newGeom = newGeometry;
84
                        
85
                        try {
86
                                lastEditFeature = persister.addFeature(newGeom, id, distRing);
87
                                id ++;
88
                                previousInteriorRing = newGeom;
89
                        } catch (CreateGeometryException e) {
90
                                Sextante.addErrorToLog(e);
91
                        } catch (DataException e) {
92
                                Sextante.addErrorToLog(e);
93
                        }
94
                }
95
                return lastEditFeature;
96
        }
97
        
98
        /*
99
         * (non-Javadoc)
100
         * @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
101
         */
102
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
103
                invoke(g, (Feature)feature);
104
        }
105
}