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

History | View | Annotate | Download (4.88 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
/*
25

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

    
45
package org.gvsig.geoprocess.algorithm.difference;
46

    
47
import com.vividsolutions.jts.geom.Geometry;
48
import com.vividsolutions.jts.precision.EnhancedPrecisionOp;
49

    
50
import es.unex.sextante.core.Sextante;
51

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

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

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

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

    
146
}
147