Merge layers and export to PNG

738
0
10-17-2011 12:31 AM
NorbertSuski
New Contributor
Hello,
I have some question associated with .NET SDK. How can I realize this scenario: merge choosen layers (many vector layers and one raster) into one raster layer and export to PNG-s? I want to achieve functionality to export all map in ArcMap.

I can export raster layer to PNG files, but I want to merge vector and raster layers into one raster layer.

Here is some example code to export raster layer (IFeature in argument is feature layer):
protected void ExportRaster(ESRI.ArcGIS.Geodatabase.IFeature pFeature, int pIdx, string pFilePath)
        {
            ESRI.ArcGIS.Geodatabase.IRasterCatalogItem rci = (ESRI.ArcGIS.Geodatabase.RasterCatalogItem)pFeature;
            ESRI.ArcGIS.Geodatabase.IRasterDataset pRDS = rci.RasterDataset;
            ESRI.ArcGIS.Geodatabase.IRasterValue pRV = new ESRI.ArcGIS.Geodatabase.RasterValue();
            pRV.RasterDataset = pRDS;
            ESRI.ArcGIS.Geodatabase.IWorkspace pWS;
            ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWSF = new ESRI.ArcGIS.DataSourcesRaster.RasterWorkspaceFactory();
            pWS = pWSF.OpenFromFile(pFilePath, 0);
            string fileName;
            fileName = "raster" + pIdx.ToString();
            this.TileRaster(pRDS, pWS, RasterDimentionX, RasterDimentionY, fileName); // dzieli raster na pliki o podanych wymiarach
        }
        private void TileRaster(ESRI.ArcGIS.Geodatabase.IRasterDataset pRasterDs, ESRI.ArcGIS.Geodatabase.IWorkspace pOutputWs, int pRow, int pColumn, string pRasterName)
        {
            ESRI.ArcGIS.DataSourcesRaster.IRasterProps pRaster;
            ESRI.ArcGIS.Geodatabase.ISaveAs2 pSaveAs;
            ESRI.ArcGIS.Geometry.IEnvelope pExt;
            ESRI.ArcGIS.Geometry.IEnvelope pOrg;
            ESRI.ArcGIS.Geodatabase.IDataset pDs;
            IRasterStretch pRasterStr;
            int iCntX;
            int iCntY;
            int rowleft;
            int colleft;
            int iWidth;
            int iHeight;

            try
            {
                //' ++ if rasterdef is missing, create one with specified/unknown spatialreference
                //'If pRasterDef Is Nothing Then
                //'  Set pRasterDef = createRasterDef(False, pSR)
                //'End If

                pRaster = (IRasterProps)pRasterDs.CreateDefaultRaster(); // Get raster object to manipulate

                pDs = (ESRI.ArcGIS.Geodatabase.IDataset)pRasterDs; // QI dataset to get name
                //' Calculate how many subsets will be created
                iWidth = pRaster.Width;
                iHeight = pRaster.Height;
                iCntX = iWidth / pColumn;
                iCntY = iHeight / pRow;
                rowleft = iHeight % pRow; // modulo
                colleft = iWidth % pColumn;
                //' Loop through all the subsets and create IMGs
                //for(int i = 0; i < iCntX; i++)
                for (int i = 0; i <= iCntX; i++)
                {
                    iWidth = pColumn;
                    iHeight = pRow;

                    if (i == iCntX)
                        iWidth = colleft;

                    if (iWidth > 5)
                    {
                        //for(int j = 0; j < iCntY; j++)
                        for (int j = 0; j <= iCntY; j++)
                        {
                            IRaster iraster = pRasterDs.CreateDefaultRaster();
                            pRaster = (ESRI.ArcGIS.DataSourcesRaster.IRasterProps)iraster;//(ESRI.ArcGIS.DataSourcesRaster.IRasterProps)pRasterDs.CreateDefaultRaster();
                            //=============MODYFIKACJA NA 8 bitowe RASTRY=======
                            if (pRaster.PixelType == rstPixelType.PT_SHORT || pRaster.PixelType == rstPixelType.PT_USHORT)
                                pRaster.PixelType = rstPixelType.PT_UCHAR; // SPECJALNA MODYFIKACJA RASTROW NA 8 bitowe 
                            //=============================================

                            pOrg = pRaster.Extent;
                            
                            pExt = (ESRI.ArcGIS.Geometry.IEnvelope)new ESRI.ArcGIS.Geometry.Envelope();
                            
                            if (j == iCntY)
                                iHeight = rowleft;

                            if (iHeight > 5) //' Set the extents of the output raster
                            {
                                pExt.XMin = Math.Floor((double)pOrg.XMin + ((double)i * (double)pColumn * (double)pRaster.MeanCellSize().X));
                                pExt.YMin = Math.Floor((double)pOrg.YMin + (double)j * (double)pRow * (double)pRaster.MeanCellSize().Y);
                                pExt.XMax = (double)pOrg.XMin + (double)(i * pColumn + iWidth) * (double)pRaster.MeanCellSize().X;
                                pExt.YMax = (double)pOrg.YMin + (double)(j * pRow + iHeight) * (double)pRaster.MeanCellSize().Y;
                                

                                //zrobione zeby w GEWO nie byly widoczne biale linie na laczeniach rastrow (rastry sa zwiekszane o jeden pixel w kazdym kierunku
                                //pExt.XMax += 1;
                                //pExt.YMax += 1;
                                //pExt.XMin -= 1;
                                //pExt.YMin -= 1;

                                pRaster.Extent = pExt;
                                pRaster.Width = iWidth;
                                pRaster.Height = iHeight;
                                
                                pSaveAs = (ESRI.ArcGIS.Geodatabase.ISaveAs2)pRaster; //' Save to a file with datasetname_Xtilenumber_Ytilenumber.img                      
                                
                                ESRI.ArcGIS.Geodatabase.IRasterStorageDef rsd = new ESRI.ArcGIS.Geodatabase.RasterStorageDef();
                                
                                //ESRI.ArcGIS.  
                                rsd.PyramidLevel = 0;
                                //rsd.CompressionQuality = 65;

                                rsd.CompressionType = ESRI.ArcGIS.Geodatabase.esriRasterCompressionType.esriRasterCompressionLZW;
                                string rasterFilename = pRasterName + "_" + i.ToString() + "_" + j.ToString() + ".png";
                                pSaveAs.SaveAsRasterDataset(rasterFilename, pOutputWs, "PNG", rsd);
                                InvertRaster(pOutputWs.PathName, rasterFilename);
                            }
                        }
                    }
                }
                pSaveAs = null;
                pRasterDs = null;
                pDs = null;
                pExt = null;
                pOrg = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Wyst??pi?? b????d podczas eksportu podk??adów rastrowych!", "B????d!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw ex;
            }
        }
0 Kudos
0 Replies