Why can't I use a FeatureLayer object directly in arcpy?

1701
6
Jump to solution
02-01-2019 11:59 AM
BenjaminSperry1
Occasional Contributor

Hello,

I am trying to use some arcpy tools in Jupyter Notebooks but it doesn't appear as though I can use a FeatureLayer object as an input. Here is an example.

I obtain my feature layer by getting the feature collection using the item id and then calling the first (and only in this case) layer. This returns a FeatureLayer object. I can easily find the fields as follows:

However, when I use the arcpy equivalent function it tells me that

OSError: "<FeatureLayer url:"https://services8.arcgis.com/.../FeatureServer/0">" does not exist"
even though it actually does.

Interestingly when I pass the FeatureLayer object's URL in it works.

Can someone explain why I can't use the FeatureLayer object directly or what the difference is?

Many thanks.

0 Kudos
1 Solution

Accepted Solutions
BenjaminSperry1
Occasional Contributor

Searching though something entirely unrelated I found this:

The .as_arcpy converts the features into features that can be used in arcpy. I haven't seen any official documentation on this anywhere but it seems to work.

Here is a link to the reference visualizing data with the spatially enabled dataframe | ArcGIS for Developers 

View solution in original post

6 Replies
MunachisoOgbuchiekwe
New Contributor III

Hello Benjamin, 

I think it is because arcpy.ListFields is expecting a string type and not an object type for the first parameter according to the documentation. This would make sense why feature_layer.url works because that would be a string.

0 Kudos
BenjaminSperry1
Occasional Contributor

Hello Munachiso, yes that appears to be the case. The integration of arcpy and the python api is proving more difficult that I thought it would be. Is there a place where I can find more information on how the two work together?

thanks

0 Kudos
MunachisoOgbuchiekwe
New Contributor III

Essentially, there isn't concrete information on how the two API's work together. In general, they are two separate products. ArcGIS API for Python, is supposed to be used for automation of your web GIS while ArcPy is used for automation of GIS workflows when using ArcMap or ArcGIS Pro.

You might get instances where you can use the two API's together and it works but you can't always rely on that since an object in ArcGIS API for Python may not be the same in ArcPy and vise versa (as Joshua Bixby pointed out).

Your best bet is to stick with one API. It looks like you are mostly doing Web GIS related workflows. So, Joshua Bixby sample would get you the fields without using ArcPy. 

0 Kudos
BenjaminSperry1
Occasional Contributor

Thanks again Munachiso,

What I am really trying to do is compensate for not having ArcGIS for Server and geoprocessing services. I need to run desktop caliber tools in a distributed web environment. I was hoping that this could be accomplished by using arcpy in conjunction with the python API. I get the sense from your's and Joshua's comments that it is not a natural thing.

It would be handy if there was a reference that compared the two and outlined any possible touch points.

I really appreciate your's and Joshua's insights.

Regards

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Honestly, I am surprised arcpy.ListFields() works at all in this case.  FeatureLayer is an ArcGIS API for Python object, not ArcPy, and the interchange/translation between the two APIs doesn't always work, even when the objects share the same name (see arcgis...FeatureSet != arcpy...FeatureSet ).

The way to print field names using only the ArcGIS API for Python, which I suggest for stability and portability, is:

field_list = feature_layer.properties.fields
for f in field_list:
    print(f.name)
0 Kudos
BenjaminSperry1
Occasional Contributor

Searching though something entirely unrelated I found this:

The .as_arcpy converts the features into features that can be used in arcpy. I haven't seen any official documentation on this anywhere but it seems to work.

Here is a link to the reference visualizing data with the spatially enabled dataframe | ArcGIS for Developers