Select to view content in your preferred language

Item ID only returning title

508
4
Jump to solution
10-24-2024 12:54 PM
Labels (2)
jmpmcmanus
Occasional Contributor

I am trying to connect to ArcGIS Online and retrieve information on an item using an item ID. My code is as follows:

import arcgis

from arcgis.gis import GIS

gis = GIS(url='https://unc.maps.arcgis.com', username='my_username', password='mypassword')

itemid = '9d4c5e75fa5b4bcabbb6161bafe10f57'

item = gis.content.get(itemid)

The code runs with out error, but it only returns the title:

print(item)

<Item title:"Vision_Zero_Safety" type:Shapefile owner:my_username'>

It I attempt to get other information such as:

item.resources.list()

it returns an empty list:

[]

Could this be due to permission issues? I've tried to retrieve information on other items I have but it always just returns the title. I've doubled checked the item id's and they are correct.

 

0 Kudos
1 Solution

Accepted Solutions
jmpmcmanus
Occasional Contributor

That worked. Thanks Glen!

View solution in original post

0 Kudos
4 Replies
Clubdebambos
MVP Regular Contributor

Hi @jmpmcmanus 

By printing item you are printing the item object and what you are being returned is correct.

What do you want to get from the item object? An item object has a dict base.

import json
...

## probably wont print pretty
print(dict(item))

## print out the json for the item in a more readable fashion
print(json.dumps(item, indent= 4)

## if a feature service item
print(item.layers) # a list of FeatureLayer objects
print(item.tables) # a list of Table objects

 

Each item object has different options based on the item type. For example, a WebMap item return information when using item.get_data() where a Feature Layer Collection item (Feature Service) will return an empty list.

You can access some class properties, similar to .layers and .tables. Just check out the reference docs here.

You are accessing a Shapefile item, so you need to figure out if printing dict(item) returns the information you desire or what is it exactly you want to get from the item?

All the best,

Glen

 

~ learn.finaldraftmapping.com
0 Kudos
jmpmcmanus
Occasional Contributor

Hi Glen

That is very helpful.

I'm taking the course Updating Realtime Data Using ArcGIS Python Libraries . In the section Update and online feature service STEP 2 h the following code returns and error:

item = gis.content.get(itemid)
sd_file_name = os.path.basename(original_sd_file)
if sd_file_name != item.related_items("Service2Data")[0].name:
    raise Exception('Erroneous itemid or original sd file'.format(itemid))

 The error returned is:

*** IndexError: list index out of range

I changed the if statement using a dict(item) as you described and it work:

if sd_file_name != dict(item)['name']:
  raise Exception('Erroneous itemid or original sd file'.format(itemid))

However, further down the code item is used again:

manager = arcgis.features.FeatureLayerCollection.fromitem(item).manager 

This time the error is:

*** TypeError: item must be a type of service, not Service Definition

I looked at the Documentation for FeatureLayerCollection fromitem but did not find anything that made sense to me. I'm not sure what items in item it is trying to use. Possibly it uses them all. The documentation you provide on item says it should be a dictionary, but using dict(item) did not work

Any suggestions would be greatly appreciated.

Thanks

Jim

0 Kudos
Clubdebambos
MVP Regular Contributor

Hi Jim,

A FeatureLayerCollection is a Feature Service at heart. Let's begin with an Item object. An Item object represents any content item in your AGOL/Portal. Once you have an Item object, you can get what Item type it is representing.

from arcgis.gis import GIS

## access ArcGIS Online
agol = GIS("home")

## get Feature Service Item (Feature Layer Collection)
fs_item = agol.content.get("ITEM_ID")

print(fs_item)
<Item title:"Buffer_Testing" type:Feature Layer Collection owner:FinalDraftMapping>

print(fs_item.type)
Feature Service

## get WebMap Item
wm_item = agol.content.get("ITEM_ID")

print(wm_item)
<Item title:"Mean_Center_WebMap" type:Web Map owner:FinalDraftMapping>

print(wm_item.type)
Web Map

## get Shapefile Item
shp_item = agol.content.get("ITEM_ID")

print(shp_item)
<Item title:"NPWS_BIO_DesignatedAreaBoundaries" type:Shapefile owner:FinalDraftMapping>

print(shp_item.type)
Shapefile

## get CSV Item
csv_item = agol.content.get("ITEM_ID")

print(csv_item)
<Item title:"ARCGISAFP03_DHLGH_HER_NIAH_CSV" type:CSV owner:FinalDraftMapping>

print(csv_item.type)
CSV

 

Now, depending on the Item type, you can convert/cast to its proper object. For examples, you can create a FeatureLayerCollection object from a Feature Service, or a WebMap (Map at version 2.4.0) from a Web Map item.

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
from arcgis.mapping import WebMap
## from arcgis.map import Map # this is for version 2.4.0+

## access ArcGIS Online
agol = GIS("home")

## get Feature Service Item (Feature Layer Collection)
fs_item = agol.content.get("ITEM_ID")

## create FLC object
flc = FeatureLayerCollection.fromitem(fs_item) # will fail if you supply any other item type
print(type(flc))
<class 'arcgis.features.layer.FeatureLayerCollection'>

## get WebMap Item
wm_item = agol.content.get("ITEM_ID")
wm = WebMap(wm_item)
print(type(wm))
<class 'arcgis.mapping._types.WebMap'>

 

Your code below will fail because item is a Service Definition and not a Feature Service. You need your item to be of type Feature Service.

 

## item needs to be a Feature Service not a Service Definition
manager = arcgis.features.FeatureLayerCollection.fromitem(item).manager

 

 

For the most part, an Item object represents what you see in that Items page and setting in AGOL. Converting/casting to its proper object (FLC, WebMap etc) open up interacting in more depth. For example, FLC will allow updating symbology and popups for example. For a WebMap you can add and remove layers. A FLC object has its own set of methods and properties, and similar with a WebMap object.

 

Only use dict(item) when you want to access the properties of an Item object. It takes the item object and casts it to a dictionary so you can see the JSON definition for the item.

Hope that helps.

 

~ learn.finaldraftmapping.com
0 Kudos
jmpmcmanus
Occasional Contributor

That worked. Thanks Glen!

0 Kudos