Visualize with the ArcGIS API for Python

The ArcGIS API for Python contains a mapping module that can extend the visualization capabilities of ArcGIS GeoAnalytics Engine via an interactive web map widget. This widget can be used to view the geometries in a Spark DataFrame column while leveraging features like online basemaps, panning, zooming, and clicking on geometries to identify them.

To visualize a Spark DataFrame geometry column in the map widget, the DataFrame must first be converted to a pandas Spatially Enabled DataFrame (sedf) using st.to_pandas_sdf().

This tutorial will show you how to convert a Spark DataFrame to a Spatially Enabled DataFrame and use arcgis.mapping to visualize the geometries.

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 arcgis module.
  4. An internet connection (for accessing sample data).

Steps

Import and authorize

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

    Python
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    import geoanalytics
    geoanalytics.auth(username="user1", password="p@ssword")
    

Plot geometry columns in the map widget

  1. Load a polygon feature service of US state boundaries into a DataFrame.
Python
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
url = "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_States_Generalized/FeatureServer/0"
us_states = spark.read.format("feature-service").load(url)
  1. Convert the Spark DataFrame to a Spatially Enabled DataFrame. This requires that you have the arcgis module installed. arcgis will be imported automatically when calling st.to_pandas_sdf.
Python
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
states_sdf = us_states.st.to_pandas_sdf()
  1. Show the geometries in the map widget using the plot method on the Spatially Enabled DataFrame. In this example the state polygons are symbolized using the population of each state. The following arguments are used:

    • renderer_type—Set to c for class breaks renderer.
    • method—The class breaks algorithm used to define classes.
    • class_count—The number of classes to symbolize with.
    • col—The column to symbolize on.
    • cmap—The colormap to use.
    • alpha—Determines the transparency of the geometries.
    Python
    Use dark colors for code blocksCopy
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    states_sdf = states_sdf[["shape", "POPULATION"]].astype({"POPULATION":"int64"})
    
    states_sdf.spatial.plot(renderer_type='c', method='esriClassifyNaturalBreaks', class_count=20,
                            col='POPULATION', cmap='summer', alpha=0.7)
    mapwidget

What's next?

The examples shown here are just a basic introduction to what's possible with arcgis.mapping and Spatially Enabled DataFrames. For more information, see the map widget documentation and the following ArcGIS API for Python guide topics:

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