Overview

You will learn: how to import data into ArcGIS Online using ArcGIS API for Python.

With an ArcGIS account, you can upload geographical data to ArcGIS in several formats, including CSV, XLS, and GPX, GeoJSON, or Shapefiles. Once saved, you can publish your data on ArcGIS Online as a feature layer, and then call the underlying ArcGIS REST Feature Service to edit and make spatial queries on that layer.

Initially your feature layer is private, but you can change the sharing permissions in ArcGIS for Developers and ArcGIS Online.

With the ArcGIS API for Python you can automate the process of importing data into ArcGIS Online and publishing that data as a web layer. The import process is a fast and easy way to turn static data into live services that can be displayed, filtered, and edited within your app.

If you haven't already, make sure you import the data from Los Angeles GeoHub in the Download data tutorial.

Before you begin

The ArcGIS API for Python tutorials use Jupyter Notebooks to execute Python code. If you are new to this environment, please see the guide to install the API and use notebooks locally.

Steps

Import dataset to ArcGIS Online

  1. Go to the same notebook that you created in the Download data tutorial.

    In each step below, type (or copy and paste) the commands into a new notebook cell and run the code by clicking run cell or pressing shift + Enter.

  2. Click insert cell below to create a new code cell below the last cell. Log into ArcGIS Online by making a GIS connection to ArcGIS Online using your developer account. Replace "username" and "password" with the credentials for your ArcGIS account.

    from arcgis.gis import GIS
    
    gis = GIS("https://www.arcgis.com", username="your_username", password="your_password")
    
  3. Next we will import the Parks and Open Space.zip Shapefile, and leave the .csv and .geojson files as Challenges below. Make a dict to store the metadata for the Shapefile with the fields title, tags, and type.

    parks_properties = {
        'title': 'Parks and Open Space',
        'tags': 'parks, open data, tutorials',
        'type': 'Shapefile'
    }
    
  4. Now load the data as a feature layer with a ContentManager by calling gis.content.add which returns an Item object.

     data_file_location = 'location/to/Parks_and_Open_Space.zip'
     parks_shp = gis.content.add(parks_properties, data=data_file_location)
    
     # You can now visualize the Item object `parks_shp` in rich HTML notation in your notebook.
     parks_shp
    
  5. Then call the publish method of parks_shp to publish the Shapefile, which returns another arcgis.gis.Item instance for the feature layer.

    parks_feature_layer_item = parks_shp.publish()
    
    # Then visualize the new feature layer
    parks_feature_layer_item
    

Identify the URL

  1. Use the url property of the feature layer object parks_features_layer to get the Service URL. Use this URL to gain access to your feature layer in other tutorials and projects.

    parks_feature_layer_item.url
    

    Which should show you something like this:

    https://services<number>.arcgis.com/<unique user ID>/arcgis/rest/services/Parks_and_Open_Space/FeatureServer
    
  2. Paste the URL in a new browser tab to inspect the feature layer.

Congratulations, you're done!

Your layer should look something like Parks and Open Spaces.

Challenge

Publish the Trailheads.csv and Trails.geojson files

Publish the Trailheads.csv and Trails.geojson files using these String values for each different file type:

File Type String values for type field
.geojson 'GeoJson'
.csv 'CSV'

Add the files as items, and then call the publish() method on the arcgis.gis.Item object to publish it on ArcGIS Online. You can refer to help on publishing csv files if necessary.

Create an instance of a Feature Layer

To access the underlying feature layer from parks_feature_layer_item, try in your notebook:

from arcgis.features import FeatureLayer

layer = FeatureLayer(parks_feature_layer_item.url)

Now try printing the result of type(layer). You should see arcgis.features.layer.FeatureLayer. You can then view the properties for this layer in JSON format by typing layer.properties.

You can learn more about arcgis.features.FeatureLayer, which can only be used for items stored on ArcGIS Online, at the ArcGIS for API Python reference.