Quick Method to Create Views for Multiple Features (one view per feature)

250
3
Jump to solution
12-21-2021 02:24 PM
Labels (1)
AdminAccount2
Occasional Contributor

I have a web map with roughly 50 features and counting. I'm looking for a quick and dirty way to create a view of each layer where a given field is null. The good news is that this field exists in each of the 50 features. 

 

Is there an easy way to do this? 

 

Thanks! 

 

Mike

0 Kudos
2 Solutions

Accepted Solutions
Kanin
by Esri Contributor
Esri Contributor

You can try using ArcGIS API for Python to automate view creation. This can be done via ArcGIS Notebooks in ArcGIS Online or ArcGIS Pro. Please see more steps at the links:

Best regards,
Kanin 

If this answer solved your question, please mark it as "Accept as Solution" to help others who have the same question.

View solution in original post

0 Kudos
AdminAccount2
Occasional Contributor

@Kanin ,

Thanks for pointing me in the right direction. Here's the code snippet that I used: 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
from arcgis.mapping import WebMap

gis = GIS("<portal>", "user","pw")

fl_wm_search = gis.content.get("web_map_item_id")
fl_wm_object = WebMap(fl_wm_search)

fl_layers = {item.itemId for item in fl_wm_object.layers}
update_dict = {"viewDefinitionQuery" : "field1 = ''"}

for f in fl_layers:
    source_search = gis.content.search(f)[0]
    source_flc = FeatureLayerCollection.fromitem(source_search)
    new_view = source_flc.manager.create_view(name="{}_Null".format(source_search.title.replace(" ","_")))
    view_search = gis.content.search(new_view.title)[0]
    view_flc = FeatureLayerCollection.fromitem(view_search)
    service_layer = view_flc.layers
    for i in range (len(service_layer)):
        service_layer[i].manager.update_definition(update_dict)
    view_search.move("Home Folder")

Happy New Years!

 

Best,

 

Mike

View solution in original post

3 Replies
Kanin
by Esri Contributor
Esri Contributor

You can try using ArcGIS API for Python to automate view creation. This can be done via ArcGIS Notebooks in ArcGIS Online or ArcGIS Pro. Please see more steps at the links:

Best regards,
Kanin 

If this answer solved your question, please mark it as "Accept as Solution" to help others who have the same question.

0 Kudos
AdminAccount2
Occasional Contributor

@Kanin ,

Thanks for pointing me in the right direction. Here's the code snippet that I used: 

from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
from arcgis.mapping import WebMap

gis = GIS("<portal>", "user","pw")

fl_wm_search = gis.content.get("web_map_item_id")
fl_wm_object = WebMap(fl_wm_search)

fl_layers = {item.itemId for item in fl_wm_object.layers}
update_dict = {"viewDefinitionQuery" : "field1 = ''"}

for f in fl_layers:
    source_search = gis.content.search(f)[0]
    source_flc = FeatureLayerCollection.fromitem(source_search)
    new_view = source_flc.manager.create_view(name="{}_Null".format(source_search.title.replace(" ","_")))
    view_search = gis.content.search(new_view.title)[0]
    view_flc = FeatureLayerCollection.fromitem(view_search)
    service_layer = view_flc.layers
    for i in range (len(service_layer)):
        service_layer[i].manager.update_definition(update_dict)
    view_search.move("Home Folder")

Happy New Years!

 

Best,

 

Mike

Kanin
by Esri Contributor
Esri Contributor

Hello Mike,

Glad this works for you. Happy New Years to you too!

Cheers,
Kanin

0 Kudos