Assessing the Categories Schema for ArcGIS Online with the ArcGIS API for Python

Table of Contents

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, and updating components of ArcGIS Online such as content item category schemas. Creating an Item Categories Schema with the ArcGIS API for Python (API) can be somewhat difficult to get your head around, hopefully this blog post will help with simplifying the process. In a nutshell, an Item Categories Schema enables an organization to categorize their items thematically. There can be 3 levels to a Category; the main category, a sub-category of the main category, and a sub-category of the sub-category. This depth is important to remember when it comes to creating or updating the Categories Schema with the API. We are going to focus purely on using the API to build a Categories Schema instead of having pre-built one manually in ArcGIS Online.

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. 

				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS

				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

The Categories Schema JSON Shell

The Categories Schema is a list containing nested dictionary objects, so let’s start with a very basic list with an empty dictionary.

				
					categories = [{
 
}]
				
			

The Overarching Dictionary - outer

There is an overarching dictionary that contains the rest of the nested dictionaries. We’ll call this dictionary the Categories Container. The Categories Container dictionary has two key, value pairs, the first is “title” : “Categories”, and the second is “categories” : [] (pssst!! this list is what really holds the Categories Schema)

				
					categories = [{
    "title" : "Categories",
    "categories" : []
}]
				
			

Categories Level 1

Within the “categories” : [] list is where we start to build the categories. Each category is a dictionary entry within this list. Each dictionary has two key, value pairs, “title” : <category name>, and “categories” : [] (this list enables us to add in sub-categories if needed).

				
					categories = [{
    "title" : "Categories",
    "categories" : [{
        "title" : "Category 1",
        "categories" : []
    }]
}]
				
			

And let’s add in a second category.

				
					categories = [{
    "title" : "Categories",
    "categories" : [{
        "title" : "Category 1",
        "categories" : []
    },{
        "title" : "Category 2",
        "categories" : []
    }]
}]
				
			

Categories Level 2 (subcategory of level 1)

We can now fill out the empty lists with sub-categories, we won’t go crazy, let’s just add in one for each of the two main categories so we can see what it looks like. Each sub-category is a dictionary that has two key, value pairs, “title” : <subcategory name>, and “categories” : [] (this list enables us to add in sub-categories for the sub-category if needed).

				
					categories = [{
    "title" : "Categories",
    "categories" : [{
        "title" : "Category 1",
        "categories" : [{
            "title" : "Category 1 Subcategory 1",
            "categories": []
        }]
    },{
        "title" : "Category 2",
        "categories" : [{
            "title" : "Category 2 Subcategory 1",
            "categories": []
        }]
    }]
}]
				
			

And let’s add in a second category.

				
					categories = [{
    "title" : "Categories",
    "categories" : [{
        "title" : "Category 1",
        "categories" : [{
            "title" : "Category 1 Subcategory 1",
            "categories": []
        },{
            "title" : "Category 1 Subcategory 2",
            "categories": []
        }]
    },{
        "title" : "Category 2",
        "categories" : [{
            "title" : "Category 2 Subcategory 1",
            "categories": []
        },{
            "title" : "Category 2 Subcategory 2",
            "categories": []
        }]
    }]
}]
				
			

Categories Level 3 (subcategory of level 2)

Lastly, we can add a sub-category to a sub-category, this is as far the depth of the categories can go, therefore, there is only one key, value pair for the dictionaries at this level; “title” : <sub-subcategory name>. Let’s put in one each for a subcategory from Category 1 and 2.

				
					categories = [{
    "title" : "Categories",
    "categories" : [{
        "title" : "Category 1",
        "categories" : [{
            "title" : "Category 1 Subcategory 1",
            "categories": [{
                "title" : "Category 1 Subcategory 1 Sub-subcategory 1"
            }]
        },{
            "title" : "Category 1 Subcategory 2",
            "categories": []
        }]
    },{
        "title" : "Category 2",
        "categories" : [{
            "title" : "Category 2 Subcategory 1",
            "categories": [{
                "title" : "Category 2 Subcategory 1 Sub-subcategory 1"
            }]
        },{
            "title" : "Category 2 Subcategory 2",
            "categories": []
        }]
    }]
}]
				
			

We’re getting the hang of it, we’ll add in a couple of more sub-subcategories.

				
					categories = [{
    "title" : "Categories",
    "categories" : [{
        "title" : "Category 1",
        "categories" : [{
            "title" : "Category 1 Subcategory 1",
            "categories": [{
                "title" : "Category 1 Subcategory 1 Sub-subcategory 1"
            },{
                "title" : "Category 1 Subcategory 1 Sub-subcategory 2"
            },{
                "title" : "Category 1 Subcategory 1 Sub-subcategory 3"
            }]
        },{
            "title" : "Category 1 Subcategory 2",
            "categories": []
        }]
    },{
        "title" : "Category 2",
        "categories" : [{
            "title" : "Category 2 Subcategory 1",
            "categories": [{
                "title" : "Category 2 Subcategory 1 Sub-subcategory 1"
            },{
                "title" : "Category 2 Subcategory 1 Sub-subcategory 2"
            }]
        },{
            "title" : "Category 2 Subcategory 2",
            "categories": []
        }]
    }]
}]
				
			

Current Empty Categories in ArcGIS Online

Let me show you my empty Item Categories Schema in ArcGIS Online.

Finish the script

We access the categories_schema via the ContentManager and set it to be our defined categories.

				
						
agol.content.categories.schema = categories
				
			

Run the script and see the results in ArcGIS Online

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

All the code in one place

You can find the entire code workflow below with links to important components in the documentation that were used.

				
					from arcgis.gis import GIS
 
## Access AGOL
agol = GIS("home")
 
## define the schema
categories = [{
    "title" : "Categories",
    "categories" : [{
        "title" : "Category 1",
        "categories" : [{
            "title" : "Category 1 Subcategory 1",
            "categories": [{
                "title" : "Category 1 Subcategory 1 Sub-subcategory 1"
            },{
                "title" : "Category 1 Subcategory 1 Sub-subcategory 2"
            },{
                "title" : "Category 1 Subcategory 1 Sub-subcategory 3"
            }]
        },{
            "title" : "Category 1 Subcategory 2",
            "categories": []
        }]
    },{
        "title" : "Category 2",
        "categories" : [{
            "title" : "Category 2 Subcategory 1",
            "categories": [{
                "title" : "Category 2 Subcategory 1 Sub-subcategory 1"
            },{
                "title" : "Category 2 Subcategory 1 Sub-subcategory 2"
            }]
        },{
            "title" : "Category 2 Subcategory 2",
            "categories": []
        }]
    }]
}]
 
## update the categories schema
agol.content.categories.schema = categories
				
			

Leave a Comment

Your email address will not be published. Required fields are marked *