Hi,
I'm new to arcgis API for python. I can't figure out how to get a list of domain values from a hosted feature layer. I did read the api-reference, and can see that there is a query_domain method for the FeatureLayerCollection class (here: arcgis.features module — arcgis 1.8.2 documentation ) but I don't understand the syntax well enough to figure out how to actually write the code.
Thank you,
Jes
The best I could come up with is that you'll have to access the properties of the FL and loop through it to find coded values.
Here's a sample:
from arcgis import GIS gis = ("https://arcgis.com", "username", "password") #Get the arcgis item (this would be your hosted FGDB). item = gis.content.get("itemID") #Get the layers list from the item. layers = item.layers #Select the layer from the list you want to use. fl = layers[0] #Get the properties. properties = fl.properties
#Loop through the JSON to extract the coded values and save them to a list.
lst = []
for row in properties[fields]:
if row['domain']:
lst.append(row['domain'])
That should get you a list of the dictionaries associated with each coded domain into a neat list.
Thank you Kevin!
Your post was very helpful and set me on the right path. This is what I ended up doing, in case this is useful to others.
<SPAN class="keyword token">from</SPAN> arcgis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">def</SPAN> <SPAN class="token function">getDomainNames</SPAN><SPAN class="punctuation token">(</SPAN>itemID<SPAN class="punctuation token">,</SPAN> layerNumber<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Returns a set of domain names from a feature layer within a feature layer collection. :param itemID: identification number for an ArcOnline or Portal item (Feature Layer Collection or FGDB) :type itemID: str :param layerNumber: the integer [0 and up] that calls the feature layer of interest :type layerNumber: int """</SPAN> <SPAN class="comment token"># Assumes user is connected to their ArcOnline account in ArcPro.</SPAN> gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"pro"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Get the arcgis item (this would be your hosted FGDB).</SPAN> item <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>itemID<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Select the layer within the feature layer collection.</SPAN> fl <SPAN class="operator token">=</SPAN> item<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Get the properties.</SPAN> properties <SPAN class="operator token">=</SPAN> fl<SPAN class="punctuation token">.</SPAN>properties <SPAN class="comment token"># create an empty set</SPAN> nameSet <SPAN class="operator token">=</SPAN> set<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Loop through properties, get a list of all coded values for the given domain.</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> properties<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"fields"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># if domain exists for the field and the domain type is codedValue:</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'domain'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">and</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'domain'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'type'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">'codedValue'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># add the domain name to the set.</SPAN> nameSet<SPAN class="punctuation token">.</SPAN>add<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'domain'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'name'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">(</SPAN>nameSet<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">getDomainValues</SPAN><SPAN class="punctuation token">(</SPAN>itemID<SPAN class="punctuation token">,</SPAN> layerNumber<SPAN class="punctuation token">,</SPAN> domName<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" Returns a list of domain codes from a specific domain within a feature layer. :param itemID: identification number for an ArcOnline or Portal item (Feature Layer Collection or FGDB) :type itemID: str :param layerNumber: the integer [0 and up] that calls the feature layer of interest :type layerNumber: int :param domName: name of the domain of interest """</SPAN> <SPAN class="comment token"># Assumes connected to duinc in ArcPro</SPAN> gis <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"pro"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Get the arcgis item (this would be your hosted FGDB).</SPAN> item <SPAN class="operator token">=</SPAN> gis<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>itemID<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Select the layer within the feature layer collection.</SPAN> fl <SPAN class="operator token">=</SPAN> item<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN>layerNumber<SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Get the properties.</SPAN> properties <SPAN class="operator token">=</SPAN> fl<SPAN class="punctuation token">.</SPAN>properties <SPAN class="comment token"># Loop through properties, get a list of all coded value domains.</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> properties<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"fields"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># if domain exists and the domain name is correct:</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'domain'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">and</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'domain'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'name'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> domName<SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># create a list of all codes included in that domain</SPAN> valueList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'code'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'domain'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'codedValues'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">(</SPAN>valueList<SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Edit: After tinkering with the query domains method for Feature Layer Collections, I found out that it wasn't working at all and submitted the issue to ESRI. I think it's an issue with the REST API.
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.