Revision 960 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

View differences:

IntersectsSpatialJoinOperation.java
26 26
import java.util.List;
27 27

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

  
29 30
import java.util.Iterator;
30 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

  
31 36
import org.gvsig.fmap.dal.DataTypes;
32 37
import org.gvsig.fmap.dal.exception.DataException;
33 38
import org.gvsig.fmap.dal.feature.EditableFeature;
......
35 40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
36 41
import org.gvsig.fmap.dal.feature.FeatureIndex;
37 42
import org.gvsig.fmap.dal.feature.FeatureIndexes;
43
import org.gvsig.fmap.dal.feature.FeatureReference;
38 44
import org.gvsig.fmap.dal.feature.FeatureSelection;
39 45
import org.gvsig.fmap.dal.feature.FeatureSet;
40 46
import org.gvsig.fmap.dal.feature.FeatureStore;
41 47
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
48
import org.gvsig.fmap.geom.Geometry;
49
import org.gvsig.fmap.geom.SpatialIndex;
42 50
import org.gvsig.fmap.geom.exception.CreateGeometryException;
43 51
import org.gvsig.fmap.geom.primitive.Envelope;
44 52
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation;
45 53
import org.gvsig.geoprocess.algorithm.dissolve.Summary;
46 54
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess;
47 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;
48 59

  
49 60
/**
50
 * 
61
 *
51 62
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
52 63
 */
53 64
public class IntersectsSpatialJoinOperation extends GeometryOperation {
54 65
	/**
55 66
	 * Specialized instance in nearest neighbor search.
56 67
	 */
57
	private FeatureIndex              index                 = null;
68
	private SpatialIndex              index                 = null;
58 69
	private Summary                   summary               = null;
59 70
	private FeatureStore              storeOverlay          = null;
60 71
	private FeatureSelection          featureSelection      = null;
61
	
62
	public IntersectsSpatialJoinOperation(FlyrVectIVectorLayer targetLayer, String indexName, Summary summary, AbstractSextanteGeoProcess p) {
72

  
73
	public IntersectsSpatialJoinOperation(FlyrVectIVectorLayer targetLayer, SpatialIndex index, Summary summary, AbstractSextanteGeoProcess p) {
63 74
		super(p);
64
		FeatureIndexes indexes = targetLayer.getFeatureStore().getIndexes();
65
		index = indexes.getFeatureIndex(indexName);
75
	    this.index = index;
66 76
		this.summary = summary;
67 77
		storeOverlay = targetLayer.getFeatureStore();
68 78
	}
69
	
79

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

  
79 89
		if(featureSelection == null) {
80 90
			try {
81 91
				featureSelection = storeOverlay.getFeatureSelection();
......
85 95
		}
86 96

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

  
89
			Envelope rect = g.getEnvelope();
90
			FeatureSet featSet = index.getMatchFeatureSet(rect);
91
			
92
			boolean first = true;
93
			int numReg = 0;
94
			Iterator 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
			
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

  
113 121
		} catch(FeatureIndexException e) {
114 122
			Sextante.addErrorToLog(e);
115 123
		} catch (DataException e) {
116 124
			Sextante.addErrorToLog(e);
117 125
		}
118
		
126

  
119 127
		return lastEditFeature;
120 128
	}
121
	
129

  
122 130
	/**
123 131
	 * Builds a new output feature and adds it to the output file
124 132
	 * @param feat1
......
131 139
	private void buildNewFeature(Feature feat, Summary summary, int numReg) throws DataException {
132 140
		EditableFeature outFeat = persister.getOutputFeatureStore().createNewFeature();
133 141
		FeatureAttributeDescriptor[] attrs = feat.getType().getAttributeDescriptors();
134
		
142

  
135 143
		//Loads the old feature
136 144
		int pos = 0;
137 145
		for (int i = 0; i < attrs.length; i++) {
......
140 148
				pos ++;
141 149
			}
142 150
		}
143
		
151

  
144 152
		//Loads the statistics
145 153
		summary.loadEditableFeature(outFeat);
146
		
154

  
147 155
		//Loads the new field
148 156
		outFeat.set(SpatialJoinAlgorithm.NEW_FIELD, new Integer(numReg));
149
		
157

  
150 158
		//Saves the geometry
151 159
		try {
152 160
			List l = feat.getGeometries();
......
172 180
	 * @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
173 181
	 */
174 182
	public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) {
175
		
183

  
176 184
	}
177 185

  
178 186
}

Also available in: Unified diff