jiayuasu commented on code in PR #1769: URL: https://github.com/apache/sedona/pull/1769#discussion_r1926440855
########## spark/common/src/main/java/org/apache/sedona/core/spatialPartitioning/IndexedGridPartitioner.java: ########## @@ -0,0 +1,88 @@ +/* + * 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; + +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)) { Review Comment: What is the purpose of doing containFlag? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
