1

I am working within a Jupyter notebook, trying to display a query layer (querying a SQL database) into a map view within the notebook. I receive a Runtime Error (described below) when I try to add this layer to the map view.

Using the following code (with specific names changed for privacy reasons) I can successfully create a map and add a layer from my organization's enterprise portal.

# Import Statements
from arcgis.gis import GIS
import arcpy

# Instantiate Connection to Portal
gis = GIS("portal_url", username="my_username")

# Get item from portal
portalItem = gis.content.get('itemID')

# Create map and add portal item to it
m = gis.map()
m.add_layer(portalItem.layers[0])

I can also successfully create my query layer using the following code, including testing that makeQueryLayer returned a feature class with geometry as expected. This same query was copied directly from a successful query layer being created in ArcGIS Desktop 10.7.1.

# Create query layer input variables
out_layer_name = "queryLayerOutput"
sql_Query="""SELECT OBJECTID
                    ,Field1
                    ,Field2
                    ,Shape.STEnvelope() as MBR
                FROM SQLDatabase.Table 
                WHERE Shape.STIntersects(geometry::STGeomFromText('LINESTRING(""" + lat_long_string + """)', 4326).STEnvelope()) = 1""" # lat_long_string being the coordinates of my area of interest in proper 'E S, W N' format
oid_fields="OBJECTID"
shape_type="POLYGON"

# Creating the query layer
MBRQueryLayer = arcpy.MakeQueryLayer_management(geodatabase, 
                                                out_Layername, 
                                                sql_Query, 
                                                "OBJECTID", 
                                                "POLYGON", 
                                                "4326", 
                                                spatial_reference=spatialReference)

desc = arcpy.Describe("queryLayerOutput")
print("Feature class type: " + desc.featureClass.featureType) #returns 'Simple'
print("Shape field name: " + desc.featureClass.shapeFieldName) #returns 'MBR'
print("Shape type: " + desc.featureClass.shapeType) #returns 'Polygon'

Where I encounter problems is when I go to add the query layer to the map.

m.add_layer("queryLayerOutput")

Which returns the following error, the key line of which is "RuntimeError: Cannot infer layer: will not be added to map": Python output error from m.add_layer("queryLayerOutput"): RuntimeError: Cannot infer layer: will not be added to map

The documentation for MapView.add_layer() implies that I should be able to add a FeatureLayer, which to my understanding is what makeQueryLayer() produced. Does anyone understand why my layer is unable to be added? My best guess would be that somehow my data is not in an acceptable format for add_layer() to use. Somehow not the "right kind" of FeatureLayer???

I am working in the ArcGIS Jupyter Notebook environment (ArcGIS 10.7.1) on Windows 10.

1 Answer 1

2

You're running into the same problem/answer here: Adding data using Python from ArcGIS Online/ArcGIS REST Services directory to a ArcMap MXD --just reversing the layers and the map technology.

You're trying to use an arcpy thing and put that into a ArcGIS Python API map. Basically, the layer you're trying to add to your map is not something the Python API understands. Yes, they might be both considered a Feature Layer (in broad terms), but they aren't equivalent.

I'm not an expert with the Python API, but my feeling is the map will want a web-based feature layer. The Python API is first and foremost a mechanism to work with services. It does integrate with arcpy and desktop functions; just not in this case you're trying.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.