Hello!
I'm working with the arcgis API for Python, but I'm having some serious issues getting it to work as expected.
I'm using portal-tier authentication.
First I try loading a Web Map directly. That gives me the following error.
from arcgis.gis import GIS
portal = GIS(url="https://maps.xyz.com/portal", username="xyz", password="xyz")
web_map_id = 'xyz'
web_map = portal.content.get(web_map_id)
portal.map(web_map)
============
ValidationError: 3680 validation errors for Webmap
operationalLayers.5.AnnotationLayerArcGISAnnotationLayer.layerType
Input should be 'ArcGISAnnotationLayer' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
operationalLayers.5.CatalogLayerCatalogLayer.layerType
Input should be 'CatalogLayer' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
operationalLayers.5.CSVLayerCSV.layerType
Input should be 'CSV' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
operationalLayers.5.DimensionLayerArcGISDimensionLayer.layerType
...
Field required [type=missing, input_value={'id': 911, 'layerDefinit...efaultVisibility': True}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.4/v/missing
operationalLayers.5.LinkChartLayerLinkChartLayer.layerType
Input should be 'LinkChartLayer' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
Subsequently, I tried starting with an empty map, and loading Feature Layers directly
from arcgis.layers import Service
layers = Service(
"https://xyz.com/arcgis/rest/services/xyz/xyz/FeatureServer"
)
m = portal.map()
m.basemap.basemap = 'satellite'
for lyr in layers.layers:
m.content.add(lyr)
m.center = [xyz, xyz]
m.zoom = 30
That just outputs the basemap, with no layers to be seen.
Printing `layers` gives the following
<FeatureLayerCollection url:"https://xyz.com/arcgis/rest/services/xyz/xyz/FeatureServer">
Is this maybe a permission issue? I have no other ideas left. I've followed the guides in the documentation.
Also, this is all on version 2.4.0
Cheers.
Hi @torbenal.,
.map() is for use with Notebooks. If you are using a script for version 2.4.0 try the following...
from arcgis.gis import GIS
from arcgis.map import Map
## access portal
portal = GIS(url="URL", username="UserName", password="Password")
## the WebMap Item ID
web_map_id = "WM_ITEM_ID"
## get the WebMap as an Item Object
wm_item = portal.content.get(web_map_id)
## create a WMap object
map_obj = Map(wm_item)
## access elements of he Map obejct
print(map_obj)
print(map_obj.extent)
print(map_obj.basemap.basemap) # get basemap from BasemapManager
## add the layers from a Feature Service to the Map
fs_item = portal.content.get("FS_ITEMN_ID")
for layer in fs_item.layers:
map_obj.content.add(layer)
## update the Map object
map_obj.update()
Some of the Map properties/methods are only available with Notebooks such as center and zoom.
If using Notebooks you can perform the following.
## Cell 1
from arcgis.gis import GIS
from arcgis.map import Map
## Cell 2: access portal
portal = GIS(url="URL", username="UserName", password="Password")
## Cell 3: Get the Map Object
web_map_id = "WM_ITEM_ID"
wm_item = portal.content.get(web_map_id)
map_obj = Map(wm_item)
map_obj
## your Map will appear
## CELL 4: Update Map properties
map.zoom = 30
Hope that helps.
Glen
Hi @Clubdebambos
Thanks for your reply.
I am using a Notebook. The main issue is I don't even get past calling the `Map()` initializer, due to the validation errors in my post.
Hi @torbenal,
1. You need to first import the Map class
from arcgis.map import Map
2. Then you get your WebMap as an Item object.
wm_item = portal.content.get("WM_ITEM_ID")
3. Next you create a Map object from the Item.
map_obj = Map(wm_item)
Your original post does not have number 1 and 3 above in place. You are trying to use the WebMap item as a parameter to the map() method which it won't accept. Check the map() method documentation here. If you want to use a WebMap from your Portal follow the above and then you can simply call map_obj to display your map in the Notebook. Once displayed, you can add layers and alter the zoom.
map() is not the same as Map(). map() is a method from the GIS class. Map() is its own class and creates a Map object with it's own properties and methods. When you create a Map() object in Notebook it is essentially a prepopulated map(). The map() documentation has a link to the Map() class to use properties such as zoom and center.
Hi @Clubdebambos
Unfortunately, does not fix my issue. The following is from my Jupyter Notebook.
from arcgis.map import Map
web_map_id = 'xyz'
wm_item = portal.content.get(web_map_id)
map_obj = Map(wm_item)
map_obj
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
Cell In[39], line 4
2 web_map_id = '02280e1953a743a8b97f65782ca1512b'
3 wm_item = portal.content.get(web_map_id)
----> 4 map_obj = Map(wm_item)
5 map_obj
File ~/Documents/fluxense/flowzone/.venv_gis/lib/python3.10/site-packages/arcgis/map/map_widget.py:254, in Map.__init__(self, location, item, gis, **kwargs)
250 self._helper._setup_gis_properties(gis)
252 # Set up the map that will be used
253 # either existing or new instance
--> 254 self._setup_webmap_properties(item)
256 # Assign the definition to helper class. This is the pydantic dataclass.
257 self._helper._set_widget_definition(self._webmap)
File ~/Documents/fluxense/flowzone/.venv_gis/lib/python3.10/site-packages/arcgis/map/map_widget.py:402, in Map._setup_webmap_properties(self, item)
399 data = self._fix_basemap_data(data)
401 # Use pydantic dataclass from webmap_spec
--> 402 self._webmap = ws.Webmap(**data)
404 # For bookmarks class
405 if self._webmap.bookmarks is None:
File ~/Documents/fluxense/flowzone/.venv_gis/lib/python3.10/site-packages/pydantic/main.py:164, in BaseModel.__init__(__pydantic_self__, **data)
162 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
163 __tracebackhide__ = True
--> 164 __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
ValidationError: 3680 validation errors for Webmap
operationalLayers.5.AnnotationLayerArcGISAnnotationLayer.layerType
Input should be 'ArcGISAnnotationLayer' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
operationalLayers.5.CatalogLayerCatalogLayer.layerType
Input should be 'CatalogLayer' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
operationalLayers.5.CSVLayerCSV.layerType
Input should be 'CSV' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
operationalLayers.5.DimensionLayerArcGISDimensionLayer.layerType
Input should be 'ArcGISDimensionLayer' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
operationalLayers.5.FeatureLayerArcGISFeatureLayer.layerType
Input should be 'ArcGISFeatureLayer' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
operationalLayers.5.GeoJSONLayerGeoJSON.layerType
...
Field required [type=missing, input_value={'id': 911, 'layerDefinit...efaultVisibility': True}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.4/v/missing
operationalLayers.5.LinkChartLayerLinkChartLayer.layerType
Input should be 'LinkChartLayer' [type=literal_error, input_value='ArcGISMapServiceLayer', input_type=str]
For further information visit https://errors.pydantic.dev/2.4/v/literal_error
😢😭
I am unable to reproduce. In-fact, I tested your original code and could get it to work. If you create another WebMap manually and just add one layer and re-attempt does it work? Just to see if it is something wrong with the WebMap itself or the functionality.