Help with Deleting features from a View Layer

138
2
Jump to solution
06-07-2018 02:51 PM
New Contributor II

Hi

I control a large database of Statewide data. I need to take the 'master' layer (a feature layer collection) and generate multiple child view layers for each county within the state. We use view layers so when we update the master layer, all the views will change with it. 

Normally I can go into AGOL and do this one by one, by filtering the master by county, then saving each filtered layer independently. However there are 70 counties, and I don't want to do this by hand, and as I am learning Python, I decided to lean into it.

It started well:

I could create county views from the master layer:

I could query each newly minted view layer by county

but when I try to save the query to a new layer

ok - more then one way skin the cat - I try to delete all the unwanted features from the original child view layer

Feels like I am missing something fundamental here, but I admit to being stuck. Can anyone help?

Howard

Reply
0 Kudos
1 Solution

Accepted Solutions
New Contributor III

I think you just need to set the layer's viewDefinitionQuery value on each of the views. For example:

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

gis = GIS(username='user', password='pass')
state_item = gis.content.get('STATE_ITEM_ID')
state_flc = FeatureLayerCollection.fromitem(state_item)
state_flc_mgr = state_flc.manager

counties = ... # setup a list of county names

for county in counties:
    county_view = state_flc_mgr.create_view(county)
    county_layer = county_view.layers[0]
    defn_str = "CountyName = '{}'".format(county)
    county_layer.manager.update_definition({"viewDefinitionQuery": defn_str})‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

Reply
0 Kudos
2 Replies
New Contributor III

I think you just need to set the layer's viewDefinitionQuery value on each of the views. For example:

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

gis = GIS(username='user', password='pass')
state_item = gis.content.get('STATE_ITEM_ID')
state_flc = FeatureLayerCollection.fromitem(state_item)
state_flc_mgr = state_flc.manager

counties = ... # setup a list of county names

for county in counties:
    county_view = state_flc_mgr.create_view(county)
    county_layer = county_view.layers[0]
    defn_str = "CountyName = '{}'".format(county)
    county_layer.manager.update_definition({"viewDefinitionQuery": defn_str})‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

Reply
0 Kudos
New Contributor II

Thanks J.R. !

Sorry for the delay, but I got on this again last night, and after some manipulation of the SQL term, it worked beautifully!

Thank you for the help

Howard

Reply
0 Kudos