/** * @param <T> * @param storagePath * absolute path to the file * @return a new instance * @throws IOException */ public static <T> RTree<T> loadFromDisk( String storagePath ) throws IOException { RTree<T> tree = null; File storageFile = new File( storagePath ); DataInputStream dis = new DataInputStream( new FileInputStream( storageFile ) ); float[] loadedBbox = loadBboxFromDisk( dis ); int loadedBigM = dis.readInt(); tree = new RTree<T>( loadedBbox, loadedBigM ); tree.root = tree.loadNodeFromDisk( dis ); dis.close(); return tree; }
/** * @param shapeReader */ private static Pair<RTree<Long>, Boolean> createIndex( SHPReader shapeReader ) { Envelope env = shapeReader.getEnvelope(); // use 128 values per rect. RTree<Long> result = new RTree<Long>( createEnvelope( env ), -1 ); // to work around Java's non-existent variant type LOG.debug( "Read envelopes from shape file..." ); Pair<ArrayList<Pair<float[], Long>>, Boolean> p = shapeReader.readEnvelopes(); LOG.debug( "done reading envelopes." ); result.insertBulk( p.first ); return new Pair<RTree<Long>, Boolean>( result, p.second ); }
private void rebuildRtrees() { ftToIndex.clear(); for ( FeatureType ft : ftToFeatures.keySet() ) { FeatureCollection fc = ftToFeatures.get( ft ); Envelope env = fc.getEnvelope(); if ( env != null ) { RTree<Feature> index = new RTree<Feature>( toFloats( env ), 16 ); List<Pair<float[], Feature>> fBboxes = new ArrayList<Pair<float[], Feature>>( fc.size() ); for ( Feature f : fc ) { Envelope fEnv = f.getEnvelope(); if ( fEnv != null ) { float[] floats = toFloats( fEnv ); fBboxes.add( new Pair<float[], Feature>( floats, f ) ); } } index.insertBulk( fBboxes ); ftToIndex.put( ft, index ); } } }
/** * Adds the given {@link Feature} instance and updates the index structures. * * @param features * feature to be added, must not be <code>null</code> and must have an id (as well as every geometry) */ void addFeature( Feature feature ) { FeatureType ft = feature.getType(); FeatureCollection fc = ftToFeatures.get( ft ); if ( fc == null ) { fc = new GenericFeatureCollection(); ftToFeatures.put( ft, fc ); } fc.add( feature ); idToObject.put( feature.getId(), feature ); if ( feature.getEnvelope() != null ) { RTree<Feature> rTree = ftToIndex.get( ft ); float[] insertBox = toFloats( feature.getEnvelope() ); if ( rTree == null ) { rTree = new RTree<Feature>( insertBox, 16 ); } rTree.insert( insertBox, feature ); } }