Import data

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.

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

  1. Log into ArcGIS Online by making a GIS connection to ArcGIS Online using your ArcGIS Online or developer account. It is not good security practice to leave account credentials in source code shared by others, but it is beyond the scope of this tutorial to go over security best practices. Read about the Python getpass module for a way to avoid display of a password.

    Use dark colors for code blocks
    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. Make a dict to store the metadata for the CSV file with the fields title, description, tags.

    Use dark colors for code blocks
    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:")
    )
    
    trailhead_properties = {
      "title": "Trailheads",
      "description": "Trailheads imported from CSV file",
      "tags": "LA Trailheads"
    }
    
    
  2. Import the file as a data file item using ContentManager by calling gis.content.add which returns an Item object.

    Use dark colors for code blocks
    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 csv_item to publish the CSV, which returns another arcgis.gis.Item instance for the feature layer.

    Use dark colors for code blocks
    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. Make a dict to store the metadata for the GeoJSON file with the fields title, description, tags, and type.

    Use dark colors for code blocks
    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 using ContentManager by calling gis.content.add which returns an Item object.

    Use dark colors for code blocks
    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 geojson_item to publish the GeoJSON file, which returns another arcgis.gis.Item instance for the feature layer.

    Use dark colors for code blocks
    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.

    Use dark colors for code blocks
    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.

    Use dark colors for code blocks
    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 shp_item to publish the Shapefile, which returns another arcgis.gis.Item instance for the feature layer.

    Use dark colors for code blocks
    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.

    Use dark colors for code blocks
    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.urll}")

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