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":
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.