Select to view content in your preferred language

Adding WFS to webmap using Python API

1521
7
Jump to solution
09-05-2023 07:19 AM
Labels (2)
RoryMacGregor
Occasional Contributor

Has anyone has success using the Python API to add OGC WFS data to webmaps? Either directly or by creating a new item on AGOL and then adding that?

I am unable to get the add_layer() method of the WebMap class to work with WFS type items as I get a 'layers' KeyError.

I have tried adding a 'layers' attribute to the object which, interestingly, shows when I use dir() on the object but returns the same KeyError when I try and access item.layers.

Any help appreciated.

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
David_McRitchie
Esri Contributor

Hey Rory,

Apologies but checking more into this via the arcgis.mapping.ogc documentation this will not be supported with wfs layers. Instead it will need to be a WMTS or WMS layer that is used.

I think this would be a good case to raise to ArcGIS Ideas as it might be something that could be included in a future release for the ArcGIS API for Python.

Many thanks,

David

 

 

Esri UK -Technical Support Analyst

View solution in original post

7 Replies
David_McRitchie
Esri Contributor

Hey Rory,

The add_layer method should be correct for this so it could be a data issue.

Could you check the following guide if you have not already? - https://developers.arcgis.com/python/samples/publish-ogc-services/

If this doesn't help then could you please share your WMS service and I can test on my side.

Many thanks,

David

Esri UK -Technical Support Analyst
0 Kudos
RoryMacGregor
Occasional Contributor

Hi David,

Thanks for getting back to me.

I have followed that guide and published the WFS using python. That works. However, I cannot then add the published WFS to a webmap using the add_layer method in the Python API. I can add the layer to a webmap using ArcGIS online in the browser but to do this for every webmap we need to update would be very labour intensive.

This is the url for the WFS: https://api.agrimetrics.co.uk/geoservices/datasets/fb8da72f-4938-4100-ac91-d9b8438ffd4c/wfs?request=...

If you could test it and see if you have the same issue, I would be very grateful!

Best regards,
Rory

David_McRitchie
Esri Contributor

Hey Rory,

Apologies but checking more into this via the arcgis.mapping.ogc documentation this will not be supported with wfs layers. Instead it will need to be a WMTS or WMS layer that is used.

I think this would be a good case to raise to ArcGIS Ideas as it might be something that could be included in a future release for the ArcGIS API for Python.

Many thanks,

David

 

 

Esri UK -Technical Support Analyst
RoryMacGregor
Occasional Contributor

Hi David,

Thanks for clarifying this for us, at least we know where we stand. So, As far as you know, there is no way to automate adding wfs data to webmaps using the Python API? I will take your advice and suggest it for a future API update.

Best,
Rory

David_McRitchie
Esri Contributor

That is correct Rory I do not think this is possible via the Python API, of course, I would love to be proven wrong on this.

Thanks,

David

Esri UK -Technical Support Analyst
KimOllivier
Honored Contributor

Well it is possible in ArcGISPro without any python programming. All you do is add a WFS server, tweak the URL, add any cql_filter expressions and get the layers listed. Add these to a map and tweak symbology, visible items and definition queries, layer names etc. Then select all the layers and Share>featurelayer>publish. This will create a better FeatureLayerCollection in AGOL. Why better? Because you can edit, make WebMaps and anything else. The only drawback? It is not dynamic. But wait, neither is WFS usually. The one I have just extracted only updates nightly anyway.

So how can you replicate this in Python. In principle duplicate the steps:

1. Use the requests module to set up a request on the WFS endpoint just like the WFS server

2. Download each sublayer as a json extract and move into a Pandas SpatiallyEnabledDataFrame. Not quite so easy in AGOL because the geopandas module is missing. In principle it should work but is not supported directly. But you can copy a geopandas dataframe to an esri spatialdataframe.

3. upload the sdf to AGOL with the GeoAccessor esri extension to arcgis.

If the WFS data can be queried for changes you could use the gis.content.edit() functions to just add new records. Don't know about delete records.

0 Kudos
KimOllivier
Honored Contributor

Correction: you cannot hide fields in Pro, they all have to be visible.

0 Kudos