Tutorial: Import data

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

Download data

With an ArcGIS Online account, ArcGIS Location Platform account, or ArcGIS Enterprise account you can upload geographic data to your portal 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 of the layer in your portal (only for ArcGIS Online and ArcGIS Enterprise accounts).

With the ArcGIS API for Python you can automate the process of importing and then publishing feature 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 items 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 your portal using your ArcGIS Location Platform account, ArcGIS Online account, or ArcGIS Enterprise account.

    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
    54
    
    from arcgis.gis import GIS, ItemProperties, ItemTypeEnum
    
    portal = GIS(username="<YOUR_USERNAME>")
    
    

Import the Trailheads CSV file

  1. Create an ItemProperties object to store the metadata for the CSV file. Use the fields title, description, tags, and item_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
    54
    
    portal = GIS(username="<YOUR_USERNAME>")
    
    trailhead_item_properties = ItemProperties(
        item_type = ItemTypeEnum.CSV,
        title="Trailheads",
        description = "Trailheads CSV file imported using the ArcGIS API for Python",
        tags="la, trailheads, csv, python, los, angeles"
    )
    trailhead_item_properties
    
    
  2. Upload the file by calling the add method on the Folder class. Pass in the local path of the file and the ItemProperties object. The add method is run asynchronously, so it return a Future object. Use the result() method to return the resulting 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
    54
    
    trailhead_item_properties = ItemProperties(
        item_type = ItemTypeEnum.CSV,
        title="Trailheads",
        description = "Trailheads CSV file imported using the ArcGIS API for Python",
        tags="la, trailheads, csv, python, los, angeles"
    )
    trailhead_item_properties
    
    csv_file = './data/LA_Hub_datasets/LA_Hub_datasets/Trailheads.csv'
    
    # get the root folder, i.e. "MyContent" > "All my content"
    folder = portal.content.folders.get()
    folder
    
    csv_item = folder.add(item_properties = trailhead_properties, file = csv_file).result()
    
    
  3. Call the publish method on the returned Item to publish a feature layer. The results will be a new Item representing the published service.

    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
    54
    
    trailhead_item_properties = ItemProperties(
        item_type = ItemTypeEnum.CSV,
        title="Trailheads",
        description = "Trailheads CSV file imported using the ArcGIS API for Python",
        tags="la, trailheads, csv, python, los, angeles"
    )
    trailhead_item_properties
    
    csv_file = './data/LA_Hub_datasets/LA_Hub_datasets/Trailheads.csv'
    
    # get the root folder, i.e. "MyContent" > "All my content"
    folder = portal.content.folders.get()
    folder
    
    csv_item = folder.add(item_properties = trailhead_properties, file = csv_file).result()
    
    trailhead_service = csv_item.publish()
    trailhead_service.url
    
    

Import the Trails GeoJSON file

  1. Create an ItemProperties object to store the metadata for the GeoJSON file. Use the fields title, description, tags, and item_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
    54
    
    trailhead_service = csv_item.publish()
    trailhead_service.url
    
    trails_properties = ItemProperties(
      title = "Trails",
      description = "Trails GeoJSON file imported using the ArcGIS API for Python",
      tags = "la, trails, geojson, python, los, angeles",
      item_type = ItemTypeEnum.GEOJSON
    )
    trails_properties
    
    
  2. Upload the local GeoJSON file by calling the add method on the Folder class. Pass in the local file path and the ItemProperties object. Since the add method runs asynchronously, it will return a Future object. Use the result() method to access the resulting 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
    54
    
    trails_properties = ItemProperties(
      title = "Trails",
      description = "Trails GeoJSON file imported using the ArcGIS API for Python",
      tags = "la, trails, geojson, python, los, angeles",
      item_type = ItemTypeEnum.GEOJSON
    )
    trails_properties
    
    geojson_file = './data/LA_Hub_datasets/LA_Hub_datasets/Trails.geojson'
    geojson_item = folder.add(item_properties = trails_properties, file = geojson_file).result()
    
    
  3. Call the publish method on the Item to publish the service. This will return a new Item representing the published service.

    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
    54
    
    trails_properties = ItemProperties(
      title = "Trails",
      description = "Trails GeoJSON file imported using the ArcGIS API for Python",
      tags = "la, trails, geojson, python, los, angeles",
      item_type = ItemTypeEnum.GEOJSON
    )
    trails_properties
    
    geojson_file = './data/LA_Hub_datasets/LA_Hub_datasets/Trails.geojson'
    geojson_item = folder.add(item_properties = trails_properties, file = geojson_file).result()
    
    trails_service = geojson_item.publish()
    trails_service
    
    

Import the Parks and Open Spaces Shapefile

  1. Create an ItemProperties object to store the metadata for the ZIP file. Use the fields title, description, tags, and item_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
    54
    
    trails_service = geojson_item.publish()
    trails_service
    
    parks_properties = {
        'title': 'Parks and Open space',
        'tags':'parks, open data, tutorials',
        'type': 'Shapefile'
    }
    
    
  2. Upload the local ZIP file by calling the add method on the Folder class. Pass in the local file path and the ItemProperties object. Since the add method runs asynchronously, it will return a Future object. Use the result() method to access the resulting 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
    54
    
    parks_properties = {
        'title': 'Parks and Open space',
        'tags':'parks, open data, tutorials',
        'type': 'Shapefile'
    }
    
    zip_file = './data/Parks_and_Open_Space.zip' # relative path to notebook
    shp_item = folder.add(item_properties = parks_properties, file = zip_file).result()
    shp_item
    
    
  3. Call the publish method on the returned Item to publish the service. This will return new Item representing the published service.

    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
    54
    
    parks_properties = {
        'title': 'Parks and Open space',
        'tags':'parks, open data, tutorials',
        'type': 'Shapefile'
    }
    
    zip_file = './data/Parks_and_Open_Space.zip' # relative path to notebook
    shp_item = folder.add(item_properties = parks_properties, file = zip_file).result()
    shp_item
    
    parks_service = shp_item.publish()
    parks_service
    
    

Identify the Item Ids and URLs

  1. Use the url and id properties of each of the published service items to get the item ID and service URL. You can use these to access your data 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
    54
    
    print(f"Trailheads (points) item id: {trailhead_service.id} , feature layer url : {trailhead_service.url}")
    print(f"Trails (polylines) item id: {trails_service.id}, feature layer url: {trails_service.url}")
    print(f"Parks (polygons) item id: {parks_service.id}, feature layer url: {parks_service.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.