Revision 37951 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/spatialindex/QuadtreeGt2.java

View differences:

QuadtreeGt2.java
61 61
import java.awt.geom.Rectangle2D;
62 62
import java.io.File;
63 63
import java.io.IOException;
64
import java.nio.BufferUnderflowException;
64 65
import java.util.ArrayList;
65 66
import java.util.Collection;
66 67
import java.util.List;
......
153 154
				return;
154 155
			}
155 156
		}
156
		quadtree = new QuadTree(numRecs, this.bounds);
157
		// FJP: Change to avoid too much depth. (Produces a bug with very big layers)
158
		int maxCalculatedDepth = calculateMaxDepth(numRecords);
159
		if (maxCalculatedDepth > 14)
160
			maxCalculatedDepth = 14;
161
		quadtree = new QuadTree(numRecs, maxCalculatedDepth, this.bounds);
157 162
	}
163
	
164
	private int calculateMaxDepth(int numShapes) {
165
        /* No max depth was defined, try to select a reasonable one
166
         * that implies approximately 8 shapes per node.
167
         */
168
        int numNodes = 1;
169
        int maxDepth = 0;
170
          
171
        while(numNodes * 4 < numShapes) {
172
            maxDepth += 1;
173
            numNodes = numNodes * 2;
174
        }
175
        return maxDepth;
158 176

  
177
	}
178
	// End FJP: depth
179

  
159 180
	/**
160 181
	 * If the spatial index file exists and has content
161 182
	 */
......
175 196
	}
176 197

  
177 198

  
178
	public List query(Rectangle2D rect) {
199
	public synchronized List query(Rectangle2D rect) {
179 200
		try {
180 201
			return (List) queryQuadTree(toJtsEnvelope(rect));
181 202
		} catch (IOException e) {
......
270 291
        List solution = null;
271 292
		if ((quadtree != null)){
272 293
				//&& !bbox.contains(quadtree.getRoot().getBounds())) {
273
			solution = quadtree.query(bbox);
294
			try {
295
				solution = quadtree.query(bbox);
296
			}
297
			catch (Exception e) {
298
				e.printStackTrace();
299
				close();
300
				openQuadTree();
301
				solution = quadtree.query(bbox);
302
			}
274 303
//            tmp = quadtree.search(bbox);
275 304
//            if( tmp==null || !tmp.isEmpty())
276 305
//            	return tmp;

Also available in: Unified diff