ArcGIS Pro Model Builder Help

1256
9
Jump to solution
10-08-2021 07:55 AM
ABishop
MVP Regular Contributor

I am hoping somebody can help me with this...

I am working in ArcGIS Pro 2.8.2.  I want to build a model to get the counts on two different sets of data (one being a feature class in SDE and the other being a shapefile), then compare those two counts and if they match, do nothing, if they don't match send me an email so I can check out the issue.

So far I have the following model started.  Any help or direction on this would be greatly appreciated!

model.PNG

Amanda Bishop, GISP
0 Kudos
2 Solutions

Accepted Solutions
MichaelVolz
Esteemed Contributor

I think <> might be deprecated at the Pro version of python as I am much more familiar with the older ArcMap version of python.

Try != instead and see if that corrects the error.

I would also print the actual Row_Count values to make sure the value is what you expect.  Remember to convert numeric value to a string.

View solution in original post

ABishop
MVP Regular Contributor

Thank you to @MichaelVolz for assisting me with this!  

In case anyone reads this thread later and wants to know the final code that worked:

 

import arcpy,ssl,smtplib

def send_mail(message=None):
    port = ENTER YOUR PORT HERE
    smtp_server = "ENTER YOUR SERVER URL HERE"
    sender_email ="ENTER SENDER EMAIL HERE"
    receiver_email = "ENTER RECEIVER EMAIL HERE"
    password = "ENTER PASSWORD FOR EMAIL HERE"

    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
        server.starttls(context=context)
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message)

#Set geoprocessing environments
arcpy.env.workspace = r"ENTER YOUR WORKSPACE PATH HERE"

# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = True

DATA_VARIABLE_1 = "ENTER YOUR DATA PATH HERE"
DATA_VARIONBLE_2 = "ENTER YOUR DATA PATH HERE"

# Process: Get Count (Get Count) (management)
Row_Count = arcpy.management.GetCount(in_rows=DATA_VARIABLE_1)[0]
print(str(Row_Count))

# Process: Get Count (2) (Get Count) (management)
Row_Count_2_ = arcpy.management.GetCount(in_rows=DATA_VARIABLE_2)[0]
print(str(Row_Count_2_))

# Process: If Row Count Is (If Row Count Is) ()
if (Row_Count) != (Row_Count_2_):
    msg = "unmatched"
    print(msg)
   
else:
    msg = "matched"
    print(msg)
    send_mail("Data Error")

 

Amanda Bishop, GISP

View solution in original post

0 Kudos
9 Replies
MichaelVolz
Esteemed Contributor

I have never E-mailed using a model, but if you export it out to a python script I might be able to assist you.

ABishop
MVP Regular Contributor

@MichaelVolz 

See python code I exported from the model below:

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2021-10-08 13:32:05
"""
import arcpy
def #  NOT  IMPLEMENTED# Function Body not implemented

def Model():  # Model

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = False

    COMM_SALES_shp = "K:\\GIS_UPDATE\\DATA\\SHP\\COMM_SALES.shp"
    mcpagis_DBO_Commercial_Sales = "C:\\Users\\abishop\\AppData\\Roaming\\Esri\\ArcGISPro\\Favorites\\arcgis2.sde\\mcpagis.DBO.Commercial_Sales"

    # Process: Get Count (Get Count) (management)
    Row_Count = arcpy.management.GetCount(in_rows=COMM_SALES_shp)[0]

    # Process: Get Count (2) (Get Count) (management)
    Row_Count_2_ = arcpy.management.GetCount(in_rows=mcpagis_DBO_Commercial_Sales)[0]

    # Process: If Row Count Is (If Row Count Is) ()
    True_8, False_9 = #  NOT  IMPLEMENTED(in_layer_or_view=COMM_SALES_shp, count_condition="IS_EQUAL_TO", count=Row_Count_2_, count_min=0, count_max=0)

if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Users\abishop\AppData\Local\Temp\ArcGISProTemp11548\80112cea-b9f4-4735-a8b9-66ed0fbbbacf\Default.gdb", workspace=r"C:\Users\abishop\AppData\Local\Temp\ArcGISProTemp11548\80112cea-b9f4-4735-a8b9-66ed0fbbbacf\Default.gdb"):
        Model()
Amanda Bishop, GISP
0 Kudos
MichaelVolz
Esteemed Contributor

Thanks for the code.  Does the model currently provide you with the expected results and now you just want to add the ability to have it generate an E-mail to you if the counts are different?

ABishop
MVP Regular Contributor

@MichaelVolz 

I wasn't sure how to produce the results of a true or false on the count.

Amanda Bishop, GISP
0 Kudos
MichaelVolz
Esteemed Contributor

I would try the following code at line 23:

if Row_Count<> Row_Count_2_:

for now just print out a statement that the values are not equal

Also lets check if Row_Count  and Row_Count_2_ are providing legit results with some print statements.

After this is done, we can move onto the E-mail code.

ABishop
MVP Regular Contributor

I did what you said and also added print statements.  I get a syntax error for the Row_Count <> Row_Count_2.  See python code below:

 

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2021-10-08 14:05:30
"""
import arcpy

def Model():  # Model

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = False

    COMM_SALES_shp = "K:\\GIS_UPDATE\\DATA\\SHP\\COMM_SALES.shp"
    mcpagis_DBO_Commercial_Sales = "C:\\Users\\abishop\\AppData\\Roaming\\Esri\\ArcGISPro\\Favorites\\arcgis2.sde\\mcpagis.DBO.Commercial_Sales"

    # Process: Get Count (Get Count) (management)
    Row_Count = arcpy.management.GetCount(in_rows=COMM_SALES_shp)[0]
    print("in_rows")

    # Process: Get Count (2) (Get Count) (management)
    Row_Count_2_ = arcpy.management.GetCount(in_rows=mcpagis_DBO_Commercial_Sales)[0]
    print("in_rows")

    # Process: If Row Count Is (If Row Count Is) ()
    If Row_Count <> Row_Count_2_:
        print("row counts are not equal")

if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Users\abishop\AppData\Local\Temp\ArcGISProTemp11548\80112cea-b9f4-4735-a8b9-66ed0fbbbacf\Default.gdb", workspace=r"C:\Users\abishop\AppData\Local\Temp\ArcGISProTemp11548\80112cea-b9f4-4735-a8b9-66ed0fbbbacf\Default.gdb"):
        Model()
Amanda Bishop, GISP
0 Kudos
MichaelVolz
Esteemed Contributor

I think <> might be deprecated at the Pro version of python as I am much more familiar with the older ArcMap version of python.

Try != instead and see if that corrects the error.

I would also print the actual Row_Count values to make sure the value is what you expect.  Remember to convert numeric value to a string.

ABishop
MVP Regular Contributor

I'm not that good at python programming.  I understand some of it and I am also able automate workflow processes using geoprocessing tools and models.  Would you mind helping me by editing the code I provided you with your suggested edits?  

Amanda Bishop, GISP
0 Kudos
ABishop
MVP Regular Contributor

Thank you to @MichaelVolz for assisting me with this!  

In case anyone reads this thread later and wants to know the final code that worked:

 

import arcpy,ssl,smtplib

def send_mail(message=None):
    port = ENTER YOUR PORT HERE
    smtp_server = "ENTER YOUR SERVER URL HERE"
    sender_email ="ENTER SENDER EMAIL HERE"
    receiver_email = "ENTER RECEIVER EMAIL HERE"
    password = "ENTER PASSWORD FOR EMAIL HERE"

    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
        server.starttls(context=context)
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message)

#Set geoprocessing environments
arcpy.env.workspace = r"ENTER YOUR WORKSPACE PATH HERE"

# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = True

DATA_VARIABLE_1 = "ENTER YOUR DATA PATH HERE"
DATA_VARIONBLE_2 = "ENTER YOUR DATA PATH HERE"

# Process: Get Count (Get Count) (management)
Row_Count = arcpy.management.GetCount(in_rows=DATA_VARIABLE_1)[0]
print(str(Row_Count))

# Process: Get Count (2) (Get Count) (management)
Row_Count_2_ = arcpy.management.GetCount(in_rows=DATA_VARIABLE_2)[0]
print(str(Row_Count_2_))

# Process: If Row Count Is (If Row Count Is) ()
if (Row_Count) != (Row_Count_2_):
    msg = "unmatched"
    print(msg)
   
else:
    msg = "matched"
    print(msg)
    send_mail("Data Error")

 

Amanda Bishop, GISP
0 Kudos