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 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
Add line. Add line. Add line. Add line. Add line. Add line. Add line.
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 Make a dict
to store the metadata for the CSV file with the fields title
, description
, tags
.
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line.
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"
}
Import the file as a data file item using Content Manager
by calling gis.content.add
which returns an Item
object.
Use dark colors for code blocks
Add line. Add line.
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)
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
Add line. Add line.
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
If you receive an error, Runtime Error: Service name 'Trailheads' already exists
, you already have a hosted feature layer item sourced from a feature service named Trailheads in your organization. Feature service names must be unique, so use the publish_ parameters
parameter of the publish()
function to ensure a unique name and avoid duplicate service name errors.
Use dark colors for code blocks Copy
1
trailhead_service = csv_item.publish(publish_parameters={ 'name' : 'Trailheads_py' })
Import the Trails GeoJSON file 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
Add line. Add line. Add line. Add line. Add line. Add line.
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"
}
Import the file as a data file item using Content Manager
by calling gis.content.add
which returns an Item
object.
Use dark colors for code blocks
Add line. Add line.
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)
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
Add line. Add line.
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
If you receive an error, Runtime Error: Service name 'Trails' already exists
, you already have a hosted feature layer item sourced from a feature service named Trails in your organization. Feature service names must be unique, use the publish_ parameters
parameter of the publish()
function to ensure a unique name and avoid duplicate service name errors.
Use dark colors for code blocks Copy
1
trails_service = geojson_item.publish(publish_parameters={ 'name' : 'Trails_py' })
Import the Parks and Open Spaces Shapefile Add a dict
object to store the metadata for the Shapefile with the fields title
, tags
, and type
.
Use dark colors for code blocks
Add line. Add line. Add line. Add line. Add line.
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'
}
Load the Shapefile zip as a data file item with a Content Manager
by calling gis.content.add
which returns an Item
object.
Use dark colors for code blocks
Add line. Add line. Add line.
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
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
Add line. Add line.
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
If you receive an error, Runtime Error: Service name 'Parks_ and_ Open_ Space' already exists
, you already have a hosted feature layer item sourced from a feature service named parks_and_open_space in your organization. Feature service names must be unique, so use the publish_ parameters
parameter of the publish()
function to ensure a unique name and avoid duplicate service name errors.
Use dark colors for code blocks Copy
1
parks_feature_layer = parks_shp.publish(publish_parameters={ 'name' : 'parks_and_open_space_py' })
Identify the Item URLs 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
Add line. Add line. Add line. Add line. Add line.
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} " )