0

I'm running a for loop to rescale a bunch of rasters and then save them for use in another function. However, I'm having trouble saving the rasters properly. My current solution is to copy the rasters so that they have the correct names (they're initially called "numpyarraytoraster_[long unique string of characters]") and then delete the old temporary layer. Using arcpy.Delete_management deletes the extraneous layers but also crashes the program (ArcGIS Pro 2.0.0) on the final iteration. I'd love to know why.

Relevant code, all within a for loop:

            name = inRaster.name.replace('Int_Raster','Rescl')
            outName = os.path.join(arcpy.env.scratchWorkspace, name)

            # Convert back to raster
            rescaledRaster = arcpy.NumPyArrayToRaster(rescArray, lowerLeft, cellSize, value_to_nodata=NULL)
            arcpy.DefineProjection_management(rescaledRaster, 4326)

            # Save the rescaled raster
            arcpy.env.overwriteOutput = True
            if arcpy.Exists(outName + ".lyrx"):
                os.remove(outName + ".lyrx")
            copy = arcpy.CopyRaster_management(rescaledRaster, outName)
            rasLayer = arcpy.MakeRasterLayer_management(copy, name)
            lyrx = arcpy.SaveToLayerFile_management(rasLayer, outName)
            #arcpy.Delete_management(rescaledRaster)    #causes ArcGIS to crash when not commented out
            members.append(name)

I'll note that this solution still doesn't allow me to access the rescaled rasters without saving, quitting, and reopening the program, but if I can get it to at least output all the correct rasters with the correct names then I'll be happy.

  • 1
    Can you run this outside of ArcGIS and see if you can an exception/error logged? If not, you can wrap it in a try: with an except: and in the except part, print (or arcpy.AddMessage()) to output the exception/error. It may still crash ArcMap though. So running it outside of ArcMap may be the best option. – Son of a Beach Aug 16 '18 at 4:50
  • rescaledRaster is a Raster object, to release the reference use del rescaledRaster.. Delete_management is for objects that are persisted (real file objects). I notice that you are overwriting your raster, once in CopyRaster then again in SaveToLayerFile.. perhaps you don't mean to overwrite the raster by a layer; it's a bit confusing what you're trying to achieve as all the saving is done in your scratchworkspace which should disappear when the script is finished, unless you've set your scratch workspace to a folder. Shouldn't DefineProjection 2nd parameter be arcpy.SpatialReference(4326)? – Michael Stimson Aug 16 '18 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.