Select to view content in your preferred language

Get the Hosted Feature Layers used to create layers in a Hosted Feature Layer View

545
1
Jump to solution
06-10-2024 03:24 PM
Dirk_Vandervoort
Frequent Contributor

I have a collection of Hosted Feature Layers Views (Views) that are sourced by layers in a Hosted Feature Layer (HFL).

The collection of Views is ever-changing, we need to know the layer sources for the Views after they are created.

In python code, I would like to examine each layer in each View in order to retrieve its source HFL sublayer.

I can see these values in the ArcGIS Online portal UI. However I have not been able to connect the dots between the layer view and it's source layer in the API.

TIA

0 Kudos
1 Solution

Accepted Solutions
EarlMedina
Esri Regular Contributor

If you know what the item ids for your views are you can do it like this:

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
gis = GIS("https://www.arcgis.com", "user", "passwd")

view_id = "425b785aa3564a7d8d71dcc524a8eb9f"
vw_item = gis.content.get(view_id)
relationships = vw_item.related_items(rel_type="Service2Data")

 

This is okay if you only have one relationship. It will give you access to the source item. If the only relatonship is to the source, then you'll get a list of length 1. Simply access that item and get whatever information you need from the Item as usual (id, url, name, etc.)

 

 

I looked and I didn't see an obvious way to get the FeatureService sources endpoint, but this would be the most direct way to get the info you want. Assuming it actually doesn't exist in the API (someone please let us know if it does), you can get the info like this:

 

import requests
from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", "user", "passwd")

# if you already know the direct urls you don't need to do it like this
view_id = "425b785aa3564a7d8d71dcc524a8eb9f"
vw_item = gis.content.get(view_id)

sources_url = f"{vw_item.url}/sources"
data = {"token": gis._con.token, "f": "json"}
r = requests.post(sources_url, data=data)

 

 

r.json() will look something like this:

 

{
  "currentVersion" : 11.2, 
  "services" : [
    {
      "name" : "schedule_pnt", 
      "type" : "FeatureServer", 
      "url" : "https://services3.arcgis.com/ON5SbcAnb3abcdef/ArcGIS/rest/services/schedule_pnt/FeatureServer", 
      "serviceItemId" : "9a65d1256fd04370807a60c11988asdfg"
    }
  ]
}

 

 

If you have a lot to go through, you may need to refresh the token with gis._con.relogin()

View solution in original post

0 Kudos
1 Reply
EarlMedina
Esri Regular Contributor

If you know what the item ids for your views are you can do it like this:

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
gis = GIS("https://www.arcgis.com", "user", "passwd")

view_id = "425b785aa3564a7d8d71dcc524a8eb9f"
vw_item = gis.content.get(view_id)
relationships = vw_item.related_items(rel_type="Service2Data")

 

This is okay if you only have one relationship. It will give you access to the source item. If the only relatonship is to the source, then you'll get a list of length 1. Simply access that item and get whatever information you need from the Item as usual (id, url, name, etc.)

 

 

I looked and I didn't see an obvious way to get the FeatureService sources endpoint, but this would be the most direct way to get the info you want. Assuming it actually doesn't exist in the API (someone please let us know if it does), you can get the info like this:

 

import requests
from arcgis.gis import GIS
gis = GIS("https://www.arcgis.com", "user", "passwd")

# if you already know the direct urls you don't need to do it like this
view_id = "425b785aa3564a7d8d71dcc524a8eb9f"
vw_item = gis.content.get(view_id)

sources_url = f"{vw_item.url}/sources"
data = {"token": gis._con.token, "f": "json"}
r = requests.post(sources_url, data=data)

 

 

r.json() will look something like this:

 

{
  "currentVersion" : 11.2, 
  "services" : [
    {
      "name" : "schedule_pnt", 
      "type" : "FeatureServer", 
      "url" : "https://services3.arcgis.com/ON5SbcAnb3abcdef/ArcGIS/rest/services/schedule_pnt/FeatureServer", 
      "serviceItemId" : "9a65d1256fd04370807a60c11988asdfg"
    }
  ]
}

 

 

If you have a lot to go through, you may need to refresh the token with gis._con.relogin()

0 Kudos