Skip to content

Instantly share code, notes, and snippets.

@ptompalski
Created March 16, 2017 20:26
Show Gist options
  • Save ptompalski/723ac8ce7e8989b3625166f1873ecca8 to your computer and use it in GitHub Desktop.
Save ptompalski/723ac8ce7e8989b3625166f1873ecca8 to your computer and use it in GitHub Desktop.
raster mosaicing from command line
# command line based raster mosaicing
# example usage:
# mosaic.py c:\rasters\to\mosaic\*.asc c:\output\raster\mosaic.tif
# script requires ArcMap license
import arcpy
import os
import glob
f_in = sys.argv[1]
f_out = sys.argv[2]
list_of_files_to_mosaic = glob.glob(f_in)
l = f_out.split("\\")
outputname = l[len(l)-1]
outputfolder = '\\'.join(l[0:len(l)-1])
#get raster properties from the first raster on the input list
pixel_size = str(arcpy.GetRasterProperties_management(list_of_files_to_mosaic[0], "CELLSIZEX"))
valuetype = str(arcpy.GetRasterProperties_management(list_of_files_to_mosaic[0], "VALUETYPE"))
no_of_bands = str(arcpy.GetRasterProperties_management(list_of_files_to_mosaic[0], "BANDCOUNT"))
# determine pixel value type
def f(x):
return {
'0' : '1_BIT',
'1' : '2_BIT',
'2' : '4_BIT',
'3' : '8_BIT_UNSIGNED',
'4' : '8_BIT_SIGNED',
'5' : '16_BIT_UNSIGNED',
'6' : '16_BIT_SIGNED',
'7' : '32_BIT_UNSIGNED',
'8' : '32_BIT_SIGNED',
'9' : '32_BIT_FLOAT',
'10' : '64_BIT ',
}[x]
print("mosaicing " + str(len(list_of_files_to_mosaic)) + " rasters")
print("pixel size: " + pixel_size)
print("pixel type: " + f(valuetype))
print("number of bands: " + str(no_of_bands))
# run mosaic function
arcpy.MosaicToNewRaster_management(list_of_files_to_mosaic, outputfolder, outputname, "", str(f(valuetype)), pixel_size, no_of_bands, "BLEND","FIRST")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment