st.with_geodisplay() adds a spatially optimized parquet column (named geodisplay) to a DataFrame. The spatially optimized parquet column is computed using the primary geometry column in the DataFrame. The column includes a spatial index to sort geometries by, as well as simplified representations of geometries for fast visualization in a web map across multiple scales.

Spatially optimized parquet files reduce the time required to render large amounts of data in a web map by storing data in spatially-contiguous chunks and providing simplified, compressed geometries for use when full resolution geometries aren't needed. Applications that read spatially optimized parquet can use parquet page indices, column indices, and row groups to only retrieve data that is strictly needed for visualization. This reduces the amount of data that needs to be streamed over the web and thus improves performance, especially when reading from large datasets.

This tutorial will show you how to use st.with_geodisplay to spatially optimize your large datasets for visualization in a web map.

Prerequisites

The following are required for this tutorial:

  1. A running Spark session configured with ArcGIS GeoAnalytics Engine.
  2. A Jupyter or JupyterLab notebook connected to your Spark session.
  3. The geoanalytics_engine_jsmapping.py Python Script downloaded to your workspace.

Steps

Import and authorize

  1. In your notebook, import geoanalytics and authorize the module using a username and password, an API key, or a license file.

    Python
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    
    import geoanalytics
    geoanalytics.auth(username="user1", password="p@ssword")
    
    import geoanalytics.sql.functions as ST
    import pyspark.sql.functions as F
  2. Import the geoanalytics_engine_jsmapping.py script into your notebook.

    Python
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    
    import geoanalytics_engine_jsmapping
    
    map = geoanalytics_engine_jsmapping.EsriJSMap()

    The esrimap.py Python Script allows for interactive visualization of geometries in GeoAnalytics Engine using the ArcGIS Maps SDK for JavaScript. For a tutorial on how to navigate and use the basic features of the script, see this blog post.

Load your dataset

  1. The dataset used in this tutorial comes from the Overture Maps places dataset, containing over 75 million point geometry representations of real-world places. Here, we limit the dataset to grocery stores in the United States. Please change the S3 path to the latest release of the Overture Maps places dataset.

    Python
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    data_path = "s3a://overturemaps-us-west-2/release/CURRENT-RELEASE/theme=places/type=place/*"
    
    # Create the geometry DataFrame
    df = spark.read.format("parquet").load(data_path) \
            .where(F.col("addresses").country[0] == "US") \
            .withColumn("point_geom", ST.point_from_binary("geometry", 4326)) \
            .withColumn("bbox_intersect", ST.bbox_intersects("point_geom", xmin=-125.0, ymin=24.0, xmax=-66.5, ymax=49.5)) \
            .where("bbox_intersect == true") \
            .where(F.col("taxonomy.primary") == "grocery_store")
  2. Show the first 5 rows of the DataFrame to confirm that the dataset has been loaded correctly.

    Python
    Use dark colors for code blocksCopy
    1
    2
    
    df.select(["id", "names", "point_geom"]).show(5)
    Result
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    +--------------------+--------------------+--------------------+
    |                  id|               names|          point_geom|
    +--------------------+--------------------+--------------------+
    |2bb34c23-823a-443...|{La Curcubita Gro...|{"x":-99.08826051...|
    |01bfcbd5-a3ce-415...|{MB Corner Store,...|{"x":-99.247551,"...|
    |5130e55d-1bfd-477...|{Super S Foods, N...|{"x":-99.26928,"y...|
    |a93ace26-9112-4ca...|{The Snack Shop, ...|{"x":-99.271015,"...|
    |e87a8907-32ad-45c...|{3 G's Grocery & ...|{"x":-99.26204455...|
    +--------------------+--------------------+--------------------+
    only showing top 5 rows

Add the spatially optimized parquet column

  1. Use st.with_geodisplay() to add a spatially optimized parquet column to the DataFrame. This will spatially optimize your dataset for faster web map rendering with clients that can read spatially optimized parquet. The example DataFrame used here has a geometry column named point_geom. Here I am using 16 partitions to create the spatially optimized parquet column. You can adjust the number of partitions based on your dataset size and performance needs. More partitions generally result in more files when writing to parquet or geoparquet. Having too many files relative to the row count in each file can degrade performance. Conversely, storing too much data in a single file will reduce read and write performance with Spark.

    Python
    Use dark colors for code blocksCopy
    1
    2
    
    df_with_geodisplay = df.st.with_geodisplay("point_geom", 16)
  2. View the schema of the DataFrame to confirm that the spatially optimized parquet column has been added.

    Python
    Use dark colors for code blocksCopy
    1
    2
    
    df_with_geodisplay.select("geodisplay").printSchema()
    Result
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    root
     |-- geodisplay: struct (nullable = true)
     |    |-- indexkey: long (nullable = false)
     |    |-- xcoord: double (nullable = false)
     |    |-- ycoord: double (nullable = false)
  3. View the spatially optimized parquet column in the DataFrame.

    Python
    Use dark colors for code blocksCopy
    1
    2
    
    df_with_geodisplay.select("geodisplay").show(5, truncate = False)
    Result
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    +----------------------------------------+
    |geodisplay                              |
    +----------------------------------------+
    |{93404992874, -97.6042, 26.00138}       |
    |{93406840187, -97.62851251, 26.0252559} |
    |{94001622344, -97.4459, 25.8871}        |
    |{94002231533, -97.42378068, 25.87843554}|
    |{94002395732, -97.42360785, 25.88743898}|
    +----------------------------------------+
    only showing top 5 rows
  4. View the metadata of the spatially optimized parquet column. WKID 4326 is the spatial reference for WGS 84. A code of indexkey indicates that the spatially optimized parquet column is a spatial index. The fullExtent property shows the bounding box of the geometries in the DataFrame. The z type indicates that the geometries are points and have been sorted using z-order indexing. The xColumn and yColumn properties indicate the names of the columns that contain the x and y coordinates of the geometries.

    Python
    Use dark colors for code blocksCopy
    1
    2
    
    print(df_with_geodisplay.schema["geodisplay"].metadata)
    Result
    Use dark colors for code blocksCopy
    1
    {'coordinatePrecision': 20, 'writer': {'name': 'ArcGIS GeoAnalytics Engine', 'version': '2.1.0.212.5642'}, 'wkid': 4326, 'code': 'indexkey', 'version': '0.1', 'fullExtent': {'xmax': -66.99002051912875, 'ymin': 24.55337639, 'ymax': 48.99495876, 'xmin': -124.63006908}, 'type': 'z', 'yColumn': 'ycoord', 'xColumn': 'xcoord'}

Save your spatially optimized parquet

You can save your spatially optimized DataFrame as a parquet or geoparquet file. The parquet file format is a columnar storage file format that is optimized for use with big data processing frameworks. The geoparquet file format is an extension of the parquet file format that is optimized for geospatial data.

  1. Use the write method to save the DataFrame as a spatially optimized parquet file.

    Python
    Use dark colors for code blocksCopy
    1
    2
    
    df_with_geodisplay.write.format("geoparquet").save("C:/your/file/location")

Render your dataset

You can host your spatially optimized parquet file on a web server or cloud storage service, using a URL to access the file through clients that can understand spatially optimized parquet, like the ArcGIS Maps SDK for JavaScript.

Note that you must properly configure Cross Origin Resource Sharing (CORS) on your web server or cloud storage service to make your spatially optimized parquet file accessible to the ArcGIS Maps SDK for JavaScript.

  1. After collecting a list of accessible URLs to represent your parquet files, use the display_parquet_layer method to render the spatially optimized parquet file in the map widget.

    Python
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    map.display_parquet_layer(["https:your/file/location/spatially-optimized-000.parquet",
                               "https:your/file/location/spatially-optimized-001.parquet",
                               "https:your/file/location/spatially-optimized-002.parquet",
                               "https:your/file/location/spatially-optimized-003.parquet",
                               "https:your/file/location/spatially-optimized-004.parquet",
                               "https:your/file/location/spatially-optimized-005.parquet",
                               "https:your/file/location/spatially-optimized-006.parquet",
                               "https:your/file/location/spatially-optimized-007.parquet",
                               "https:your/file/location/spatially-optimized-008.parquet",
                               "https:your/file/location/spatially-optimized-009.parquet",
                               "https:your/file/location/spatially-optimized-010.parquet",
                               "https:your/file/location/spatially-optimized-011.parquet",
                               "https:your/file/location/spatially-optimized-012.parquet",
                               "https:your/file/location/spatially-optimized-013.parquet",
                               "https:your/file/location/spatially-optimized-014.parquet",
                               "https:your/file/location/spatially-optimized-015.parquet"])
    parquet display

What's next?

The examples shown here show a basic understanding of how to use st.with_geodisplay() to spatially optimize your datasets for faster rendering in a web map. For more information on parquet files and the map widget, visit these resources:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.