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

History | View | Annotate | Download (6.01 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 java.util.Iterator;
31

    
32
import org.apache.commons.lang3.mutable.MutableBoolean;
33
import org.apache.commons.lang3.mutable.MutableInt;
34
import org.jfree.base.config.ModifiableConfiguration;
35

    
36
import org.gvsig.fmap.dal.DataTypes;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.feature.EditableFeature;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureIndex;
42
import org.gvsig.fmap.dal.feature.FeatureIndexes;
43
import org.gvsig.fmap.dal.feature.FeatureReference;
44
import org.gvsig.fmap.dal.feature.FeatureSelection;
45
import org.gvsig.fmap.dal.feature.FeatureSet;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.SpatialIndex;
50
import org.gvsig.fmap.geom.exception.CreateGeometryException;
51
import org.gvsig.fmap.geom.primitive.Envelope;
52
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
53
import org.gvsig.geoprocess.algorithm.dissolve.Summary;
54
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
55
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
56
import org.gvsig.tools.exception.BaseException;
57
import org.gvsig.tools.visitor.VisitCanceledException;
58
import org.gvsig.tools.visitor.Visitor;
59

    
60
/**
61
 *
62
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
63
 */
64
public class IntersectsSpatialJoinOperation extends GeometryOperation {
65
        /**
66
         * Specialized instance in nearest neighbor search.
67
         */
68
        private SpatialIndex              index                 = null;
69
        private Summary                   summary               = null;
70
        private FeatureStore              storeOverlay          = null;
71
        private FeatureSelection          featureSelection      = null;
72

    
73
        public IntersectsSpatialJoinOperation(FlyrVectIVectorLayer targetLayer, SpatialIndex index, Summary summary, AbstractSextanteGeoProcess p) {
74
                super(p);
75
            this.index = index;
76
                this.summary = summary;
77
                storeOverlay = targetLayer.getFeatureStore();
78
        }
79

    
80
        /*
81
         * (non-Javadoc)
82
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
83
         */
84
        @SuppressWarnings("deprecation")
85
        public EditableFeature invoke(final org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
86
                if(g == null)
87
                        return lastEditFeature;
88

    
89
                if(featureSelection == null) {
90
                        try {
91
                                featureSelection = storeOverlay.getFeatureSelection();
92
                        } catch (DataException e1) {
93
                                //Sin selecci?n tiramos
94
                        }
95
                }
96

    
97
                try {
98
                  final MutableBoolean first = new MutableBoolean(true);
99
                  final MutableInt numReg = new MutableInt(0);
100

    
101
            index.query(g, new Visitor() {
102

    
103
                @Override
104
                public void visit(Object obj) throws VisitCanceledException, BaseException {
105
                    FeatureReference ref = (FeatureReference) obj;
106
                    Feature feat = ref.getFeature();
107
                    Geometry g2 = feat.getDefaultGeometry();
108
                    if(g.intersects(g2)){
109
                        if(first.getValue()) {
110
                            summary.loadDefaultSummarizes(feat);
111
                            first.setFalse();
112
                        }
113
                        summary.updateValues(feat);
114
                        numReg.increment();
115
                    }
116
                }
117
            });
118

    
119
                        buildNewFeature(featureInput, summary, numReg.getValue());
120

    
121
                } catch(FeatureIndexException e) {
122
                        Sextante.addErrorToLog(e);
123
                } catch (DataException e) {
124
                        Sextante.addErrorToLog(e);
125
                }
126

    
127
                return lastEditFeature;
128
        }
129

    
130
        /**
131
         * Builds a new output feature and adds it to the output file
132
         * @param feat1
133
         * @param feat2
134
         * @param value
135
         * @param g
136
         * @throws DataException
137
         */
138
        @SuppressWarnings("unchecked")
139
        private void buildNewFeature(Feature feat, Summary summary, int numReg) throws DataException {
140
                EditableFeature outFeat = persister.getOutputFeatureStore().createNewFeature(feat);
141

    
142
                //Loads the statistics
143
                summary.loadEditableFeature(outFeat);
144

    
145
                //Loads the new field
146
                outFeat.set(SpatialJoinAlgorithm.NEW_FIELD, new Integer(numReg));
147

    
148
                //Saves the geometry
149
                try {
150
                        List l = feat.getGeometries();
151
                        if(l == null) {
152
                                persister.addFeature(outFeat, feat.getDefaultGeometry());
153
                        } else {
154
                                EditableFeature editFeat = null;
155
                                for (int i = 0; i < l.size(); i++) {
156
                                        if(editFeat == null) {
157
                                                editFeat = persister.addFeature(outFeat, (org.gvsig.fmap.geom.Geometry)l.get(i));
158
                                        } else {
159
                                                persister.addGeometryToExistingFeature(editFeat, (org.gvsig.fmap.geom.Geometry)l.get(i));
160
                                        }
161
                                }
162
                        }
163
                } catch (CreateGeometryException e) {
164
                        Sextante.addErrorToLog(e);
165
                }
166
        }
167

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

    
174
        }
175

    
176
}