如何使用GeoSpark对spatialRDD进行空间划分?

时间:2018-04-27 15:42:33

标签: scala apache-spark geospatial rdd

有效地在GeoSpark中对spatialRDD进行空间分区吗? 例如:使用GeoSpark或类似的东西将多个点彼此靠近的分区放在1个分区中?

2 个答案:

答案 0 :(得分:1)

作为Georg评论的扩展,我想向您展示一个使用QuadTree的示例。尚未使用其余分区方法,但我希望它们的行为相同(当然,实际分区除外)。假设您要分区的变量是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.spatialPartitionedRDD.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
...

可以使用您喜欢的绘图工具将其可视化(抱歉,不能包含此代码)

QuadNodes

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

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

答案 1 :(得分:0)