ARCGIS 10, VB.NET, calculate centroids

2671
6
06-17-2016 02:08 AM
BrunoVincent
New Contributor

Hello,

  I have migrated an toolbar initially developped for ArcGis 9,3. This tool is used by non-programmer or cartographers.

  One procedure is called to generate centroids of attributes table and after to launch an calculation.


  Anyway,

  The source code used in 9,3 was :


  (A)

'instantiating new calculator and cursor

  pCalculator = New Calculator

  pCursor = CType(pFeatureClass.Update(Nothing, True), ICursor)


    'Centroid Y

  With pCalculator

  .Cursor = pCursor

.PreExpression = "Dim pGeomShp as IGeometry" & vbNewLine & "Set pGeomShp = [shape]" & vbNewLine & "Dim pArea as IArea" & vbNewLine & "Set pArea = pGeomShp" & vbNewLine

.Expression = "pArea.Centroid.Y"

  .Field = "centroidY"

  End With

 
    'try & catch *************************************************

  Try

pCalculator.Calculate()

  Catch ex As System.Exception

MessageBox.Show("Error: " + ex.Message)

  End Try



  This was working until moving to ArcGis 10 (an upper). A crash was observed while launching pCalculator.Calculate().


   
  I’ve migrated this code that way (like proposed here http://gis.stackexchange.com/questions/52983/syntax-for-arcgis-icalculator-object/198681#198681 )  :

 
  (B)

  Dim nCentroidXFieldIndex As Integer = pFeatureClass.FindField("centroidX")

  Dim nCentroidYFieldIndex As Integer = pFeatureClass.FindField("centroidY")

  Dim nBldg_sizeFieldIndex As Integer = pFeatureClass.FindField("Bldg_size")


      If TypeOf pFeatureClass Is IFeatureClass Then

  If TypeOf pFeatureCursor Is IFeatureCursor Then


 

  Dim pArea As IArea

  Dim pFeature As IFeature = pFeatureCursor.NextFeature()

  Dim myProgressCalc As New FileProgressForm

myProgressCalc.set_maximum(iTotalRecords)

myProgressCalc.Show()

myProgressCalc.Text = "Veuillez patienter svp"

myProgressCalc.lblProgress.Text = "Calcul des données géographiques"

myProgressCalc.Update()


   pArea = CType(pFeature.Shape, IArea)


   While pFeature IsNot Nothing

pFeature.Value(nCentroidXFieldIndex) = pArea.Centroid.X

pFeature.Value(nCentroidYFieldIndex) = pArea.Centroid.Y

pFeature.Value(nBldg_sizeFieldIndex) = pArea.Area


   pFeature.Store()

pFeature = pFeatureCursor.NextFeature


   myProgressCalc.Go()


    End While

myProgressCalc.Close()

  End If

  End If


 
  It’s working but it’s working extremely slowly ! The first code (A) was executed in less than 3 minutes. The new one (B) in 25 minutes !


 
  In fact, I just want to programmaticaly call a the new calculate geometry tool (when displaying attributes table - http://desktop.arcgis.com/fr/arcmap/10.3/manage-data/tables/calculating-area-length-and-other-geomet... )


 

Screen capture 1

Too bad, can't upload PNG and JPEG files...

or Arc tool => Data Management Tools => Entities => Add geometric attributes (http://pro.arcgis.com/fr/pro-app/tool-reference/data-management/add-geometry-attributes.htm )


  Screen capture 2

or any other methods !

   

Both tools are working great in ArcGis Desktop but I can’t find my way to make them work in VB.NET.

Please, any suggestions ? (It’s just driving me crazy).

   

Thanks by advance for your answer, and sorry for screen capture in french and for poor english speaking


  Matthieu

0 Kudos
6 Replies
KenBuja
MVP Esteemed Contributor

One issue with your ICalculate PreExpression script is with ArcGIS 10, VBScript will now crash when try to declare a variable as a type.

Dim pGeomShp as IGeometry

should be replaced with

Dim pGeomShp

0 Kudos
BrunoVincent
New Contributor

Hello,

Thank you.

I just made the change you proposed without any success.

The following error is still showing :

"Error -2147467259 HRESULT E_FAIL has been returned from a call to a COM component"

0 Kudos
KenBuja
MVP Esteemed Contributor

Did you also make that change to the Dim pArea line? Also, have you tried running the PreExpression script manually in the Field Calculator?

0 Kudos
BrunoVincent
New Contributor

Hello,

Yes, I also made the change for the "Dim pArea" line. With no success.

I tried to execute this code manually in ArcGis desktop. But unfortunately, it did'nt work.  I'm realizing that I should write the good script here before trying to program it.

.

I made many tries in VB. Tthe only thing that were working correctly (manually, in desktop ) was in Python with that code :

!shape.centroid.X!

But I just can't make it work in the program.

0 Kudos
KenBuja
MVP Esteemed Contributor

Unfortunately, there's a bug with the ICalculator (#NIM061356) that doesn't allow it to use Python syntax. This has been around since 2010 and while it's classified as High Severity, they still haven't fixed it.

0 Kudos
BrunoVincent
New Contributor

Damn.

It's so easy to make it work in Python with 1 line of code and I can't make it with VBScript ...

0 Kudos