krishnarb3 commented on code in PR #609: URL: https://github.com/apache/incubator-sedona/pull/609#discussion_r851513402
########## core/src/main/java/org/apache/sedona/core/dbscanJudgement/DBScanJudgement.java: ########## @@ -0,0 +1,180 @@ +/* + * 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.dbscanJudgement; + +import org.apache.spark.api.java.function.FlatMapFunction; +import org.locationtech.jts.geom.Envelope; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.Point; +import org.locationtech.jts.index.strtree.STRtree; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +// TODO: Auto-generated Javadoc + +/** + * The Class DBScanJudgement. + */ +public class DBScanJudgement<T extends Geometry> + implements FlatMapFunction<Iterator<T>, Integer> { + + /** + * The eps distance + */ + double eps; + + /** + * The minimum number of points to form a cluster + */ + int minPoints; + + /** + * The indexes of geoms that are already part of a cluster + */ + Set<Integer> isInCluster; + + /** + * Instantiates a new geometry knn judgement. + * + * @param eps the distance eps + * @param minPoints the minimum number of points to form a cluster + */ + public DBScanJudgement(double eps, int minPoints, Set<Integer> isInCluster) { + this.eps = eps; + this.minPoints = minPoints; + this.isInCluster = isInCluster; + } + + + @Override + public Iterator<Integer> call(Iterator<T> input) throws Exception { + List<T> geoms = new ArrayList<>(); + while (input.hasNext()) { + geoms.add(input.next()); + } + if (geoms.size() < minPoints) { + return Collections.emptyIterator(); + } + Set<Integer> isInCore = new HashSet<>(); + Integer[] neighbors = new Integer[minPoints]; + UnionFind unionFind = new UnionFindImpl(geoms.size()); + STRtree strtree = new STRtree(geoms.size()); + for (Geometry geom : geoms) { + strtree.insert(geom.getEnvelopeInternal(), geom); + } + for (int i = 0; i < geoms.size(); i++) { + int numNeighbors = 0; + List<Integer> geomsInEnvelope = getGeomsInEnvelope(geoms, i, eps, strtree); + if (geomsInEnvelope.size() < minPoints) { + continue; + } + + for (Integer j : geomsInEnvelope) { + if (numNeighbors >= minPoints) { + /* + * If we've already identified p as a core point, and it's already Review Comment: @jiayuasu Have added the comments from POSTGIS code for clarity -- 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: dev-unsubscr...@sedona.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org