Select to view content in your preferred language

How to stop raster loading from stopping arcpy script

1093
3
Jump to solution
05-18-2020 10:33 AM
by Anonymous User
Not applicable

Hey all.

First question here and relatively new to arcpy, so please bear with me.

I'm automating some mapping processes for my team (in this case kernel density generation, symbology changes, and layout exporting) and the last issue that has me confused is when running kernel density maps, the script stops running a little after finishing the raster. This means it can run some more simple code (like print statements) before the script stops. There's no error message and pasting in the remaining code into the python window completes the process as it should have.I think that the script stops when the raster is actually loaded onto the map, which would explain the slight delay before stopping the script.

import arcpy
import datetime
arcpy.env.workspace = r"W:\Analytical Products\Austin Glass\Reports\2D\MVT TFA\MVT TFA Report.gdb"
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps("Map")[0]
lyt = aprx.listLayouts("Layout")[0]

today = str(datetime.date.today())
today = today.replace("-", "_")

in_table = r"W:/Analytical Products/Austin Glass/Reports/2D/MVT TFA/" + today + r".csv"
out_feature_class = 'MVTTFA_' + today
x_coords = "Offense Location GEOX"
y_coords = "Offense Location GEOY"

arcpy.management.XYTableToPoint(in_table, out_feature_class,
                               x_coords, y_coords, '', arcpy.SpatialReference(26985, 115700))

arcpy.env.extent = arcpy.Extent(388004, 130267, 400000, 149167)
out_raster = arcpy.sa.KernelDensity("MVTTFA_" + today, "NONE", 5, 500, "SQUARE_METERS", "DENSITIES", "PLANAR"); out_raster.save(r"W:\Analytical Products\Austin Glass\Reports\2D\MVT TFA\MVT TFA Report.gdb\HM_" + today)
kernel = map.listLayers("out_raster")[0]
Density = map.listLayers("Density")[0]
arcpy.management.ApplySymbologyFromLayer(kernel, Density, None, "UPDATE")

lyt.exportToJPEG(r"W:/Analytical Products/Austin Glass/Reports/2D/MVT TFA/AutoMaps/" + today + "_Density", 300)

Let me know if I need to provide additional info. I appreciate the help.

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I found a workaround for this issue. By disabling arcpy.env.addOutputsToMap and manually saving+adding the raster to the map, the script stopping issue is avoided. At the end of the script, the original out_raster is still added to the map in addition to the one added via the addDataFromPath method, but this is not a concern for my current use case and so I'm not terribly concerned with it.

I've bolded the changes to the original code below:

arcpy.env.extent = arcpy.Extent(388004, 130267, 400000, 149167)
arcpy.env.addOutputsToMap = 0
out_raster = arcpy.sa.KernelDensity("MVTTFA_" + today, "NONE", 5, 500, "SQUARE_METERS", "DENSITIES", "PLANAR"); out_raster.save(r"W:\Analytical Products\Austin Glass\Reports\2D\MVT TFA\MVT TFA Report.gdb\HM_" + today)

map.addDataFromPath(r"W:\Analytical Products\Austin Glass\Reports\2D\MVT TFA\MVT TFA Report.gdb\HM_" + today)
arcpy.env.addOutputsToMap = 1

kernel = map.listLayers("Kernel*")[0]

Density = map.listLayers("Density")[0]
arcpy.management.ApplySymbologyFromLayer(kernel, Density, None, "UPDATE")

View solution in original post

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

You should really show the whole script

out_raster   is a variable

today ... is it a string or date?

root .... what is root? a path to a folder?

"out_raster" .... where did you make a layer called out_raster? or are you referring to the previous variable?

"Density"  .... ditto, where do you make layers called "Density"

Getting hard to follow now.


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

Thanks for the feedback, Dan. The whole script is 150 lines long and only about 10% of it has any relevance to the issue, so I was reluctant to just drop the whole thing. I've recreated the error with a smaller script and edited the original post to hopefully make it more legible.

out_raster is created by the kernel density tool in the line that it is first mentioned.

Density is the layer that I am importing the symbology from (color scheme and class count with class breaks being updated), and it is already present in the project.

0 Kudos
by Anonymous User
Not applicable

I found a workaround for this issue. By disabling arcpy.env.addOutputsToMap and manually saving+adding the raster to the map, the script stopping issue is avoided. At the end of the script, the original out_raster is still added to the map in addition to the one added via the addDataFromPath method, but this is not a concern for my current use case and so I'm not terribly concerned with it.

I've bolded the changes to the original code below:

arcpy.env.extent = arcpy.Extent(388004, 130267, 400000, 149167)
arcpy.env.addOutputsToMap = 0
out_raster = arcpy.sa.KernelDensity("MVTTFA_" + today, "NONE", 5, 500, "SQUARE_METERS", "DENSITIES", "PLANAR"); out_raster.save(r"W:\Analytical Products\Austin Glass\Reports\2D\MVT TFA\MVT TFA Report.gdb\HM_" + today)

map.addDataFromPath(r"W:\Analytical Products\Austin Glass\Reports\2D\MVT TFA\MVT TFA Report.gdb\HM_" + today)
arcpy.env.addOutputsToMap = 1

kernel = map.listLayers("Kernel*")[0]

Density = map.listLayers("Density")[0]
arcpy.management.ApplySymbologyFromLayer(kernel, Density, None, "UPDATE")

0 Kudos