Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Save fails on raster object created from NumpyArrayToRaster

$
0
0

I am trying to save an ESRI grid built from a numpy array and am getting an error I’ve never seen before:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly
‘GpMetadataFunctions, Version=10.2.0.0, Culture=neutral,
PublicKeyToken=8fc3cc631e44ad86′ or one of its dependencies. The
system cannot find the file specified. File name:
‘GpMetadataFunctions, Version=10.2.0.0, Culture=neutral,
PublicKeyToken=8fc3cc631e44ad86′ at
System.Reflection.Assembly._nLoad(AssemblyName fileName, String
codeBase, Evidence assemblySecurity, Assembly locationHint,
StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean
forIntrospection) at
System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef,
Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean
forIntrospection) at System.Reflection.Assembly.InternalLoad(String
assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark,
Boolean forIntrospection) at System.Reflection.Assembly.Load(String
assemblyString) at GetManagedType(Char* _assembly, Char* _path,
Char* _className)

WRN: Assembly binding logging is turned OFF. To enable assembly bind
failure logging, set the registry value
[HKLMSoftwareMicrosoftFusion!EnableLog] (DWORD) to 1. Note: There
is some performance penalty associated with assembly bind failure
logging. To turn this feature off, remove the registry value
[HKLMSoftwareMicrosoftFusion!EnableLog].

I’m using an alternative workflow for now, but I’d still like to try to solve the problem with saving a raster from a numpy array.

Here is what I was trying to do:

  1. read in an ESRI grid,
  2. create an numpy array,
  3. do some manual reclassification, and
  4. save the processed array to a new ESRI grid

The process proceeds as expected through step 3 and fails upon step 4 AFTER the array has been converted to a raster object (see code below). It is when the save method of the raster is called that the error is encountered. Any help in tracking this down would be greatly appreciated.

PS – I got the same error when trying to create a numpy array from geo-tiff rasters (failed on calling arcpy.RasterToNumPyArray); I was able to get around this by copying the original .tifs to ESRI grid format first.

Here is my code:

import arcpy
import numpy as np

arcpy.env.overwriteOutput = True
arcpy.env.workspace = 'SwbLandUseParameters.gdb'

# List tables to use in the reclassification
tables = arcpy.ListTables('SwbLandUseParameters*')

# List land use rasters
lu_rasters = arcpy.ListRasters('nlcd*', 'GRID')

for lu_raster in lu_rasters:

    print 'Processing raster', lu_raster

    # Get raster properties
    r = arcpy.Raster(lu_raster)
    ll = r.extent.lowerLeft
    x_cell_size = r.meanCellWidth
    y_cell_size = r.meanCellHeight
    ncol = r.width
    nrow = r.height
    year = lu_raster[-4:]

    # Convert the raster to a numpy array
    lu = arcpy.RasterToNumPyArray(r, ll, ncol, nrow)

    # Create a suite of reclassified rasters using parameter tables
    for tbl in tables:
        temp = np.copy(lu)
        with arcpy.da.SearchCursor(tbl, '*') as cur:
            for row in cur:
                print row
                old_val = row[1]
                new_val = row[2]
                temp[temp==old_val] = new_val
        new_raster = arcpy.NumPyArrayToRaster(temp, ll, x_cell_size, y_cell_size, 0)
        new_raster_name = tbl + '_' + year
        print '  ', new_raster_name
        new_raster.save(new_raster_name)

Viewing all articles
Browse latest Browse all 767

Trending Articles