Revision 33899

View differences:

tags/v2_0_0_Build_2020/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/jsi/JSIIndexLibrary.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal.index.spatial.jsi;
29

  
30
import org.gvsig.fmap.dal.DALLocator;
31
import org.gvsig.fmap.dal.DataTypes;
32
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
33
import org.gvsig.tools.library.AbstractLibrary;
34
import org.gvsig.tools.library.LibraryException;
35

  
36
public class JSIIndexLibrary extends AbstractLibrary {
37

  
38
	@Override
39
	protected void doInitialize() throws LibraryException {
40
	}
41

  
42
	@Override
43
	protected void doPostInitialize() throws LibraryException {
44
    	DataManagerProviderServices man = (DataManagerProviderServices) DALLocator.getDataManager();
45

  
46
    	if (!man.getFeatureIndexProviders().contains(JSIPersistentRTree.NAME)) {
47
			man.registerFeatureIndexProvider(JSIPersistentRTree.NAME, "Persistent RTree index based on JSI",
48
					JSIPersistentRTree.class, DataTypes.GEOMETRY);
49
    	}
50

  
51
    	if (!man.getFeatureIndexProviders().contains(JSIRTree.NAME)) {
52
			man.registerFeatureIndexProvider(JSIRTree.NAME, "RTree index based on JSI",
53
					JSIRTree.class, DataTypes.GEOMETRY);
54
    	}
55
	}
56
}
tags/v2_0_0_Build_2020/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/jsi/JSIRTree.java
1
/*
2
 * Created on 15-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: RTreeJsi.java 13884 2007-09-19 16:26:04Z jaume $
47
* $Log$
48
* Revision 1.5  2007-09-19 16:25:39  jaume
49
* ReadExpansionFileException removed from this context and removed unnecessary imports
50
*
51
* Revision 1.4  2007/06/27 20:17:30  azabala
52
* new spatial index (rix)
53
*
54
* Revision 1.3  2007/03/06 17:08:59  caballero
55
* Exceptions
56
*
57
* Revision 1.2  2006/06/05 16:59:08  azabala
58
* implementada busqueda de vecino mas proximo a partir de rectangulos
59
*
60
* Revision 1.1  2006/05/24 21:58:04  azabala
61
* *** empty log message ***
62
*
63
*
64
*/
65
package org.gvsig.fmap.dal.index.spatial.jsi;
66

  
67
import java.util.ArrayList;
68
import java.util.Iterator;
69
import java.util.List;
70
import java.util.Properties;
71

  
72
import org.gvsig.fmap.dal.exception.InitializeException;
73
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
74
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
75
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider;
76
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider;
77
import org.gvsig.fmap.geom.Geometry;
78
import org.gvsig.fmap.geom.primitive.Envelope;
79
import org.gvsig.fmap.geom.primitive.Point;
80

  
81
import com.infomatiq.jsi.IntProcedure;
82
import com.infomatiq.jsi.Rectangle;
83
import com.infomatiq.jsi.rtree.RTree;
84

  
85
/**
86
 * RTree spatial index implementation based in library
87
 * JSI (java spatial index).
88
 *
89
 * http://jsi.sourceforge.net/
90
 *
91
 * This RTree has better performance that Spatial Index Library
92
 * RTree, and that JTS'RTree, because
93
 * it uses the GNU's Trove Collections API.
94
 *
95
 * We are doing some probes with it, because it offers
96
 * a Nearest Neighbour algorithm implementation
97
 * (useful for Spatial Join geoprocess, for example).
98
 *
99
 * It isnt persistent, and We've found some problems
100
 * with delete operations.
101
 *
102
 *
103
 *
104
 *
105
 * @author azabala
106
 * @author jyarza
107
 *
108
 */
109
public class JSIRTree extends AbstractFeatureIndexProvider implements FeatureIndexProvider {
110

  
111
	public static final String NAME = JSIRTree.class.getSimpleName();
112

  
113
	protected RTree rtree;
114

  
115
	public JSIRTree() {
116
		rtree = new RTree();
117
	}
118

  
119

  
120
	public void initialize() throws InitializeException {
121
		Properties props = new Properties();
122
//		props.setProperty("MaxNodeEntries", "500");
123
//		props.setProperty("MinNodeEntries", "200");
124
		rtree.init(props);
125
	}
126

  
127
	class ListIntProcedure implements IntProcedure{
128
		ArrayList solution = new ArrayList();
129

  
130
		public boolean execute(int arg0) {
131
			solution.add(new Integer(arg0));
132
			return true;
133
		}
134

  
135
		public List getSolution(){
136
			return solution;
137
		}
138
	}
139

  
140
	protected List findNNearest(int numberOfNearest, Point point){
141
		com.infomatiq.jsi.Point jsiPoint =
142
			new com.infomatiq.jsi.Point((float)point.getX(), (float)point.getY());
143
		return (List) rtree.nearest(jsiPoint, numberOfNearest);
144
	}
145

  
146
	public Iterator iterator(){
147
		return rtree.iterator();
148
	}
149

  
150
	public int size(){
151
		return rtree.size();
152
	}
153

  
154
	protected Rectangle toJsiRect(Envelope env){
155
		Point min = env.getLowerCorner();
156
		Point max = env.getUpperCorner();
157

  
158
		Rectangle jsiRect = new Rectangle((float)min.getX(),
159
				(float)min.getY(),
160
				(float)max.getX(),
161
				(float)max.getY());
162
		return jsiRect;
163
	}
164

  
165
	public void insert(Object value, FeatureReferenceProviderServices fref) {
166
		Envelope env = getEnvelope(value);
167

  
168
		if (env == null) {
169
			throw new IllegalArgumentException("value is neither Geometry or Envelope");
170
		}
171
		
172
		Object oid = fref.getOID();
173
		if (!isCompatibleOID(oid)) {
174
			throw new IllegalArgumentException("OID type not compatible: " + oid.getClass().getName());
175
		}		
176

  
177
		rtree.add(toJsiRect(env), ((Number) oid).intValue());
178
	}
179

  
180
	public void delete(Object value, FeatureReferenceProviderServices fref) {
181
		Envelope env = getEnvelope(value);
182

  
183
		if (env == null) {
184
			throw new IllegalArgumentException("value is neither Geometry or Envelope");
185
		}
186
		
187
		Object oid = fref.getOID();
188
		if (!isCompatibleOID(oid)) {
189
			throw new IllegalArgumentException("OID type not compatible: " + oid.getClass().getName());
190
		}
191
		
192
		rtree.delete(toJsiRect(env), ((Number) oid).intValue());
193
	}
194

  
195
	public List match(Object value) throws FeatureIndexException {
196
		Envelope env = getEnvelope(value);
197

  
198
		if (env == null) {
199
			throw new IllegalArgumentException("value is neither Geometry or Envelope");
200
		}
201
		ListIntProcedure solution = new ListIntProcedure();
202
		rtree.intersects(toJsiRect(env), solution);
203
		return new LongList(solution.getSolution());
204
	}
205

  
206
	public List nearest(int count, Object value) {
207
		if (value == null) {
208
			throw new IllegalArgumentException("value is null");
209
		}
210
		
211
		if (value instanceof Point) {
212
			Point p = (Point) value;
213
			com.infomatiq.jsi.Point jsiPoint =
214
				new com.infomatiq.jsi.Point((float) p.getDirectPosition().getOrdinate(0), (float) p.getDirectPosition().getOrdinate(1));
215
			return (List) rtree.nearest(jsiPoint, count);
216
		} else {
217
			Envelope env = getEnvelope(value);
218

  
219
			if (env == null) {
220
				throw new IllegalArgumentException("value is neither Geometry or Envelope");
221
			}
222
			return new LongList((List) rtree.nearest(toJsiRect(env), count));
223
		}
224
	}
225

  
226

  
227
	public boolean isMatchSupported() {
228
		return true;
229
	}
230

  
231
	public boolean isNearestSupported() {
232
		return true;
233
	}
234

  
235
	public boolean isNearestToleranceSupported() {
236
		return false;
237
	}
238

  
239
	public boolean isRangeSupported() {
240
		return false;
241
	}
242

  
243
	public List nearest(int count, Object value, Object tolerance)
244
			throws FeatureIndexException {
245
		throw new UnsupportedOperationException();
246
	}
247

  
248
	public List range(Object value1, Object value2) throws FeatureIndexException {
249
		throw new UnsupportedOperationException();
250
	}
251
	
252
	/**
253
	 * Indicates whether the given OID's type is compatible
254
	 * with this provider
255
	 * 
256
	 * @param oid
257
	 * 
258
	 * @return
259
	 * 		true if this index provider supports the given oid type
260
	 */
261
	private boolean isCompatibleOID(Object oid) {
262
		if (!(oid instanceof Number)) {
263
			return false;
264
		}
265
		
266
		long num = ((Number) oid).longValue();
267
				 
268
		if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) {
269
			return false;
270
		}
271
		
272
		return true;
273
	}
274
	
275
	protected Envelope getEnvelope(Object value) {
276
		Envelope env = null;
277
		
278
		if (value instanceof Envelope) {
279
			env = (Envelope) value;
280
		} else if (value instanceof Geometry) {
281
			env = ((Geometry) value).getEnvelope();
282
		}
283
		return env;
284
	}	
285

  
286
}
287

  
tags/v2_0_0_Build_2020/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/jsi/JSIPersistentRTree.java
1
/*
2
 * Created on 13-jun-2007
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: PersistentRTreeJsi.java 12380 2007-06-27 20:17:30Z azabala $
47
* $Log$
48
* Revision 1.1  2007-06-27 20:17:30  azabala
49
* new spatial index (rix)
50
*
51
*
52
*/
53
package org.gvsig.fmap.dal.index.spatial.jsi;
54

  
55
import java.awt.geom.Point2D;
56
import java.io.File;
57
import java.io.FileNotFoundException;
58
import java.io.IOException;
59
import java.io.RandomAccessFile;
60
import java.nio.ByteOrder;
61
import java.nio.MappedByteBuffer;
62
import java.nio.channels.FileChannel;
63
import java.util.Iterator;
64
import java.util.LinkedHashMap;
65
import java.util.List;
66
import java.util.Properties;
67

  
68
import javax.imageio.stream.FileImageOutputStream;
69

  
70
import org.gvsig.fmap.dal.exception.InitializeException;
71
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
72
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
73
import org.gvsig.fmap.geom.Geometry;
74
import org.gvsig.fmap.geom.primitive.Envelope;
75

  
76
import com.infomatiq.jsi.Rectangle;
77
import com.infomatiq.jsi.rtree.RTree;
78

  
79
/**
80
 * Persistent spatial index which can resolve nearest neighbour queries.
81
 * <br>
82
 *
83
 * To use:
84
 *
85
 * PersistentRTreeJsi sptidx = new PersistentRtreeJsi("/home/kk");
86
 * if(sptidx.exists())
87
 *  sptidx.load();
88
 *
89
 *
90
 *  sptidx.add(rect, int);
91
 *  ...
92
 *  sptidx.add(rect2,int2);
93
 *  sptidx.flush();
94
 *
95
 * @author azabala
96
 *
97
 */
98
public class JSIPersistentRTree extends JSIRTree {
99

  
100
	public static final String NAME = JSIPersistentRTree.class.getSimpleName();
101

  
102
	/**
103
	 * Spatial index file
104
	 */
105
	private File file;
106

  
107
	private boolean hasChanged = false;
108
	/**
109
	 * Spatial index file extension
110
	 */
111
	final String rExt = ".rix";
112

  
113
	private LinkedHashMap  rectangles;
114

  
115
	public JSIPersistentRTree() {
116
		super();
117
	}
118

  
119
	public void initialize() throws InitializeException {
120
		super.initialize();
121
		try {
122
			file = File.createTempFile("RTreeJsi" + getFeatureIndexProviderServices().getTemporaryFileName(), rExt);
123
			rectangles = new LinkedHashMap();
124
			load(file);
125
		} catch (IOException e) {
126
			throw new InitializeException(e);
127
		} catch (FeatureIndexException e) {
128
			throw new InitializeException(e);
129
		}
130
	}
131

  
132
	public void flush(File f) throws FeatureIndexException {
133
		try {
134
			if(! hasChanged) {
135
				return;
136
			}
137
			RandomAccessFile file = new RandomAccessFile(f,
138
															"rw");
139
			FileImageOutputStream output = new FileImageOutputStream(file);
140
			output.setByteOrder(ByteOrder.LITTLE_ENDIAN);
141
			int numShapes = rtree.size();
142
			output.writeInt(numShapes);
143

  
144
			Iterator iterator = rtree.iterator();
145
			int count = 0;
146
			while(iterator.hasNext()){
147
				Integer  idx = (Integer) iterator.next();
148
				Rectangle nr = (Rectangle) rectangles.get(idx);
149
				float xmin = nr.min[0];
150
				float ymin = nr.min[1];
151

  
152
				float xmax = nr.max[0];
153
				float ymax = nr.max[1];
154

  
155
				output.writeFloat(xmin);
156
				output.writeFloat(ymin);
157
				output.writeFloat(xmax);
158
				output.writeFloat(ymax);
159

  
160
				output.writeInt(idx.intValue());
161
				count++;
162
			}
163
			output.flush();
164
			output.close();
165
			file.close();
166
			hasChanged = false;
167
		} catch (FileNotFoundException e) {
168
			throw new FeatureIndexException(e);
169
		} catch (IOException e) {
170
			throw new FeatureIndexException(e);
171
		}
172

  
173
	}
174

  
175
	private void load(File f) throws FeatureIndexException {
176
		if (f == null) {
177
			throw new IllegalArgumentException("File f cannot be null");
178
		}
179

  
180
		try {
181
			if(! f.exists()){
182
				return;
183
			}
184
			RandomAccessFile file = new RandomAccessFile(f, "r");
185
			FileChannel channel = file.getChannel();
186
			MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
187
			buf.order(ByteOrder.LITTLE_ENDIAN);
188
			int numShapes = buf.getInt();
189
			for(int i = 0; i < numShapes; i++){
190
				float xmin, ymin, xmax, ymax;
191
				int shapeIndex;
192
				xmin = buf.getFloat();
193
				ymin = buf.getFloat();
194
				xmax = buf.getFloat();
195
				ymax = buf.getFloat();
196
				shapeIndex = buf.getInt();
197

  
198
				Rectangle jsiRect = new Rectangle(xmin, ymin, xmax, ymax);
199
				rtree.add(jsiRect, shapeIndex);
200
			}
201
		}catch(Exception e){
202
			throw new FeatureIndexException(e);
203
		}
204
	}
205

  
206
	public void insert(Object value, FeatureReferenceProviderServices fref) {	
207
		super.insert(value, fref);
208
		Envelope env = getEnvelope(value);
209
		rectangles.put(fref.getOID(), toJsiRect(env));
210
		hasChanged = true;
211
	}
212

  
213

  
214
	public void delete(Object value, FeatureReferenceProviderServices fref) {
215
		super.delete(value, fref);
216
		rectangles.remove(fref.getOID());
217
		hasChanged = true;
218
	}
219

  
220
}
221

  
tags/v2_0_0_Build_2020/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/spatialindex/SPTLIBRTree.java
1
package org.gvsig.fmap.dal.index.spatial.spatialindex;
2

  
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.io.IOException;
6
import java.util.ArrayList;
7
import java.util.List;
8

  
9
import org.gvsig.fmap.dal.exception.InitializeException;
10
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
11
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
12
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider;
13
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider;
14
import org.gvsig.fmap.geom.Geometry;
15
import org.gvsig.fmap.geom.primitive.Envelope;
16
import org.gvsig.fmap.geom.primitive.Point;
17

  
18
import spatialindex.rtree.RTree;
19
import spatialindex.spatialindex.IData;
20
import spatialindex.spatialindex.INode;
21
import spatialindex.spatialindex.IVisitor;
22
import spatialindex.spatialindex.Region;
23
import spatialindex.storagemanager.DiskStorageManager;
24
import spatialindex.storagemanager.IBuffer;
25
import spatialindex.storagemanager.IStorageManager;
26
import spatialindex.storagemanager.PropertySet;
27
import spatialindex.storagemanager.RandomEvictionsBuffer;
28

  
29
/**
30
 * <p>
31
 * RTree spatial index based in spatial index library: <br>
32
 * http://u-foria.org/marioh/spatialindex/index.html <br>
33
 * marioh@cs.ucr.edu
34
 * </p>
35
 * It has the problem that spatial index file creation is a bit slowly (in
36
 * comparation with other indexes).
37
 */
38
public class SPTLIBRTree extends AbstractFeatureIndexProvider implements
39
		FeatureIndexProvider {
40

  
41
	public static final String NAME = SPTLIBRTree.class.getSimpleName();
42
	/**
43
	 * Page size of associated file
44
	 */
45
	private static final int defaultPageSize = 32 * 1024;
46
	private static final double defaultFillFactor = 0.85d;
47

  
48
	/**
49
	 * Size of memory buffer of the index
50
	 */
51
	private static final int BUFFER_SIZE = 25000;
52
	RTree rtree;
53
	String fileName;
54
	IStorageManager diskfile;
55

  
56
	public SPTLIBRTree() {
57

  
58
	}
59

  
60
	public void initialize() throws InitializeException {
61
		try {
62
			PropertySet ps = new PropertySet();
63
			ps.setProperty("Overwrite", new Boolean(false));
64
			// .idx and .dat extensions will be added.
65
			fileName = getFeatureIndexProviderServices().getNewFileName(null, null);
66
			ps.setProperty("FileName", fileName);
67
			ps.setProperty("PageSize", new Integer(defaultPageSize));
68
			diskfile = new DiskStorageManager(ps);
69
			load();
70
		} catch (SecurityException e) {
71
			throw new InitializeException(e);
72
		} catch (FileNotFoundException e) {
73
			throw new InitializeException(e);
74
		} catch (IOException e) {
75
			throw new InitializeException(e);
76
		}
77
	}
78

  
79
	/**
80
	 * If the spatial index file exists and has content
81
	 */
82
	public boolean exists() {
83
		return (new File(fileName + ".dat").length() != 0);
84
	}
85

  
86
	class RTreeVisitor implements IVisitor {
87
		ArrayList solution = new ArrayList();
88

  
89
		public void visitNode(INode n) {
90
		}
91

  
92
		public void visitData(IData d) {
93
			solution.add(new Integer(d.getIdentifier()));
94
		}
95

  
96
		public List getSolution() {
97
			return solution;
98
		}
99
	}
100

  
101
	public List containtmentQuery(Envelope env) {
102
		List solution = null;
103
		Region region = createRegion(env);
104
		RTreeVisitor visitor = new RTreeVisitor();
105
		rtree.containmentQuery(region, visitor);
106
		solution = visitor.getSolution();
107
		return solution;
108
	}
109

  
110
	/**
111
	 * Warn! This RTree implemention doesnt care if 'index' entry has been
112
	 * indexed yet
113
	 */
114
	public void insert(Envelope env, int index) {
115
		rtree.insertData(null, createRegion(env), index);
116
	}
117

  
118
	private Region createRegion(Envelope env) {
119
		double[] x = new double[2];
120
		double[] y = new double[2];
121
		x[0] = env.getLowerCorner().getX();
122
		y[0] = env.getLowerCorner().getY();
123
		x[1] = env.getUpperCorner().getX();
124
		y[1] = env.getUpperCorner().getY();
125
		return new Region(x, y);
126
	}
127

  
128
	/**
129
	 * Looks for N indexes nearest to the specified rect.
130
	 *
131
	 * @param numberOfNearest
132
	 * @param rect
133
	 * @return
134
	 */
135
	public List findNNearest(int numberOfNearest, Envelope env) {
136
		List solution = null;
137
		Region region = createRegion(env);
138
		RTreeVisitor visitor = new RTreeVisitor();
139
		rtree.nearestNeighborQuery(numberOfNearest, region, visitor);
140
		solution = visitor.getSolution();
141
		return solution;
142
	}
143

  
144
	/**
145
	 * Looks for the N indexes nearest to the specified point
146
	 *
147
	 * @param numberOfNearest
148
	 * @param point
149
	 * @return
150
	 */
151
	public List findNNearest(int numberOfNearest, Point point) {
152
		List solution = null;
153
		spatialindex.spatialindex.Point sptPoint = new spatialindex.spatialindex.Point(
154
				new double[] { point.getX(), point.getY() });
155
		RTreeVisitor visitor = new RTreeVisitor();
156
		rtree.nearestNeighborQuery(numberOfNearest, sptPoint, visitor);
157
		solution = visitor.getSolution();
158
		return solution;
159
	}
160

  
161
	public void flush() {
162
		rtree.flush();
163
	}
164

  
165
	public void load() {
166
		// applies a main memory random buffer on top of the persistent
167
		// storage manager
168
		IBuffer buffer = new RandomEvictionsBuffer(diskfile, BUFFER_SIZE, false);
169

  
170
		// Create a new, empty, RTree with dimensionality 2, minimum load
171
		// 70%, using "file" as
172
		// the StorageManager and the RSTAR splitting policy.
173
		PropertySet ps2 = new PropertySet();
174

  
175
		Double f = new Double(defaultFillFactor);
176
		ps2.setProperty("FillFactor", f);
177

  
178
		Integer i = new Integer(2);
179
		ps2.setProperty("Dimension", i);
180

  
181
		File file = new File(fileName + ".dat");
182
		if (file.length() != 0) {
183
			ps2.setProperty("IndexIdentifier", new Integer(1));
184
		}
185
		rtree = new RTree(ps2, buffer);
186
	}
187

  
188
	public void load(File f) throws FeatureIndexException {
189
		load();
190
	}
191

  
192
	public void flush(File f) throws FeatureIndexException {
193
		flush();
194
	}
195

  
196
	public void close() {
197
	}
198

  
199
	public File getFile() {
200
		return new File(fileName + ".dat");
201
	}
202

  
203
	public void delete(Object value, FeatureReferenceProviderServices fref) {
204
		rtree.deleteData(createRegion(((Geometry) value).getEnvelope()), ((Integer) fref
205
				.getOID()).intValue());
206
	}
207

  
208
	public void insert(Object value, FeatureReferenceProviderServices fref) {
209
		Envelope env = null;
210
		if (value instanceof Envelope) {
211
			env = (Envelope) value;
212
		} else if (value instanceof Geometry) {
213
			env = ((Geometry) value).getEnvelope();
214
		}
215
		rtree.insertData(null, createRegion(env), ((Integer) fref
216
				.getOID()).intValue());
217
	}
218

  
219
	public List match(Object value) throws FeatureIndexException {
220
		List solution = null;
221
		Envelope env = null;
222
		if (value instanceof Envelope) {
223
			env = (Envelope) value;
224
		} else if (value instanceof Geometry) {
225
			env = ((Geometry) value).getEnvelope();
226
		}
227
		Region region = createRegion(env);
228
		RTreeVisitor visitor = new RTreeVisitor();
229
		rtree.intersectionQuery(region, visitor);
230
		solution = visitor.getSolution();
231
		return new LongList(solution);
232
	}
233

  
234
	public List match(Object min, Object max) {
235
		throw new UnsupportedOperationException();
236
	}
237

  
238
	public List nearest(int count, Object value) throws FeatureIndexException {
239
		if (value instanceof Envelope) {
240
			return this.findNNearest(count, (Envelope) value);
241
		} else if (value instanceof Point) {
242
			return this.findNNearest(count, (Point) value);
243
		} else if (value instanceof Geometry) {
244
			return this.findNNearest(count, ((Geometry) value).getEnvelope());
245
		} else {
246
			throw new IllegalArgumentException ("value must be either an Envelope or either a Point2D");
247
		}
248
	}
249

  
250
	public boolean isMatchSupported() {
251
		return true;
252
	}
253

  
254
	public boolean isNearestSupported() {
255
		return true;
256
	}
257

  
258
	public boolean isNearestToleranceSupported() {
259
		return false;
260
	}
261

  
262
	public boolean isRangeSupported() {
263
		// TODO Auto-generated method stub
264
		return false;
265
	}
266

  
267
	public List nearest(int count, Object value, Object tolerance)
268
			throws FeatureIndexException {
269
		throw new UnsupportedOperationException();
270
	}
271

  
272
	public List range(Object value1, Object value2)
273
			throws FeatureIndexException {
274
		throw new UnsupportedOperationException();
275
	}
276
}
tags/v2_0_0_Build_2020/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/spatialindex/SPTLIBIndexLibrary.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27

  
28
package org.gvsig.fmap.dal.index.spatial.spatialindex;
29

  
30
import org.gvsig.fmap.dal.DALLocator;
31
import org.gvsig.fmap.dal.DataTypes;
32
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
33
import org.gvsig.tools.library.AbstractLibrary;
34
import org.gvsig.tools.library.LibraryException;
35

  
36
public class SPTLIBIndexLibrary extends AbstractLibrary {
37

  
38
	@Override
39
	protected void doInitialize() throws LibraryException {
40
	}
41

  
42
	@Override
43
	protected void doPostInitialize() throws LibraryException {
44
    	DataManagerProviderServices man = (DataManagerProviderServices) DALLocator.getDataManager();
45

  
46

  
47
    	if (!man.getFeatureIndexProviders().contains(SPTLIBRTree.NAME)) {
48
			man.registerFeatureIndexProvider(SPTLIBRTree.NAME, "RTree index based on spatialindex",
49
					SPTLIBRTree.class, DataTypes.GEOMETRY);
50
    	}
51
	}
52
}
53

  
tags/v2_0_0_Build_2020/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/gt2/GT2Quadtree.java
1
/*
2
 * Created on 16-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: QuadtreeGt2.java 10627 2007-03-06 17:10:21Z caballero $
47
 * $Log$
48
 * Revision 1.3  2007-03-06 17:08:59  caballero
49
 * Exceptions
50
 *
51
 * Revision 1.2  2006/11/29 19:27:59  azabala
52
 * bug fixed (caused when we query for a bbox which is greater or equal to a layer bbox)
53
 *
54
 * Revision 1.1  2006/05/24 21:58:04  azabala
55
 * *** empty log message ***
56
 *
57
 *
58
 */
59
package org.gvsig.fmap.dal.index.spatial.gt2;
60

  
61
import java.io.File;
62
import java.io.IOException;
63
import java.util.List;
64
import java.util.Stack;
65

  
66
import org.geotools.index.quadtree.Node;
67
import org.geotools.index.quadtree.QuadTree;
68
import org.geotools.index.quadtree.StoreException;
69
import org.geotools.index.quadtree.fs.FileSystemIndexStore;
70
import org.geotools.index.quadtree.fs.IndexHeader;
71
import org.gvsig.fmap.dal.exception.InitializeException;
72
import org.gvsig.fmap.dal.feature.FeatureStore;
73
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
74
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
75
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider;
76
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider;
77
import org.gvsig.fmap.geom.Geometry;
78
import org.gvsig.fmap.geom.primitive.Point;
79
import org.gvsig.tools.exception.BaseException;
80

  
81
import com.vividsolutions.jts.geom.Envelope;
82

  
83
/**
84
 * This Quadtree spatial index implementation is based in a fork of
85
 * org.geotools.index.quadtree.Quadtree implementation. <br>
86
 * This implementation offers us:
87
 * <ol>
88
 * <li>Persistence of spatial index</li>
89
 * </ol>
90
 * We had to fork geotools quadtree for many reasons:
91
 * <ol>
92
 * <li>It was strongly dependent of SHP format, so it returned not only a num
93
 * of rectangle, it also returned byte offset of this rectangle in shp file</li>
94
 * <li> Query artifact wasnt run well at all </li>
95
 * </ol>
96
 *
97
 * @author azabala
98
 *
99
 */
100
public class GT2Quadtree extends AbstractFeatureIndexProvider implements FeatureIndexProvider {
101

  
102
	public static final String NAME = GT2Quadtree.class.getSimpleName();
103
	/**
104
	 * Geotools quadtree implementation
105
	 */
106
	QuadTree quadtree;
107
	/**
108
	 * Persistent storage
109
	 */
110
	String fileName;
111
	/**
112
	 * Spatial index file extension
113
	 */
114
	final String qExt = ".qix";
115
	/**
116
	 * qix format has many versions, and allows different byte orders.
117
	 */
118
	String byteOrder;
119
	/**
120
	 * Bounds of the layer to index
121
	 */
122
	//Envelope bounds;
123
	/**
124
	 * Number of records of the layer to index
125
	 */
126
	//int numRecs = 0;
127

  
128
	boolean inMemory = false;
129

  
130
	public GT2Quadtree() {
131

  
132
	}
133

  
134
	public void initialize() throws InitializeException {
135
		try {
136
			File file = File.createTempFile(getFeatureStore().getName(), ".qix");
137
			this.fileName = file.getAbsolutePath();
138
			org.gvsig.fmap.geom.primitive.Envelope env = getFeatureStore()
139
					.getEnvelope();
140
			int featureCount = (int) getFeatureStore().getFeatureCount();
141
			this.byteOrder = "NM";
142
			quadtree = new QuadTree(featureCount, toJtsEnvelope(env));
143
			if (exists()) {
144
				load();
145
			}
146
		} catch (IOException e) {
147
			throw new InitializeException(e);
148
		} catch (BaseException e) {
149
			throw new InitializeException(e);
150
		}
151
	}
152

  
153
	/**
154
	 * If the spatial index file exists and has content
155
	 */
156
	public boolean exists() {
157
		return (new File(fileName).length() != 0);
158
	}
159

  
160
	public void load() throws FeatureIndexException {
161
		if (quadtree == null) {
162
			load(new File(fileName));
163
		}
164
	}
165

  
166
	public void load(File f) throws FeatureIndexException {
167
		try {
168
			FileSystemIndexStore store = new FileSystemIndexStore(f);
169
			quadtree = store.load();
170
			this.fileName = f.getAbsolutePath();
171
		} catch (StoreException e) {
172
			throw new FeatureIndexException(e);
173
		}
174
	}
175

  
176
	/**
177
	 * Inserts an object in the index
178
	 */
179
	public void insert(org.gvsig.fmap.geom.primitive.Envelope env, int index) {
180
		if (env == null) {
181
			throw new IllegalArgumentException("Envelope cannot be null");
182
		}
183
		Envelope e = toJtsEnvelope(env);
184
		if (e == null) {
185
			throw new IllegalStateException(
186
					"JTS Envelope conversion returns null");
187
		}
188
		System.out.println("recno=" + index);
189
		if (quadtree == null) {
190
			throw new IllegalStateException("quadtree is null");
191
		}
192
		try {
193
			quadtree.insert(index, toJtsEnvelope(env));
194
		} catch (StoreException se) {
195
			// TODO Auto-generated catch block
196
			se.printStackTrace();
197
		}
198
	}
199

  
200
	public void delete(org.gvsig.fmap.geom.primitive.Envelope env, int index) {
201
		if (inMemory) {
202
			quadtree.delete(toJtsEnvelope(env), index);
203
		}
204
	}
205

  
206
	public Envelope toJtsEnvelope(org.gvsig.fmap.geom.primitive.Envelope env) {
207
		if (env == null) {
208
			return null;
209
		}
210
		Point min = env.getLowerCorner();
211
		Point max = env.getUpperCorner();
212

  
213
		return new Envelope(min.getX(), max.getX(), min.getY(), max.getY());
214
	}
215

  
216
	/**
217
	 *
218
	 * @throws StoreException
219
	 * @deprecated
220
	 */
221
	void openQuadTree() throws StoreException {
222
		if (quadtree == null) {
223
			File file = new File(this.fileName);
224
			// Intento de cargar todo el quadtree en memoria
225
			FileSystemIndexStore store = new FileSystemIndexStore(file);
226
			quadtree = store.load();
227
		}
228
	}
229

  
230
	void openQuadTreeInMemory() throws StoreException {
231
		if (quadtree == null) {
232
			File file = new File(fileName);
233
			// Intento de cargar todo el quadtree en memoria
234
			FileSystemIndexStore store = new FileSystemIndexStore(file);
235
			QuadTree filequadtree = store.load();
236
			quadtree = new QuadTree(filequadtree.getNumShapes(), filequadtree
237
					.getMaxDepth(), filequadtree.getRoot().getBounds());
238
			Stack nodes = new Stack();
239
			nodes.push(filequadtree.getRoot());
240
			while (nodes.size() != 0) {
241
				Node node = (Node) nodes.pop();
242
				Envelope nodeEnv = node.getBounds();
243
				int[] shapeIds = node.getShapesId();
244
				for (int i = 0; i < shapeIds.length; i++) {
245
					quadtree.insert(shapeIds[i], nodeEnv);
246
				}
247
				int numSubnodes = node.getNumSubNodes();
248
				for (int i = 0; i < numSubnodes; i++) {
249
					nodes.push(node.getSubNode(i));
250
				}
251
			}// while
252
			filequadtree.close();
253
		}
254
	}
255

  
256
	public void flush() throws FeatureIndexException {
257
		flush(new File(fileName));
258
	}
259

  
260
	public void flush(File f) throws FeatureIndexException {
261
		byte order = 0;
262
		if ((byteOrder == null) || byteOrder.equalsIgnoreCase("NM")) {
263
			order = IndexHeader.NEW_MSB_ORDER;
264
		} else if (byteOrder.equalsIgnoreCase("NL")) {
265
			order = IndexHeader.NEW_LSB_ORDER;
266
		}
267
		FileSystemIndexStore store = new FileSystemIndexStore(f, order);
268
		try {
269
			store.store(quadtree);
270
			this.fileName = f.getAbsolutePath();
271
		} catch (StoreException e) {
272
			throw new FeatureIndexException(e);
273
		}
274
	}
275

  
276
	public File getFile() {
277
		return new File(this.fileName);
278
	}
279

  
280
	private FeatureStore getFeatureStore() {
281
		return getFeatureIndexProviderServices()
282
				.getFeatureStoreProviderServices().getFeatureStore();
283
	}
284

  
285
	public void delete(Object value, FeatureReferenceProviderServices fref) {
286

  
287
		if (!isCompatibleOID(fref.getOID())) {
288
			throw new IllegalArgumentException("OID not compatible. Must be an instance of Number within the Integer range.");
289
		}
290

  
291
		delete(((org.gvsig.fmap.geom.Geometry) value).getEnvelope(), ((Number) fref.getOID()).intValue());
292
	}
293

  
294
	public void insert(Object value, FeatureReferenceProviderServices fref) {
295

  
296
		if (!isCompatibleOID(fref.getOID())) {
297
			throw new IllegalArgumentException("OID not compatible. Must be an instance of Number within the Integer range.");
298
		}
299

  
300
		insert(((org.gvsig.fmap.geom.Geometry) value).getEnvelope(), ((Number) fref.getOID()).intValue());
301
	}
302

  
303
	public List match(Object value) throws FeatureIndexException {
304
		if (quadtree == null) {
305
			throw new IllegalStateException("This quadtree is null.");
306
		}
307
		if (value == null) {
308
			throw new IllegalArgumentException("Envelope cannot be null.");
309
		}
310
		if (!(value instanceof org.gvsig.fmap.geom.primitive.Envelope)) {
311
			throw new IllegalArgumentException("Not an envelope.");
312
		}
313
		org.gvsig.fmap.geom.primitive.Envelope env = null;
314
		if (value instanceof org.gvsig.fmap.geom.primitive.Envelope) {
315
			env = (org.gvsig.fmap.geom.primitive.Envelope) value;
316
		} else if (value instanceof Geometry) {
317
			env = ((Geometry) value).getEnvelope();
318
		}
319
		return new LongList(quadtree.query(toJtsEnvelope(env)));
320
	}
321

  
322
	public List nearest(int count, Object value) throws FeatureIndexException {
323
		throw new UnsupportedOperationException();
324
	}
325

  
326
	public boolean isMatchSupported() {
327
		return true;
328
	}
329

  
330
	public boolean isNearestSupported() {
331
		return false;
332
	}
333

  
334
	public boolean isNearestToleranceSupported() {
335
		return false;
336
	}
337

  
338
	public boolean isRangeSupported() {
339
		return false;
340
	}
341

  
342
	public List nearest(int count, Object value, Object tolerance)
343
			throws FeatureIndexException {
344
		throw new UnsupportedOperationException();
345
	}
346

  
347
	public List range(Object value1, Object value2) throws FeatureIndexException {
348
		throw new UnsupportedOperationException();
349
	}
350

  
351
	/**
352
	 * Indicates whether the given OID's type is compatible
353
	 * with this provider
354
	 *
355
	 * @param oid
356
	 *
357
	 * @return
358
	 * 		true if this index provider supports the given oid type
359
	 */
360
	private boolean isCompatibleOID(Object oid) {
361
		if (!(oid instanceof Number)) {
362
			return false;
363
		}
364

  
365
		long num = ((Number) oid).longValue();
366

  
367
		if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) {
368
			return false;
369
		}
370

  
371
		return true;
372
	}
373

  
374
}
tags/v2_0_0_Build_2020/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/gt2/Gt2IndexLibrary.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
* MA  02110-1301, USA.
20
*
21
*/
22

  
23
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 {{Company}}   {{Task}}
26
*/
27

  
28

  
29
package org.gvsig.fmap.dal.index.spatial.gt2;
30

  
31
import org.gvsig.fmap.dal.DALLocator;
32
import org.gvsig.fmap.dal.DataTypes;
33
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
34
import org.gvsig.tools.library.AbstractLibrary;
35
import org.gvsig.tools.library.LibraryException;
36

  
37
public class Gt2IndexLibrary extends AbstractLibrary {
38

  
39
	@Override
40
	protected void doInitialize() throws LibraryException {
41
	}
42

  
43
	@Override
44
	protected void doPostInitialize() throws LibraryException {
45
    	DataManagerProviderServices man = (DataManagerProviderServices) DALLocator.getDataManager();
46

  
47
    	if (!man.getFeatureIndexProviders().contains(GT2Quadtree.NAME)) {
48
			man.registerFeatureIndexProvider(GT2Quadtree.NAME, "Quadtree index based on Geotools 2",
49
					GT2Quadtree.class, DataTypes.GEOMETRY);
50
    	}
51
	}
52
}
tags/v2_0_0_Build_2020/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/jts/JTSQuadtree.java
1
/*
2
 * Created on 28-abr-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: QuadtreeJts.java 11288 2007-04-19 17:32:50Z azabala $
47
* $Log$
48
* Revision 1.2  2007-04-19 17:32:50  azabala
49
* new constructor (fmap spatial index from an existing jts spatial index)
50
*
51
* Revision 1.1  2006/05/01 18:38:41  azabala
52
* primera version en cvs del api de indices espaciales
53
*
54
*
55
*/
56
package org.gvsig.fmap.dal.index.spatial.jts;
57

  
58
import java.util.List;
59

  
60
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
61
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
62
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider;
63
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider;
64
import org.gvsig.fmap.geom.Geometry;
65
import org.gvsig.fmap.geom.primitive.NullGeometry;
66
import org.gvsig.fmap.geom.primitive.Point;
67

  
68
import com.vividsolutions.jts.geom.Envelope;
69
import com.vividsolutions.jts.index.quadtree.Quadtree;
70
/**
71
 * Adapter for ISPatialIndex gvSIG's interface to
72
 * JTS Quadtree.
73
 *
74
 *
75
 * @author azabala
76
 *
77
 */
78
public class JTSQuadtree extends AbstractFeatureIndexProvider implements FeatureIndexProvider {
79

  
80
	public static final String NAME = JTSQuadtree.class.getName();
81

  
82
	private Quadtree quadtree;
83

  
84
	public JTSQuadtree() {
85
	}
86

  
87
	public void initialize() {
88
		quadtree = new Quadtree();
89
	}
90

  
91
	private Envelope fromEnvelope(org.gvsig.fmap.geom.primitive.Envelope env){
92
		Point min = env.getLowerCorner();
93
		Point max = env.getUpperCorner();
94
		Envelope env2 = new Envelope(min.getX(), max.getX(), min.getY(), max.getY());
95
		return env2;
96
	}
97

  
98
	public void delete(Object o, FeatureReferenceProviderServices fref) {
99
		Integer integer=new Integer(((Long)(fref).getOID()).intValue());
100
		quadtree.remove(
101
				fromEnvelope(((Geometry) o).getEnvelope()), integer);
102
	}
103

  
104
	public void insert(Object o, FeatureReferenceProviderServices fref) {
105
		if (o == null || o instanceof NullGeometry) {
106
			return;
107
		}
108
		Integer integer=new Integer(((Long)(fref).getOID()).intValue());
109
		quadtree.insert(
110
				fromEnvelope(((Geometry) o).getEnvelope()), integer);
111

  
112
	}
113

  
114
	public List match(Object value) throws FeatureIndexException {
115
		org.gvsig.fmap.geom.primitive.Envelope env = null;
116
		if (value instanceof org.gvsig.fmap.geom.primitive.Envelope) {
117
			env = (org.gvsig.fmap.geom.primitive.Envelope) value;
118
		} else if (value instanceof Geometry) {
119
			env = ((Geometry) value).getEnvelope();
120
		}
121
		return new LongList(quadtree.query(fromEnvelope(env)));
122
	}
123

  
124
	public List match(Object min, Object max) {
125
		throw new UnsupportedOperationException("Can't perform this kind of search.");
126
	}
127

  
128
	public List nearest(int count, Object value) throws FeatureIndexException {
129
		throw new UnsupportedOperationException("Can't perform this kind of search.");
130
	}
131

  
132
	public boolean isMatchSupported() {
133
		return true;
134
	}
135

  
136
	public boolean isNearestSupported() {
137
		return false;
138
	}
139

  
140
	public boolean isNearestToleranceSupported() {
141
		return false;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff