Select to view content in your preferred language

Reclassify based on text field

1507
4
Jump to solution
09-14-2017 04:31 AM
JakobSlipersæter
Deactivated User

I'm trying to reclassify a large amount of rasters. They contain a wide set of values that I want to get to numbers. I'm trying to reclassifying by using the RemapValue, but I can't get it to work.

import arcpy
import os
from arcpy.sa import *

arcpy.env.workspace = r'C:\Users\...\Arbeidsdata.gdb'

rasList = arcpy.ListRasters()
for raster in rasList:
    inRaster = raster
    remap = RemapValue([["A1BE", 2],["A1BR", 1],["A1EN", 2],["A1GR", 2],["A1IN", 2],["A1KO", 2],["A1NA", 2],["A1NY", 2],["A1PL", 2],["A1UB", 0],["A2AL", 2],["A2BU", 2],["A2FR", 2],["A2JO", 2],["A2UB", 0],["A2VE", 2],["A3BE", 1],["A3EN", 2],["A3IN", 2],["A3KR", 1],["A3PL", 2],["A3SB", 1],["A4BE", 1],["A4EN", 2],["A4KR", 1],["A4KR", 1],["A4PL", 2],["A4SB", 1],["B1BL", 1],["B1FJ", 1],["B1GR", 1],["B2DY", 1],["B2FI", 1],["B2JO", 1],["B2TI", 1],["B2TO", 1],["F1KR", 1],["F1PL", 2],["F1SB", 1],["F1VI", 1],["F2EI", 1],["F1GR", 1],["F2LA", 1],["F2PL", 2],["F2RA", 1],["F2RI", 1],["F2VI", 1],["F3DR", 1],["F3ST", 1],["F4BR", 1],["F4GA", 1],["F3HO", 1],["F4VI", 1],["M1BU", 1],["M1GR", 1],["M1RI", 1],["M1SU", 1],["M2BU", 1],["M2SU", 1],["S1LA", 1],["S2BL", 1],["S3BA", 1],["U1BI", 1],["U1FL", 2],["U1GR", 1],["U1JE", 2],["U1MO", 2],["U1PA", 1],["U1PA", 1],["U1SY", 1],["U1TR", 1],["U2BG", 3],["U2BO", 3],["U2FG", 1],["U2FO", 1],["U2IG", 3],["U2IN", 3],["U2TG", 3],["U2TU", 3],["U3GJ", 2],["U3LA", 2],["U3SK", 1],["U3TI", 1],["U3TO", 1],["U4CA", 1],["U4GO", 1],["U4HA", 3],["U4ID", 1],["U4KI", 1],["U4TR", 3],["U5BR", 1],["U5BY", 2],["U5DA", 1],["U5GR", 1],["U5HA", 1],["U4KI", 1],["U4TR", 3],["U5BR", 1],["U5BY", 2],["U5DA", 1],["U5GR", 1],["U5HA", 1],["U5KL", 2],["U5LE", 1],["U5NA", 1],["U5RO", 1],["U5SO", 1],["U5TO", 1],["V1EL", 2],["V1KA", 2],["V1TJ", 2],["V1IS", 1],["V2SN", 1],["V3SA", 2],["F2SK", 1]])

outReclassify = Reclassify(inRaster, "q_atype1", remap, "NODATA")


outputReclass = r'C:\Users\...\Output\Reclassified.gdb'
outputFileName = "reclass"

outRasterName = '{}{}'.format(inRaster, outputFileName)

outReclassify.save(os.path.join(outputReclass, outRasterName))

These are the errors I get:

Runtime error  Traceback (most recent call last):   File "<string>", line 6, in <module>   File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Functions.py", line 5753, in Reclassify     missing_values)   File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Utils.py", line 53, in swapper     result = wrapper(*args, **kwargs)   File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\sa\Functions.py", line 5747, in Wrapper     missing_values)   File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing\_base.py", line 510, in <lambda>     return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 000622: Failed to execute (Reclassify). Parameters are not valid.
ERROR 000628: Cannot set input into parameter remap.‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

A quick check using numpy... conclusion, you have duplicates which need to be removed... sorry to abandon you to figure out which ones, but they need to be removed.

uni = np.unique(a[:,0])  # ---- I made an array out of your remap values

uni   # --- uni is the unique names ... ie the first of the pair
array(['A1BE', 'A1BR', 'A1EN', ..., 'V1TJ', 'V2SN', 'V3SA'], 
      dtype='<U4')

len(uni) # there are 106 unique names
106

len(a)   # but there are 115 classes in total, meaning 9 are dups
115

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

Could you reformat your code Code Formatting ++ the basics

A quck observation, you aren't saving the result to begin with

Secondly, you give no indication on how you are importing the spatial analyst  

did you use from sa import * somewhere?

Check the help topic for options

0 Kudos
JakobSlipersæter
Deactivated User

Thanks for the reply. I've updated the code now. The original code is part of a bigger script, but I've added some more context to it now. It seems like it cannot run the remaped values in the reclassify.

0 Kudos
DanPatterson_Retired
MVP Emeritus

A quick check using numpy... conclusion, you have duplicates which need to be removed... sorry to abandon you to figure out which ones, but they need to be removed.

uni = np.unique(a[:,0])  # ---- I made an array out of your remap values

uni   # --- uni is the unique names ... ie the first of the pair
array(['A1BE', 'A1BR', 'A1EN', ..., 'V1TJ', 'V2SN', 'V3SA'], 
      dtype='<U4')

len(uni) # there are 106 unique names
106

len(a)   # but there are 115 classes in total, meaning 9 are dups
115
JakobSlipersæter
Deactivated User

Thanks Dan! That was the problem, now its running fine.

0 Kudos