Revision 64 org.gvsig.sextante/trunk/org.gvsig.sextante.app/org.gvsig.sextante.app.algorithm/org.gvsig.sextante.app.algorithm.spatialjoin/src/main/java/org/gvsig/sextante/app/algorithm/spatialjoin/IntersectsSpatialJoinOperation.java
IntersectsSpatialJoinOperation.java | ||
---|---|---|
18 | 18 |
*/ |
19 | 19 |
package org.gvsig.sextante.app.algorithm.spatialjoin; |
20 | 20 |
|
21 |
import java.util.Iterator; |
|
22 |
import java.util.List; |
|
23 |
|
|
24 |
import org.gvsig.fmap.dal.exception.DataException; |
|
25 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
|
21 | 26 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
22 | 27 |
import org.gvsig.fmap.dal.feature.Feature; |
23 |
import org.gvsig.fmap.geom.Geometry; |
|
28 |
import org.gvsig.fmap.dal.feature.FeatureIndex; |
|
29 |
import org.gvsig.fmap.dal.feature.FeatureIndexes; |
|
30 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
|
31 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
|
32 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
33 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
24 | 34 |
import org.gvsig.sextante.app.algorithm.base.core.GeometryOperation; |
35 |
import org.gvsig.sextante.app.algorithm.base.util.GeometryUtil; |
|
36 |
import org.gvsig.sextante.app.algorithm.dissolve.Summary; |
|
37 |
import org.gvsig.sextante.app.extension.core.gvVectorLayer; |
|
25 | 38 |
|
39 |
import com.vividsolutions.jts.geom.Geometry; |
|
40 |
|
|
41 |
import es.unex.sextante.core.Sextante; |
|
42 |
|
|
26 | 43 |
/** |
27 | 44 |
* |
28 | 45 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a> |
29 | 46 |
*/ |
30 | 47 |
public class IntersectsSpatialJoinOperation extends GeometryOperation { |
48 |
/** |
|
49 |
* Geometry.distance() is a costly operation. Thats the reason for we are |
|
50 |
* only using a nearest neighbor. TODO SpatialIndex works with Rectangle2D, |
|
51 |
* and this may be a simplification that drives to errors. Make additional |
|
52 |
* probes to use a number of default neighbors |
|
53 |
*/ |
|
54 |
static final int DEFAULT_NUM_NEIGBOURS = 1000; |
|
31 | 55 |
|
56 |
/** |
|
57 |
* Number of neighbors that nearestFinder must found. |
|
58 |
*/ |
|
59 |
int numOfNeighbours = DEFAULT_NUM_NEIGBOURS; |
|
60 |
|
|
61 |
/** |
|
62 |
* Specialized instance in nearest neighbor search. |
|
63 |
*/ |
|
64 |
private FeatureIndex index = null; |
|
65 |
private Summary summary = null; |
|
66 |
|
|
67 |
public IntersectsSpatialJoinOperation(gvVectorLayer targetLayer, String indexName, Summary summary) { |
|
68 |
FeatureIndexes indexes = targetLayer.getFeatureStore().getIndexes(); |
|
69 |
index = indexes.getFeatureIndex(indexName); |
|
70 |
this.summary = summary; |
|
71 |
} |
|
72 |
|
|
32 | 73 |
/* |
33 | 74 |
* (non-Javadoc) |
34 | 75 |
* @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature) |
35 | 76 |
*/ |
36 |
public EditableFeature invoke(Geometry g, Feature featureInput) { |
|
37 |
return null; |
|
77 |
@SuppressWarnings("unchecked") |
|
78 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) { |
|
79 |
if(g == null) |
|
80 |
return lastEditFeature; |
|
81 |
|
|
82 |
try { |
|
83 |
|
|
84 |
Envelope rect = g.getEnvelope(); |
|
85 |
FeatureSet featSet = index.getNearestFeatureSet(numOfNeighbours, rect); |
|
86 |
double nearestDistance = Double.MAX_VALUE; |
|
87 |
|
|
88 |
boolean first = true; |
|
89 |
DisposableIterator it = featSet.iterator(); |
|
90 |
while(it.hasNext()) { |
|
91 |
Feature feat = (Feature)it.next(); |
|
92 |
|
|
93 |
if(first) { |
|
94 |
summary.loadDefaultSummarizes(feat); |
|
95 |
first = false; |
|
96 |
} |
|
97 |
|
|
98 |
List geomList = feat.getGeometries(); |
|
99 |
|
|
100 |
if(geomList == null) { |
|
101 |
org.gvsig.fmap.geom.Geometry g2 = feat.getDefaultGeometry(); |
|
102 |
|
|
103 |
|
|
104 |
buildFeature(featureInput, feat, new Double(nearestDistance), g); |
|
105 |
continue; |
|
106 |
} |
|
107 |
|
|
108 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator(); |
|
109 |
while(itGeom.hasNext()) { |
|
110 |
org.gvsig.fmap.geom.Geometry g2 = itGeom.next(); |
|
111 |
|
|
112 |
|
|
113 |
buildFeature(featureInput, feat, new Double(nearestDistance), g); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
} catch(FeatureIndexException e) { |
|
118 |
Sextante.addErrorToLog(e); |
|
119 |
} catch (DataException e) { |
|
120 |
Sextante.addErrorToLog(e); |
|
121 |
} |
|
122 |
|
|
123 |
return lastEditFeature; |
|
38 | 124 |
} |
125 |
|
|
126 |
/** |
|
127 |
* Builds a feature and adds it to the output file |
|
128 |
* @param feat1 |
|
129 |
* @param feat2 |
|
130 |
* @param value |
|
131 |
* @param g |
|
132 |
* @throws DataException |
|
133 |
*/ |
|
134 |
private void buildFeature(Feature feat1, Feature feat2, Object value, org.gvsig.fmap.geom.Geometry g) throws DataException { |
|
135 |
EditableFeature outFeat = persister.getOutputFeatureStore().createNewFeature(); |
|
136 |
int sizeFeat1 = feat1.getType().size() - 1; |
|
137 |
|
|
138 |
for (int i = 0; i < sizeFeat1; i++) |
|
139 |
outFeat.set(i, feat1.get(i)); |
|
140 |
|
|
141 |
for (int i = sizeFeat1; i < sizeFeat1 + feat2.getType().size() - 1; i++) |
|
142 |
outFeat.set(i, feat2.get(i - sizeFeat1)); |
|
143 |
|
|
144 |
outFeat.set(outFeat.getType().size() - 2, value); |
|
145 |
try { |
|
146 |
persister.addFeature(outFeat, g); |
|
147 |
} catch (CreateGeometryException e) { |
|
148 |
Sextante.addErrorToLog(e); |
|
149 |
} |
|
150 |
} |
|
39 | 151 |
|
40 | 152 |
/* |
41 | 153 |
* (non-Javadoc) |
42 | 154 |
* @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature) |
43 | 155 |
*/ |
44 |
public void invoke(Geometry g, EditableFeature featureInput) { |
|
156 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) {
|
|
45 | 157 |
|
46 | 158 |
} |
47 | 159 |
|
Also available in: Unified diff