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 @ 94
History | View | Annotate | Download (4.83 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.List; |
22 |
|
23 |
import org.gvsig.fmap.dal.DataTypes; |
24 |
import org.gvsig.fmap.dal.exception.DataException; |
25 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
26 |
import org.gvsig.fmap.dal.feature.Feature; |
27 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
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.dissolve.Summary; |
36 |
import org.gvsig.sextante.app.extension.core.gvVectorLayer; |
37 |
import org.gvsig.tools.dispose.DisposableIterator; |
38 |
|
39 |
import es.unex.sextante.core.Sextante; |
40 |
|
41 |
/**
|
42 |
*
|
43 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
44 |
*/
|
45 |
public class IntersectsSpatialJoinOperation extends GeometryOperation { |
46 |
/**
|
47 |
* Specialized instance in nearest neighbor search.
|
48 |
*/
|
49 |
private FeatureIndex index = null; |
50 |
private Summary summary = null; |
51 |
|
52 |
public IntersectsSpatialJoinOperation(gvVectorLayer targetLayer, String indexName, Summary summary) { |
53 |
FeatureIndexes indexes = targetLayer.getFeatureStore().getIndexes(); |
54 |
index = indexes.getFeatureIndex(indexName); |
55 |
this.summary = summary;
|
56 |
} |
57 |
|
58 |
/*
|
59 |
* (non-Javadoc)
|
60 |
* @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
61 |
*/
|
62 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature featureInput) {
|
63 |
if(g == null) |
64 |
return lastEditFeature;
|
65 |
|
66 |
try {
|
67 |
|
68 |
Envelope rect = g.getEnvelope(); |
69 |
FeatureSet featSet = index.getMatchFeatureSet(rect); |
70 |
|
71 |
boolean first = true; |
72 |
int numReg = 0; |
73 |
DisposableIterator it = featSet.iterator(); |
74 |
while(it.hasNext()) {
|
75 |
Feature feat = (Feature)it.next(); |
76 |
|
77 |
if(first) {
|
78 |
summary.loadDefaultSummarizes(feat); |
79 |
first = false;
|
80 |
} |
81 |
|
82 |
summary.updateValues(feat); |
83 |
numReg ++; |
84 |
} |
85 |
|
86 |
buildNewFeature(featureInput, summary, numReg); |
87 |
|
88 |
} catch(FeatureIndexException e) {
|
89 |
Sextante.addErrorToLog(e); |
90 |
} catch (DataException e) {
|
91 |
Sextante.addErrorToLog(e); |
92 |
} |
93 |
|
94 |
return lastEditFeature;
|
95 |
} |
96 |
|
97 |
/**
|
98 |
* Builds a new output feature and adds it to the output file
|
99 |
* @param feat1
|
100 |
* @param feat2
|
101 |
* @param value
|
102 |
* @param g
|
103 |
* @throws DataException
|
104 |
*/
|
105 |
@SuppressWarnings("unchecked") |
106 |
private void buildNewFeature(Feature feat, Summary summary, int numReg) throws DataException { |
107 |
EditableFeature outFeat = persister.getOutputFeatureStore().createNewFeature(); |
108 |
FeatureAttributeDescriptor[] attrs = feat.getType().getAttributeDescriptors();
|
109 |
|
110 |
//Loads the old feature
|
111 |
int pos = 0; |
112 |
for (int i = 0; i < attrs.length; i++) { |
113 |
if (attrs[i].getDataType().getType() != DataTypes.GEOMETRY) {
|
114 |
outFeat.set(pos, feat.get(i)); |
115 |
pos ++; |
116 |
} |
117 |
} |
118 |
|
119 |
//Loads the statistics
|
120 |
summary.loadEditableFeature(outFeat); |
121 |
|
122 |
//Loads the new field
|
123 |
outFeat.set(SpatialJoinAlgorithm.NEW_FIELD, new Integer(numReg)); |
124 |
|
125 |
//Saves the geometry
|
126 |
try {
|
127 |
List l = feat.getGeometries();
|
128 |
if(l == null) { |
129 |
persister.addFeature(outFeat, feat.getDefaultGeometry()); |
130 |
} else {
|
131 |
EditableFeature editFeat = null;
|
132 |
for (int i = 0; i < l.size(); i++) { |
133 |
if(editFeat == null) { |
134 |
editFeat = persister.addFeature(outFeat, (org.gvsig.fmap.geom.Geometry)l.get(i)); |
135 |
} else {
|
136 |
persister.addGeometryToExistingFeature(editFeat, (org.gvsig.fmap.geom.Geometry)l.get(i)); |
137 |
} |
138 |
} |
139 |
} |
140 |
} catch (CreateGeometryException e) {
|
141 |
Sextante.addErrorToLog(e); |
142 |
} |
143 |
} |
144 |
|
145 |
/*
|
146 |
* (non-Javadoc)
|
147 |
* @see org.gvsig.sextante.app.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
148 |
*/
|
149 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature featureInput) { |
150 |
|
151 |
} |
152 |
|
153 |
} |