Search code examples
databricksazure-databricksmosaic

Error: AttributeError: module 'mosaic' has no attribute 'st_centroid2D'


Getting an error when trying to run an example given in the Mosaic website: https://databrickslabs.github.io/mosaic/api/spatial-functions.html#st-centroid2d

Code:

from mosaic import enable_mosaic
import mosaic as mos
enable_mosaic(spark, dbutils)

from pyspark.sql.functions import *

df = spark.createDataFrame([{'wkt': 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'}])
df.select(mos.st_centroid2D('wkt')).show()

Result:

Error:  AttributeError: module 'mosaic' has no attribute 'st_centroid2D'

This is tried on Microsoft Azure Databricks.

Versions: Mosaic - 0.3.9; Databricks Runtime Version - 11.3 LTS (includes Apache Spark 3.3.0, Scala 2.12)

Can you please help to fix this issue?


Solution

  • It's a bug in the documentation that wasn't updated to reflect the change made to remove st_centroid2D and st_centroid3D functions. Instead of that function you need to use the st_centroid function that will return a WKT object:

    df = spark.createDataFrame([{'wkt': 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'}])
    df.select(mos.st_centroid('wkt')).show(truncate=False)
    

    will give:

    +--------------------------------------------+
    |st_centroid(wkt)                            |
    +--------------------------------------------+
    |POINT (25.454545454545453 26.96969696969697)|
    +--------------------------------------------+
    

    Documentation will be corrected soon.