Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.difference / src / main / java / org / gvsig / geoprocess / algorithm / difference / DifferenceOperation.java @ 237

History | View | Annotate | Download (4.75 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
/*
22

23
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
24
 *
25
 * Copyright (C) 2010 Generalitat Valenciana.
26
 *
27
 * This program is free software; you can redistribute it and/or
28
 * modify it under the terms of the GNU General Public License
29
 * as published by the Free Software Foundation; either version 2
30
 * of the License, or (at your option) any later version.
31
 *
32
 * This program is distributed in the hope that it will be useful,
33
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35
 * GNU General Public License for more details.
36
 *
37
 * You should have received a copy of the GNU General Public License
38
 * along with this program; if not, write to the Free Software
39
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
40
 */
41

    
42
package org.gvsig.geoprocess.algorithm.difference;
43

    
44
import com.vividsolutions.jts.geom.Geometry;
45
import com.vividsolutions.jts.precision.EnhancedPrecisionOp;
46

    
47
import es.unex.sextante.core.Sextante;
48

    
49
import org.gvsig.fmap.dal.exception.DataException;
50
import org.gvsig.fmap.dal.feature.EditableFeature;
51
import org.gvsig.fmap.dal.feature.Feature;
52
import org.gvsig.fmap.geom.exception.CreateGeometryException;
53
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
54
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
55
/**
56
 * Difference between two geometries
57
 * @author Nacho Brodin (nachobrodin@gmail.com)
58
 */
59
public class DifferenceOperation extends GeometryOperation {
60
        /**
61
         * Clipping geometry: the convex hull of the
62
         * clipping layer
63
         */
64
        private Geometry                         overlaysGeom     = null;
65
        
66
        public DifferenceOperation(org.gvsig.fmap.geom.Geometry overlays) {
67
                if(overlays != null)
68
                        this.overlaysGeom = GeometryUtil.geomToJTS(overlays);
69
        }
70

    
71
        /**
72
         * clips feature's geometry with the clipping geometry, preserving
73
         * feature's original attributes.
74
         * If feature's geometry doesn't touch clipping geometry, it will be
75
         * ignored.
76
         */
77
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
78
                if(g == null)
79
                        return lastEditFeature;
80
                
81
                com.vividsolutions.jts.geom.Geometry jtsGeom = GeometryUtil.geomToJTS(g);
82
                
83
                try {
84
                        if(overlaysGeom == null)
85
                                return persister.addFeature(feature, jtsGeom);
86
                        
87
                        if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope())) {
88
                                lastEditFeature = persister.addFeature(feature, jtsGeom);
89
                                return lastEditFeature;
90
                        }
91

    
92
                        if(jtsGeom.intersects(overlaysGeom)) {
93
                                Geometry newGeom = EnhancedPrecisionOp.difference(jtsGeom, overlaysGeom);
94
                                if(!newGeom.isEmpty())
95
                                        lastEditFeature = persister.addFeature(feature, newGeom);
96
                        } else
97
                                lastEditFeature = persister.addFeature(feature, jtsGeom);
98
                } catch (CreateGeometryException e) {
99
                        Sextante.addErrorToLog(e);
100
                } catch (DataException e) {
101
                        Sextante.addErrorToLog(e);
102
                }
103
                return lastEditFeature;
104
        }
105
        
106
        /**
107
         * clips feature's geometry with the clipping geometry, preserving
108
         * feature's original attributes.
109
         * If feature's geometry doesn't touch clipping geometry, it will be
110
         * ignored.
111
         */
112
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
113
                if(g == null)
114
                        return;
115
                
116
                com.vividsolutions.jts.geom.Geometry jtsGeom = GeometryUtil.geomToJTS(g);
117
        
118
                try {
119
                        if(overlaysGeom == null)
120
                                persister.addFeature(feature, jtsGeom);
121
                        
122
                        if(!jtsGeom.getEnvelope().intersects(overlaysGeom.getEnvelope()))
123
                                persister.addFeature(feature, jtsGeom);
124

    
125
                        if(jtsGeom.intersects(overlaysGeom)) {
126
                                try {
127
                                        Geometry newGeom = EnhancedPrecisionOp.difference(jtsGeom, overlaysGeom);
128
                                        persister.addFeature(feature, newGeom);
129
                                } catch (CreateGeometryException e) {
130
                                        Sextante.addErrorToLog(e);
131
                                } catch (DataException e) {
132
                                        Sextante.addErrorToLog(e);
133
                                }
134
                        } else
135
                                persister.addFeature(feature, jtsGeom);
136
                } catch (DataException e) {
137
                        Sextante.addErrorToLog(e);
138
                } catch (CreateGeometryException e) {
139
                        Sextante.addErrorToLog(e);
140
                }
141
        }
142

    
143
}
144