Mosaic to new raster

2020
5
10-17-2014 02:23 AM
Leo_KrisPalao
New Contributor II

Hi ALL,

I want to mosaic a set of rasters in separate folders but I am stuck with my code. Basically, I have two sets of modis tiles located in two separate folders, and I want to create a mosaic dataset.

case:

Folder 1 = mod_h26v06_2012001.tif, mod_h26v06_2012009.tif, mod_h26v06_2012017.tif, mod_h26v06_2012025.tif,......., mod_h26v06_2012361.tif

Folder 2 = mod_h27v06_2012001.tif, mod_h27v06_2012009.tif, mod_h27v06_2012017.tif, mod_h27v06_2012025.tif,......., mod_h27v06_2012361.tif

What I want to do:

mod_h26v06_2012001.tif merge with mod_h27v06_2012001.tif, ........, mod_h26v06_2012361.tif merge with mod_h27v06_2012361.tif

This is my initial code:

# import necessary modules of ArcGIS

import arcpy, os, sys

from arcpy import env

from arcpy.sa import *

# this will overwrite output. Important in testing codes

arcpy.env.overwriteOutput = True

# activate the spatial analyst extention of ArcGIS

arcpy.CheckOutExtension("Spatial")

# specify your workspace. This is where raster is stored

raster1 = 'X:/raster_ws'

arcpy.env.workspace = raster1

h26v06 = [os.path.join(raster1, l) for l in arcpy.ListRasters("*2012*", "TIF")]

raster2 = 'X:/raster_ws'

arcpy.env.workspace = raster2

h27v06 = [os.path.join(raster2, r) for r in arcpy.ListRasters("*2012*", "TIF")]

out_ws = 'X:/output_ws/'

# (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" #'modis_8day_2012_{:03d}.tif'.format(h+1)

    mosaic = arcpy.Mosaic_management(Raster(h26v06);Raster(h27v06), out_ws, filename, "", "16_BIT_SIGNED", "", "1", "LAST","FIRST")

    print mosaic

Any help is much appreciated.

Thanks,

-Leo

0 Kudos
5 Replies
JeffreySwain
Esri Regular Contributor

So are you encountering an error? Or what his happening.  On first look, all I see is that you are using the 'Mosaic' tool rather than the 'Mosaic to New Raster' tool?  If as your title says you are looking for a new raster, then use the second tool.  Unless you are going to create a new raster prior to using the mosaic tool.

CodyBenkelman
Esri Regular Contributor

Leo

To add to what Jeff said, you mention three different terms - "mosaic" tool, " 'Mosaic to New Rastertool" and "mosaic dataset".  I am guessing the last one you simply meant "a dataset on disk that is a mosaic" but I want to be sure you are aware of the Mosaic Dataset which is a specific data structure in ArcGIS. 

If you're not familiar with it, I'd encourage you to look at its capabilities - if you just use one of the above tools to create a permanent mosaic on disk, you may find you want more control over the seamline placement, color correction, etc. whereas the Mosaic Dataset is a virtual mosaic that gives you control over all of that, plus the ability to work with pixels from any scene in the overlap regions, and the source data can be in different projections.  (Just a few of MANY other features)

Building a Mosaic Dataset is extremely fast - just a data structure with pointers to the data - no data is copied, reformatted, etc. Once you build a Mosaic Dataset, if you want to Export a fixed raster to disk, that's an option if desired.

I hope that is helpful, although I am suggesting you change your approach - If you need to proceed as planned please clarify what is not working, and we can have a developer look at your code (I'm not any help with that)

Cody B.

Leo_KrisPalao
New Contributor II

Hi Jeffrey and Codey, thanks for your reply and sorry for the delayed response and confusion on my inquiry. What I really want to use is the mosaic to new raster in arcpy -- Mosaics multiple raster datasets into a new raster dataset. I tried several codes and must have mistakenly copy and paste the latest code that I am using. Orignally this is the tool that I am trying to run:

arcpy.MosaicToNewRaster_management(Raster(h26v06);Raster(h27v06), out_ws, filename, "", "16_BIT_SIGNED", "", "1", "LAST","FIRST")

I get an error while running this code. I cannot determine right now the exact error (error code) that I am getting (since I am running it in my office laptop), but if I can remember correctly, the error message is pointing me on the arguments that I put in the MosaicToNewRaster_management. I am suspecting that it has something to do with my first argument Raster(h26v06);Raster(h27v06). In this loop I am using the range function, that is why I am casting out the raster (Raster()) for each step, and referring it to the rasterlist that I created.

Any help and suggestion is very much appreciated,

-Leo

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Leo,

The argument (list of rasters) is indeed incorrect. If you look at the help page, at the bottom there is a python sample. The list of raster is actually a string and each raster is separated by a semicolon. Be aware that the name of each raster should include the workspace in case the rasters do no reside in the current workspace.

There is actually more. I noticed that first and second list will actually contain the same information, since they point to the same folder. So you have to check that too.

If you are sure that there are as many h26 as h27 tiffs and the structure of the name is the same. You can create a list for the h26 rasters and derive the h27 name from it.

Kind regards,

Xander

0 Kudos
JeffreySwain
Esri Regular Contributor

When dealing with lists like this, I usually try printing out the list to see if it is what you think it is.  To piggy back to what Xander said, consider those help docs, but also I have found printing them out, copying them and they trying to run them in the Py Window seems to help me determine the proper syntax. 

0 Kudos