cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Create H3 hexagons Mosaic

shanakap
New Contributor II

I am new to using `Mosaic` on Databricks. The task is to create a Heatmap with the counts within Hexagons of H3 resolution. Since the dataset is quite large I am opting for Mosaic. As the first step I am trying to create the hexagons covering the given area but the following code does not generate hexagons covering the full square as illustrated below:

from pyspark.sql.functions import *
from mosaic import enable_mosaic
enable_mosaic(spark, dbutils)
 
from pyspark import SparkContext
from pyspark.sql import functions as F
import mosaic as mos
 
 
from mosaic import st_point
 
lons = [-80., -80., -70., -70., -80.]
lats = [ 35.,  45.,  45.,  35.,  35.]
 
bounds_df = (
  spark
  .createDataFrame({"lon": lon, "lat": lat} for lon, lat in zip(lons, lats))
  .coalesce(1)
  .withColumn("point_geom", st_point("lon", "lat"))
)
bounds_df.show()
 
from mosaic import st_makeline
 
bounds_df = (
  bounds_df
  .groupBy()
  .agg(F.collect_list("point_geom").alias("bounding_coords"))
  .select(st_makeline("bounding_coords").alias("bounding_ring"))
)
bounds_df.show()
 
from mosaic import st_makepolygon
 
bounds_df = bounds_df.select(st_makepolygon("bounding_ring").alias("bounds"))
bounds_df.show()
 
 
hexs = (bounds_df
       .select(mos.mosaic_explode("bounds", lit(5)))
       .select("index.*")
    )
 
hexs.show()
 
%%mosaic_kepler
hexs "index_id" "h3"

2ATLt 

Do you know why there are gaps between the hexagons?

2 REPLIES 2

shanakap
New Contributor II

This only shows a sample, hence the gap between the hexagons.

Anonymous
Not applicable

Hi @Shanaka Pererea​ 

Hope all is well! Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help. 

We'd love to hear from you.

Thanks!

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group