Differences between results obtained with Raster Calculator and Map Algebra (VB)

4943
9
Jump to solution
08-07-2014 07:05 AM
CarlesMiquel
New Contributor II

Hello,

 

I would want to work with Map Algebra launched in VB from ArcGis 9.3.

The results obtained, are a little bit different from those obtained with Raster Calculator.

Does someone know where could be the problem?

Thank you very much,

 

Carles.

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

If the same map algebra expression generates different results when called from different environments (for example, from running the 9x Raster Calculator tool, ModelBuilder, or using a geoprocessing tool), I believe a good place to start is to verify the environments used -- cell size, extent, coordinate system, etc. -- and make sure they are the same.

In 9.x this is particularly problematic as the the Raster Calculator (and VB/VBA calls to RasterOp objects) observe the ArcObjects settings (set in the Spatial Analyst toolbar Options), while the map algebra geoprocessing tools (SOMA, MOMA) use the (perhaps different) settings in the geoprocessing environment. In 10.x, all spatial analyst tools use the geoprocessing environment -- much less confusing that way!

View solution in original post

0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

Examples would be great...the syntax and the outputs would be useful

0 Kudos
curtvprice
MVP Esteemed Contributor

If the same map algebra expression generates different results when called from different environments (for example, from running the 9x Raster Calculator tool, ModelBuilder, or using a geoprocessing tool), I believe a good place to start is to verify the environments used -- cell size, extent, coordinate system, etc. -- and make sure they are the same.

In 9.x this is particularly problematic as the the Raster Calculator (and VB/VBA calls to RasterOp objects) observe the ArcObjects settings (set in the Spatial Analyst toolbar Options), while the map algebra geoprocessing tools (SOMA, MOMA) use the (perhaps different) settings in the geoprocessing environment. In 10.x, all spatial analyst tools use the geoprocessing environment -- much less confusing that way!

0 Kudos
CarlesMiquel
New Contributor II

Thank you to both,

I am sure you are right Curtis, but I don't know how the environment settings could be fixed in VBA for Map Algebra. As suggested by Dan, I attach the code used just in case it could help to bring more information.

Thank you very much,

Carles.

Sub Map_algebra_calculation(calculation_text As String, Output_raster_name As String)

    strData = "C:\OUTPUT_PATH\"

   

    Dim pMxDoc As IMxDocument

    Set pMxDoc = ThisDocument

    Dim pMap As IMap

    Set pMap = pMxDoc.FocusMap

    Dim pRLayer As IRasterLayer

    Dim pLayer_AAA As ILayer

    Dim pInRaster_AAA As IRaster

    Dim pLayer_BBB As ILayer

    Dim pInRaster_BBB As IRaster

    Dim pLayer_CCC As ILayer

    Dim pInRaster_CCC As IRaster

    Dim pLayer_DDD As ILayer

    Dim pInRaster_DDD As IRaster

    Dim pLayer_EEE As ILayer

    Dim pInRaster_EEE As IRaster

      

   ' Create a Spatial operator

   Dim pAlgbOp As IMapAlgebraOp

   Set pAlgbOp = New RasterMapAlgebraOp

   Set pLayer_AAA = pMap.Layer(2)

   Set pRLayer = pLayer_AAA

   Set pInRaster_AAA = pRLayer.Raster

   Set pLayer_BBB = pMap.Layer(3)

   Set pRLayer = pLayer_BBB

   Set pInRaster_BBB = pRLayer.Raster

   Set pLayer_CCC = pMap.Layer(4)

   Set pRLayer = pLayer_CCC

   Set pInRaster_CCC = pRLayer.Raster

   Set pLayer_DDD = pMap.Layer(5)

   Set pRLayer = pLayer_DDD

   Set pInRaster_DDD = pRLayer.Raster

   Set pLayer_EEE = pMap.Layer(6)

   Set pRLayer = pLayer_EEE

   Set pInRaster_EEE = pRLayer.Raster

    ' Bind a raster

    Call pAlgbOp.BindRaster(pInRaster_AAA, "AAA")

    Call pAlgbOp.BindRaster(pInRaster_BBB, "BBB")

    Call pAlgbOp.BindRaster(pInRaster_CCC, "CCC")

    Call pAlgbOp.BindRaster(pInRaster_DDD, "DDD")

    Call pAlgbOp.BindRaster(pInRaster_EEE, "EEE")

    ' Set output workspace

    Dim pEnv As IRasterAnalysisEnvironment

    Set pEnv = pAlgbOp

    Dim pWS As IWorkspace

    Dim pWSF As IWorkspaceFactory

    Set pWSF = New RasterWorkspaceFactory

    Set pWS = pWSF.OpenFromFile("C:\OUTPUT_PATH\TEMP", 0)

    Set pEnv.OutWorkspace = pWS

    ' Perform Spatial operation

   Dim pOutRaster As IRaster

   

    ' Defineix les variable de la iteracio de l'Excel llegit

    ' Dim Texte_calculadora_raster As String

   '  Set Texte_calculadora_raster = calculation_text

    ' Dim Nom_fitxer_sortida As String

   '  Set Nom_fitxer_sortida = Output_raster_name

               

    'aquí comança el càlcul de la regressio multiple

    Set pOutRaster = pAlgbOp.Execute(calculation_text) '(Texte_calculadora_raster)

' Unbind rasters

     Call pAlgbOp.UnbindRaster("AAA")

     Call pAlgbOp.UnbindRaster("BBB")

     Call pAlgbOp.UnbindRaster("CCC")

     Call pAlgbOp.UnbindRaster("DDD")

     Call pAlgbOp.UnbindRaster("EEE")

    ' Add it into ArcMap

    Set pRLayer = New RasterLayer

    pRLayer.CreateFromRaster pOutRaster

    ' Add its name (Output raster name)

    pRLayer.Name = Output_raster_name pMap.AddLayer pRLayer

    Call createFileFromRaster(pOutRaster, strData + pRLayer.Name)

   

               

End Sub

0 Kudos
curtvprice
MVP Esteemed Contributor

I don't know how the environment settings could be fixed in VBA for Map Algebra.

I believe these settings are properties on your pEnv (IRasterAnalysisEnvironment) object. I would query your pEnv object and see if the values are the same that were set in the Spatial Analyst toolbar options when you ran the Raster Calculator. it's true the Raster Calculator interface is different from the raster tools and this a major reason it went away at 10.x.

Have you considered eschewing the the ArcObjects Ops and instantiate a geoprocessor object instead so you can use the single-output map algebra tool - this lets the GP do a lot of the work for you -- a lot less code! A side benefit of doing it that way is your code will be a lot more portable to 10.x.  Also, note that VBA is deprecated from 10.x on.

0 Kudos
CarlesMiquel
New Contributor II

Thank you all.

I am not an expert in programming code and do not know how to set and write this environment.

Could someone posting an example setting all the properties of an pEnv (IRasterAnalysisEnvironment) object?

Thank you very much.

Carles.

0 Kudos
curtvprice
MVP Esteemed Contributor

I am not a developer either, but it seems pretty straight forward, along the  lines of:

cell_size = 30
Dim pRasEnv as IRasterAnalysisEnvironment
pRasEnv.SetCell(esriRasterEnvValue, cell_size)

I still think going to geoprocessing is an easier path - and more forward-compatible.

CarlesMiquel
New Contributor II

Thank you Curtis,

I will try your example. I tried geoprocessing with an adapted code in the base of this one, but the results are very different from the ones obtained with the raster calculator. I don't know where the problem is.

Do you have an idea?

# Create the Geoprocessor object

gp = arcgisscripting.create()

try:

  # Set local variables

  InExpression = "focalsum(C:/data/flowdir, Circle, 3)"

  OutRaster = "C:/data/soma01"

  # Check out Spatial Analyst extension license

  gp.CheckOutExtension("Spatial")

  # Process: MapAlgebraStatement

  gp.SingleOutputMapAlgebra_sa(InExpression, OutRaster)

except:

  # If an error occurred while running a tool, then print the messages.

  print gp.GetMessages()

0 Kudos
CarlesMiquel
New Contributor II

Dim gp As Object

    Set gp = CreateObject("esriGeoprocessing.GPDispatch.1")

    gp.CheckOutExtension "spatial"

    gp.AddToolbox "C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx"

    Dim StrDataComplerta As String

    StrDataComplerta = StrData + nom_raster_sortida

    gp.SingleOutputMapAlgebra_sa text_calcul, StrDataComplerta

0 Kudos
CarlesMiquel
New Contributor II

Thank you Curtis,

An how could I define the other parametre from pEnv (Extent as interesection, etc...)?

0 Kudos