gvsig-geoprocess / 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 @ 64
History | View | Annotate | Download (5.23 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2010 Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.sextante.app.algorithm.spatialjoin; |
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; |
26 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
27 |
import org.gvsig.fmap.dal.feature.Feature; |
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; |
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; |
38 |
|
39 |
import com.vividsolutions.jts.geom.Geometry; |
40 |
|
41 |
import es.unex.sextante.core.Sextante; |
42 |
|
43 |
/**
|
44 |
*
|
45 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
46 |
*/
|
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; |
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 |
|
73 |
/*
|
74 |
* (non-Javadoc)
|
75 |
* @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
76 |
*/
|
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;
|
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 |
} |
151 |
|
152 |
/*
|
153 |
* (non-Javadoc)
|
154 |
* @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
155 |
*/
|
156 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
157 |
|
158 |
} |
159 |
|
160 |
} |