Update raster width/height with pro sdk

506
8
Jump to solution
12-08-2019 05:08 PM
ThanHtetAung_EsriAu
Esri Regular Contributor

Hi Gurus,

Refer to this 

Create RasterDataset 

After managed to create raster dataset, tried and change the raster size and update the extent and it does not update my raster properties.

SetExtent, SetWidth, SetHeight methods are used.

May I know what went wrong?

Below is my code snippet.

  string ExportTifFile = FileName + ".tif";
                    var parameters = Geoprocessing.MakeValueArray(
                         this.InputModel.DestinationPath, // Output path
                        ExportTifFile, // Raster name : make it as tif file
                         rasterInfo.CellSize.ToString(), // Cellsize
                         this.InputModel.PixelType, // pixel type
                         this._lastSelectedSR, // Spatial reference,
                         1); // Bands count

                    //IReadOnlyList<KeyValuePair<string, string>> environments = Geoprocessing.MakeEnvironmentArray(extent: pEnvelope);
                    var gpResult = Geoprocessing.ExecuteToolAsync("CreateRasterDataset_management", parameters, null,
                                        CancelableProgressor.None, GPExecuteToolFlags.GPThread);
                    gpResult.Wait();
                    IGPResult ProcessingResult =  gpResult.Result;
                    if (ProcessingResult.IsFailed)
                    {
                       foreach(IGPMessage message in ProcessingResult.Messages)
                        {
                            ConversionToolModule.Current.ModuleLogManager.LogError("Error on create raster dataset tool calling");
                            ConversionToolModule.Current.ModuleLogManager.LogError($"{{Error Code: {message.ErrorCode}, Text :  {message.Text} }}");
                        }
                        Geoprocessing.ShowMessageBox(ProcessingResult.Messages, "Raster creation error", GPMessageBoxStyle.Error);
                    }
//Try and update 
await QueuedTask.Run(() =>
                    {
                        FileSystemConnectionPath connectionPath = new FileSystemConnectionPath(new System.Uri(this.InputModel.DestinationPath), FileSystemDatastoreType.Raster);
                        FileSystemDatastore datastore = new FileSystemDatastore(connectionPath);

                        RasterDataset tmpFileRasterDataset = datastore.OpenDataset<RasterDataset>(ExportTifFile);
                                                
                        ArcGIS.Core.Data.Raster.Raster tmpRaster = tmpFileRasterDataset.CreateDefaultRaster();
                        if (tmpRaster.CanEdit())
                        {                          
                            Envelope RasterExtent = EnvelopeBuilder.CreateEnvelope((double)rasterInfo.MinX, (double)rasterInfo.MinY, (double)rasterInfo.MaxX, (double)rasterInfo.MaxX, this._lastSelectedSR);
                            tmpRaster.SetExtent(RasterExtent);
                            tmpRaster.SetWidth(rasterInfo.RasterColumnCount);
                            tmpRaster.SetHeight(rasterInfo.RasterRowCount);

                          
                            tmpRaster.Refresh();

                                                    }
                    }, ps.Progressor);

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Prashant_MukeshMangtani
Esri Contributor

Than,

In your code above when you create the extent envelope, you are setting MaxX instead of MaxY. Bolded below.

Envelope RasterExtent = 
EnvelopeBuilder.CreateEnvelope((double)rasterInfo.MinX, (double)rasterInfo.MinY, (double)rasterInfo.MaxX, (double)rasterInfo.MaxX, this._lastSelectedSR);

View solution in original post

0 Kudos
8 Replies
GintautasKmieliauskas
Regular Contributor

Hi Than,

I think you need to save your raster to get updated properties. Look at this link:

https://community.esri.com/thread/213505-raster-setwidth-not-working

0 Kudos
ThanHtetAung_EsriAu
Esri Regular Contributor

Thank Gintautas Kmieliauskas

That seems to be working now but it introduce another issue . The new set  of saved raster dataset create dam different cell size of Y value.

Yep it save extent, width, height but it changes the cell size.

0 Kudos
GintautasKmieliauskas
Regular Contributor

Hi Than,

What type is your rasterInfo.CellSize variable? Try to set constant value first (5,10 or 50). And you don't need to convert MakeValueArray arguments to string. They are all object type. MakeValueArray checks argument types itself and converts how it needs.

After MakeValueArray you can check parameters variable with debugger watch and see what value for cell size was defined.

0 Kudos
ThanHtetAung_EsriAu
Esri Regular Contributor

Hi Gintautas,

Look like I confuse you.

Below is the full code snippet I used, the Raster Cellsize is good, when I used with Geoprocessing, after I called saved as method to save new set of raster data, it changed the cell size.

I attached the raster file before saveas and after save as function call.

string ExportTifFile = SourceFileName + ".tif";
                    string ExportFullTifFile = SourceFileName + "FullData.tif";
                    string tempFolderPath = Path.Combine(this.XYZInputModel.DestinationPath, "temp");

                    Directory.CreateDirectory(tempFolderPath);
                    //string ExportTifFile = SourceFileName;
                    var parameters = Geoprocessing.MakeValueArray(
                         tempFolderPath, // Output path
                        ExportTifFile, // Raster name : make it as tif file
                         rasterInfo.CellSize.ToString(), // Cellsize
                         this.XYZInputModel.PixelType, // pixel type
                         this._lastSelectedSR, // Spatial reference,
                         1, "", SourceFileName,
                         "128 128", "NONE", ""
                         ); // Bands count

                    //IReadOnlyList<KeyValuePair<string, string>> environments = Geoprocessing.MakeEnvironmentArray(extent: pEnvelope);
                    var gpResult = Geoprocessing.ExecuteToolAsync("CreateRasterDataset_management", parameters, null,
                                        CancelableProgressor.None, GPExecuteToolFlags.GPThread);
                    gpResult.Wait();
                    IGPResult ProcessingResult = gpResult.Result;
                    if (ProcessingResult.IsFailed)
                    {
                        foreach (IGPMessage message in ProcessingResult.Messages)
                        {
                            ConversionToolModule.Current.ModuleLogManager.LogError("Error on create raster dataset tool calling");
                            ConversionToolModule.Current.ModuleLogManager.LogError($"{{Error Code: {message.ErrorCode}, Text :  {message.Text} }}");
                        }
                        Geoprocessing.ShowMessageBox(ProcessingResult.Messages, "Raster creation error", GPMessageBoxStyle.Error);
                    }

                    //FillRasterData , FileSystemDatastore required to run under MCT
                    await QueuedTask.Run(() =>
                    {
                        FileSystemConnectionPath connectionPath = new FileSystemConnectionPath(new System.Uri(tempFolderPath), FileSystemDatastoreType.Raster);
                        FileSystemDatastore datastore = new FileSystemDatastore(connectionPath);

                        //temporary dataset
                        RasterDataset tmpFileRasterDataset = datastore.OpenDataset<RasterDataset>(ExportTifFile);
                        FileSystemConnectionPath finalConnectionPath = new FileSystemConnectionPath(new System.Uri(this.XYZInputModel.DestinationPath), FileSystemDatastoreType.Raster);
                        FileSystemDatastore finalDataStore = new FileSystemDatastore(finalConnectionPath);

                        ArcGIS.Core.Data.Raster.Raster tmpRaster = tmpFileRasterDataset.CreateFullRaster();
                        if (tmpRaster.CanEdit())
                        {
                            Envelope RasterExtent = EnvelopeBuilder.CreateEnvelope((double)rasterInfo.MinX, (double)rasterInfo.MinY, (double)rasterInfo.MaxX, (double)rasterInfo.MaxX, this._lastSelectedSR);
                            tmpRaster.SetExtent(RasterExtent);
                            tmpRaster.SetWidth(rasterInfo.RasterColumnCount);
                            tmpRaster.SetHeight(rasterInfo.RasterRowCount);

                            RasterStorageDef rasterStorageDef = new RasterStorageDef();
                            //New dataset -this method has issue * cellsize Y become shoot into very big number
                            RasterDataset FinalRasterDataset = tmpRaster.SaveAs(ExportFullTifFile, finalDataStore, "TIFF", rasterStorageDef);
                            //Need to remove temp folder and that Save as function update the original raster file cell size generated by geoprocessing

                            datastore.Dispose();
                            datastore = null;
                            connectionPath = null;

                            ArcGIS.Core.Data.Raster.Raster finalRaster = FinalRasterDataset.CreateFullRaster();
                            bool status = rasterUtility.FillRasterData(finalRaster, this.XYZInputModel.SourcePath, this.XYZInputModel.Delimiter, rasterInfo, this.XYZInputModel.PixelType);
                        }
                    }, ps.Progressor);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
GintautasKmieliauskas
Regular Contributor

Hi Than,

Try to add line of code before calling SaveAs:

  rasterStorageDef.SetCellSize(rasterInfo.CellSize, rasterInfo.CellSize);

0 Kudos
ThanHtetAung_EsriAu
Esri Regular Contributor

Thank Gintautas,

But no hope, still changing the cell size y. 

 
0 Kudos
Prashant_MukeshMangtani
Esri Contributor

Than,

In your code above when you create the extent envelope, you are setting MaxX instead of MaxY. Bolded below.

Envelope RasterExtent = 
EnvelopeBuilder.CreateEnvelope((double)rasterInfo.MinX, (double)rasterInfo.MinY, (double)rasterInfo.MaxX, (double)rasterInfo.MaxX, this._lastSelectedSR);
0 Kudos
ThanHtetAung_EsriAu
Esri Regular Contributor

Thank Prashant Mukesh Mangtani‌,

That right, I did typo error.

It solve the cell size issue .

Best Regards,

Than

0 Kudos