Scala 如何使用GeoSpark对空间RDD进行空间分区?

Scala 如何使用GeoSpark对空间RDD进行空间分区?,scala,apache-spark,geospatial,rdd,Scala,Apache Spark,Geospatial,Rdd,有没有一种方法可以有效地对GeoSpark中的空间RDD进行空间划分? e、 g:使用GeoSpark或类似的东西将多个点彼此靠近的分区放在一个分区中?请参见 KDB树 四叉树 R-树 已实施请参见 KDB树 四叉树 R-树 作为Georg comment的扩展,我想向您展示一个使用四叉树的示例。我没有使用其余的分区方法,但我希望它们的行为相同(当然,实际的分区除外)。如果要划分的变量是pointsRDD(在我的例子中,它实际上是PointRDD类型的对象),则可以通过以下方式进行划分:

有没有一种方法可以有效地对GeoSpark中的空间RDD进行空间划分? e、 g:使用GeoSpark或类似的东西将多个点彼此靠近的分区放在一个分区中?

请参见

  • KDB树
  • 四叉树
  • R-树
已实施

请参见

  • KDB树
  • 四叉树
  • R-树

作为Georg comment的扩展,我想向您展示一个使用四叉树的示例。我没有使用其余的分区方法,但我希望它们的行为相同(当然,实际的分区除外)。如果要划分的变量是
pointsRDD
(在我的例子中,它实际上是PointRDD类型的对象),则可以通过以下方式进行划分:

import com.vividsolutions.jts.index.quadtree.Quadtree
import com.vividsolutions.jts.index.SpatialIndex

val buildOnSpatialPartitionedRDD = true // Set to TRUE only if run join query
val numPartitions = 48
pointsRDD.analyze()
pointsRDD.spatialPartitioning(GridType.QUADTREE, numPartitions)
pointsRDD.buildIndex(IndexType.QUADTREE, buildOnSpatialPartitionedRDD)
您将在
pointsRDD.spatialpartitioneddd.rdd
中找到分区数据:

pointsRDD
  .spatialPartitionedRDD
  .rdd
  .mapPartitions(yourFunctionYouWantToRunOnEachPartition)
您可以参考分区树来检查分区:

pointsRDD.partitionTree.getAllZones.asScala.foreach(println)
这将产生类似于

x: 15.857028 y: 53.36364 w: 9.872338000000003 h: 2.7383549999999985 PartitionId: null Lineage: null
x: 15.857028 y: 54.732817499999996 w: 4.936169000000001 h: 1.3691774999999993 PartitionId: null Lineage: null
x: 15.857028 y: 55.41740625 w: 2.4680845000000007 h: 0.6845887499999996 PartitionId: null Lineage: null
x: 15.857028 y: 55.759700625 w: 1.2340422500000003 h: 0.3422943749999998 PartitionId: null Lineage: null
x: 15.857028 y: 55.9308478125 w: 0.6170211250000002 h: 0.1711471874999999 PartitionId: 0 Lineage: null
...
这可以通过您最喜欢的绘图工具可视化(抱歉,无法包含此代码):

要检查分区统计信息,请使用以下代码:

import org.apache.spark.sql.functions._
pointsRDD
  .spatialPartitionedRDD
  .rdd
  .mapPartitionsWithIndex{case (i,rows) => Iterator((i,rows.size))}
  .toDF("partition_number","number_of_records")
  .show()
这将给你:

+----------------+-----------------+
|partition_number|number_of_records|
+----------------+-----------------+
|               0|             8240|
|               1|             7472|
|               2|             5837|
|               3|             3753|
+----------------+-----------------+
only showing top 4 rows

作为Georg comment的扩展,我想向您展示一个使用四叉树的示例。我没有使用其余的分区方法,但我希望它们的行为相同(当然,实际的分区除外)。如果要划分的变量是
pointsRDD
(在我的例子中,它实际上是PointRDD类型的对象),则可以通过以下方式进行划分:

import com.vividsolutions.jts.index.quadtree.Quadtree
import com.vividsolutions.jts.index.SpatialIndex

val buildOnSpatialPartitionedRDD = true // Set to TRUE only if run join query
val numPartitions = 48
pointsRDD.analyze()
pointsRDD.spatialPartitioning(GridType.QUADTREE, numPartitions)
pointsRDD.buildIndex(IndexType.QUADTREE, buildOnSpatialPartitionedRDD)
您将在
pointsRDD.spatialpartitioneddd.rdd
中找到分区数据:

pointsRDD
  .spatialPartitionedRDD
  .rdd
  .mapPartitions(yourFunctionYouWantToRunOnEachPartition)
您可以参考分区树来检查分区:

pointsRDD.partitionTree.getAllZones.asScala.foreach(println)
这将产生类似于

x: 15.857028 y: 53.36364 w: 9.872338000000003 h: 2.7383549999999985 PartitionId: null Lineage: null
x: 15.857028 y: 54.732817499999996 w: 4.936169000000001 h: 1.3691774999999993 PartitionId: null Lineage: null
x: 15.857028 y: 55.41740625 w: 2.4680845000000007 h: 0.6845887499999996 PartitionId: null Lineage: null
x: 15.857028 y: 55.759700625 w: 1.2340422500000003 h: 0.3422943749999998 PartitionId: null Lineage: null
x: 15.857028 y: 55.9308478125 w: 0.6170211250000002 h: 0.1711471874999999 PartitionId: 0 Lineage: null
...
这可以通过您最喜欢的绘图工具可视化(抱歉,无法包含此代码):

要检查分区统计信息,请使用以下代码:

import org.apache.spark.sql.functions._
pointsRDD
  .spatialPartitionedRDD
  .rdd
  .mapPartitionsWithIndex{case (i,rows) => Iterator((i,rows.size))}
  .toDF("partition_number","number_of_records")
  .show()
这将给你:

+----------------+-----------------+
|partition_number|number_of_records|
+----------------+-----------------+
|               0|             8240|
|               1|             7472|
|               2|             5837|
|               3|             3753|
+----------------+-----------------+
only showing top 4 rows