Create View of Hosted Feature Service using Python API

2650
15
Jump to solution
02-15-2018 10:17 AM
MitchEast
New Contributor III

Has anyone used the Python API to create Views of a hosted feature service on ArcGIS Online?

I have a hosted feature service (type = feature layer collection) and want to automate creation of Views. The arcgis.gis module has a method called create_service with an optional parameter to specify if the service is a View. 

I've tested this on a dummy feature service. See the python below that results in an error ('Item' object has no attribute 'create_service'). 

from arcgis.gis import GIS

from arcgis.gis import ContentManager
mygis = GIS("https://www.arcgis.com", "username", "password")
GMitem = mygis.content.get('itemID')

GMitem.create_service(name = "GM_COPY_Reg1",is_view = TRUE)

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
MicahBabinski
Regular Contributor

Hi Mitch,

I haven't actually used the ArcGIS Python API yet (I really need to get on that!). However, from the API docs it appears that the create_service is a method that is called on an instance of the ContentManager class. See the description for the ContentManager class:

Helper class for managing content in ArcGIS Online or ArcGIS Enterprise. This class is not created by users directly. An instance of this class, called ‘content’, is available as a property of the GIS object. Users call methods on this ‘content’ object to manipulate (create, get, search, etc) items.

So it sounds like the last line of code maybe should actually be:

mygis.create_service(name = "GM_COPY_Reg1",is_view = True)

Also, just a note on your syntax: the is_view parameter should probably be title case, not all caps. I think Python will only recognize a boolean as "True" and not "true" or "TRUE".

Hope this helps,

Micah

View solution in original post

15 Replies
MicahBabinski
Regular Contributor

Hi Mitch,

I haven't actually used the ArcGIS Python API yet (I really need to get on that!). However, from the API docs it appears that the create_service is a method that is called on an instance of the ContentManager class. See the description for the ContentManager class:

Helper class for managing content in ArcGIS Online or ArcGIS Enterprise. This class is not created by users directly. An instance of this class, called ‘content’, is available as a property of the GIS object. Users call methods on this ‘content’ object to manipulate (create, get, search, etc) items.

So it sounds like the last line of code maybe should actually be:

mygis.create_service(name = "GM_COPY_Reg1",is_view = True)

Also, just a note on your syntax: the is_view parameter should probably be title case, not all caps. I think Python will only recognize a boolean as "True" and not "true" or "TRUE".

Hope this helps,

Micah

View solution in original post

MitchEast
New Contributor III

Thanks Micah,

That does help. I see now that the method creates an empty feature service. 

Mitch

0 Kudos
MicahBabinski
Regular Contributor

Hi Mitch. Happy to help. I really need to get into that Python API it looks awesome. Mind marking me correct if that indeed did the trick?

0 Kudos
XabierVelasco_Echeverría
Occasional Contributor

Hi Mitch,

I achieved the same, an empty feature service view. Did you make it work properly? i.e. view related to feature service...

Xabier

0 Kudos
XabierVelasco_Echeverría
Occasional Contributor
0 Kudos
MitchEast
New Contributor III

Xabier,

I did get it to work with the below in the past. But it now no longer works, I get a "Key Error: 'itemId'". I also could never figure out how to specify the view_layers portion.

from arcgis import GIS, features
mygis = GIS("https://www.arcgis.com", username, password)

flc = features.managers.FeatureLayerCollectionManager(url of feature server,mygis)
flc.create_view("GM_copy_view", capabilities = 'Update,Editing,Query,Create,Sync')

0 Kudos
GaëtanLAVENU
Esri Contributor

Hi,

I also tried to get it work with the last version of the API (1.5) following the documentation but I get the same error message. Is there a chance to get an answer from Esri Python team ?

0 Kudos
YannKACENELEN
New Contributor

??

Sinon ça va l’ami ?

Ai eu le plaisir de voir ta compagne en Webex il y a 1 mois ☺

La bise à vous deux,

- Y

De : Gaëtan LAVENU

Envoyé : lundi 22 octobre 2018 19:53

À : Kacenelen Yann

Objet : Re: - Re: Create View of Hosted Feature Service using Python API

GeoNet <https://community.esri.com/?et=watches.email.thread>

Re: Create View of Hosted Feature Service using Python API

reply from Gaëtan LAVENU<https://community.esri.com/people/glavenuesrifrance-fr-esridist?et=watches.email.thread> in ArcGIS Online - View the full discussion<https://community.esri.com/message/807912-re-create-view-of-hosted-feature-service-using-python-api?commentID=807912&et=watches.email.thread#comment-807912>

0 Kudos
XabierVelasco_Echeverría
Occasional Contributor

Hi,

This function works ok

def crearVista(coleccionFeatures, nombreVista, permisos, capas, edicionGeometria, limiteConsulta, gruposAGOL):
    global vflc ## Declaración uso variable global para usarla fuera de la función
    global itemVista
    vflc = 0
    itemVista = 0
    # Eliminar vista si existe
    print("nombreVista: " + nombreVista)
    items = gis.content.search(nombreVista, "Feature Layer Collection")
    if items:
        vistaExistente = gis.content.get(items[0].itemid)
        print("vista para eliminar: " + str(items[0].itemid))
        vistaExistente.delete()
        print("vista eliminada")
    # Crear vista   
    vista = coleccionFeatures.manager.create_view(name=nombreVista, spatial_reference=None, extent=None, allow_schema_changes=True, updateable=True, capabilities=permisos, view_layers=capas)
    itemVista = gis.content.get(vista.itemid)
    print("itemVista: " + str(vista.itemid))
    vflc = arcgis.features.FeatureLayerCollection.fromitem(itemVista)
    print("vflc: " + str(vflc))
    update_dict = {'maxRecordCount': limiteConsulta, 'allowGeometryUpdates': edicionGeometria} ## Se señalan 25.000 puesto que es el límite máximo de todos los elementos consultables, incluyendo establecimientos que es la capa que más elementos puede tener (parcelas y naves solo se consideran las disponibles para el límite, etc.)
    vflc.manager.update_definition(update_dict)
    for grupo in gruposAGOL:
        print(grupo)
        idGrupo = gis.groups.search(grupo)[0].groupid
        print(idGrupo)
        if idGrupo:
            itemVista.share(groups=idGrupo)
        else:
            msg = u"ATENCIÓN: grupo " + grupo + u" sin asignar permisos para el servicio de vista " + nombreVista
            print(msg)
            arcpy.AddWarning(msg)
    return
0 Kudos