st.with
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 to spatially optimize your large datasets for visualization
in a web map.
Prerequisites
The following are required for this tutorial:
- A running Spark session configured with ArcGIS GeoAnalytics Engine.
- A Jupyter or JupyterLab notebook connected to your Spark session.
- The
geoanalyticsPython Script downloaded to your workspace._engine _jsmapping.py
Steps
Import and authorize
-
In your notebook, import
geoanalyticsand authorize the module using a username and password, an API key, or a license file.PythonUse dark colors for code blocks Copy import geoanalytics geoanalytics.auth(username="user1", password="p@ssword") import geoanalytics.sql.functions as ST import pyspark.sql.functions as F -
Import the
geoanalyticsscript into your notebook._engine _jsmapping.py PythonUse dark colors for code blocks Copy 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
-
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.
PythonUse dark colors for code blocks Copy 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") -
Show the first 5 rows of the DataFrame to confirm that the dataset has been loaded correctly.
PythonUse dark colors for code blocks Copy df.select(["id", "names", "point_geom"]).show(5)ResultUse dark colors for code blocks Copy +--------------------+--------------------+--------------------+ | 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
-
Use
st.withto 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_geodisplay() point. 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._geom PythonUse dark colors for code blocks Copy df_with_geodisplay = df.st.with_geodisplay("point_geom", 16) -
View the schema of the DataFrame to confirm that the spatially optimized parquet column has been added.
PythonUse dark colors for code blocks Copy df_with_geodisplay.select("geodisplay").printSchema()ResultUse dark colors for code blocks Copy root |-- geodisplay: struct (nullable = true) | |-- indexkey: long (nullable = false) | |-- xcoord: double (nullable = false) | |-- ycoord: double (nullable = false) -
View the spatially optimized parquet column in the DataFrame.
PythonUse dark colors for code blocks Copy df_with_geodisplay.select("geodisplay").show(5, truncate = False)ResultUse dark colors for code blocks Copy +----------------------------------------+ |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 -
View the metadata of the spatially optimized parquet column. WKID 4326 is the spatial reference for WGS 84. A code of
indexkeyindicates that the spatially optimized parquet column is a spatial index. Thefullproperty shows the bounding box of the geometries in the DataFrame. TheExtent ztype indicates that the geometries are points and have been sorted using z-order indexing. ThexandColumn yproperties indicate the names of the columns that contain the x and y coordinates of the geometries.Column PythonUse dark colors for code blocks Copy print(df_with_geodisplay.schema["geodisplay"].metadata)ResultUse dark colors for code blocks Copy {'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.
-
Use the
writemethod to save the DataFrame as a spatially optimized parquet file.PythonUse dark colors for code blocks Copy 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.
-
After collecting a list of accessible URLs to represent your parquet files, use the
displaymethod to render the spatially optimized parquet file in the map widget._parquet _layer PythonUse dark colors for code blocks Copy 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"])
What's next?
The examples shown here show a basic understanding of how to use st.with 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: