svn-gvsig-desktop / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / impl / spatialjoin / fmap / SpatiallyIndexedSpatialJoinVisitor.java @ 10626
History | View | Annotate | Download (6.14 KB)
1 |
/*
|
---|---|
2 |
* Created on 02-may-2006
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id: SpatiallyIndexedSpatialJoinVisitor.java 10626 2007-03-06 16:55:54Z caballero $
|
47 |
* $Log$
|
48 |
* Revision 1.2 2007-03-06 16:47:58 caballero
|
49 |
* Exceptions
|
50 |
*
|
51 |
* Revision 1.1 2006/06/20 18:20:45 azabala
|
52 |
* first version in cvs
|
53 |
*
|
54 |
* Revision 1.2 2006/06/02 18:21:28 azabala
|
55 |
* *** empty log message ***
|
56 |
*
|
57 |
* Revision 1.1 2006/05/24 21:09:47 azabala
|
58 |
* primera version en cvs despues de refactoring orientado a crear un framework extensible de geoprocessing
|
59 |
*
|
60 |
* Revision 1.1 2006/05/02 18:58:47 azabala
|
61 |
* first version in cvs
|
62 |
*
|
63 |
*
|
64 |
*/
|
65 |
package com.iver.cit.gvsig.geoprocess.impl.spatialjoin.fmap; |
66 |
|
67 |
import java.awt.geom.Rectangle2D; |
68 |
import java.util.List; |
69 |
|
70 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
71 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
72 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
73 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException; |
74 |
import com.iver.cit.gvsig.exceptions.visitors.VisitorException; |
75 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
76 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
77 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
78 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
79 |
import com.iver.cit.gvsig.fmap.spatialindex.INearestNeighbourFinder; |
80 |
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex; |
81 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeatureProcessor; |
82 |
import com.vividsolutions.jts.geom.Geometry; |
83 |
|
84 |
/**
|
85 |
* This visitor does spatial join with nearest neighbour criteria by using an
|
86 |
* spatial index with nearest neighbour searching capabilities.<br>
|
87 |
* By now, RTreeSptLib is the only spatial index that is also a
|
88 |
* INearestNeigbourFinder implementation. <br>
|
89 |
*
|
90 |
* @author azabala
|
91 |
*
|
92 |
*/
|
93 |
public class SpatiallyIndexedSpatialJoinVisitor extends |
94 |
NearestSpatialJoinVisitor { |
95 |
/**
|
96 |
* Geometry.distance() is a costly operation. Thats the reason for we are
|
97 |
* only using a nearest neighbour. TODO SpatialIndex works with Rectangle2D,
|
98 |
* and this may be a simplification that drives to errors. Make aditional
|
99 |
* probes to use a number of default neighbours
|
100 |
*/
|
101 |
static final int DEFAULT_NUM_NEIGBOURS = 1; |
102 |
|
103 |
/**
|
104 |
* Number of neighbours that nearestFinder must found.
|
105 |
*/
|
106 |
int numOfNeighbours = DEFAULT_NUM_NEIGBOURS;
|
107 |
|
108 |
/**
|
109 |
* Spetialized instance in nearest neighbour searchs.
|
110 |
*/
|
111 |
private INearestNeighbourFinder nearestFinder;
|
112 |
|
113 |
/**
|
114 |
* Constructor
|
115 |
*
|
116 |
* @param sourceLayer
|
117 |
* @param targetLayer
|
118 |
* @param processor
|
119 |
* @throws DriverException
|
120 |
*/
|
121 |
public SpatiallyIndexedSpatialJoinVisitor(FLyrVect sourceLayer,
|
122 |
FLyrVect targetLayer, FeatureProcessor processor) |
123 |
throws ReadDriverException {
|
124 |
super(sourceLayer, targetLayer, processor);
|
125 |
// lo comentamos porque debe ser llamado externamente
|
126 |
// initialize();
|
127 |
} |
128 |
|
129 |
public void initialize() { |
130 |
ISpatialIndex spatialIndex = targetLayer.getISpatialIndex(); |
131 |
if (spatialIndex instanceof INearestNeighbourFinder) |
132 |
nearestFinder = (INearestNeighbourFinder) spatialIndex; |
133 |
else
|
134 |
throw new IllegalArgumentException( |
135 |
"La segunda capa de spatial join ha de tener un ?ndice espacial de tipo INearestNeighbourFinder para usar este visitor");
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Processes a Feature of source layer, looking for its nearest feature of
|
140 |
* target layer and taking attributes from it
|
141 |
*/
|
142 |
public void visit(IGeometry g, int sourceIndex) throws VisitorException, ProcessVisitorException { |
143 |
if (g == null) |
144 |
return;
|
145 |
try {
|
146 |
// no se si el rtree har? la busqueda bien si el rectangulo
|
147 |
// de busqueda no est? insertado. Hacer las pruebas (si no,
|
148 |
// a?adimos ahora y borramos luego)
|
149 |
Geometry gJts = g.toJTSGeometry(); |
150 |
Rectangle2D rect = g.getBounds2D();
|
151 |
List nearestLst = nearestFinder.findNNearest(numOfNeighbours, rect);
|
152 |
int targetIndex = -1;// index of nearest neighbour |
153 |
double nearestDistance = Double.MAX_VALUE; |
154 |
ReadableVectorial rv = targetLayer.getSource(); |
155 |
for (int i = 0; i < nearestLst.size(); i++) { |
156 |
int index2 = ((Integer) nearestLst.get(i)).intValue(); |
157 |
IGeometry g2 = rv.getShape(index2); |
158 |
Geometry g2Jts = g2.toJTSGeometry(); |
159 |
double dist = gJts.distance(g2Jts);
|
160 |
if (dist <= nearestDistance) {
|
161 |
// by adding <=, we follow the convention that
|
162 |
// if two features are at the same distance, take
|
163 |
// the last as nearest neighbour
|
164 |
nearestDistance = dist; |
165 |
targetIndex = index2; |
166 |
}// if
|
167 |
}// for
|
168 |
if (targetIndex == -1) |
169 |
throw new ProcessVisitorException(targetRecordset.getName(),null, |
170 |
"Problemas durante el spatial join, no se encontr? un vecino mas proximo");
|
171 |
IFeature joinedFeature = createFeature(g, sourceIndex, targetIndex, |
172 |
nearestDistance); |
173 |
this.featureProcessor.processFeature(joinedFeature);
|
174 |
} catch (ReadDriverException e) {
|
175 |
throw new ProcessVisitorException(targetRecordset.getName(),e, |
176 |
"Error de driver al escribir un feature resultante de un spatial join");
|
177 |
} catch (ExpansionFileReadException e) {
|
178 |
throw new ProcessVisitorException(targetRecordset.getName(),e, |
179 |
"Error de driver al escribir un feature resultante de un spatial join");
|
180 |
} |
181 |
|
182 |
} |
183 |
|
184 |
} |