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.
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?