1

I am converting a couple rasters to arrays, then performing calculations on them. First step- polygon to raster. This creates a 1/0 mask that I can multiply the larger raster by. After noticing my final outputs were a little off, I output the intermediate steps to see what was going on.

# Set environmental variables for output
lossm = r'C:\data'
arcpy.env.overwriteOutput = True
arcpy.env.outputCoordinateSystem = lossm
arcpy.env.cellSize = lossm
arcpy.env.snapRaster = lossm
cell_size = 0.00025

# convert polygon to raster- the mask
arcpy.PolygonToRaster_conversion(polygon,"ISO",country_boundary_tiff)
#get number of rows and columns of aoi
arcpy.GetRasterProperties_management(country_boundary_tiff,"COLUMNCOUNT")

numRowResult = arcpy.GetRasterProperties_management(country_boundary_tiff,"ROWCOUNT")
numCol = int(numColResult.getOutput(0))
numRow = int(numRowResult.getOutput(0))

# Create raster object from file
boundary_obj = arcpy.Raster(country_boundary_tiff)

# get the xy coords of lowerleft corner of boundary object
desc = arcpy.Describe(boundary_obj)
gmb_mx = desc.extent.XMin
gmb_my = desc.extent.YMin

# convert boundary object to array
boundary_array = arcpy.RasterToNumPyArray(boundary_obj,arcpy.Point(gmb_mx,gmb_my),numCol,numRow,cell_size)

# conver array back to tif
outputraster = arcpy.NumPyArrayToRaster (boundary_array ,arcpy.Point(gmb_mx,gmb_my),0.00025)

outputraster.save(r'C:\Users\' + '\\' + country+ '_boundary2.tif')

At this point the file has gone from polygon -> raster -> numpyarray -> raster, and in the process has moved up 1 pixel. I did another test where I added another iteration of raster -> array, and it moved the output raster even further up. enter image description here

The cell size, numrows, numcol, and extent are identical for these 2 files pictured (green= raster in the right place, orange= raster shifted). So fixing the x/y parameters when running rastertoarray won't change anything. It is the location of the cells being coded to 1 that is changing. The mask (value 1) is shifting around within the extent of the entire raster.

Can someone help by telling me how to keep the raster from shifting?

  • Looking at your code you set snap raster to lossm but lossm is just a folder, not a dataset? – Hornbydd Aug 12 '15 at 13:39
  • Polygon to raster can cause it. Have a good look at options of this tool. I always use more basic feature to raster – FelixIP Aug 12 '15 at 20:39
  • The lossm is a mosaic dataset. – Sam Aug 13 '15 at 14:04

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.