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 / SpatiallyIndexedSpatialJoinOperation.java @ 960

History | View | Annotate | Download (6.37 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.Iterator;
27

    
28
import org.apache.commons.lang3.mutable.MutableDouble;
29
import org.apache.commons.lang3.mutable.MutableObject;
30

    
31
import es.unex.sextante.core.Sextante;
32

    
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.EditableFeature;
35
import org.gvsig.fmap.dal.feature.Feature;
36
import org.gvsig.fmap.dal.feature.FeatureIndex;
37
import org.gvsig.fmap.dal.feature.FeatureIndexes;
38
import org.gvsig.fmap.dal.feature.FeatureReference;
39
import org.gvsig.fmap.dal.feature.FeatureSelection;
40
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.geom.Geometry;
43
import org.gvsig.fmap.geom.GeometryLocator;
44
import org.gvsig.fmap.geom.GeometryManager;
45
import org.gvsig.fmap.geom.SpatialIndex;
46
import org.gvsig.fmap.geom.exception.CreateGeometryException;
47
import org.gvsig.fmap.geom.primitive.Envelope;
48
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
49
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
50
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer;
51
import org.gvsig.tools.exception.BaseException;
52
import org.gvsig.tools.visitor.VisitCanceledException;
53
import org.gvsig.tools.visitor.Visitor;
54

    
55
/**
56
 *
57
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
58
 */
59
public class SpatiallyIndexedSpatialJoinOperation extends GeometryOperation {
60
        private FeatureStore               storeOverlay        = null;
61
        private FeatureSelection           featureSelection    = null;
62

    
63
        /**
64
         * Geometry.distance() is a costly operation. Thats the reason for we are
65
         * only using a nearest neighbor. TODO SpatialIndex works with Rectangle2D,
66
         * and this may be a simplification that drives to errors. Make additional
67
         * probes to use a number of default neighbors
68
         */
69
        static final int                  DEFAULT_NUM_NEIGBOURS = 1;
70

    
71
        /**
72
         * Number of neighbors that nearestFinder must found.
73
         */
74
        int                               numOfNeighbours       = DEFAULT_NUM_NEIGBOURS;
75

    
76
        /**
77
         * Specialized instance in nearest neighbor search.
78
         */
79
        private SpatialIndex              index                 = null;
80

    
81
        public SpatiallyIndexedSpatialJoinOperation(FlyrVectIVectorLayer targetLayer, SpatialIndex index, AbstractSextanteGeoProcess p) {
82
                super(p);
83
                this.index = index;
84
                storeOverlay = targetLayer.getFeatureStore();
85
        }
86

    
87
        /*
88
         * (non-Javadoc)
89
         * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
90
         */
91
        @SuppressWarnings({ "unchecked", "deprecation" })
92
    public EditableFeature invoke(final org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
93
            final MutableObject<Feature> foundFeature = new MutableObject<Feature>(null);
94
        boolean addedFeature = false;
95
        if (g == null)
96
            return lastEditFeature;
97

    
98
        try {
99
            final MutableDouble nearestDistance = new MutableDouble(Double.POSITIVE_INFINITY);
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
                    double dist = g.distance(g2);
109
                    if (dist <= nearestDistance.getValue()) {
110
                        nearestDistance.setValue(dist);
111
                        foundFeature.setValue(feat.getCopy());
112
                    }
113
                }
114
            });
115

    
116
            Iterator<?> iterator = index.queryNearest(g);
117
            while (iterator.hasNext()) {
118
                FeatureReference ref = (FeatureReference) iterator.next();
119
                Feature feat = ref.getFeature();
120
                Geometry g2 = feat.getDefaultGeometry();
121
                double dist = g.distance(g2);
122
                if (dist <= nearestDistance.getValue()) {
123
                    nearestDistance.setValue(dist);
124
                    foundFeature.setValue(feat.getCopy());
125
                }
126
            }
127

    
128
            if(foundFeature!=null){
129
                buildFeature(featureInput, foundFeature.getValue(), new Double(nearestDistance.getValue()), g);
130
                addedFeature = true;
131
            }
132

    
133
        } catch (Exception e) {
134
            Sextante.addErrorToLog(e);
135
        }
136

    
137
        return lastEditFeature;
138
    }
139

    
140
        /**
141
         * Builds a feature and adds it to the output file
142
         * @param feat1
143
         * @param feat2
144
         * @param value
145
         * @param g
146
         * @throws DataException
147
         */
148
        private void buildFeature(Feature feat1, Feature feat2, Object value, org.gvsig.fmap.geom.Geometry g) throws DataException {
149
                EditableFeature outFeat = persister.getOutputFeatureStore().createNewFeature();
150
                int sizeFeat1 = feat1.getType().size() - 1;
151

    
152
                for (int i = 0; i < sizeFeat1; i++)
153
                        outFeat.set(i, feat1.get(i));
154

    
155
                for (int i = sizeFeat1; i < sizeFeat1 + feat2.getType().size() - 1; i++)
156
                        outFeat.set(i, feat2.get(i - sizeFeat1));
157

    
158
                outFeat.set(outFeat.getType().size() - 2, value);
159
                try {
160
                        persister.addFeature(outFeat, g);
161
                } catch (CreateGeometryException e) {
162
                        Sextante.addErrorToLog(e);
163
                }
164
        }
165

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

    
172
        }
173

    
174
}