Download presentation
Presentation is loading. Please wait.
Published byΒάαλ Γούναρης Modified over 6 years ago
1
Using VB Active-X components to customize a new tool on ArcMap
3
Define the extent of the selected features which will be the extent of the output layer and saving the object IDs of the selected features in an array (which will be used later). extent Set pEnumFeat = pMap.FeatureSelection Set pFeatpol = pEnumFeat.Next If pFeatpol Is Nothing Then 'If there is no selected features MsgBox ("Please select at least one poygon feature") GoTo RlsMm Exit Function End If Set pfExtent = pFeatpol.Extent Dim NumObj As Integer NumObj = 0 'loop thru selected features and redefine the extent accordingly While Not pFeatpol Is Nothing With pFeatpol.Extent If .XMax > pfExtent.XMax Then pfExtent.XMax = .XMax If .XMin < pfExtent.XMin Then pfExtent.XMin = .XMin If .YMax > pfExtent.YMax Then pfExtent.YMax = .YMax If .YMin < pfExtent.YMin Then pfExtent.YMin = .YMin End With objID(NumObj) = pFeatpol.OID NumObj = NumObj + 1 'number of selected objects Set pFeatpol = pEnumFeat.Next Wend 24 23 28 27 26 25 29
4
Reading the safe array of the original raster layer on the final extent (with an option of saving this block of the safe array.) 'Create a DblPnt to hold the PixelBlock size Dim pSize As IPnt Set pSize = New DblPnt Dim CellSizeX, CellSizeY As Double CellSizeX = pRasterProp.MeanCellSize.X CellSizeY = pRasterProp.MeanCellSize.Y Dim m, n As Long m = CLng((pfExtent.XMax - pfExtent.XMin + 1) / CellSizeX) n = CLng((pfExtent.YMax - pfExtent.YMin + 1) / CellSizeY) pSize.SetCoords m, n Dim pTopLeftCrn As IPnt Set pTopLeftCrn = New DblPnt Dim g, f As Long g = (pfExtent.XMin - pRasterProp.Extent.XMin) / CellSizeX g = Abs(CLng(g)) f = (pfExtent.YMax - pRasterProp.Extent.YMax) / CellSizeY f = Abs(CLng(f)) 'Redefine the Extent to gurantee exact allignment with the original layer pfExtent.XMax = CDbl(pRasterProp.Extent.XMin) + CDbl(g + m) * CellSizeX pfExtent.YMin = CDbl(pRasterProp.Extent.YMax) - CDbl(f + n) * CellSizeY pfExtent.XMin = CDbl(pRasterProp.Extent.XMin) + CDbl(g) * CellSizeX pfExtent.YMax = CDbl(pRasterProp.Extent.YMax) - CDbl(f) * CellSizeY pTopLeftCrn.SetCoords g, f Dim pBlock As IPixelBlock 'pRawPixel.Read pTopLeftCrn, pBlock 'Set pBlock = pRawPixel.CreatePixelBlock(pSize) Set pBlock = pRasterNew.CreatePixelBlock(pSize) Dim pRawPixel As IRawPixels Set pRawPixel = pBand pRawPixel.Read pTopLeftCrn, pBlock 'pRasterNew.Read pTopLeftCrn, pBlock Dim pOrigSafeArray As Variant pOrigSafeArray = pBlock.SafeArray(0)
5
Transform the polygon feature layer into a raster layer
Transform the polygon feature layer into a raster layer. pixel values in this raster layer will be the same as the containing polygon object IDs Dim pEnv As IRasterAnalysisEnvironment, pConv As IConversionOp Set pEnv = New RasterAnalysis Set pConv = New RasterConversionOp Set pEnv = pConv pEnv.SetCellSize esriRasterEnvValue, CDbl(pRasterProp.MeanCellSize.X) pEnv.SetExtent esriRasterEnvValue, pfExtent Dim pTempDS As IGeoDataset, polRDS As IRasterDataset Set pTempDS = pFeatLyr.FeatureClass Dim Set polRDS = New RasterDataset Set polRDS = pConv.ToRasterDataset(pTempDS, "IMAGINE Image", pWS, "Tempcov.img") Dim pNewRaster As IRaster,pNewRasProps As IRasterProps Set pNewRaster = polRDS.CreateDefaultRaster Set pNewRasProps = pNewRaster ' Get RasterBand from the raster Dim pNewBand As IRasterBand, pNewBands As IRasterBandCollection Set pNewBands = pNewRaster Set pNewBand = pNewBands.Item(0) ' Create a DblPnt to hold the PixelBlock size Dim pNewSize As IPnt Set pNewSize = New DblPnt Dim pOrigin As IPnt Set pOrigin = New DblPnt pNewSize.SetCoords pNewRasProps.Width, pNewRasProps.Height pOrigin.SetCoords 0, 0 'QI RawPixel interface Dim pRawPixel2 As IRawPixels Set pRawPixel2 = pNewBand Dim pBlock2 As IPixelBlock Set pBlock2 = pNewRaster.CreatePixelBlock(pNewSize) pRawPixel2.Read pOrigin, pBlock2 Dim pNewArray As Variant pNewArray = pBlock2.SafeArray(0)
6
Creating a new raster dataset (which will be the output) with the defined extent. the pixel values in this raster will be the same as those in the original raster if the correspondent pixel value of the transformed layer (step 3) is equal to any value in the object ID array (step 1) else the pixel value of the output raster will be NoData (transparent)
7
Dim pRWS As IRasterWorkspace2 Dim pWSF As IWorkspaceFactory Set pWSF = New RasterWorkspaceFactory Set pRWS = pWSF.OpenFromFile(sPath, 0) Dim OutPutRDS As IRasterDataset Dim ColCount, RCount As Long ColCount = m RCount = n Dim Spat As ISpatialReference Set Spat = pNewRasProps.SpatialReference pNewRasProps.Extent = pfExtent Dim pOrigin2 As IPoint Set pOrigin2 = New Point pOrigin2.X = pfExtent.XMin pOrigin2.Y = pfExtent.YMin PB.Value = 80 Set OutPutRDS = pRWS.CreateRasterDataset(sFileName3, "GRID", pOrigin2, ColCount, RCount, _ CellSizeX, CellSizeY, 1, PT_LONG, Spat, True) PB.Value = 90 ' Create a default raster and QI raster properties interface Dim pOutRaster As IRaster Set pOutRaster = OutPutRDS.CreateDefaultRaster Dim pOutBandCol As IRasterBandCollection Set pOutBandCol = pOutRaster Dim pOutBand As IRasterBand Set pOutBand = pOutBandCol.Item(0) Dim pOutRasProps As IRasterProps Set pOutRasProps = pOutBand ' QI RawPixel interface Dim pOutRawPixel As IRawPixels Set pOutRawPixel = pOutBand ' Create a DblPnt to hold the PixelBlock size Dim pOutSize As IPnt Set pOutSize = New DblPnt pOutSize.SetCoords pOutRasProps.Width, pOutRasProps.Height 'pRasProps.NoDataValue = 0 ' Create PixelBlock with defined size Dim pOutBlock As IPixelBlock Set pOutBlock = pOutRawPixel.CreatePixelBlock(pOutSize)
8
Dim pOutSafeArray As Variant pOutSafeArray = pOutBlock
Dim pOutSafeArray As Variant pOutSafeArray = pOutBlock.SafeArray(0) 'Setting the nodata value to some odd value for display reasons pOutRasProps.NoDataValue = Dim ii, j, k As Long For ii = 0 To pNewSize.X - 1 For j = 0 To pNewSize.Y For k = 0 To NumObj If B_(ii, j) = CLng(objID(k)) Then pOutSafeArray(ii, j) = CDbl(A_(ii, j)) GoTo sss: End If Next k pOutSafeArray(ii, j) = CDbl(pOutRasProps.NoDataValue) sss: Next j PB.Value = * ii / (pNewSize.X - 1) Next ii 'pOutBlock.SafeArray(0) = pOutSafeArray pOrigin.SetCoords 0, 0 pOutRawPixel.Write pOrigin, pOutBlock Dim pRasPyramid As IRasterPyramid Set pRasPyramid = OutPutRDS ' Create the pyramid If Not pRasPyramid.Present Then pRasPyramid.Create End If 'Recompute statistics and histogram in the band for display reasons too pOutBand.ComputeStatsAndHist pOutBand.Statistics.Recalculate 'Add the raster layer Dim pOutputRasLy As IRasterLayer Set pOutputRasLy = New RasterLayer pOutputRasLy.CreateFromDataset OutPutRDS 'pOutputRasLy.Name = "Clip" pMap.ClearSelection pMap.AddLayer pOutputRasLy pMxDoc.ActiveView.Refresh
10
What else can be done next?
Code refining… Better error handling trying the program with more variety of grids with different sizes and/or pixel-types. More careful dealing with data types. User-friendlier program with a nicer interface More features
11
Done! http://ceefs.cee.usu.edu/yasir/gisproject/Example.htm
To download the DLL file, go to To see an example of how the tool works…Check this out For those who are interested, The source code will be posted on the web right after the presentation! Done!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.