Using the ArcGIS API for Python with a REST service

8612
11
Jump to solution
08-15-2018 06:12 AM
JustinBridwell2
Occasional Contributor II

Hey All- I have a public REST service that has several layers and feature classes that I want to access. According to the documentation. Once I have the 'arcgis' package imported, I can inject my outside REST service as such:

 *Note: I am using the Jupyter Notebook to test this code. 

from arcgis.gis import GIS
gis = GIS("https://hazards.fema.gov/gis/nfhl/rest/services/CSLF/Prelim_CSLF/MapServer")

Since this is a public service, I didn't have to include any username or password parameters. The service has several layers (item_type="Feature Layer) that I want to access; namely Special Flood Hazard Area Change layer, but perhaps others:

Using the example from the API docs, I tried to do a search in the service just to see all the layers and any info I might need in future calls. I am using the '.search' method from the content manager.

# search and list all feature layers in my contents
search_result = gis.content.search(query="", item_type="Feature Layer") 
search_result

Every time I try this, I get an either a KeyError='num' or a TypeError: must be str, not int (if I try to add a query). What am I doing wrong here and what is the proper way to search within this REST service?

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

According to the documentation.

What documentation, exactly?

According to API Reference for the ArcGIS API for Python — arcgis 1.4.2 documentation, a GIS object "is representative of a single ArcGIS Online organization or an ArcGIS Enterprise deployment," and the "constructor constructs a GIS object given a url and user credentials to ArcGIS Online or an ArcGIS Enterprise Portal."  The service you reference is not part of ArcGIS Online or an ArcGIS Enterprise Portal, it is a map service published from an unfederated ArcGIS Server.

If you are interested in accessing the properties of an individual map service, you can use the arcgis.mapping module — arcgis 1.4.2 documentation:

>>> from arcgis.mapping import MapImageLayer
>>> mil = MapImageLayer(r"https://hazards.fema.gov/gis/nfhl/rest/services/CSLF/Prelim_CSLF/MapServer")
>>> for lyr in mil.layers:
...   print(lyr.properties["name"], lyr.properties["type"])
...
Preliminary Group Layer
Coastal High Hazard Area Change Feature Layer
Floodway Change Feature Layer
Special Flood Hazard Area Change Feature Layer
Non-Special Flood Hazard Area Change  Feature Layer
>>>

 

View solution in original post

11 Replies
JoshuaBixby
MVP Esteemed Contributor

According to the documentation.

What documentation, exactly?

According to API Reference for the ArcGIS API for Python — arcgis 1.4.2 documentation, a GIS object "is representative of a single ArcGIS Online organization or an ArcGIS Enterprise deployment," and the "constructor constructs a GIS object given a url and user credentials to ArcGIS Online or an ArcGIS Enterprise Portal."  The service you reference is not part of ArcGIS Online or an ArcGIS Enterprise Portal, it is a map service published from an unfederated ArcGIS Server.

If you are interested in accessing the properties of an individual map service, you can use the arcgis.mapping module — arcgis 1.4.2 documentation:

>>> from arcgis.mapping import MapImageLayer
>>> mil = MapImageLayer(r"https://hazards.fema.gov/gis/nfhl/rest/services/CSLF/Prelim_CSLF/MapServer")
>>> for lyr in mil.layers:
...   print(lyr.properties["name"], lyr.properties["type"])
...
Preliminary Group Layer
Coastal High Hazard Area Change Feature Layer
Floodway Change Feature Layer
Special Flood Hazard Area Change Feature Layer
Non-Special Flood Hazard Area Change  Feature Layer
>>>

 

JustinBridwell2
Occasional Contributor II

OK. That is exactly what I was looking for. It appears I missed the part about using that functionality (GIS and .content.search) with an outside service. However, what if I added that service in my arcGIS online account? I could access it that way, right? BTW- Your mapping module idea looks like it will work fine as well. 

by Anonymous User
Not applicable

Thank you. I was trying to get information for layers within map services by using "layers" on the web map object, and trying to get information out when layerType = "ArcGISMapServiceLayer" and getting nowhere. 

properties on map image layer layers is what I needed. 

0 Kudos
by Anonymous User
Not applicable

Where would I find a description of which properties I have access to when working with a layer from a map image layer object?

I am still learning how to navigate the API documentation and am finding it a bit daunting.

Thank you,

Randy McGregor.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am assuming you are searching or browsing here:  API Reference for the ArcGIS API for Python — arcgis 1.6.0 documentation .  I would start with MapImageLayer and MapImageLayerManager 

by Anonymous User
Not applicable

Thanks. I have checked MapImageLayer documentation, but what I don't understand is how I can get a list of what properties are available. Clearly "name" and "type" are, but I don't know what else is, and don't know where I would find it. I would like to list fields, field aliases, source data, popup information, etc... Is that all available via mapimagelayer? I don't want any one to just feed me an answer to every question, but I guess I need a little help figuring out how to navigate the documentation to find answers to specific questions. 

Thank you,

Randy

by Anonymous User
Not applicable

"keys" is the answer to your question, Randy.

for example,

"lyr.keys" will tell what keys are available for a particular layer.

0 Kudos
darrenng123123
New Contributor

Hi all, 

I am working in a secure environment that only has connection to the map server that i want to use. I have no access to the internet

In this case, whenever i use the above suggested method to connect to the standalone arcgis server, i get a connection error as it seems that the arcgis python api is attempting to authenticate to www.arcgis.com as an anonymous user. 

Is there any way to use the MapImageLayer with the rest API without first authenticating / connecting to any arcgis enterprise or arcgis portals?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It helps to share a code snippet that replicates the error and the exact error message.  The example above is for connecting to a Portal/AGOL layer, and you are talking about stand-alone ArcGIS Server.  It may be the same code works, but it very well may not.

0 Kudos