Tutorial: Import data

Learn how to import data into ArcGIS Online using ArcGIS API for Python.

Download data

With an or , you can upload geographic data to ArcGIS in several formats, including CSV, XLS, GPX, GeoJSON, or Shapefiles. Once imported, you can publish your data 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 level in the developer dashboard or ArcGIS Online.

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

In this tutorial, you will import different file types as stored in ArcGIS.

Prerequisites

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.

Be sure you have completed the Download data tutorial or have downloaded the LA_Hub_Datasets from ArcGIS Online.

Steps

Import modules and log in

  1. Use the GIS class to log in to ArcGIS using your ArcGIS Online account or ArcGIS Developer account.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    from arcgis.gis import GIS
    import getpass
    
    gis = GIS(
      url="https://www.arcgis.com",
      username="username",
      password=getpass.getpass("Enter password:")
    )
    
    

Import the Trailheads CSV file

  1. Create a dictionary object to store the metadata for the CSV file. Use the fields title, description, tags.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    gis = GIS(
      url="https://www.arcgis.com",
      username="username",
      password=getpass.getpass("Enter password:")
    )
    
    trailhead_properties = {
      "title": "Trailheads",
      "description": "Trailheads imported from CSV file",
      "tags": "LA Trailheads"
    }
    
    
  2. Import the file as a data file item by calling the add method. Pass in the local path of the file and the metadata object. This will return an Item object.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    trailhead_properties = {
      "title": "Trailheads",
      "description": "Trailheads imported from CSV file",
      "tags": "LA Trailheads"
    }
    
    csv_file = './data/LA_Hub_datasets/LA_Hub_datasets/Trailheads.csv'
    csv_item = gis.content.add(trailhead_properties, csv_file)
    
    
  3. Call the publish method on the returned csv_item to publish the as a feature layer, which returns another Item instance for the feature layer.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    trailhead_properties = {
      "title": "Trailheads",
      "description": "Trailheads imported from CSV file",
      "tags": "LA Trailheads"
    }
    
    csv_file = './data/LA_Hub_datasets/LA_Hub_datasets/Trailheads.csv'
    csv_item = gis.content.add(trailhead_properties, csv_file)
    
    trailhead_service = csv_item.publish()
    trailhead_service
    
    

Import the Trails GeoJSON file

  1. Create a dictionary object to store the metadata for the GeoJSON file with the fields title, description, tags, and type.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    trailhead_service = csv_item.publish()
    trailhead_service
    
    trails_properties = {
      "title": "Trails",
      "description": "Trails imported from GeoJSON file",
      "tags": "LA Trails",
      "type": "GeoJson"
    }
    
    
  2. Import the file as a data file item by calling the add method. Pass in the local path of the file and the metadata object. This will return an Item object.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    trails_properties = {
      "title": "Trails",
      "description": "Trails imported from GeoJSON file",
      "tags": "LA Trails",
      "type": "GeoJson"
    }
    
    geojson_file = './data/LA_Hub_datasets/LA_Hub_datasets/Trails.geojson'
    geojson_item = gis.content.add(trails_properties, geojson_file)
    
    
  3. Call the publish method on the returned csv_item to publish the as a feature layer, which returns another Item instance for the feature layer.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    trails_properties = {
      "title": "Trails",
      "description": "Trails imported from GeoJSON file",
      "tags": "LA Trails",
      "type": "GeoJson"
    }
    
    geojson_file = './data/LA_Hub_datasets/LA_Hub_datasets/Trails.geojson'
    geojson_item = gis.content.add(trails_properties, geojson_file)
    
    trails_service = geojson_item.publish()
    trails_service
    
    

Import the Parks and Open Spaces Shapefile

  1. Add a dict object to store the metadata for the Shapefile with the fields title, tags, and type.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    trails_service = geojson_item.publish()
    trails_service
    
    parks_properties = {
        'title': 'Parks and Open space test',
        'tags':'parks, open data, tutorials',
        'type': 'Shapefile'
    }
    
    
  2. Load the Shapefile zip as a data file item with a ContentManager by calling gis.content.add which returns an Item object.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    parks_properties = {
        'title': 'Parks and Open space test',
        'tags':'parks, open data, tutorials',
        'type': 'Shapefile'
    }
    
    data_file_location = './data/Parks_and_Open_Space.zip' # relative path to notebook
    shp_item = gis.content.add(parks_properties, data=data_file_location)
    shp_item
    
    
  3. Call the publish method on the returned csv_item to publish the as a feature layer, which returns another Item instance for the feature layer.

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    parks_properties = {
        'title': 'Parks and Open space test',
        'tags':'parks, open data, tutorials',
        'type': 'Shapefile'
    }
    
    data_file_location = './data/Parks_and_Open_Space.zip' # relative path to notebook
    shp_item = gis.content.add(parks_properties, data=data_file_location)
    shp_item
    
    parks_feature_layer_item = shp_item.publish()
    parks_feature_layer_item
    
    

Identify the Item URLs

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

    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
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    print(f"Trailheads feature layer url (Points): {trailhead_service.url}")
    
    print(f"Trails feature layer url (Polylines): {trails_service.url}")
    
    print(f"Parks feature layer url (Polygons): {parks_feature_layer_item.url}")

What's next?

Learn how to use additional functionality in these tutorials:

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

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close