├── .settings ├── org.eclipse.m2e.core.prefs ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── codecov.yml ├── .gitignore ├── .project ├── .classpath ├── src ├── main │ └── java │ │ └── com │ │ └── github │ │ └── jnidzwetzki │ │ └── spatialindex │ │ ├── HyperrectangleEntity.java │ │ ├── Const.java │ │ ├── SpatialIndexException.java │ │ ├── rtree │ │ ├── RTreeNodeFactory.java │ │ ├── AbstractRTreeReader.java │ │ ├── QuadraticSeedPicker.java │ │ ├── mmf │ │ │ ├── DirectoryNode.java │ │ │ └── RTreeMMFReader.java │ │ ├── RTreeMemoryReader.java │ │ ├── RTreeSerializer.java │ │ ├── RTreeDirectoryNode.java │ │ └── RTreeBuilder.java │ │ ├── SpatialIndexReader.java │ │ ├── SpatialIndexBuilder.java │ │ └── SpatialIndexEntry.java └── test │ └── java │ └── org │ └── bboxdb │ └── storage │ └── rtree │ ├── TestRTreeMMFDeserializer.java │ ├── RTreeTestHelper.java │ ├── TestRTreeMemoryDeserializer.java │ └── TestRTreeIndex.java ├── DCO ├── .travis.yml ├── README.md ├── pom.xml └── LICENSE /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | range: "30..80" 3 | round: down 4 | precision: 2 5 | status: 6 | project: off 7 | patch: off 8 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | encoding/=UTF-8 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | /target/ 24 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore 7 | org.eclipse.jdt.core.compiler.release=disabled 8 | org.eclipse.jdt.core.compiler.source=1.8 9 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | spatial-index-java 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/HyperrectangleEntity.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex; 19 | 20 | import org.bboxdb.commons.math.Hyperrectangle; 21 | 22 | public interface HyperrectangleEntity { 23 | 24 | /** 25 | * Get the bounding box 26 | * @return 27 | */ 28 | public Hyperrectangle getHyperrectangle(); 29 | 30 | } -------------------------------------------------------------------------------- /src/test/java/org/bboxdb/storage/rtree/TestRTreeMMFDeserializer.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package org.bboxdb.storage.rtree; 19 | 20 | import com.github.jnidzwetzki.spatialindex.rtree.AbstractRTreeReader; 21 | import com.github.jnidzwetzki.spatialindex.rtree.mmf.RTreeMMFReader; 22 | 23 | public class TestRTreeMMFDeserializer extends TestRTreeMemoryDeserializer { 24 | 25 | @Override 26 | protected AbstractRTreeReader getRTreeReader() { 27 | return new RTreeMMFReader(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/Const.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex; 19 | 20 | import java.nio.ByteOrder; 21 | 22 | public class Const { 23 | 24 | /** 25 | * The Byte order for encoded values 26 | */ 27 | public final static ByteOrder APPLICATION_BYTE_ORDER = ByteOrder.BIG_ENDIAN; 28 | 29 | /** 30 | * The magic bytes at the beginning of every spatial index file 31 | */ 32 | public final static byte[] MAGIC_BYTES_SPATIAL_RTREE_INDEX = "java-spatial-index".getBytes(); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/SpatialIndexException.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex; 19 | 20 | public class SpatialIndexException extends Exception { 21 | 22 | /** 23 | * 24 | */ 25 | private static final long serialVersionUID = 7667805805585457167L; 26 | 27 | public SpatialIndexException() { 28 | } 29 | 30 | public SpatialIndexException(String message) { 31 | super(message); 32 | } 33 | 34 | public SpatialIndexException(Throwable cause) { 35 | super(cause); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /DCO: -------------------------------------------------------------------------------- 1 | Developer Certificate of Origin 2 | Version 1.1 3 | 4 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 5 | 1 Letterman Drive 6 | Suite D4700 7 | San Francisco, CA, 94129 8 | 9 | Everyone is permitted to copy and distribute verbatim copies of this 10 | license document, but changing it is not allowed. 11 | 12 | 13 | Developer's Certificate of Origin 1.1 14 | 15 | By making a contribution to this project, I certify that: 16 | 17 | (a) The contribution was created in whole or in part by me and I 18 | have the right to submit it under the open source license 19 | indicated in the file; or 20 | 21 | (b) The contribution is based upon previous work that, to the best 22 | of my knowledge, is covered under an appropriate open source 23 | license and I have the right under that license to submit that 24 | work with modifications, whether created in whole or in part 25 | by me, under the same open source license (unless I am 26 | permitted to submit under a different license), as indicated 27 | in the file; or 28 | 29 | (c) The contribution was provided directly to me by some other 30 | person who certified (a), (b) or (c) and I have not modified 31 | it. 32 | 33 | (d) I understand and agree that this project and the contribution 34 | are public and that a record of the contribution (including all 35 | personal information I submit with it, including my sign-off) is 36 | maintained indefinitely and may be redistributed consistent with 37 | this project or the open source license(s) involved. 38 | 39 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | 5 | before_install: 6 | - echo -n | openssl s_client -connect scan.coverity.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | sudo tee -a /etc/ssl/certs/ca- 7 | 8 | after_success: 9 | - bash <(curl -s https://codecov.io/bash) 10 | 11 | cache: 12 | directories: 13 | - $HOME/.m2 14 | 15 | sudo: false 16 | dist: trusty 17 | 18 | env: 19 | global: 20 | # The next declaration is the encrypted COVERITY_SCAN_TOKEN, created 21 | # via the "travis encrypt" command using the project repo's public key 22 | - secure: "KgI42WNLTtJPCT5C2/bpma9qHnJubc3vSAKvn2ePkyHatdUreaboknNmRfefrhOLhtq7Kjg5msZKlNjHFFyLtM0T+v2CWsfX810MtAwNkOKdSsFk9pyD3djDs8Z9Dy7L+BSVNfGqsS+LHkyWzub86JlU+1ynSroPDcdsOZvpROB/BYj8djbfqTSm1HmjIiz/ccWaokjaIlMCEzIXXDoLbb+oN3nr7iNxuHtq4e46q2AjPBUWH8eLQjMQVMuLfWNL0NeLHVCg2WHnTtxkPvypVAF2EHzXHZy345B6d+TKMSzzSHRgXS8bSaVoUe+wZ1uVjfrDL1jq69X7b82kko7z3dTu32Cfu5YtiniQjcMnY6fr2dNYV0kYqJTTLCfVCwsRpbzKMmZlpC0ur+gc/s49PEaVZOSytLmNF2vBhWwZLmwtTWrAstaIb0UK7uusigHoiqJcd9ddkAwspbHkhH0e0sRz1jxIUckc5/0g9bGLkkJmOFbkON2eQXaN1OSsvTxonBSHRRUIFtYKa/k+CdEVvtOMLnsD+ghjB0l9HgVI0JScmPnpJ8duWXNSRRRGIa2nrYMirm+WwgZiV40ghQN0KQcP1lH3dmouF0tm2jAOuModz7cWdVeO3jf4pJ91tyaLnm2tMIrlWRNFqvFrgH4rdbwTr+d0kjhnaRPt7SrE1Ow=" 23 | 24 | addons: 25 | coverity_scan: 26 | project: 27 | name: "jnidzwetzki/spatial-index-java" 28 | description: "Build submitted via Travis CI" 29 | notification_email: jnidzwetzki@gmx.de 30 | build_command_prepend: "mvn clean" 31 | build_command: "mvn -DskipTests=true compile" 32 | branch_pattern: coverity_scan 33 | 34 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/RTreeNodeFactory.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree; 19 | 20 | import java.util.concurrent.atomic.AtomicInteger; 21 | 22 | public class RTreeNodeFactory { 23 | 24 | /** 25 | * The node id generator 26 | */ 27 | protected final AtomicInteger nodeIdInteger; 28 | 29 | public RTreeNodeFactory() { 30 | this.nodeIdInteger = new AtomicInteger(0); 31 | } 32 | 33 | /** 34 | * Build a new directory node 35 | * @return 36 | */ 37 | public RTreeDirectoryNode buildDirectoryNode() { 38 | return new RTreeDirectoryNode(getNextNodeId()); 39 | } 40 | 41 | /** 42 | * Get the next available node id 43 | * @return 44 | */ 45 | protected int getNextNodeId() { 46 | return nodeIdInteger.getAndIncrement(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/SpatialIndexReader.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex; 19 | 20 | import java.io.Closeable; 21 | import java.io.RandomAccessFile; 22 | import java.util.List; 23 | 24 | import org.bboxdb.commons.math.Hyperrectangle; 25 | 26 | public interface SpatialIndexReader extends Closeable { 27 | 28 | /** 29 | * Persist the index 30 | * 31 | * @param inputStream 32 | */ 33 | public void readFromFile(final RandomAccessFile randomAccessFile) throws SpatialIndexException, InterruptedException; 34 | 35 | /** 36 | * Close the index 37 | */ 38 | public void close(); 39 | 40 | /** 41 | * Find the entries for the given region 42 | * @param Hyperrectangle 43 | * @return 44 | */ 45 | public List getEntriesForRegion(final Hyperrectangle Hyperrectangle) throws SpatialIndexException; 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/SpatialIndexBuilder.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex; 19 | 20 | import java.io.RandomAccessFile; 21 | import java.util.List; 22 | 23 | import org.bboxdb.commons.math.Hyperrectangle; 24 | 25 | 26 | public interface SpatialIndexBuilder { 27 | 28 | /** 29 | * Construct the index from a list of tuples 30 | * 31 | * @param tuples 32 | * @return 33 | */ 34 | public boolean bulkInsert(final List elements); 35 | 36 | /** 37 | * Insert one element into the index 38 | * 39 | * @param element 40 | * @return 41 | */ 42 | public boolean insert(final SpatialIndexEntry element); 43 | 44 | /** 45 | * Read the index from a data stream 46 | * 47 | * @param outputStream 48 | * @throws StorageManagerException 49 | */ 50 | public void writeToFile(final RandomAccessFile randomAccessFile) throws SpatialIndexException; 51 | 52 | /** 53 | * Find the entries for the given region 54 | * @param Hyperrectangle 55 | * @return 56 | */ 57 | public List getEntriesForRegion(final Hyperrectangle Hyperrectangle); 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/AbstractRTreeReader.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree; 19 | 20 | import java.io.IOException; 21 | import java.io.RandomAccessFile; 22 | import java.util.Arrays; 23 | 24 | import com.github.jnidzwetzki.spatialindex.Const; 25 | import com.github.jnidzwetzki.spatialindex.SpatialIndexException; 26 | import com.github.jnidzwetzki.spatialindex.SpatialIndexReader; 27 | 28 | public abstract class AbstractRTreeReader implements SpatialIndexReader { 29 | 30 | /** 31 | * The max size of a child node 32 | */ 33 | protected int maxNodeSize; 34 | 35 | /** 36 | * Get the max node size for the index 37 | * @return 38 | */ 39 | public int getMaxNodeSize() { 40 | return maxNodeSize; 41 | } 42 | 43 | /** 44 | * Validate the magic bytes of a stream 45 | * 46 | * @return a InputStream or null 47 | * @throws StorageManagerException 48 | * @throws IOException 49 | */ 50 | protected void validateStream(final RandomAccessFile randomAccessFile) throws IOException, SpatialIndexException { 51 | 52 | // Validate file - read the magic from the beginning 53 | final byte[] magicBytes = new byte[Const.MAGIC_BYTES_SPATIAL_RTREE_INDEX.length]; 54 | randomAccessFile.readFully(magicBytes, 0, Const.MAGIC_BYTES_SPATIAL_RTREE_INDEX.length); 55 | 56 | if(! Arrays.equals(magicBytes, Const.MAGIC_BYTES_SPATIAL_RTREE_INDEX)) { 57 | throw new SpatialIndexException("Spatial index file does not contain the magic bytes"); 58 | } 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/QuadraticSeedPicker.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree; 19 | 20 | import java.util.List; 21 | 22 | import org.bboxdb.commons.Pair; 23 | import org.bboxdb.commons.math.Hyperrectangle; 24 | 25 | import com.github.jnidzwetzki.spatialindex.HyperrectangleEntity; 26 | 27 | public class QuadraticSeedPicker { 28 | 29 | /** 30 | * Find the seeds for the split 31 | * @param insertedNode 32 | * @return 33 | */ 34 | protected Pair quadraticPickSeeds(final List allEntries) { 35 | 36 | assert(allEntries.size() >= 2); 37 | 38 | double maxWaste = Double.MAX_VALUE; 39 | Pair result = null; 40 | 41 | for(final T box1 : allEntries) { 42 | for(final T box2 : allEntries) { 43 | 44 | if(box1 == box2) { 45 | continue; 46 | } 47 | 48 | final Hyperrectangle Hyperrectangle1 = box1.getHyperrectangle(); 49 | final Hyperrectangle Hyperrectangle2 = box2.getHyperrectangle(); 50 | 51 | final double coveringArea 52 | = Hyperrectangle.getCoveringBox(Hyperrectangle1, Hyperrectangle2).getVolume(); 53 | 54 | final double waste = coveringArea - Hyperrectangle1.getVolume() 55 | - Hyperrectangle2.getVolume(); 56 | 57 | if(waste < maxWaste) { 58 | result = new Pair(box1, box2); 59 | maxWaste = waste; 60 | } 61 | } 62 | } 63 | 64 | assert(result != null) : "Unable to find seeds"; 65 | 66 | // Remove seeds from available objects 67 | allEntries.remove(result.getElement1()); 68 | allEntries.remove(result.getElement2()); 69 | 70 | return result; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spatial indexing algorithms for java (sia4j) 2 | 3 | Implementation of spatial indexing algorithms in java. At the moment, only an [r-tree](https://en.wikipedia.org/wiki/R-tree) index is implemented by this project. 4 | 5 | 6 | Build Status 7 | 8 | 9 | 10 | 11 | 12 | 13 | ## Features of the R-tree implementation 14 | * Supports rectangle geometries 15 | * Supports n-dimensional data 16 | * Support serializing to file 17 | * Can be used as in-memory data structure 18 | * R-Tree can be serialized and accessed via _memory mapped io_. This is usefull for very large datasets. 19 | 20 | ## Examples 21 | 22 | ### Building the r-tree and execute a rane query 23 | ```java 24 | // Two entries with a two-dimensional bounding box 25 | final SpatialIndexEntry entry1 = new SpatialIndexEntry(new Hyperrectangle(1d, 2d, 1d, 2d), "abc"); 26 | final SpatialIndexEntry entry2 = new SpatialIndexEntry(new Hyperrectangle(10d, 20d, 10d, 20d), "def"); 27 | 28 | final SpatialIndexBuilder index = new RTreeBuilder(); 29 | index.bulkInsert(Arrays.asList(entry1, entry2); 30 | 31 | // Query data 32 | final Hyperrectangle queryBox = new Hyperrectangle(1d, 1.5d, 1d, 1.5d); 33 | final List resultList = index.getEntriesForRegion(queryBox); 34 | ``` 35 | 36 | ### Write the r-tree into a file and read into memory 37 | ```java 38 | // Write and read to file 39 | final File tempFile = File.createTempFile("rtree-", "-test"); 40 | final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); 41 | index.writeToFile(raf); 42 | raf.close(); 43 | 44 | final AbstractRTreeReader indexRead = new RTreeMemoryReader(); 45 | final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r"); 46 | indexRead.readFromFile(rafRead); 47 | rafRead.close(); 48 | ``` 49 | 50 | ### Write the r-tree into a file and access the file via memory mapped io 51 | ```java 52 | // Write and read to file 53 | final File tempFile = File.createTempFile("rtree-", "-test"); 54 | final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); 55 | index.writeToFile(raf); 56 | raf.close(); 57 | 58 | final AbstractRTreeReader indexRead = new RTreeMMFReader(); 59 | final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r"); 60 | indexRead.readFromFile(rafRead); 61 | rafRead.close(); 62 | ``` -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/mmf/DirectoryNode.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree.mmf; 19 | 20 | import java.io.IOException; 21 | import java.nio.MappedByteBuffer; 22 | import java.util.ArrayList; 23 | import java.util.Arrays; 24 | import java.util.List; 25 | 26 | import org.bboxdb.commons.io.DataEncoderHelper; 27 | import org.bboxdb.commons.math.Hyperrectangle; 28 | 29 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 30 | import com.github.jnidzwetzki.spatialindex.rtree.RTreeBuilder; 31 | 32 | public class DirectoryNode { 33 | 34 | /** 35 | * The node id 36 | */ 37 | protected int nodeId; 38 | 39 | /** 40 | * The bounding box 41 | */ 42 | protected Hyperrectangle hyperrectangle; 43 | 44 | /** 45 | * The leaf node children 46 | */ 47 | protected final List indexEntries; 48 | 49 | /** 50 | * The child nodes 51 | */ 52 | protected final List childNodes; 53 | 54 | public DirectoryNode() { 55 | this.indexEntries = new ArrayList<>(); 56 | this.childNodes = new ArrayList<>(); 57 | } 58 | 59 | /** 60 | * Read the node from byte buffer 61 | * @param memory 62 | * @param maxNodeSize 63 | * @throws IOException 64 | */ 65 | public void initFromByteBuffer(final MappedByteBuffer memory, final int maxNodeSize) throws IOException { 66 | nodeId = memory.getInt(); 67 | 68 | // Bounding box data 69 | final int HyperrectangleLength = memory.getInt(); 70 | final byte[] HyperrectangleBytes = new byte[HyperrectangleLength]; 71 | memory.get(HyperrectangleBytes, 0, HyperrectangleBytes.length); 72 | 73 | hyperrectangle = Hyperrectangle.fromByteArray(HyperrectangleBytes); 74 | 75 | final byte[] followingByte = new byte[RTreeBuilder.MAGIC_VALUE_SIZE]; 76 | 77 | // Read index entries 78 | for(int i = 0; i < maxNodeSize; i++) { 79 | memory.get(followingByte, 0, followingByte.length); 80 | 81 | if(Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_FOLLOWING)) { 82 | final SpatialIndexEntry spatialIndexEntry = SpatialIndexEntry.readFromByteBuffer(memory); 83 | indexEntries.add(spatialIndexEntry); 84 | } else if(! Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_NOT_EXISTING)) { 85 | throw new IllegalArgumentException("Unknown node type following: " + followingByte); 86 | } 87 | } 88 | 89 | // Read pointer positions 90 | for(int i = 0; i < maxNodeSize; i++) { 91 | memory.get(followingByte, 0, followingByte.length); 92 | if(! Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_NOT_EXISTING)) { 93 | final int childPointer = DataEncoderHelper.readIntFromByte(followingByte); 94 | assert (childPointer > 0) : "Child pointer needs to be > 0 " + childPointer; 95 | childNodes.add(childPointer); 96 | } 97 | } 98 | } 99 | 100 | public Hyperrectangle getHyperrectangle() { 101 | return hyperrectangle; 102 | } 103 | 104 | public int getNodeId() { 105 | return nodeId; 106 | } 107 | 108 | public List getChildNodes() { 109 | return childNodes; 110 | } 111 | 112 | public List getIndexEntries() { 113 | return indexEntries; 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/mmf/RTreeMMFReader.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree.mmf; 19 | 20 | import java.io.IOException; 21 | import java.io.RandomAccessFile; 22 | import java.nio.MappedByteBuffer; 23 | import java.nio.channels.FileChannel; 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | import java.util.Queue; 27 | import java.util.concurrent.LinkedTransferQueue; 28 | import java.util.stream.Collectors; 29 | 30 | import org.bboxdb.commons.io.DataEncoderHelper; 31 | import org.bboxdb.commons.io.UnsafeMemoryHelper; 32 | import org.bboxdb.commons.math.Hyperrectangle; 33 | import org.slf4j.Logger; 34 | import org.slf4j.LoggerFactory; 35 | 36 | import com.github.jnidzwetzki.spatialindex.Const; 37 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 38 | import com.github.jnidzwetzki.spatialindex.SpatialIndexException; 39 | import com.github.jnidzwetzki.spatialindex.rtree.AbstractRTreeReader; 40 | 41 | public class RTreeMMFReader extends AbstractRTreeReader { 42 | 43 | /** 44 | * The mapped memory 45 | */ 46 | private MappedByteBuffer memory; 47 | 48 | /** 49 | * The file channel 50 | */ 51 | private FileChannel fileChannel; 52 | 53 | /** 54 | * The position of the first node 55 | */ 56 | private int firstNodePos; 57 | 58 | /** 59 | * The Logger 60 | */ 61 | private final static Logger logger = LoggerFactory.getLogger(RTreeMMFReader.class); 62 | 63 | @Override 64 | public void readFromFile(final RandomAccessFile randomAccessFile) 65 | throws SpatialIndexException, InterruptedException { 66 | 67 | try { 68 | validateStream(randomAccessFile); 69 | maxNodeSize = DataEncoderHelper.readIntFromDataInput(randomAccessFile); 70 | 71 | firstNodePos = (int) randomAccessFile.getFilePointer(); 72 | 73 | fileChannel = randomAccessFile.getChannel(); 74 | final long size = fileChannel.size(); 75 | memory = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, size); 76 | memory.order(Const.APPLICATION_BYTE_ORDER); 77 | } catch (IOException e) { 78 | throw new SpatialIndexException(e); 79 | } 80 | } 81 | 82 | @Override 83 | public void close() { 84 | 85 | if(memory != null) { 86 | UnsafeMemoryHelper.unmapMemory(memory); 87 | memory = null; 88 | } 89 | 90 | if(fileChannel != null) { 91 | try { 92 | fileChannel.close(); 93 | } catch (IOException e) { 94 | logger.error("Got IO exception while closing file channel", e); 95 | } 96 | fileChannel = null; 97 | } 98 | } 99 | 100 | @Override 101 | public synchronized List getEntriesForRegion(final Hyperrectangle Hyperrectangle) 102 | throws SpatialIndexException { 103 | 104 | final List resultList = new ArrayList<>(); 105 | final Queue readTasks = new LinkedTransferQueue<>(); 106 | readTasks.add(firstNodePos); 107 | 108 | try { 109 | 110 | while(! readTasks.isEmpty()) { 111 | 112 | final int position = readTasks.remove(); 113 | memory.position(position); 114 | final DirectoryNode directoryNode = new DirectoryNode(); 115 | directoryNode.initFromByteBuffer(memory, maxNodeSize); 116 | 117 | if(directoryNode.getHyperrectangle().intersects(Hyperrectangle)) { 118 | readTasks.addAll(directoryNode.getChildNodes()); 119 | 120 | final List foundEntries = 121 | directoryNode.getIndexEntries() 122 | .stream() 123 | .filter(e -> e.getHyperrectangle().intersects(Hyperrectangle)) 124 | .collect(Collectors.toList()); 125 | 126 | resultList.addAll(foundEntries); 127 | } 128 | } 129 | 130 | return resultList; 131 | } catch (IOException e) { 132 | throw new SpatialIndexException(e); 133 | } 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /src/test/java/org/bboxdb/storage/rtree/RTreeTestHelper.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package org.bboxdb.storage.rtree; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | import java.util.Random; 23 | import java.util.stream.Collectors; 24 | 25 | import org.bboxdb.commons.math.Hyperrectangle; 26 | import org.junit.Assert; 27 | 28 | import com.github.jnidzwetzki.spatialindex.SpatialIndexBuilder; 29 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 30 | import com.github.jnidzwetzki.spatialindex.SpatialIndexException; 31 | import com.github.jnidzwetzki.spatialindex.SpatialIndexReader; 32 | 33 | public class RTreeTestHelper { 34 | 35 | /** 36 | * Test the query 37 | * 38 | * @param entries 39 | * @param index 40 | * @throws StorageManagerException 41 | */ 42 | public static void queryIndex(final List entries, final SpatialIndexReader index) throws SpatialIndexException { 43 | 44 | for(final SpatialIndexEntry entry: entries) { 45 | final List resultList = index.getEntriesForRegion(entry.getHyperrectangle()); 46 | Assert.assertTrue(resultList.size() >= 1); 47 | 48 | final List keyResult = resultList 49 | .stream() 50 | .map(e -> e.getValue()) 51 | .filter(k -> k.equals(entry.getValue())) 52 | .collect(Collectors.toList()); 53 | 54 | Assert.assertTrue("Searching for: " + entry, keyResult.size() == 1); 55 | } 56 | } 57 | 58 | /** 59 | * Test the query 60 | * 61 | * @param entries 62 | * @param index 63 | */ 64 | public static void queryIndex(final List entries, final SpatialIndexBuilder index) { 65 | 66 | for(final SpatialIndexEntry entry: entries) { 67 | final List resultList = index.getEntriesForRegion(entry.getHyperrectangle()); 68 | Assert.assertTrue(resultList.size() >= 1); 69 | 70 | final List keyResult = resultList 71 | .stream() 72 | .map(e -> e.getValue()) 73 | .filter(k -> k.equals(entry.getValue())) 74 | .collect(Collectors.toList()); 75 | 76 | Assert.assertTrue("Searching for: " + entry, keyResult.size() == 1); 77 | } 78 | } 79 | 80 | /** 81 | * Generate a list of tuples 82 | * @return 83 | */ 84 | public static List getEntryList() { 85 | final List entryList = new ArrayList(); 86 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(0d, 1d, 0d, 1d), 1)); 87 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(1d, 2d, 1d, 3d), 2)); 88 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(2d, 3d, 0d, 1d), 3)); 89 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(3d, 4d, 3d, 7d), 4)); 90 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(1.2d, 2.2d, 0d, 1d), 5)); 91 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(4.6d, 5.6d, 0d, 1d), 6)); 92 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(5.2d, 6.2d, 4d, 5d), 7)); 93 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(5.1d, 6.1d, 0d, 1d), 8)); 94 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(6.1d, 7.1d, 0d, 1d), 9)); 95 | entryList.add(new SpatialIndexEntry(new Hyperrectangle(8.1d, 9.1d, 2d, 5d), 10)); 96 | return entryList; 97 | } 98 | 99 | /** 100 | * Generate some random tuples 101 | * @return 102 | */ 103 | public static List generateRandomTupleList(final int dimensions) { 104 | final List entryList = new ArrayList(); 105 | final Random random = new Random(); 106 | 107 | for(int i = 0; i < 5000; i++) { 108 | final double[] HyperrectangleData = new double[dimensions * 2]; 109 | 110 | for(int d = 0; d < dimensions; d++) { 111 | final double begin = random.nextInt() % 1000; 112 | final double extent = Math.abs(random.nextInt() % 1000); 113 | HyperrectangleData[2 * d] = begin; // Start coordinate 114 | HyperrectangleData[2 * d + 1] = begin+extent; // End coordinate 115 | } 116 | 117 | final SpatialIndexEntry entry = new SpatialIndexEntry(new Hyperrectangle(HyperrectangleData), i); 118 | entryList.add(entry); 119 | } 120 | 121 | return entryList; 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/SpatialIndexEntry.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex; 19 | 20 | import java.io.IOException; 21 | import java.io.RandomAccessFile; 22 | import java.nio.ByteBuffer; 23 | 24 | import org.bboxdb.commons.io.DataEncoderHelper; 25 | import org.bboxdb.commons.math.Hyperrectangle; 26 | 27 | public class SpatialIndexEntry implements HyperrectangleEntity { 28 | 29 | /** 30 | * The key 31 | */ 32 | protected final int value; 33 | 34 | /** 35 | * The bounding box 36 | */ 37 | protected final Hyperrectangle hyperrectangle; 38 | 39 | public SpatialIndexEntry(final Hyperrectangle hyperrectangle, final int value) { 40 | this.value = value; 41 | this.hyperrectangle = hyperrectangle; 42 | } 43 | 44 | /** 45 | * Get the value 46 | * @return 47 | */ 48 | public int getValue() { 49 | return value; 50 | } 51 | 52 | /* (non-Javadoc) 53 | * @see com.github.jnidzwetzki.spatialindex.HyperrectangleEntity#getHyperrectangle() 54 | */ 55 | @Override 56 | public Hyperrectangle getHyperrectangle() { 57 | return hyperrectangle; 58 | } 59 | 60 | @Override 61 | public int hashCode() { 62 | final int prime = 31; 63 | int result = 1; 64 | result = prime * result + ((hyperrectangle == null) ? 0 : hyperrectangle.hashCode()); 65 | result = prime * result + (int) (value ^ (value >>> 32)); 66 | return result; 67 | } 68 | 69 | @Override 70 | public boolean equals(Object obj) { 71 | if (this == obj) 72 | return true; 73 | if (obj == null) 74 | return false; 75 | if (getClass() != obj.getClass()) 76 | return false; 77 | SpatialIndexEntry other = (SpatialIndexEntry) obj; 78 | if (hyperrectangle == null) { 79 | if (other.hyperrectangle != null) 80 | return false; 81 | } else if (!hyperrectangle.equals(other.hyperrectangle)) 82 | return false; 83 | if (value != other.value) 84 | return false; 85 | return true; 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | return "SpatialIndexEntry [value=" + value + ", Hyperrectangle=" + hyperrectangle + "]"; 91 | } 92 | 93 | /** 94 | * Write the node to a stream 95 | * @param randomAccessFile 96 | * @throws IOException 97 | */ 98 | public void writeToFile(final RandomAccessFile randomAccessFile) throws IOException { 99 | final ByteBuffer keyBytes = DataEncoderHelper.intToByteBuffer(value); 100 | randomAccessFile.write(keyBytes.array()); 101 | 102 | final byte[] HyperrectangleBytes = hyperrectangle.toByteArray(); 103 | final ByteBuffer boxLengthBytes = DataEncoderHelper.intToByteBuffer(HyperrectangleBytes.length); 104 | 105 | randomAccessFile.write(boxLengthBytes.array()); 106 | randomAccessFile.write(HyperrectangleBytes); 107 | } 108 | 109 | /** 110 | * Read the node from a stream 111 | * @param inputStream 112 | * @return 113 | * @throws IOException 114 | */ 115 | public static SpatialIndexEntry readFromFile(final RandomAccessFile randomAccessFile) throws IOException { 116 | 117 | final byte[] keyBytes = new byte[DataEncoderHelper.INT_BYTES]; 118 | final byte[] boxLengthBytes = new byte[DataEncoderHelper.INT_BYTES]; 119 | 120 | randomAccessFile.readFully(keyBytes, 0, keyBytes.length); 121 | randomAccessFile.readFully(boxLengthBytes, 0, boxLengthBytes.length); 122 | 123 | final int key = DataEncoderHelper.readIntFromByte(keyBytes); 124 | final int bboxLength = DataEncoderHelper.readIntFromByte(boxLengthBytes); 125 | 126 | final byte[] bboxBytes = new byte[bboxLength]; 127 | randomAccessFile.readFully(bboxBytes, 0, bboxBytes.length); 128 | 129 | final Hyperrectangle hyperrectangle = Hyperrectangle.fromByteArray(bboxBytes); 130 | 131 | return new SpatialIndexEntry(hyperrectangle, key); 132 | } 133 | 134 | /** 135 | * Read the node from a byte buffer 136 | * @param inputStream 137 | * @return 138 | * @throws IOException 139 | */ 140 | public static SpatialIndexEntry readFromByteBuffer(final ByteBuffer buffer) throws IOException { 141 | final byte[] keyBytes = new byte[DataEncoderHelper.INT_BYTES]; 142 | final byte[] boxLengthBytes = new byte[DataEncoderHelper.INT_BYTES]; 143 | 144 | buffer.get(keyBytes, 0, keyBytes.length); 145 | buffer.get(boxLengthBytes, 0, boxLengthBytes.length); 146 | 147 | final int key = DataEncoderHelper.readIntFromByte(keyBytes); 148 | final int bboxLength = DataEncoderHelper.readIntFromByte(boxLengthBytes); 149 | 150 | final byte[] bboxBytes = new byte[bboxLength]; 151 | buffer.get(bboxBytes, 0, bboxBytes.length); 152 | 153 | final Hyperrectangle hyperrectangle = Hyperrectangle.fromByteArray(bboxBytes); 154 | 155 | return new SpatialIndexEntry(hyperrectangle, key); 156 | } 157 | 158 | } 159 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/RTreeMemoryReader.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree; 19 | 20 | import java.io.IOException; 21 | import java.io.RandomAccessFile; 22 | import java.util.AbstractMap; 23 | import java.util.Arrays; 24 | import java.util.List; 25 | import java.util.Map.Entry; 26 | import java.util.Queue; 27 | import java.util.concurrent.LinkedTransferQueue; 28 | 29 | import org.bboxdb.commons.io.DataEncoderHelper; 30 | import org.bboxdb.commons.math.Hyperrectangle; 31 | 32 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 33 | import com.github.jnidzwetzki.spatialindex.SpatialIndexException; 34 | 35 | public class RTreeMemoryReader extends AbstractRTreeReader { 36 | 37 | /** 38 | * The root node of the tree 39 | */ 40 | protected RTreeDirectoryNode rootNode; 41 | 42 | /** 43 | * The child to read queue 44 | * 45 | * Parent node -> Child Pointer 46 | */ 47 | protected Queue> childToReadQueue = new LinkedTransferQueue<>(); 48 | 49 | 50 | public RTreeMemoryReader() { 51 | 52 | } 53 | 54 | public RTreeMemoryReader(final RTreeBuilder indexBuilder) { 55 | this.rootNode = indexBuilder.rootNode; 56 | this.maxNodeSize = indexBuilder.maxNodeSize; 57 | } 58 | 59 | @Override 60 | public void readFromFile(final RandomAccessFile randomAccessFile) 61 | throws SpatialIndexException, InterruptedException { 62 | 63 | assert (rootNode == null); 64 | 65 | try { 66 | // Validate the magic bytes 67 | validateStream(randomAccessFile); 68 | maxNodeSize = DataEncoderHelper.readIntFromDataInput(randomAccessFile); 69 | readDirectoryNode(randomAccessFile, null); 70 | 71 | while(! childToReadQueue.isEmpty()) { 72 | final Entry element = childToReadQueue.remove(); 73 | readDirectoryNode(randomAccessFile, element.getKey()); 74 | } 75 | 76 | } catch (IOException e) { 77 | throw new SpatialIndexException(e); 78 | } 79 | } 80 | 81 | /** 82 | * Read the directory node 83 | * @param randomAccessFile 84 | * @param parent 85 | * @return 86 | * @throws IOException 87 | */ 88 | protected RTreeDirectoryNode readDirectoryNode(final RandomAccessFile randomAccessFile, 89 | final RTreeDirectoryNode parent) throws IOException { 90 | 91 | // Node data 92 | final int nodeId = DataEncoderHelper.readIntFromDataInput(randomAccessFile); 93 | final RTreeDirectoryNode node = new RTreeDirectoryNode(nodeId); 94 | node.setParentNode(parent); 95 | 96 | if(parent != null) { 97 | parent.directoryNodeChilds.add(node); 98 | } 99 | 100 | // Make this to the new root node 101 | if(rootNode == null) { 102 | rootNode = node; 103 | } 104 | 105 | // Bounding box data 106 | final int HyperrectangleLength = DataEncoderHelper.readIntFromDataInput(randomAccessFile); 107 | final byte[] HyperrectangleBytes = new byte[HyperrectangleLength]; 108 | randomAccessFile.readFully(HyperrectangleBytes, 0, HyperrectangleBytes.length); 109 | final Hyperrectangle hyperrectangle = Hyperrectangle.fromByteArray(HyperrectangleBytes); 110 | node.setHyperrectangle(hyperrectangle); 111 | 112 | // Read entry entries 113 | readEntryNodes(randomAccessFile); 114 | 115 | // Read directory nodes 116 | readDirectoryNodes(randomAccessFile, node); 117 | 118 | return node; 119 | } 120 | 121 | /** 122 | * Read the directory nodes 123 | * @param randomAccessFile 124 | * @param node 125 | * @throws IOException 126 | */ 127 | protected void readDirectoryNodes(final RandomAccessFile randomAccessFile, final RTreeDirectoryNode node) 128 | throws IOException { 129 | 130 | final byte[] followingByte = new byte[RTreeBuilder.MAGIC_VALUE_SIZE]; 131 | 132 | for(int i = 0; i < maxNodeSize; i++) { 133 | randomAccessFile.readFully(followingByte, 0, followingByte.length); 134 | 135 | if(! Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_NOT_EXISTING)) { 136 | final int childPointer = DataEncoderHelper.readIntFromByte(followingByte); 137 | 138 | // Add the pointer for later decoding 139 | childToReadQueue.add( 140 | new AbstractMap.SimpleImmutableEntry(node, childPointer) 141 | ); 142 | } 143 | } 144 | } 145 | 146 | /** 147 | * Read the entry nodes 148 | * @param inputStream 149 | * @throws IOException 150 | */ 151 | protected void readEntryNodes(final RandomAccessFile randomAccessFile) throws IOException { 152 | 153 | final byte[] followingByte = new byte[RTreeBuilder.MAGIC_VALUE_SIZE]; 154 | 155 | for(int i = 0; i < maxNodeSize; i++) { 156 | randomAccessFile.readFully(followingByte, 0, followingByte.length); 157 | 158 | if(Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_FOLLOWING)) { 159 | final SpatialIndexEntry spatialIndexEntry = SpatialIndexEntry.readFromFile(randomAccessFile); 160 | rootNode.indexEntries.add(spatialIndexEntry); 161 | } else if(! Arrays.equals(followingByte, RTreeBuilder.MAGIC_CHILD_NODE_NOT_EXISTING)) { 162 | throw new IllegalArgumentException("Unknown node type following: " + followingByte); 163 | } 164 | } 165 | } 166 | 167 | @Override 168 | public List getEntriesForRegion(final Hyperrectangle Hyperrectangle) { 169 | return rootNode.getEntriesForRegion(Hyperrectangle); 170 | } 171 | 172 | 173 | @Override 174 | public void close() { 175 | maxNodeSize = -1; 176 | rootNode = null; 177 | childToReadQueue.clear(); 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /src/test/java/org/bboxdb/storage/rtree/TestRTreeMemoryDeserializer.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package org.bboxdb.storage.rtree; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.io.RandomAccessFile; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | 26 | import org.bboxdb.commons.math.Hyperrectangle; 27 | import org.junit.Assert; 28 | import org.junit.Test; 29 | 30 | import com.github.jnidzwetzki.spatialindex.SpatialIndexBuilder; 31 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 32 | import com.github.jnidzwetzki.spatialindex.SpatialIndexException; 33 | import com.github.jnidzwetzki.spatialindex.rtree.AbstractRTreeReader; 34 | import com.github.jnidzwetzki.spatialindex.rtree.RTreeBuilder; 35 | import com.github.jnidzwetzki.spatialindex.rtree.RTreeMemoryReader; 36 | 37 | public class TestRTreeMemoryDeserializer { 38 | 39 | /** 40 | * Get the reader for the test 41 | * @return 42 | */ 43 | protected AbstractRTreeReader getRTreeReader() { 44 | return new RTreeMemoryReader(); 45 | } 46 | 47 | 48 | /** 49 | * Test different node size 50 | * @throws StorageManagerException 51 | * @throws IOException 52 | * @throws InterruptedException 53 | */ 54 | @Test(timeout=60000) 55 | public void testSerializeIndex0() throws SpatialIndexException, IOException, InterruptedException { 56 | final int maxNodeSize = 12; 57 | final RTreeBuilder index = new RTreeBuilder(maxNodeSize); 58 | 59 | final File tempFile = File.createTempFile("rtree-", "-test"); 60 | tempFile.deleteOnExit(); 61 | final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); 62 | index.writeToFile(raf); 63 | raf.close(); 64 | 65 | final AbstractRTreeReader indexRead = getRTreeReader(); 66 | final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r"); 67 | indexRead.readFromFile(rafRead); 68 | rafRead.close(); 69 | 70 | Assert.assertEquals(maxNodeSize, index.getMaxNodeSize()); 71 | Assert.assertEquals(maxNodeSize, indexRead.getMaxNodeSize()); 72 | 73 | indexRead.close(); 74 | } 75 | 76 | /** 77 | * Test the encoding and the decoding of the index with only one entry 78 | * = data is encoded in the root node 79 | * 80 | * @throws StorageManagerException 81 | * @throws IOException 82 | * @throws InterruptedException 83 | */ 84 | @Test(timeout=60000) 85 | public void testSerializeIndexSmall() throws SpatialIndexException, IOException, InterruptedException { 86 | final List tupleList = new ArrayList<>(); 87 | tupleList.add(new SpatialIndexEntry(new Hyperrectangle(1.0, 1.2), 2)); 88 | 89 | final SpatialIndexBuilder index = new RTreeBuilder(); 90 | index.bulkInsert(tupleList); 91 | 92 | RTreeTestHelper.queryIndex(tupleList, index); 93 | 94 | final File tempFile = File.createTempFile("rtree-", "-test"); 95 | tempFile.deleteOnExit(); 96 | final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); 97 | index.writeToFile(raf); 98 | raf.close(); 99 | 100 | final AbstractRTreeReader indexRead = getRTreeReader(); 101 | final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r"); 102 | indexRead.readFromFile(rafRead); 103 | rafRead.close(); 104 | 105 | final List resultList = indexRead.getEntriesForRegion(new Hyperrectangle(1.1, 1.2)); 106 | Assert.assertEquals(1, resultList.size()); 107 | 108 | indexRead.close(); 109 | } 110 | 111 | /** 112 | * Test the encoding and the decoding of the index 113 | * @throws StorageManagerException 114 | * @throws IOException 115 | * @throws InterruptedException 116 | */ 117 | @Test(timeout=60000) 118 | public void testSerializeIndex1D() throws SpatialIndexException, IOException, InterruptedException { 119 | final List tupleList = RTreeTestHelper.generateRandomTupleList(1); 120 | 121 | final SpatialIndexBuilder index = new RTreeBuilder(); 122 | index.bulkInsert(tupleList); 123 | 124 | RTreeTestHelper.queryIndex(tupleList, index); 125 | 126 | final File tempFile = File.createTempFile("rtree-", "-test"); 127 | tempFile.deleteOnExit(); 128 | final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); 129 | index.writeToFile(raf); 130 | raf.close(); 131 | 132 | final AbstractRTreeReader indexRead = getRTreeReader(); 133 | final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r"); 134 | indexRead.readFromFile(rafRead); 135 | rafRead.close(); 136 | 137 | RTreeTestHelper.queryIndex(tupleList, indexRead); 138 | } 139 | 140 | 141 | /** 142 | * Test the encoding and the decoding of the index 143 | * @throws StorageManagerException 144 | * @throws IOException 145 | * @throws InterruptedException 146 | */ 147 | @Test(timeout=60000) 148 | public void testSerializeIndex3D() throws SpatialIndexException, IOException, InterruptedException { 149 | final List tupleList = RTreeTestHelper.generateRandomTupleList(3); 150 | 151 | final SpatialIndexBuilder index = new RTreeBuilder(); 152 | index.bulkInsert(tupleList); 153 | 154 | RTreeTestHelper.queryIndex(tupleList, index); 155 | 156 | final File tempFile = File.createTempFile("rtree-", "-test"); 157 | tempFile.deleteOnExit(); 158 | final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); 159 | index.writeToFile(raf); 160 | raf.close(); 161 | 162 | final AbstractRTreeReader indexRead = getRTreeReader(); 163 | final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r"); 164 | indexRead.readFromFile(rafRead); 165 | rafRead.close(); 166 | 167 | RTreeTestHelper.queryIndex(tupleList, indexRead); 168 | } 169 | 170 | 171 | } 172 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/RTreeSerializer.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree; 19 | 20 | import java.io.IOException; 21 | import java.io.RandomAccessFile; 22 | import java.nio.ByteBuffer; 23 | import java.util.ArrayDeque; 24 | import java.util.Deque; 25 | import java.util.HashMap; 26 | import java.util.List; 27 | import java.util.Map; 28 | 29 | import org.bboxdb.commons.io.DataEncoderHelper; 30 | 31 | import com.github.jnidzwetzki.spatialindex.Const; 32 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 33 | import com.github.jnidzwetzki.spatialindex.SpatialIndexException; 34 | 35 | public class RTreeSerializer { 36 | 37 | /** 38 | * The maximal node size 39 | */ 40 | protected int maxNodeSize; 41 | 42 | /** 43 | * The root node 44 | */ 45 | protected RTreeDirectoryNode rootNode; 46 | 47 | /** 48 | * The node start position 49 | */ 50 | protected final Map nodeStartPosition = new HashMap<>(); 51 | 52 | /** 53 | * The node start child nodes position 54 | */ 55 | protected final Map nodeFixedEndPosition = new HashMap<>(); 56 | 57 | /** 58 | * The nodes queue 59 | */ 60 | protected final Deque nodesQueue = new ArrayDeque<>(); 61 | 62 | 63 | public RTreeSerializer(final RTreeDirectoryNode rootNode, final int maxNodeSize) { 64 | this.rootNode = rootNode; 65 | this.maxNodeSize = maxNodeSize; 66 | } 67 | 68 | /** 69 | * Serialize the tree to the file 70 | * @param randomAccessFile 71 | * @throws StorageManagerException 72 | */ 73 | public void writeToStream(final RandomAccessFile randomAccessFile) throws SpatialIndexException { 74 | 75 | nodesQueue.clear(); 76 | 77 | try { 78 | // Write the magic bytes 79 | randomAccessFile.write(Const.MAGIC_BYTES_SPATIAL_RTREE_INDEX); 80 | 81 | // Write the tree configuration 82 | final ByteBuffer nodeSizeBytes = DataEncoderHelper.intToByteBuffer(maxNodeSize); 83 | randomAccessFile.write(nodeSizeBytes.array()); 84 | 85 | nodesQueue.push(rootNode); 86 | 87 | while(! nodesQueue.isEmpty()) { 88 | final RTreeDirectoryNode node = nodesQueue.pop(); 89 | handleNewNode(randomAccessFile, node); 90 | } 91 | 92 | updateIndexNodePointer(randomAccessFile); 93 | 94 | } catch (IOException e) { 95 | throw new SpatialIndexException(e); 96 | } 97 | } 98 | 99 | /** 100 | * Update the index node pointer 101 | * @param randomAccessFile 102 | * @throws IOException 103 | */ 104 | protected void updateIndexNodePointer(final RandomAccessFile randomAccessFile) throws IOException { 105 | for(final RTreeDirectoryNode node : nodeFixedEndPosition.keySet()) { 106 | // Seek to the first pointer 107 | randomAccessFile.seek(nodeFixedEndPosition.get(node)); 108 | 109 | for(final RTreeDirectoryNode child : node.getDirectoryNodeChilds()) { 110 | final Integer childNodePosition = nodeStartPosition.get(child); 111 | final ByteBuffer childNodePointer = DataEncoderHelper.intToByteBuffer(childNodePosition); 112 | 113 | // Override node pointer placeholder 114 | randomAccessFile.write(childNodePointer.array()); 115 | } 116 | } 117 | } 118 | 119 | /** 120 | * Handle the nodes 121 | * @param randomAccessFile 122 | * @param nodesQueue 123 | * @param node 124 | * @throws IOException 125 | */ 126 | protected void handleNewNode(final RandomAccessFile randomAccessFile, final RTreeDirectoryNode node) 127 | throws IOException { 128 | 129 | // Node data 130 | nodeStartPosition.put(node, (int) randomAccessFile.getFilePointer()); 131 | final ByteBuffer nodeIdBytes = DataEncoderHelper.intToByteBuffer(node.getNodeId()); 132 | randomAccessFile.write(nodeIdBytes.array()); 133 | 134 | // Bounding box data 135 | final byte[] HyperrectangleBytes = node.getHyperrectangle().toByteArray(); 136 | final ByteBuffer HyperrectangleLength = DataEncoderHelper.intToByteBuffer(HyperrectangleBytes.length); 137 | randomAccessFile.write(HyperrectangleLength.array()); 138 | randomAccessFile.write(HyperrectangleBytes); 139 | 140 | // Write entry nodes 141 | writeEntryNodes(randomAccessFile, node); 142 | nodeFixedEndPosition.put(node, (int) randomAccessFile.getFilePointer()); 143 | 144 | // Write directory nodes 145 | addDirectoryNodesToQueue(randomAccessFile, node); 146 | } 147 | 148 | /** 149 | * Write the entry nodes to the output stream 150 | * @param randomAccessFile 151 | * @param node 152 | * @throws IOException 153 | */ 154 | protected void writeEntryNodes(final RandomAccessFile randomAccessFile, final RTreeDirectoryNode node) 155 | throws IOException { 156 | 157 | final List indexEntries = node.getIndexEntries(); 158 | for(int i = 0; i < maxNodeSize; i++) { 159 | if(i < indexEntries.size()) { 160 | randomAccessFile.write(RTreeBuilder.MAGIC_CHILD_NODE_FOLLOWING); 161 | indexEntries.get(i).writeToFile(randomAccessFile); 162 | } else { 163 | randomAccessFile.write(RTreeBuilder.MAGIC_CHILD_NODE_NOT_EXISTING); 164 | } 165 | } 166 | } 167 | 168 | /** 169 | * Add the directory nodes to the queue 170 | * @param nodesQueue 171 | * @param node 172 | * @throws IOException 173 | */ 174 | protected void addDirectoryNodesToQueue(final RandomAccessFile randomAccessFile, 175 | final RTreeDirectoryNode node) throws IOException { 176 | 177 | final List directoryNodeChilds = node.getDirectoryNodeChilds(); 178 | for(int i = 0; i < maxNodeSize; i++) { 179 | if(i < directoryNodeChilds.size()) { 180 | nodesQueue.addFirst(directoryNodeChilds.get(i)); 181 | } 182 | 183 | // Existing pointer will be written in a second step 184 | randomAccessFile.write(RTreeBuilder.MAGIC_CHILD_NODE_NOT_EXISTING); 185 | } 186 | } 187 | 188 | } 189 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 18 | 20 | 4.0.0 21 | 22 | com.github.jnidzwetzki 23 | sia4j 24 | jar 25 | 0.5.0-snapshot 26 | 27 | Spatial indexing algorithms for java 28 | Implementation spatial index algorithms in java 29 | https://github.com/jnidzwetzki/spatial-index-java 30 | 31 | 32 | UTF-8 33 | 34 | 35 | 36 | 37 | Apache License, Version 2.0 38 | http://www.apache.org/licenses/LICENSE-2.0.txt 39 | repo 40 | 41 | 42 | 43 | 44 | scm:git:git://github.com/jnidzwetzki/spatial-index-java.git 45 | scm:git:git://github.com/jnidzwetzki/spatial-index-java.git 46 | https://github.com/jnidzwetzki/spatial-index-java/tree/master 47 | 48 | 49 | 50 | 51 | jkn 52 | Jan Kristof Nidzwetzki 53 | jnidzwetzki@gmx.de 54 | 55 | 56 | 57 | 58 | Travis 59 | https://travis-ci.org/jnidzwetzki/spatial-index-java 60 | 61 | 62 | 63 | GitHub 64 | https://github.com/jnidzwetzki/spatial-index-java/issues 65 | 66 | 67 | 68 | 69 | ossrh 70 | https://oss.sonatype.org/content/repositories/snapshots 71 | 72 | 73 | ossrh 74 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 75 | 76 | 77 | 78 | 79 | 80 | 81 | org.apache.logging.log4j 82 | log4j-api 83 | 2.17.1 84 | 85 | 86 | org.apache.logging.log4j 87 | log4j-core 88 | 2.17.1 89 | 90 | 91 | org.apache.logging.log4j 92 | log4j-slf4j-impl 93 | 2.17.1 94 | 95 | 96 | org.bboxdb 97 | bboxdb-commons 98 | 0.9.9 99 | 100 | 101 | 102 | 103 | 104 | junit 105 | junit 106 | 4.13.1 107 | test 108 | 109 | 110 | org.mockito 111 | mockito-core 112 | 2.15.0 113 | test 114 | 115 | 116 | 117 | 118 | 119 | 120 | org.apache.maven.plugins 121 | maven-compiler-plugin 122 | 3.5 123 | 124 | 1.8 125 | 1.8 126 | 127 | 128 | 129 | org.jacoco 130 | jacoco-maven-plugin 131 | 0.7.7.201606060606 132 | 133 | 134 | 135 | prepare-agent 136 | 137 | 138 | 139 | report 140 | test 141 | 142 | report 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | release 153 | 154 | true 155 | 156 | 157 | 158 | 159 | org.apache.maven.plugins 160 | maven-source-plugin 161 | 2.2.1 162 | 163 | 164 | attach-sources 165 | 166 | jar-no-fork 167 | 168 | 169 | 170 | 171 | 172 | org.apache.maven.plugins 173 | maven-javadoc-plugin 174 | 2.9.1 175 | 176 | 177 | attach-javadocs 178 | 179 | jar 180 | 181 | 182 | 183 | 184 | 185 | 186 | org.apache.maven.plugins 187 | maven-gpg-plugin 188 | 1.5 189 | 190 | 191 | sign-artifacts 192 | verify 193 | 194 | sign 195 | 196 | 197 | 198 | 199 | 200 | 201 | org.apache.maven.plugins 202 | maven-surefire-plugin 203 | 2.16 204 | 205 | true 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | -------------------------------------------------------------------------------- /src/test/java/org/bboxdb/storage/rtree/TestRTreeIndex.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package org.bboxdb.storage.rtree; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.io.RandomAccessFile; 23 | import java.nio.ByteBuffer; 24 | import java.nio.file.Files; 25 | import java.nio.file.Path; 26 | import java.nio.file.Paths; 27 | import java.util.List; 28 | 29 | import org.bboxdb.commons.math.Hyperrectangle; 30 | import org.junit.Assert; 31 | import org.junit.Test; 32 | 33 | import com.github.jnidzwetzki.spatialindex.Const; 34 | import com.github.jnidzwetzki.spatialindex.SpatialIndexBuilder; 35 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 36 | import com.github.jnidzwetzki.spatialindex.rtree.RTreeBuilder; 37 | 38 | public class TestRTreeIndex { 39 | 40 | /** 41 | * Test to insert and to read the bounding boxes 42 | */ 43 | @Test(timeout=60000) 44 | public void testBoxesInsert() { 45 | final List elements = RTreeTestHelper.getEntryList(); 46 | 47 | final SpatialIndexBuilder index = new RTreeBuilder(); 48 | index.bulkInsert(elements); 49 | } 50 | 51 | @Test(timeout=60000) 52 | public void testQueryOnEmptytree() { 53 | final SpatialIndexBuilder index = new RTreeBuilder(); 54 | final List result = index.getEntriesForRegion(new Hyperrectangle(1d, 1d, 2d, 2d)); 55 | Assert.assertTrue(result.isEmpty()); 56 | } 57 | 58 | /** 59 | * Test to query the index 60 | */ 61 | @Test(timeout=60000) 62 | public void testBoxQuery1d0() { 63 | final List tupleList = RTreeTestHelper.getEntryList(); 64 | 65 | final SpatialIndexBuilder index = new RTreeBuilder(4); 66 | index.bulkInsert(tupleList); 67 | RTreeTestHelper.queryIndex(tupleList, index); 68 | } 69 | 70 | /** 71 | * Test to query the index 72 | */ 73 | @Test(timeout=60000) 74 | public void testBoxQuery1d() { 75 | final List tupleList = RTreeTestHelper.getEntryList(); 76 | 77 | final SpatialIndexBuilder index = new RTreeBuilder(); 78 | index.bulkInsert(tupleList); 79 | RTreeTestHelper.queryIndex(tupleList, index); 80 | } 81 | 82 | /** 83 | * Test to query the index 84 | */ 85 | @Test(timeout=60000) 86 | public void testBoxQuery2d() { 87 | final List tupleList = RTreeTestHelper.generateRandomTupleList(2); 88 | 89 | final SpatialIndexBuilder index = new RTreeBuilder(); 90 | index.bulkInsert(tupleList); 91 | RTreeTestHelper.queryIndex(tupleList, index); 92 | } 93 | 94 | /** 95 | * Test to query the index 96 | */ 97 | @Test(timeout=60000) 98 | public void testBoxQuery3d() { 99 | final List tupleList = RTreeTestHelper.generateRandomTupleList(3); 100 | 101 | final SpatialIndexBuilder index = new RTreeBuilder(); 102 | index.bulkInsert(tupleList); 103 | 104 | RTreeTestHelper.queryIndex(tupleList, index); 105 | } 106 | 107 | /** 108 | * Test to query the index 109 | */ 110 | @Test(timeout=60000) 111 | public void testBoxQuery4d() { 112 | final List tupleList = RTreeTestHelper.generateRandomTupleList(4); 113 | 114 | final SpatialIndexBuilder index = new RTreeBuilder(); 115 | index.bulkInsert(tupleList); 116 | 117 | RTreeTestHelper.queryIndex(tupleList, index); 118 | } 119 | 120 | /** 121 | * Test to query the index 122 | */ 123 | @Test(timeout=60000) 124 | public void testBoxQuery5d() { 125 | final List tupleList = RTreeTestHelper.generateRandomTupleList(5); 126 | 127 | final SpatialIndexBuilder index = new RTreeBuilder(); 128 | index.bulkInsert(tupleList); 129 | 130 | RTreeTestHelper.queryIndex(tupleList, index); 131 | } 132 | 133 | /** 134 | * Test to query the index 135 | */ 136 | @Test(timeout=60000) 137 | public void testBoxQuery10d() { 138 | final List tupleList = RTreeTestHelper.generateRandomTupleList(10); 139 | 140 | final SpatialIndexBuilder index = new RTreeBuilder(); 141 | index.bulkInsert(tupleList); 142 | 143 | RTreeTestHelper.queryIndex(tupleList, index); 144 | } 145 | 146 | 147 | /** 148 | * Test the covering of the nodes 149 | */ 150 | @Test(timeout=60000) 151 | public void testCovering() { 152 | final List tupleList = RTreeTestHelper.generateRandomTupleList(3); 153 | 154 | final RTreeBuilder index = new RTreeBuilder(); 155 | index.bulkInsert(tupleList); 156 | 157 | index.testCovering(); 158 | } 159 | 160 | /** 161 | * Test the decoding an encoding of an rtree entry 162 | * @throws IOException 163 | */ 164 | @Test(timeout=60000) 165 | public void testEncodeDecodeRTreeEntryFromFile() throws IOException { 166 | final Hyperrectangle Hyperrectangle = new Hyperrectangle(4.1, 8.1, 4.2, 8.8); 167 | 168 | final File tempFile = File.createTempFile("rtree-", "-test"); 169 | tempFile.deleteOnExit(); 170 | final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); 171 | final SpatialIndexEntry rTreeSpatialIndexEntry = new SpatialIndexEntry(Hyperrectangle, 1); 172 | rTreeSpatialIndexEntry.writeToFile(raf); 173 | raf.close(); 174 | 175 | final RandomAccessFile rafRead = new RandomAccessFile(tempFile, "r"); 176 | final SpatialIndexEntry readEntry = SpatialIndexEntry.readFromFile(rafRead); 177 | rafRead.close(); 178 | 179 | Assert.assertEquals(rTreeSpatialIndexEntry.getValue(), readEntry.getValue()); 180 | Assert.assertEquals(rTreeSpatialIndexEntry.getHyperrectangle(), readEntry.getHyperrectangle()); 181 | } 182 | 183 | /** 184 | * Test the decoding an encoding of an rtree entry 185 | * @throws IOException 186 | */ 187 | @Test(timeout=60000) 188 | public void testEncodeDecodeRTreeEntryFromByteBuffer() throws IOException { 189 | final Hyperrectangle Hyperrectangle = new Hyperrectangle(4.1, 8.1, 4.2, 8.8); 190 | 191 | final File tempFile = File.createTempFile("rtree-", "-test"); 192 | tempFile.deleteOnExit(); 193 | final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw"); 194 | final SpatialIndexEntry rTreeSpatialIndexEntry = new SpatialIndexEntry(Hyperrectangle, 1); 195 | rTreeSpatialIndexEntry.writeToFile(raf); 196 | raf.close(); 197 | 198 | final Path path = Paths.get(tempFile.getAbsolutePath()); 199 | final byte[] data = Files.readAllBytes(path); 200 | final ByteBuffer bb = ByteBuffer.wrap(data); 201 | bb.order(Const.APPLICATION_BYTE_ORDER); 202 | 203 | final SpatialIndexEntry readEntry = SpatialIndexEntry.readFromByteBuffer(bb); 204 | 205 | Assert.assertEquals(rTreeSpatialIndexEntry.getValue(), readEntry.getValue()); 206 | Assert.assertEquals(rTreeSpatialIndexEntry.getHyperrectangle(), readEntry.getHyperrectangle()); 207 | } 208 | 209 | /** 210 | * Test the creation of a rtree with a invalid max node size 211 | */ 212 | @Test(expected=IllegalArgumentException.class) 213 | public void testWrongNodeSize0() { 214 | new RTreeBuilder(0); 215 | } 216 | 217 | /** 218 | * Test the creation of a rtree with a invalid max node size 219 | */ 220 | @Test(expected=IllegalArgumentException.class) 221 | public void testWrongNodeSize1() { 222 | new RTreeBuilder(-1); 223 | } 224 | 225 | /** 226 | * Test the bounding box of an empty r-tree 227 | */ 228 | @Test(timeout=60000) 229 | public void testEmptryRTreeBBox() { 230 | final RTreeBuilder index = new RTreeBuilder(); 231 | final List result = index.getEntriesForRegion(Hyperrectangle.FULL_SPACE); 232 | Assert.assertTrue(result.isEmpty()); 233 | } 234 | 235 | } 236 | -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/RTreeDirectoryNode.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | import java.util.stream.Collectors; 23 | import java.util.stream.Stream; 24 | 25 | import org.bboxdb.commons.math.Hyperrectangle; 26 | 27 | import com.github.jnidzwetzki.spatialindex.HyperrectangleEntity; 28 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 29 | 30 | public class RTreeDirectoryNode implements HyperrectangleEntity { 31 | 32 | /** 33 | * The directory node childs 34 | */ 35 | protected final List directoryNodeChilds; 36 | 37 | /** 38 | * The leaf node childs 39 | */ 40 | protected final List indexEntries; 41 | 42 | /** 43 | * The root node 'root' reference 44 | */ 45 | protected final static RTreeDirectoryNode PARENT_ROOT = null; 46 | 47 | /** 48 | * The bounding box of the node 49 | */ 50 | protected Hyperrectangle hyperrectangle; 51 | 52 | /** 53 | * The parent node 54 | */ 55 | protected RTreeDirectoryNode parentNode; 56 | 57 | /** 58 | * The id of the node 59 | */ 60 | protected final int nodeId; 61 | 62 | public RTreeDirectoryNode(final int nodeId) { 63 | this.parentNode = PARENT_ROOT; 64 | this.nodeId = nodeId; 65 | this.directoryNodeChilds = new ArrayList<>(); 66 | this.indexEntries = new ArrayList<>(); 67 | } 68 | 69 | /** 70 | * Return the bounding box of the node 71 | * @return 72 | */ 73 | @Override 74 | public Hyperrectangle getHyperrectangle() { 75 | return hyperrectangle; 76 | } 77 | 78 | /** 79 | * Set the bounding box 80 | * @param hyperrectangle 81 | */ 82 | public void setHyperrectangle(Hyperrectangle hyperrectangle) { 83 | this.hyperrectangle = hyperrectangle; 84 | } 85 | 86 | /** 87 | * Get the parent node 88 | * @return 89 | */ 90 | public RTreeDirectoryNode getParentNode() { 91 | return parentNode; 92 | } 93 | 94 | /** 95 | * Set a new parent node 96 | * @param parentNode 97 | */ 98 | public void setParentNode(final RTreeDirectoryNode parentNode) { 99 | this.parentNode = parentNode; 100 | } 101 | 102 | @Override 103 | public String toString() { 104 | return "RTreeDirectoryNode [Hyperrectangle=" + hyperrectangle + ", nodeId=" + nodeId + "]"; 105 | } 106 | 107 | /** 108 | * Add a directory node as child 109 | * @param rTreeDirectoryNode 110 | */ 111 | public void addDirectoryNodeChild(final RTreeDirectoryNode rTreeDirectoryNode) { 112 | 113 | // We can carry directory nodes or index entries 114 | assert (indexEntries.isEmpty()); 115 | 116 | directoryNodeChilds.add(rTreeDirectoryNode); 117 | } 118 | 119 | /** 120 | * Remove child directory node 121 | * @param rTreeDirectoryNode 122 | * @return 123 | */ 124 | public boolean removeDirectoryNodeChild(final RTreeDirectoryNode rTreeDirectoryNode) { 125 | return directoryNodeChilds.remove(rTreeDirectoryNode); 126 | } 127 | 128 | /** 129 | * Remove child leaf node 130 | * @param rTreeLeafNode 131 | * @return 132 | */ 133 | public boolean removeIndexEntry(final HyperrectangleEntity entry) { 134 | 135 | // We can carry directory nodes or index entries 136 | assert (directoryNodeChilds.isEmpty()); 137 | 138 | return indexEntries.remove(entry); 139 | } 140 | 141 | /** 142 | * Recalculate the bounding box of all entries 143 | */ 144 | public void updateHyperrectangle() { 145 | final List Hyperrectanglees = getAllChildHyperrectanglees(); 146 | 147 | // Calculate bounding box 148 | this.hyperrectangle = Hyperrectangle.getCoveringBox(Hyperrectanglees); 149 | } 150 | 151 | /** 152 | * Get the bounding boxes of all childs 153 | * @return 154 | */ 155 | public List getAllChildHyperrectanglees() { 156 | 157 | // Get all Bounding boxes 158 | return Stream.concat(directoryNodeChilds.stream(), indexEntries.stream()) 159 | .map(b -> b.getHyperrectangle()) 160 | .collect(Collectors.toList()); 161 | } 162 | 163 | /** 164 | * Find the best node for insert 165 | * @param entryBox 166 | * @return 167 | */ 168 | protected RTreeDirectoryNode findBestNodeForInsert(final Hyperrectangle entryBox) { 169 | RTreeDirectoryNode bestNode = null; 170 | double bestEnlargement = -1; 171 | 172 | for(final RTreeDirectoryNode node : directoryNodeChilds) { 173 | final Hyperrectangle nodeHyperrectangle = node.getHyperrectangle(); 174 | final double nodeEnlargement = nodeHyperrectangle.calculateEnlargement(entryBox); 175 | 176 | if(bestNode == null) { 177 | bestNode = node; 178 | bestEnlargement = nodeEnlargement; 179 | continue; 180 | } 181 | 182 | if(nodeEnlargement < bestEnlargement) { 183 | bestNode = node; 184 | bestEnlargement = nodeEnlargement; 185 | continue; 186 | } 187 | 188 | if(nodeEnlargement == bestEnlargement) { 189 | if(bestNode.getSize() > node.getSize()) { 190 | bestNode = node; 191 | bestEnlargement = nodeEnlargement; 192 | continue; 193 | } 194 | } 195 | } 196 | 197 | return bestNode; 198 | } 199 | 200 | /** 201 | * Get all entries for a given region 202 | * @param Hyperrectangle 203 | * @return 204 | */ 205 | public List getEntriesForRegion(final Hyperrectangle Hyperrectangle) { 206 | 207 | assert(Hyperrectangle != null) : "Query bounding box has to be != null"; 208 | assert(indexEntries != null) : "Index entries has to be != null"; 209 | assert(directoryNodeChilds != null) : "Directory node childs has to be != null"; 210 | 211 | // One result list for all nodes of the tree 212 | // (prevents expensive list merging) 213 | final List result = new ArrayList<>(); 214 | 215 | getEntriesForRegion(Hyperrectangle, result); 216 | 217 | return result; 218 | } 219 | 220 | /** 221 | * Get the entries for the region (without creating new lists) 222 | * @param hyperrectangle 223 | * @param result 224 | */ 225 | private void getEntriesForRegion(final Hyperrectangle hyperrectangle, final List result) { 226 | 227 | try { 228 | for(final SpatialIndexEntry entry : indexEntries) { 229 | if(entry.getHyperrectangle().intersects(hyperrectangle)) { 230 | result.add(entry); 231 | } 232 | } 233 | 234 | for(final RTreeDirectoryNode entry : directoryNodeChilds) { 235 | if(entry.getHyperrectangle().intersects(hyperrectangle)) { 236 | entry.getEntriesForRegion(hyperrectangle, result); 237 | } 238 | } 239 | 240 | } catch(NullPointerException e) { 241 | System.out.println(e); 242 | System.out.println(directoryNodeChilds); 243 | return; 244 | } 245 | } 246 | 247 | /** 248 | * Test the bounding box covering (useful for test purposes) 249 | */ 250 | public void testCovering() { 251 | 252 | boolean success = true; 253 | 254 | for(final SpatialIndexEntry entry : indexEntries) { 255 | if(! hyperrectangle.isCovering(entry.getHyperrectangle())) { 256 | System.err.println("Error 1"); 257 | success = false; 258 | } 259 | 260 | if(! hyperrectangle.intersects(entry.getHyperrectangle())) { 261 | System.err.println("Error 2"); 262 | success = false; 263 | } 264 | } 265 | 266 | for(final RTreeDirectoryNode entry : directoryNodeChilds) { 267 | 268 | assert hyperrectangle != null; 269 | assert entry.getHyperrectangle() != null : "Null BBox: " + entry; 270 | 271 | if(! hyperrectangle.isCovering(entry.getHyperrectangle())) { 272 | System.err.println("Error 3a: " + hyperrectangle + " does not cover" + entry.getHyperrectangle()); 273 | entry.updateHyperrectangle(); 274 | updateHyperrectangle(); 275 | 276 | if(! hyperrectangle.isCovering(entry.getHyperrectangle())) { 277 | System.err.println("Error 3b: " + hyperrectangle + " does not cover" + entry.getHyperrectangle()); 278 | System.err.println(getAllChildHyperrectanglees()); 279 | success = false; 280 | } 281 | } 282 | 283 | if(! hyperrectangle.intersects(entry.getHyperrectangle())) { 284 | System.err.println("Error 4"); 285 | success = false; 286 | } 287 | 288 | entry.testCovering(); 289 | } 290 | 291 | if(! success) { 292 | throw new RuntimeException(); 293 | } 294 | } 295 | 296 | /** 297 | * Get the size of the node 298 | * @return 299 | */ 300 | public int getSize() { 301 | return indexEntries.size() + directoryNodeChilds.size(); 302 | } 303 | 304 | /** 305 | * Is this a leaf node 306 | */ 307 | public boolean isLeafNode() { 308 | return directoryNodeChilds.isEmpty(); 309 | } 310 | 311 | /** 312 | * Get the directory node childs 313 | * @return 314 | */ 315 | public List getDirectoryNodeChilds() { 316 | return directoryNodeChilds; 317 | } 318 | 319 | /** 320 | * Get the index entries 321 | * @return 322 | */ 323 | public List getIndexEntries() { 324 | return indexEntries; 325 | } 326 | 327 | @Override 328 | public int hashCode() { 329 | final int prime = 31; 330 | int result = 1; 331 | result = prime * result + ((hyperrectangle == null) ? 0 : hyperrectangle.hashCode()); 332 | result = prime * result + nodeId; 333 | return result; 334 | } 335 | 336 | @Override 337 | public boolean equals(Object obj) { 338 | if (this == obj) 339 | return true; 340 | if (obj == null) 341 | return false; 342 | if (getClass() != obj.getClass()) 343 | return false; 344 | RTreeDirectoryNode other = (RTreeDirectoryNode) obj; 345 | if (hyperrectangle == null) { 346 | if (other.hyperrectangle != null) 347 | return false; 348 | } else if (!hyperrectangle.equals(other.hyperrectangle)) 349 | return false; 350 | if (nodeId != other.nodeId) 351 | return false; 352 | return true; 353 | } 354 | 355 | /** 356 | * Get the node id 357 | * @return 358 | */ 359 | public int getNodeId() { 360 | return nodeId; 361 | } 362 | 363 | } -------------------------------------------------------------------------------- /src/main/java/com/github/jnidzwetzki/spatialindex/rtree/RTreeBuilder.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * 3 | * Copyright (C) 2015-2018 the BBoxDB project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | *******************************************************************************/ 18 | package com.github.jnidzwetzki.spatialindex.rtree; 19 | 20 | import java.io.RandomAccessFile; 21 | import java.util.ArrayDeque; 22 | import java.util.Deque; 23 | import java.util.List; 24 | 25 | import org.bboxdb.commons.Pair; 26 | import org.bboxdb.commons.math.Hyperrectangle; 27 | 28 | import com.github.jnidzwetzki.spatialindex.SpatialIndexBuilder; 29 | import com.github.jnidzwetzki.spatialindex.SpatialIndexEntry; 30 | import com.github.jnidzwetzki.spatialindex.SpatialIndexException; 31 | 32 | public class RTreeBuilder implements SpatialIndexBuilder { 33 | 34 | /** 35 | * The node factory 36 | */ 37 | protected final RTreeNodeFactory nodeFactory; 38 | 39 | /** 40 | * The root node of the tree 41 | */ 42 | protected RTreeDirectoryNode rootNode; 43 | 44 | /** 45 | * The max size of a child node 46 | */ 47 | protected int maxNodeSize; 48 | 49 | /** 50 | * The default max node size 51 | */ 52 | public final static int DEFAULT_NODE_SIZE = 64; 53 | 54 | /** 55 | * The byte for a non existing child node 56 | */ 57 | public final static byte[] MAGIC_CHILD_NODE_NOT_EXISTING = {-1, 0, 0, 0}; 58 | 59 | /** 60 | * The byte for a following child node 61 | */ 62 | public final static byte[] MAGIC_CHILD_NODE_FOLLOWING = {1, 0, 0, 0}; 63 | 64 | /** 65 | * The size of the magic nodes in bytes 66 | */ 67 | public final static int MAGIC_VALUE_SIZE = 4; 68 | 69 | public RTreeBuilder() { 70 | this(DEFAULT_NODE_SIZE); 71 | } 72 | 73 | public RTreeBuilder(final int maxNodeSize) { 74 | 75 | if(maxNodeSize <= 0) { 76 | throw new IllegalArgumentException("Unable to construct an index with max node size: " 77 | + maxNodeSize); 78 | } 79 | 80 | this.maxNodeSize = maxNodeSize; 81 | this.nodeFactory = new RTreeNodeFactory(); 82 | this.rootNode = nodeFactory.buildDirectoryNode(); 83 | this.rootNode.updateHyperrectangle(); 84 | } 85 | 86 | @Override 87 | public void writeToFile(final RandomAccessFile randomAccessFile) throws SpatialIndexException { 88 | final RTreeSerializer rTreeSerializer = new RTreeSerializer(rootNode, maxNodeSize); 89 | rTreeSerializer.writeToStream(randomAccessFile); 90 | } 91 | 92 | @Override 93 | public boolean bulkInsert(final List elements) { 94 | boolean result = true; 95 | 96 | for(final SpatialIndexEntry entry : elements) { 97 | final boolean insertResult = insert(entry); 98 | 99 | if(! insertResult) { 100 | result = false; 101 | } 102 | } 103 | 104 | return result; 105 | } 106 | 107 | /** 108 | * Insert the given RTreeSpatialIndexEntry into the tree 109 | * @param entry 110 | * @return 111 | */ 112 | @Override 113 | public boolean insert(final SpatialIndexEntry entry) { 114 | 115 | if(entry.getHyperrectangle() == null || entry.getHyperrectangle() == Hyperrectangle.FULL_SPACE) { 116 | return false; 117 | } 118 | 119 | final RTreeDirectoryNode childNode = insert(rootNode, entry); 120 | adjustTree(childNode); 121 | 122 | return true; 123 | } 124 | 125 | /** 126 | * Insert the given RTreeSpatialIndexEntry into the tree into the base node or below 127 | * @param insertBaseNode 128 | * @param entry 129 | * @return 130 | */ 131 | protected RTreeDirectoryNode insert(final RTreeDirectoryNode insertBaseNode, final SpatialIndexEntry entry) { 132 | 133 | final Hyperrectangle entryBox = entry.getHyperrectangle(); 134 | 135 | RTreeDirectoryNode childNode = insertBaseNode; 136 | 137 | final Deque path = new ArrayDeque<>(); 138 | path.push(childNode); 139 | 140 | while(! childNode.isLeafNode()) { 141 | if(childNode.getDirectoryNodeChilds().isEmpty()) { 142 | throw new RuntimeException("This is a !leaf node with no childs?"); 143 | } 144 | 145 | childNode = childNode.findBestNodeForInsert(entryBox); 146 | path.push(childNode); 147 | 148 | if(childNode == null) { 149 | throw new RuntimeException("Unable to find a node for insert"); 150 | } 151 | } 152 | 153 | childNode.getIndexEntries().add(entry); 154 | 155 | 156 | while(! path.isEmpty()) { 157 | final RTreeDirectoryNode tmpNode = path.pop(); 158 | tmpNode.updateHyperrectangle(); 159 | } 160 | 161 | return childNode; 162 | } 163 | 164 | /** 165 | * Adjust the tree, beginning from the argument to the tree root 166 | * @param insertedNode 167 | */ 168 | protected void adjustTree(final RTreeDirectoryNode insertedNode) { 169 | 170 | if(insertedNode == null) { 171 | return; 172 | } 173 | 174 | RTreeDirectoryNode nodeToCheck = insertedNode; 175 | 176 | // Adjust beginning from the bottom 177 | do { 178 | //nodeToCheck.testCovering(); 179 | 180 | if(nodeToCheck.getSize() > maxNodeSize) { 181 | nodeToCheck = splitNode(insertedNode); 182 | } else { 183 | nodeToCheck = nodeToCheck.getParentNode(); 184 | } 185 | 186 | } while(nodeToCheck != null); 187 | } 188 | 189 | /** 190 | * Split the given node 191 | * @param nodeToSplit 192 | * @return 193 | */ 194 | protected RTreeDirectoryNode splitNode(final RTreeDirectoryNode nodeToSplit) { 195 | final RTreeDirectoryNode newNode1 = nodeFactory.buildDirectoryNode(); 196 | final RTreeDirectoryNode newNode2 = nodeFactory.buildDirectoryNode(); 197 | RTreeDirectoryNode newParent = null; 198 | 199 | // Root node is full 200 | if(nodeToSplit.getParentNode() == null) { 201 | rootNode = nodeFactory.buildDirectoryNode(); 202 | newParent = rootNode; 203 | } else { 204 | newParent = nodeFactory.buildDirectoryNode(); 205 | nodeToSplit.getParentNode().addDirectoryNodeChild(newParent); 206 | nodeToSplit.getParentNode().removeDirectoryNodeChild(nodeToSplit); 207 | } 208 | 209 | // Insert new directory node 210 | newParent.addDirectoryNodeChild(newNode1); 211 | newParent.addDirectoryNodeChild(newNode2); 212 | newNode1.setParentNode(newParent); 213 | newNode2.setParentNode(newParent); 214 | 215 | // Find seeds and distribute data 216 | if(nodeToSplit.isLeafNode()) { 217 | distributeLeafData(nodeToSplit, newNode1, newNode2); 218 | } else { 219 | distributeIndexData(nodeToSplit, newNode1, newNode2); 220 | } 221 | 222 | // Recalculate the bounding boxes 223 | newNode1.updateHyperrectangle(); 224 | newNode2.updateHyperrectangle(); 225 | newParent.updateHyperrectangle(); 226 | 227 | return newParent; 228 | } 229 | 230 | @Override 231 | public List getEntriesForRegion(final Hyperrectangle Hyperrectangle) { 232 | return rootNode.getEntriesForRegion(Hyperrectangle); 233 | } 234 | 235 | /** 236 | * Distribute the leaf data 237 | * @param nodeToSplit 238 | * @param newNode1 239 | * @param newNode2 240 | */ 241 | protected void distributeIndexData(final RTreeDirectoryNode nodeToSplit, final RTreeDirectoryNode newNode1, 242 | final RTreeDirectoryNode newNode2) { 243 | 244 | final List dataToDistribute = nodeToSplit.getDirectoryNodeChilds(); 245 | 246 | final QuadraticSeedPicker seedPicker = new QuadraticSeedPicker<>(); 247 | final Pair seeds 248 | = seedPicker.quadraticPickSeeds(dataToDistribute); 249 | 250 | newNode1.addDirectoryNodeChild(seeds.getElement1()); 251 | newNode2.addDirectoryNodeChild(seeds.getElement2()); 252 | 253 | for(int i = 0; i < dataToDistribute.size(); i++) { 254 | newNode1.updateHyperrectangle(); 255 | newNode2.updateHyperrectangle(); 256 | 257 | final int remainingObjects = dataToDistribute.size() - i; 258 | final RTreeDirectoryNode entry = dataToDistribute.get(i); 259 | 260 | if(newNode1.getDirectoryNodeChilds().size() + remainingObjects <= maxNodeSize / 2) { 261 | newNode1.addDirectoryNodeChild(entry); 262 | continue; 263 | } 264 | 265 | if(newNode2.getDirectoryNodeChilds().size() + remainingObjects <= maxNodeSize / 2) { 266 | newNode2.addDirectoryNodeChild(entry); 267 | continue; 268 | } 269 | 270 | final double node1Enlargement = newNode1.getHyperrectangle().calculateEnlargement(entry.getHyperrectangle()); 271 | final double node2Enlargement = newNode2.getHyperrectangle().calculateEnlargement(entry.getHyperrectangle()); 272 | 273 | if(node1Enlargement == node2Enlargement) { 274 | if(newNode1.getDirectoryNodeChilds().size() < newNode2.getDirectoryNodeChilds().size()) { 275 | newNode1.addDirectoryNodeChild(entry); 276 | continue; 277 | } else { 278 | newNode2.addDirectoryNodeChild(entry); 279 | continue; 280 | } 281 | } 282 | 283 | if(node1Enlargement < node2Enlargement) { 284 | newNode1.addDirectoryNodeChild(entry); 285 | continue; 286 | } else { 287 | newNode2.addDirectoryNodeChild(entry); 288 | continue; 289 | } 290 | } 291 | } 292 | 293 | /** 294 | * Distribute the index data 295 | * @param nodeToSplit 296 | * @param newNode1 297 | * @param newNode2 298 | */ 299 | protected void distributeLeafData(final RTreeDirectoryNode nodeToSplit, 300 | final RTreeDirectoryNode newNode1, 301 | final RTreeDirectoryNode newNode2) { 302 | 303 | final List dataToDistribute = nodeToSplit.getIndexEntries(); 304 | 305 | final QuadraticSeedPicker seedPicker = new QuadraticSeedPicker<>(); 306 | final Pair seeds 307 | = seedPicker.quadraticPickSeeds(dataToDistribute); 308 | 309 | insert(newNode1, seeds.getElement1()); 310 | insert(newNode2, seeds.getElement2()); 311 | 312 | for(int i = 0; i < dataToDistribute.size(); i++) { 313 | newNode1.updateHyperrectangle(); 314 | newNode2.updateHyperrectangle(); 315 | 316 | final int remainingObjects = dataToDistribute.size() - i; 317 | final SpatialIndexEntry entry = dataToDistribute.get(i); 318 | 319 | if(newNode1.getIndexEntries().size() + remainingObjects <= maxNodeSize / 2) { 320 | insert(newNode1, entry); 321 | continue; 322 | } 323 | 324 | if(newNode2.getIndexEntries().size() + remainingObjects <= maxNodeSize / 2) { 325 | insert(newNode2, entry); 326 | continue; 327 | } 328 | 329 | final double node1Enlargement = newNode1.getHyperrectangle().calculateEnlargement(entry.getHyperrectangle()); 330 | final double node2Enlargement = newNode2.getHyperrectangle().calculateEnlargement(entry.getHyperrectangle()); 331 | 332 | if(node1Enlargement == node2Enlargement) { 333 | if(newNode1.getIndexEntries().size() < newNode2.getIndexEntries().size()) { 334 | insert(newNode1, entry); 335 | continue; 336 | } else { 337 | insert(newNode2, entry); 338 | continue; 339 | } 340 | } 341 | 342 | if(node1Enlargement < node2Enlargement) { 343 | insert(newNode1, entry); 344 | continue; 345 | } else { 346 | insert(newNode2, entry); 347 | continue; 348 | } 349 | } 350 | } 351 | 352 | /** 353 | * Get the maximal node size 354 | * @return 355 | */ 356 | public int getMaxNodeSize() { 357 | return maxNodeSize; 358 | } 359 | 360 | /** 361 | * Test the covering of the child nodes 362 | */ 363 | public void testCovering() { 364 | rootNode.testCovering(); 365 | } 366 | } 367 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------