Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.fusespatially / src / main / java / org / gvsig / geoprocess / algorithm / fusespatially / FuseSpatiallyOperation.java @ 273

History | View | Annotate | Download (4.87 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.fusespatially;
46

    
47
import java.util.ArrayList;
48
import java.util.Iterator;
49
import java.util.List;
50

    
51
import org.gvsig.fmap.dal.exception.DataException;
52
import org.gvsig.fmap.dal.feature.EditableFeature;
53
import org.gvsig.fmap.dal.feature.Feature;
54
import org.gvsig.fmap.dal.feature.FeatureSet;
55
import org.gvsig.fmap.dal.feature.FeatureStore;
56
import org.gvsig.fmap.geom.GeometryLocator;
57
import org.gvsig.fmap.geom.GeometryManager;
58
import org.gvsig.fmap.geom.exception.CreateGeometryException;
59
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
60
import org.gvsig.geoprocess.algorithm.base.util.GeometryUtil;
61

    
62
import com.vividsolutions.jts.geom.Geometry;
63
/**
64
 * Fuse spatially operation
65
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
66
 */
67
public class FuseSpatiallyOperation extends GeometryOperation {
68
        private FeatureStore                     inFeatStore      = null;
69
        protected GeometryManager                geomManager      = GeometryLocator.getGeometryManager();
70
        private FeatureSet                       featureSet       = null;
71
        private int                              id               = 0;
72
        private long                             nFeatures        = 0;
73
        private boolean[]                        featProcessed    = null;
74

    
75
        public FuseSpatiallyOperation(FeatureStore inFeatStore, boolean selectedGeom) throws DataException {
76
                this.inFeatStore = inFeatStore;
77
                
78
                if(selectedGeom) {
79
                        featureSet = (FeatureSet)inFeatStore.getSelection();
80
                } else
81
                        featureSet = inFeatStore.getFeatureSet();
82
                nFeatures = featureSet.getSize();
83
                featProcessed = new boolean[(int)nFeatures];
84
        }
85

    
86
        /*
87
         * (non-Javadoc)
88
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
89
         */
90
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
91
                if(g == null)
92
                        return lastEditFeature;
93
                
94
                try {
95
                        Geometry inputJTSGeom = GeometryUtil.geomToJTS(g);
96
                        Geometry union = null; 
97
                        
98
                        Iterator it = featureSet.fastIterator();
99
                        int index = 0;
100
                        while(it.hasNext()) {
101
                                Feature f = (Feature)it.next();
102
                                if(featProcessed[index])
103
                                        continue;
104
                                org.gvsig.fmap.geom.Geometry geom = feature.getDefaultGeometry();
105
                                Geometry jtsGeom = GeometryUtil.geomToJTS(geom); //Multigeometry support
106
                                if(inputJTSGeom.intersects(jtsGeom)) {
107
                                        inputJTSGeom = inputJTSGeom.union(jtsGeom);
108
                                        featProcessed[index] = true;
109
                                }
110
                                index ++;
111
                                continue;
112
                        }
113

    
114
                        lastEditFeature = persister.addFeature(inputJTSGeom, "FID", id);
115
                } catch (DataException e) {
116
                        return null;
117
                } catch (CreateGeometryException e) {
118
                        return null;
119
                }
120
                id++;
121
                return lastEditFeature;
122
        }
123
        
124
        /*
125
         * (non-Javadoc)
126
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
127
         */
128
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) {
129
                if(g == null)
130
                        return;
131
        }
132
        
133
        /*
134
         * (non-Javadoc)
135
         * @see org.gvsig.geoprocess.algorithm.base.core.IOperation#getResult()
136
         */
137
        public Object getResult() {
138
                return lastEditFeature;
139
        }
140
        
141
}
142