Select to view content in your preferred language

Creating a selection, storying in memory and counting the number of selection does not work in python

836
4
Jump to solution
11-02-2023 08:55 AM
Labels (2)
NadjaHertel
Occasional Contributor

Hi all,

I am working with a Python Notebook in ArcGis Pro 3.0.5. I would like to create a map series with tables. Every step should be done in Python. 

Here is my code so far:

# Laoding the libraries
import arcpy
import os
import sys

# Reference the current project, MUST be run in the ArcGIS Pro application
aprx = arcpy.mp.ArcGISProject('current')

# Reference the appropriate map and layout
m = aprx.listMaps("Map")[0]

# Reference the appropriate layout
lyt = aprx.listLayouts("Gemeinden_2023")[0]


# Storing map layers on different variables
countyLayer = aprx.listMaps()[0].listLayers("Projekte")[0]
queryLayer = aprx.listMaps()[0].listLayers("Projekte")[0]
geplanteKUBA = aprx.listMaps()[0].listLayers("KUBA_SV")[0]
geplanteKS = aprx.listMaps()[0].listLayers("Projektideen")[0] 
gemeindeLayer = aprx.listMaps()[0].listLayers("Gemeinden")[0]

state = arcpy.GetParameterAsText(0)

# Reference page layout elements
for elm in lyt.listElements():
    if elm.name == "NoGrowth": noGrowth = elm
    if elm.name == "NoMassnahmen": noMassnahmen = elm
    #if elm.name == "horzLine": horzLine = elm
    #if elm.name == "vertLine": vertLine = elm
    if elm.name == "cellTxt":  cellTxt = elm
    if elm.name == "cell2Txt": cell2Txt = elm
    if elm.name == "headerTxt": headerTxt = elm
    if elm.name == "header2Txt": header2Txt = elm
    if elm.name == "cellTxtRight": cellTxtRight = elm
    if elm.name == "cell2TxtRight": cell2TxtRight = elm
    if elm.name == "LaufendeProjekte": titelTxt = elm

# Delete elements in the layout that contain 'clone' in their name - clean up before next page
for elm in lyt.listElements("GRAPHIC_ELEMENT", "*clone*"):
    elm.delete()
for elm in lyt.listElements("TEXT_ELEMENT", "*clone*"):
    elm.delete()

# Defining the element positions
noGrowth.elementPositionX = -10
noMassnahmen.elementPositionX = -10
cellTxt.elementPositionX = -3
cell2Txt.elementPositionX = -3
headerTxt.elementPositionX = -3
header2Txt.elementPositionX = -3
#horzLine.elementPositionX = -17
#vertLine.elementPositionX = -3
cellTxtRight.elementPositionX = -2
cell2TxtRight.elementPositionX = -2
titelTxt.elementPositionX = -10

#Reference DDP object
ms = lyt.mapSeries

#Graphic table variable values
tableHeight = 4
table2Height = 3.5
tableWidth = 15
headerHeight = 0.4
titelHeight = 0.5
rowHeight = 0.4
upperX = 16.2
upperY = 9
upper2Y = 4.5

# Get the state name from the dropdown list
stateName = state
arcpy.AddMessage("Processing: "+ stateName)

# Deleting the memory
if arcpy.Exists("in_memory\select1"):
  arcpy.Delete_management("in_memory\select1")
if arcpy.Exists("in_memory\sort1"):
  arcpy.Delete_management("in_memory\sort1")
if arcpy.Exists("in_memory\select2"):
  arcpy.Delete_management("in_memory\select2")
if arcpy.Exists("in_memory\select3"):
  arcpy.Delete_management("in_memory\select3")

# Creating the first selection set
where_clause = "\"ZZGEMEINDE\" LIKE '" + stateName + "%'"
arcpy.Select_analysis(countyLayer, "in_memory\select1", where_clause)
numRecords = int(arcpy.GetCount_management("in_memory\select1").getOutput(0))

 

Well, and that is the error message, which I get:

ExecuteError                              Traceback (most recent call last)
In  [10]:
Line 4:     numRecords = int(arcpy.GetCount_management("in_memory\select1").getOutput(0))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in GetCount:
Line 22757: raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in GetCount:
Line 22754: retval = convertArcObjectToPythonObject(gp.GetCount_management(*gp_fixargs((in_rows,), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 512:   return lambda *args: val(*gp_fixargs(args, True))

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Rows: Dataset in_memory\select1 does not exist or is not supported
Failed to execute (GetCount).

I don't get the error because everything seems correct for me (well it isn't, but yeah)....

Please help me to find a solution, I would be very thankful.

Thanks in advance,

Nadja

0 Kudos
2 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

In lines 77-78 you check to see if select1 exists, and if it does, then you delete it

In lines 87- you try to do a count of select1

But... if it existed, then it was deleted, so trying to do a Select_analysis and a count on it doesn't make sense to me


... sort of retired...

View solution in original post

0 Kudos
NadjaHertel
Occasional Contributor

Thanks for that! Yep, this was the problem - I never defined the variable select1, and additionally, I deleted it. With defining a new select1 variable after deleting, the script works know. Many thanks :))

View solution in original post

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

will help by properly formatting and providing line numbers for reference


... sort of retired...
0 Kudos
NadjaHertel
Occasional Contributor

Thanks for your hint! I updated my post and now the code should be properly formated.

0 Kudos
DanPatterson
MVP Esteemed Contributor

In lines 77-78 you check to see if select1 exists, and if it does, then you delete it

In lines 87- you try to do a count of select1

But... if it existed, then it was deleted, so trying to do a Select_analysis and a count on it doesn't make sense to me


... sort of retired...
0 Kudos
NadjaHertel
Occasional Contributor

Thanks for that! Yep, this was the problem - I never defined the variable select1, and additionally, I deleted it. With defining a new select1 variable after deleting, the script works know. Many thanks :))

0 Kudos