Write to feature services

The ArcGIS API for Python allows you to write data in a Spatially Enabled DataFrame to a feature layer in ArcGIS Online or ArcGIS Enterprise using the to_featurelayer() method.

This tutorial will show you how to convert a Spark DataFrame to a Spatially Enabled DataFrame and use to_featurelayer() to create a feature service via the ArcGIS API for Python (arcgis).

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, license file, or token.

    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
    21
    22
    23
    24
    25
    26
    import geoanalytics
    geoanalytics.auth(username="user1", password="p@ssword")
    

Write a Spark DataFrame to a feature service using the ArcGIS API for Python

  1. Import arcgis and log into ArcGIS Online or ArcGIS Enterprise. For more information check out Working with different authentication schemes.

    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
    21
    22
    23
    24
    25
    26
    from arcgis.gis import GIS
    username="<username>"
    password="<password>"
    gis=GIS(username=username,password=password)
    print("Successfully logged in as: " + gis.properties.user.username)
    
  2. Load a polygon feature service of US state boundaries into a Spark 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
    21
    22
    23
    24
    25
    26
    url = "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_States_Generalized/FeatureServer/0"
    us_states = spark.read.format("feature-service").load(url)
    
  3. 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
    21
    22
    23
    24
    25
    26
    us_states_sdf = us_states.st.to_pandas_sdf()
    
    
  4. Export the Spatially Enabled Dataframe to a feature layer hosted in ArcGIS Online or ArcGIS Enterprise via the account that you have signed in during step 1.

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
21
22
23
24
25
26
feature_layer = us_states_sdf.spatial.to_featurelayer('US States')

What's next?

For more information, see the Writing GIS Data 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.