Merge multiple RasterDataset

1659
17
Jump to solution
03-28-2013 02:32 AM
oyleiste
Occasional Contributor
Hello

I can get Linear Line of Sight from a single DTED file; which had beed opened by:
IWorkspaceFactory wsf = new RasterWorkspaceFactoryClass(); IRasterWorkspace rasterWS = (IRasterWorkspace)wsf.OpenFromFile(foldername, 0); IRasterDataset rasterDS = rasterWS.OpenRasterDataset("n42.dt0"); //DTED Level 0 file

Single DTED file means that both observer and target are in the DTED file's area.

Well, the problem occurs when I load more than one DTED files and want to use these functions:
IRaster raster = rasterDS.CreateDefaultRaster(); RasterSurfaceClass rsc = new RasterSurfaceClass(); rsc.PutRaster(raster); [INDENT]rsc.GetElevation(point);[/INDENT] [INDENT]rsc.GetLineOfSight(blah, blah, blah);
[/INDENT]

If observer is in a DTED file and target is in another DTED file, GetLineOfSight function fails obviously.
How can I merge two RasterDataset to give rsc.PutRaster() function one single raster?
Or
How can I put more than one raster in RasterSurfaceClass?

Thank you
Sincerely
0 Kudos
1 Solution

Accepted Solutions
oyleiste
Occasional Contributor
Well, after a lot of struggling with this stuff, I solved the problem :cool: I'll share the code and explain it, so people can use it easily.

If you want to merge/mosaic some IRasterDataset, you can use my code:
IRasterCollection rc = (IRasterCollection) new MosaicRasterClass();

#region Add some IRasterDataset to the rc (IRasterCollection)
rc.Insert(0, rasterDataset0);
rc.Insert(1, rasterDataset1);
rc.Insert(2, rasterDataset2);
rc.Insert(3, rasterDataset3);
#endregion

ISaveAs2 save = (ISaveAs2) rc;
IRasterStorageDef st = new RasterStorageDefClass();
IRasterStorageDef3 store = (IRasterStorageDef3)st;
store.CompressionType = esriRasterCompressionType.esriRasterCompressionUncompressed;
store.Tiled = false;
IRasterDataset resultDataset = save.SaveAsRasterDataset("ResultDTED.dt0", _workspace_, "DEM", store);

First, you should create a IRasterCollection and add some IRasterDataset into it. Then you should create ISaveAs2 and cast rc to it. Then create IRasterStorageDef stuff. Finally, use ISaveAs2's SaveAsRasterDataset function to save merged/mosaicked RasterDataset to file and use it's return value as IRasterDataset. resultDataset contains the merged dataset.

Thank everyone for replying, specially Jason.
Sincerely

View solution in original post

0 Kudos
17 Replies
JasonPike
Occasional Contributor
Hello

I can get Linear Line of Sight from a single DTED file; which had beed opened by:
IWorkspaceFactory wsf = new RasterWorkspaceFactoryClass();
IRasterWorkspace rasterWS = (IRasterWorkspace)wsf.OpenFromFile(foldername, 0);
IRasterDataset rasterDS = rasterWS.OpenRasterDataset("n42.dt0"); //DTED Level 0 file

Single DTED file means that both observer and target are in the DTED file's area.

Well, the problem occurs when I load more than one DTED files and want to use these functions:
IRaster raster = rasterDS.CreateDefaultRaster();
RasterSurfaceClass rsc = new RasterSurfaceClass();
rsc.PutRaster(raster);
[INDENT]rsc.GetElevation(point);[/INDENT]
[INDENT]rsc.GetLineOfSight(blah, blah, blah);
[/INDENT]

If observer is in a DTED file and target is in another DTED file, GetLineOfSight function fails obviously.
How can I merge two RasterDataset to give rsc.PutRaster() function one single raster?
Or
How can I put more than one raster in RasterSurfaceClass?

Thank you
Sincerely


I think you are looking for a mosaic: http://resources.arcgis.com/en/help/main/10.1/index.html#//009t000000n6000000

You can find a code example here: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/0001000002z0000000.ht...

Maximum seems like it would be appropriate for what you're doing:
"The Maximum method determines the higher pixel value from the two raster datasets that are overlapping."
http://resources.arcgis.com/en/help/main/10.1/index.html#/Mosaic_operators/009t000000n8000000/
0 Kudos
oyleiste
Occasional Contributor
I think you are looking for a mosaic: http://resources.arcgis.com/en/help/main/10.1/index.html#//009t000000n6000000

Thank you Jason for your guide.
I think I should use mosaic but I tried a lot and couldn't use IRasterDataset which contains DTED Level 0; for mosaic.
The example code is so confusing also.
I think you can't use IRasterDataset for mosaic. Right?
0 Kudos
oyleiste
Occasional Contributor
After a lot of googling, I found out that I should create a IRasterDatasetEdit3, copy first DTED's IRasterDataset to it and then, append other DTEDs using IRasterDatasetEdit3.Mosaic function. Here is my code's screenshot:
[ATTACH=CONFIG]23074[/ATTACH]

Unfortunately Mosaic(IRaster raster, double tolerance) function fails at run-time execution. I have no idea why. Does anyone has idea?
By the way, that's tolerance (second parameter) in Mosaic?!
Please guide me to this 😞

Thank you
Sincerely
0 Kudos
JasonPike
Occasional Contributor
After a lot of googling, I found out that I should create a IRasterDatasetEdit3, copy first DTED's IRasterDataset to it and then, append other DTEDs using IRasterDatasetEdit3.Mosaic function. Here is my code's screenshot:
[ATTACH=CONFIG]23074[/ATTACH]

Unfortunately Mosaic(IRaster raster, double tolerance) function fails at run-time execution. I have no idea why. Does anyone has idea?
By the way, that's tolerance (second parameter) in Mosaic?!
Please guide me to this 😞

Thank you
Sincerely


You did a great job annotating the screen capture! Can you put a try-catch block around the code that fails and dump the contents of the exception to a message box (or console)?

try
{
    // code that is failing
}
catch( Exception e)
{
    MessageBox.Show( e.ToString());
}
0 Kudos
JasonPike
Occasional Contributor
After a lot of googling, I found out that I should create a IRasterDatasetEdit3, copy first DTED's IRasterDataset to it and then, append other DTEDs using IRasterDatasetEdit3.Mosaic function. Here is my code's screenshot:
[ATTACH=CONFIG]23074[/ATTACH]

Unfortunately Mosaic(IRaster raster, double tolerance) function fails at run-time execution. I have no idea why. Does anyone has idea?
By the way, that's tolerance (second parameter) in Mosaic?!
Please guide me to this 😞

Thank you
Sincerely



I don't know if there is any code above what you pasted in the screenshot, but from what I can see it appears that you didn't create the default raster for the first raster in the array. Maybe you can try the following:

IRasterDatasetEdit3 rasterDSE = (IRasterDatasetEdit3)rasterDS[0].CreateDefaultRaster();
for(int i = 1; i < rasterDS.Count; i++ )
{
 rasterDS.CreateDefaultRaster();
 rasterDSE.Mosaic((IRaster)rasterDS.CreateDefaultRaster(), 1000);
}


Also, I noticed that you're creating the default raster twice in the loop. While I doubt this is causing an exception, it is probably expensive and consuming a good bit of memory. Looking at the API, it doesn't seem like you would need the cast to IRaster either, but I haven't tried it.

rasterDS.CreateDefaultRaster();
rasterDSE.Mosaic((IRaster)rasterDS.CreateDefaultRaster(), 1000);


Try one of the following solutions:

#1 - Reuse output of first call

IRaster r = rasterDS.CreateDefaultRaster();
rasterDSE.Mosaic(r, 1000);


#2 - Don't make first call
rasterDSE.Mosaic(rasterDS.CreateDefaultRaster(), 1000);


I prefer #1 because it allows the opportunity to release the raster once you've added it to the mosaic:

IRaster r = null;

try
{
    r = rasterDS.CreateDefaultRaster();
    rasterDSE.Mosaic(r, 1000);
}
finally
{
    if( r != null )
     Marshal.ReleaseComObject( r );
}
0 Kudos
oyleiste
Occasional Contributor
Actually, the first CreateDefaultRaster was mistake (I put it there for my own debugging).
Now, my code is like this:
[ATTACH=CONFIG]23082[/ATTACH]
But, still the same problem. Fails at Mosaic function. I have no idea about it's second paramter. I just give random numbers 😐
Why there isn't any explanation or sample about this function?!
ArcGIS bored me a lot 😞

Thank you for your reply; Jason
Sincerely
0 Kudos
JasonPike
Occasional Contributor
Actually, the first CreateDefaultRaster was mistake (I put it there for my own debugging).
Now, my code is like this:
[ATTACH=CONFIG]23082[/ATTACH]
But, still the same problem. Fails at Mosaic function. I have no idea about it's second paramter. I just give random numbers 😐
Why there isn't any explanation or sample about this function?!
ArcGIS bored me a lot 😞

Thank you for your reply; Jason
Sincerely


Ah! CreateDefaultRaster() returns a raster, not a raster dataset!

You need a raster dataset to create the mosaic:
http://edndoc.esri.com/arcobjects/9.2/NET/dd494eb7-b56b-4e7a-adeb-0eccc1f9765c.htm

Once you create the raster dataset, you can cast it to IRasterDatasetEdit and use the mosaic function to add your rasters (created using the CreateDefaultRaster() method.

Make sense?
0 Kudos
JasonPike
Occasional Contributor
Ah! CreateDefaultRaster() returns a raster, not a raster dataset!

You need a raster dataset to create the mosaic:
http://edndoc.esri.com/arcobjects/9.2/NET/dd494eb7-b56b-4e7a-adeb-0eccc1f9765c.htm

Once you create the raster dataset, you can cast it to IRasterDatasetEdit and use the mosaic function to add your rasters (created using the CreateDefaultRaster() method.

Make sense?


I now realize that is what you've been trying to do--I need more coffee.

How are you creating the raster datasets in your array?

The documentation is a little light on details. I'd like to know what the tolerance does--simply knowing that "tolerance is a parameter of type double" tells us nothing. Bah!

This tells you a little bit about the raster datasets. I would do some research into the terminology. I would ask questions about whether the cell sizes have to the same between rasters your merging, etc.
http://edndoc.esri.com/arcobjects/9.2/NET/dd494eb7-b56b-4e7a-adeb-0eccc1f9765c.htm
0 Kudos
oyleiste
Occasional Contributor
Yes, CreateDefaultRaster returns IRaster; not IRasterDataset. But keep in mind that IRasterDatasetEdit.Mosaic gets IRaster as first parameter, not IRasterDataset. That's why I use rasterDS[0].CreateDefaultRaster() function; to get IRaster and pass it to Mosaic function.

The link; creates a IRasterDataset for a image (map). Because it copies pixels into it. My IRastarDataset's format is "DTED Level 0", not map nor image. So, I think I can't use it.

I need more tea 😄

I get IRasterDataset like this:
IWorkspaceFactory wsf = new RasterWorkspaceFactoryClass();
IRasterWorkspace rasterWS = (IRasterWorkspace)wsf.OpenFromFile(foldername, 0);
List<IRasterDataset> rasterDS = new List<IRasterDataset>();
rasterDS.Add(rasterWS.OpenRasterDataset(DTED_filename)); //"n34.dt0" for ex.

Yes, the documentation just says it's datatype: double 😐
0 Kudos