-
Notifications
You must be signed in to change notification settings - Fork 748
[SEDONA-702] Create IndexedGridPartitioner, Support preserveUncontain… #1769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jiayuasu
merged 3 commits into
apache:master
from
james-willis:sedona-indexedGridPartitioner
Jan 25, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...mmon/src/main/java/org/apache/sedona/core/spatialPartitioning/IndexedGridPartitioner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.sedona.core.spatialPartitioning; | ||
|
|
||
| import java.util.*; | ||
| import org.apache.sedona.core.enums.GridType; | ||
| import org.locationtech.jts.geom.Envelope; | ||
| import org.locationtech.jts.geom.Geometry; | ||
| import org.locationtech.jts.index.strtree.STRtree; | ||
| import scala.Tuple2; | ||
|
|
||
| /** | ||
| * The IndexedGridPartitioner is used when there is already a set of grids which the data should be | ||
| * partitioned into. It leverages an STRTree to quickly find the grids to place a geometry into. If | ||
| * you have very few objects to place, it may make more sense to use the FlatGridPartitioner. If you | ||
| * do not have a strict requirement to use a specific set of grids, it may make more sense to use | ||
| * another partitioner that generates its own grids from space-partitioning tree, e.g. the | ||
| * KDBTreePartitioner or the QuadTreePartitioner. | ||
| */ | ||
| public class IndexedGridPartitioner extends FlatGridPartitioner { | ||
| private final STRtree index; | ||
|
|
||
| public IndexedGridPartitioner( | ||
| GridType gridType, List<Envelope> grids, Boolean preserveUncontainedGeometries) { | ||
| super(gridType, grids, preserveUncontainedGeometries); | ||
| this.index = new STRtree(); | ||
| for (int i = 0; i < grids.size(); i++) { | ||
| final Envelope grid = grids.get(i); | ||
| index.insert(grid, i); | ||
| } | ||
| index.build(); | ||
| } | ||
|
|
||
| public IndexedGridPartitioner(GridType gridType, List<Envelope> grids) { | ||
| this(gridType, grids, false); | ||
| } | ||
|
|
||
| public IndexedGridPartitioner(List<Envelope> grids, Boolean preserveUncontainedGeometries) { | ||
| this(null, grids, preserveUncontainedGeometries); | ||
| } | ||
|
|
||
| public IndexedGridPartitioner(List<Envelope> grids) { | ||
| this(null, grids); | ||
| } | ||
|
|
||
| public STRtree getIndex() { | ||
| return index; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<Tuple2<Integer, Geometry>> placeObject(Geometry spatialObject) throws Exception { | ||
| List results = index.query(spatialObject.getEnvelopeInternal()); | ||
| if (preserveUncontainedGeometries) { | ||
| // borrowed from EqualPartitioning.placeObject | ||
| final int overflowContainerID = grids.size(); | ||
| final Envelope envelope = spatialObject.getEnvelopeInternal(); | ||
|
|
||
| Set<Tuple2<Integer, Geometry>> result = new HashSet(); | ||
| boolean containFlag = false; | ||
| for (Object queryResult : results) { | ||
| Integer i = (Integer) queryResult; | ||
| final Envelope grid = grids.get(i); | ||
| if (grid.covers(envelope)) { | ||
james-willis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| result.add(new Tuple2(i, spatialObject)); | ||
| containFlag = true; | ||
| } else if (grid.intersects(envelope)) { | ||
| result.add(new Tuple2<>(i, spatialObject)); | ||
| } | ||
| } | ||
|
|
||
| if (!containFlag) { | ||
| result.add(new Tuple2<>(overflowContainerID, spatialObject)); | ||
| } | ||
|
|
||
| return result.iterator(); | ||
| } else { | ||
| return results.stream().map(i -> new Tuple2(i, spatialObject)).iterator(); | ||
| } | ||
| } | ||
| } | ||
96 changes: 96 additions & 0 deletions
96
.../src/test/java/org/apache/sedona/core/spatialPartitioning/IndexedGridPartitionerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.sedona.core.spatialPartitioning; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import junit.framework.TestCase; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.locationtech.jts.geom.Coordinate; | ||
| import org.locationtech.jts.geom.Envelope; | ||
| import org.locationtech.jts.geom.Geometry; | ||
| import org.locationtech.jts.geom.GeometryFactory; | ||
| import scala.Tuple2; | ||
|
|
||
| public class IndexedGridPartitionerTest extends TestCase { | ||
|
|
||
| private List<Envelope> getGrids() { | ||
| List<Envelope> grids = new ArrayList<>(); | ||
| grids.add(new Envelope(0, 50, 0, 50)); | ||
| grids.add(new Envelope(50, 100, 0, 50)); | ||
| grids.add(new Envelope(0, 50, 50, 100)); | ||
| grids.add(new Envelope(50, 100, 50, 100)); | ||
| return grids; | ||
| } | ||
|
|
||
| private IndexedGridPartitioner getPartitioner(Boolean preserveUncontainedGeometries) { | ||
| return new IndexedGridPartitioner(getGrids(), preserveUncontainedGeometries); | ||
| } | ||
|
|
||
| public void testPlaceObjectPreserveContainedGeometries() throws Exception { | ||
| IndexedGridPartitioner partitioner = getPartitioner(true); | ||
| GeometryFactory geometryFactory = new GeometryFactory(); | ||
| Geometry spatialObject = geometryFactory.createPoint(new Coordinate(25, 25)); | ||
| Iterator<Tuple2<Integer, Geometry>> result = partitioner.placeObject(spatialObject); | ||
|
|
||
| List<Tuple2<Integer, Geometry>> resultList = new ArrayList<>(); | ||
| result.forEachRemaining(resultList::add); | ||
|
|
||
| Assert.assertFalse(resultList.isEmpty()); | ||
| Assert.assertEquals(1, resultList.size()); | ||
| Assert.assertEquals(0, (int) resultList.get(0)._1()); | ||
| } | ||
|
|
||
| public void testPlaceObjectDoesntPreserveUncontainedGeometries() throws Exception { | ||
| IndexedGridPartitioner partitioner = getPartitioner(false); | ||
| GeometryFactory geometryFactory = new GeometryFactory(); | ||
| Geometry spatialObject = geometryFactory.createPoint(new Coordinate(-25, -25)); | ||
| Iterator<Tuple2<Integer, Geometry>> result = partitioner.placeObject(spatialObject); | ||
| Assert.assertFalse(result.hasNext()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetGrids() { | ||
| IndexedGridPartitioner partitioner = getPartitioner(true); | ||
| Assert.assertEquals(getGrids(), partitioner.getGrids()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNumPartitions() { | ||
| IndexedGridPartitioner partitioner = getPartitioner(true); | ||
| Assert.assertEquals(5, partitioner.numPartitions()); | ||
|
|
||
| partitioner = getPartitioner(false); | ||
| Assert.assertEquals(4, partitioner.numPartitions()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEquals() { | ||
| IndexedGridPartitioner partitioner = getPartitioner(true); | ||
| List<Envelope> grids = new ArrayList<>(); | ||
| grids.add(new Envelope(0, 50, 0, 50)); | ||
| grids.add(new Envelope(50, 100, 0, 50)); | ||
| grids.add(new Envelope(0, 50, 50, 100)); | ||
| grids.add(new Envelope(50, 100, 50, 100)); | ||
| IndexedGridPartitioner otherPartitioner = new IndexedGridPartitioner(grids, true); | ||
| Assert.assertTrue(partitioner.equals(otherPartitioner)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.