Statistics
| Revision:

gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.spatialjoin / src / main / java / org / gvsig / geoprocess / algorithm / spatialjoin / IntersectsSpatialJoinOperation.java @ 336

History | View | Annotate | Download (5.67 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.spatialjoin;
25

    
26
import java.util.List;
27

    
28
import es.unex.sextante.core.Sextante;
29

    
30
import org.gvsig.fmap.dal.DataTypes;
31
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.EditableFeature;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureIndex;
36
import org.gvsig.fmap.dal.feature.FeatureIndexes;
37
import org.gvsig.fmap.dal.feature.FeatureSelection;
38
import org.gvsig.fmap.dal.feature.FeatureSet;
39
import org.gvsig.fmap.dal.feature.FeatureStore;
40
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
41
import org.gvsig.fmap.geom.exception.CreateGeometryException;
42
import org.gvsig.fmap.geom.primitive.Envelope;
43
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
44
import org.gvsig.geoprocess.algorithm.dissolve.Summary;
45
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
46
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
47
import org.gvsig.tools.dispose.DisposableIterator;
48

    
49
/**
50
 * 
51
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
52
 */
53
public class IntersectsSpatialJoinOperation extends GeometryOperation {
54
        /**
55
         * Specialized instance in nearest neighbor search.
56
         */
57
        private FeatureIndex              index                 = null;
58
        private Summary                   summary               = null;
59
        private FeatureStore              storeOverlay          = null;
60
        private FeatureSelection          featureSelection      = null;
61
        
62
        public IntersectsSpatialJoinOperation(FlyrVectIVectorLayer targetLayer, String indexName, Summary summary, AbstractSextanteGeoProcess p) {
63
                super(p);
64
                FeatureIndexes indexes = targetLayer.getFeatureStore().getIndexes();
65
                index = indexes.getFeatureIndex(indexName);
66
                this.summary = summary;
67
                storeOverlay = targetLayer.getFeatureStore();
68
        }
69
        
70
        /*
71
         * (non-Javadoc)
72
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
73
         */
74
        @SuppressWarnings("deprecation")
75
        public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
76
                if(g == null)
77
                        return lastEditFeature;
78
                
79
                if(featureSelection == null) {
80
                        try {
81
                                featureSelection = storeOverlay.getFeatureSelection();
82
                        } catch (DataException e1) {
83
                                //Sin selecci?n tiramos
84
                        }
85
                }
86

    
87
                try {
88

    
89
                        Envelope rect = g.getEnvelope();
90
                        FeatureSet featSet = index.getMatchFeatureSet(rect);
91
                        
92
                        boolean first = true;
93
                        int numReg = 0;
94
                        DisposableIterator it = featSet.iterator();
95
                        while(it.hasNext()) {
96
                                Feature feat = (Feature)it.next();
97
                                if( featureSelection != null && 
98
                                        !featureSelection.isEmpty() && 
99
                                        !featureSelection.isSelected(feat))
100
                                        continue;
101
                                
102
                                if(first) {
103
                                        summary.loadDefaultSummarizes(feat);
104
                                        first = false;
105
                                }
106
                                
107
                                summary.updateValues(feat);
108
                                numReg ++;
109
                        }
110
                        
111
                        buildNewFeature(featureInput, summary, numReg);
112
                        
113
                } catch(FeatureIndexException e) {
114
                        Sextante.addErrorToLog(e);
115
                } catch (DataException e) {
116
                        Sextante.addErrorToLog(e);
117
                }
118
                
119
                return lastEditFeature;
120
        }
121
        
122
        /**
123
         * Builds a new output feature and adds it to the output file
124
         * @param feat1
125
         * @param feat2
126
         * @param value
127
         * @param g
128
         * @throws DataException
129
         */
130
        @SuppressWarnings("unchecked")
131
        private void buildNewFeature(Feature feat, Summary summary, int numReg) throws DataException {
132
                EditableFeature outFeat = persister.getOutputFeatureStore().createNewFeature();
133
                FeatureAttributeDescriptor[] attrs = feat.getType().getAttributeDescriptors();
134
                
135
                //Loads the old feature
136
                int pos = 0;
137
                for (int i = 0; i < attrs.length; i++) {
138
                        if (attrs[i].getDataType().getType() != DataTypes.GEOMETRY) {
139
                                outFeat.set(pos, feat.get(i));
140
                                pos ++;
141
                        }
142
                }
143
                
144
                //Loads the statistics
145
                summary.loadEditableFeature(outFeat);
146
                
147
                //Loads the new field
148
                outFeat.set(SpatialJoinAlgorithm.NEW_FIELD, new Integer(numReg));
149
                
150
                //Saves the geometry
151
                try {
152
                        List l = feat.getGeometries();
153
                        if(l == null) {
154
                                persister.addFeature(outFeat, feat.getDefaultGeometry());
155
                        } else {
156
                                EditableFeature editFeat = null;
157
                                for (int i = 0; i < l.size(); i++) {
158
                                        if(editFeat == null) {
159
                                                editFeat = persister.addFeature(outFeat, (org.gvsig.fmap.geom.Geometry)l.get(i));
160
                                        } else {
161
                                                persister.addGeometryToExistingFeature(editFeat, (org.gvsig.fmap.geom.Geometry)l.get(i));
162
                                        }
163
                                }
164
                        }
165
                } catch (CreateGeometryException e) {
166
                        Sextante.addErrorToLog(e);
167
                }
168
        }
169

    
170
        /*
171
         * (non-Javadoc)
172
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
173
         */
174
        public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) {
175
                
176
        }
177

    
178
}