Select to view content in your preferred language

ArcMap crashing on simple raster calculation in Python window

1435
4
Jump to solution
12-08-2017 08:56 AM
AlexTurner4
Emerging Contributor

I have a Python script that has been crashing ever since I had to update to Windows 10 but always worked prior. Not sure if that is the reason or just a coincidence. I have isolated the problem to the part where I try some basic map algebra on rasters. The code below is what I have been trying in the Python window and ArcMap crashes every time with no error. It seems to be related to accessing the individual band because the code works on the main raster. Also, if I add the Band into ArcMap manually and run the code using the layer it also works. I have tried various ways of representing the path to Band 4 but no luck.

from arcpy.sa import *
Rast = r'C:\Projects\m_3409443_ne_15_1_20150808.tif'
Band4 = Rast + r'\Band_4'
calc = Raster(Band4) < 60
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

I usually convert to numpy.. but give this a go

import arcpy

# ---- band 1 
r = arcpy.MakeRasterLayer_management("c:/temp/untitled.png", "test", "#", "#", "1")

# ---- now see what you have
r
<Result 'test'>

r[0]
<arcpy._mp.Layer at 0x1c1af537588>

ras = arcpy.sa.Raster('test')

# ---- then knock yourself out

dir(ras)
   ['__abs__', '__add__', '__and__', '__bool__', '__class__', '__delattr__', '__dir__',
    '__divmod__', '__doc__', '__eq__', '__floordiv__', '__format__', '__ge__',
    '__getattribute__', '__gt__', '__hash__', '__iadd__', '__iand__', '__ifloordiv__',
    '__ilshift__', '__imod__', '__imul__', '__init__', '__invert__', '__ior__',
    '__ipow__', '__irshift__', '__isub__', '__itruediv__', '__ixor__', '__le__',
    '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__',
    '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__',
    '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__',
    '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__',
    '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__',
    '__sub__', '__subclasshook__', '__truediv__', '__xor__',
    'bandCount',
    'catalogPath',
    'compressionType',
    'extent', 'format',    # ----- formatting liberties taken 
    'hasRAT',
    'height',
    'isInteger',
    'isTemporary',         # you get the idea
    'maximum',
    'mean',
    'meanCellHeight',
    'meanCellWidth',
    'minimum',
    'name', 'noDataValue', 'path', 'pixelType', 'save', 'spatialReference', 'standardDeviation', 'uncompressedSize', 'width']

# ----
ras.pixelType
'U8'

ras.height, ras.width
(190, 250)

ras.bandCount, ras.format
(1, None)

# ---- etcetera

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

what you are trying does not form a correct way to get to a band in a raster.  You are right in that you can access the whole raster (ie the *.tif) and if you add the individual band to arcmap it works.  That is because it is a raster layer and not a raster.

0 Kudos
AlexTurner4
Emerging Contributor

The weird thing is that this exact code worked for me prior to updating to Windows 10. I never had problems accessing the bands this way. What would the correct way to access the band be?

0 Kudos
DanPatterson_Retired
MVP Emeritus

I usually convert to numpy.. but give this a go

import arcpy

# ---- band 1 
r = arcpy.MakeRasterLayer_management("c:/temp/untitled.png", "test", "#", "#", "1")

# ---- now see what you have
r
<Result 'test'>

r[0]
<arcpy._mp.Layer at 0x1c1af537588>

ras = arcpy.sa.Raster('test')

# ---- then knock yourself out

dir(ras)
   ['__abs__', '__add__', '__and__', '__bool__', '__class__', '__delattr__', '__dir__',
    '__divmod__', '__doc__', '__eq__', '__floordiv__', '__format__', '__ge__',
    '__getattribute__', '__gt__', '__hash__', '__iadd__', '__iand__', '__ifloordiv__',
    '__ilshift__', '__imod__', '__imul__', '__init__', '__invert__', '__ior__',
    '__ipow__', '__irshift__', '__isub__', '__itruediv__', '__ixor__', '__le__',
    '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__',
    '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__',
    '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__',
    '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__',
    '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__',
    '__sub__', '__subclasshook__', '__truediv__', '__xor__',
    'bandCount',
    'catalogPath',
    'compressionType',
    'extent', 'format',    # ----- formatting liberties taken 
    'hasRAT',
    'height',
    'isInteger',
    'isTemporary',         # you get the idea
    'maximum',
    'mean',
    'meanCellHeight',
    'meanCellWidth',
    'minimum',
    'name', 'noDataValue', 'path', 'pixelType', 'save', 'spatialReference', 'standardDeviation', 'uncompressedSize', 'width']

# ----
ras.pixelType
'U8'

ras.height, ras.width
(190, 250)

ras.bandCount, ras.format
(1, None)

# ---- etcetera
AlexTurner4
Emerging Contributor

That worked. Thanks! Should have been using this from the start.