Select to view content in your preferred language

merge rasters Mosaic to New Raster

3923
1
Jump to solution
12-03-2014 01:59 AM
Leo_KrisPalao
Emerging Contributor

Hi Python Users,

I got stuck with my code trying to merge modis rasters (in tiff format) using Mosaic to New Raster. Any help will be much appreciated.

I have  time series NDVI rasters from MODIS data for 2014 for two tiles located in separate directory. What I would like to achieve is to automate the merging of the times series NDVI rasters using the Mosaic to New Raster tool in arcpy.

MOD_h26v06_2014001_ndvi.tif merged with MOD_h27v06_2014001.tif

.

.

.

MOD_h26v06_2014297_ndvi.tif merged with MOD_h27v06_2014297.tif

Any help/suggestion is very much appreciated.

Below is the code that I am working on.

Thanks,

-Leo

import arcpy, os, sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")

raster1 = 'path/toRaster1'
arcpy.env.workspace = raster1
tile1 = [os.path.join(raster1, l) for l in arcpy.ListRasters("*2014*", "TIF")]

raster2 = 'path/toRaster2'
arcpy.env.workspace = raster2
tile2 = [os.path.join(raster2, r) for r in arcpy.ListRasters("*2014*", "TIF")]

out_ws = 'path/Out_rast'

# (input rasters,outputloc,name w/ ext, coor, pixeltype, cellsize, #of bands
# mosaic method, mosaic color map)
for h in range(0,46):
    parse = l[18:25]
    filename = "modis_evi_" + parse + ".tif"
    mosaic = arcpy.MosaicToNewRaster_management(Raster(tile1);Raster(tile2), out_ws, filename, "", "16_BIT_SIGNED", "", "1", "LAST","FIRST")
    print mosaic
0 Kudos
1 Solution

Accepted Solutions
DallasShearer
Deactivated User

It would be helpful if you could pinpoint the problem or supply the error message...

Although line 20 looks suspect. especially the "l", it only appears in line 09 that i can see, and would use the last found raster name in that set to parse for all 46 names in your array.. The way it is written it would seem that it would continually overwrite your output file for each of the 46 iterations

it may also be that you need to change your input rasters from:

Raster(tile1);Raster(tile2)

to

[Raster(tile1), Raster(tile2)]

View solution in original post

0 Kudos
1 Reply
DallasShearer
Deactivated User

It would be helpful if you could pinpoint the problem or supply the error message...

Although line 20 looks suspect. especially the "l", it only appears in line 09 that i can see, and would use the last found raster name in that set to parse for all 46 names in your array.. The way it is written it would seem that it would continually overwrite your output file for each of the 46 iterations

it may also be that you need to change your input rasters from:

Raster(tile1);Raster(tile2)

to

[Raster(tile1), Raster(tile2)]

0 Kudos