Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I used model builder to build a model which can calculate the XY coordinates and it's perfect!

However, when I export it to VBscript and run it in VBA it crashes....

The error happens at

 gp.CalculateField_management RasterT_Kriging1_shp__2_, "x", "pPoint.X", "VB", "Dim Output As Double"

and says object required.

Following is my code to calculate the X:

Dim Output As Double

Dim pPoint As IPoint

Set pPoint = [Shape]

Output = pPoint.X

Can anyone help me?

Ok, following is my code:

Dim gp As Object
Dim shape As Object
Set gp = CreateObject("esriGeoprocessing.GPDispatch.1")

' Check out any necessary licenses
gp.CheckOutExtension "spatial"

' Load required toolboxes...
gp.AddToolbox "C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx"
gp.AddToolbox "C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx"
gp.AddToolbox "C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx"


' Local variables...
Temp = "C:\temp"
Kriging_poin4 = "C:\temp\New File Geodatabase.gdb\Kriging_poin4"
Output_variance_of_prediction_raster = ""
Dim Temp As String
Dim Kriging_poin1 As String
Dim Temp__2_ As String
Dim RasterT_Kriging1_shp__4_ As String
Dim RasterT_Kriging1_shp As String
Dim RasterT_Kriging1_shp__2_ As String
Dim RasterT_Kriging1_shp__6_ As String
Dim RasterT_Kriging1_shp__5_ As String
Dim point2_Events_shp As String
Temp = "C:\Temp"
Kriging_poin1 = "C:\Temp\Kriging_poin"
Output_variance_of_prediction_raster = ""
Temp__2_ = "C:\Temp"
RasterT_Kriging1_shp__4_ = "C:\Temp\RasterT_Kriging.shp"
RasterT_Kriging1_shp = "C:\Temp\RasterT_Kriging.shp"
RasterT_Kriging1_shp__2_ = "C:\Temp\RasterT_Kriging.shp"
RasterT_Kriging1_shp__6_ = "C:\Temp\RasterT_Kriging.shp"
RasterT_Kriging1_shp__5_ = "C:\Temp\RasterT_Kriging.shp"
point2_Events_shp = "C:\Temp\point2 Events.shp"

' Process: Feature Class To Shapefile (multiple)...
gp.FeatureClassToShapefile_conversion "'C:\temp\New File Geodatabase.gdb\point2'", Temp__2_

' Process: Kriging...
 gp.Kriging_sa point2_Events_shp, "x", Kriging_poin1, "Spherical 249.101224", "250",   "VARIABLE 12", Output_variance_of_prediction_raster

' Process: Raster to Point...
gp.RasterToPoint_conversion Kriging_poin1, RasterT_Kriging1_shp__4_, "Value"

' Process: Add Field...
 gp.AddField_management RasterT_Kriging1_shp__4_, "x", "DOUBLE", "12", "4", "", "", "NON_NULLABLE", "NON_REQUIRED", ""

' Process: Add Field (2)...
gp.AddField_management RasterT_Kriging1_shp, "y", "DOUBLE", "12", "4", "", "",   "NON_NULLABLE", "NON_REQUIRED", ""

' Process: Calculate Field...
gp.CalculateField_management RasterT_Kriging1_shp__2_, "x", "pPoint.X", "VB", "Dim Output As Double"



Dim pPoint As IPoint

Set pPoint = [shape]

Output = pPoint.X


' Process: Calculate Field (2)...
gp.CalculateField_management RasterT_Kriging1_shp__6_, "y", "pPoint.Y", "VB", "Dim    Output As Double"



Output = pPoint.Y


End Sub

I am following the steps you told me last time in

Using kriging with data in GDB

but it's still doesn't work!

share|improve this question
    
It looks like you are using ArcGIS 9.x. If you are open to an ArcGIS 10 and Python solution, I am happy to try and assist. –  PolyGeo Jun 15 '11 at 9:38
    
thank you for your Concern! I am using 9.3.1 ... –  blueman010112 Jun 15 '11 at 9:47
    
you may want to post your entire code. Did you Dim gp as Object, dim the input and output tool variables, and load the required toolboxes? –  artwork21 Jun 15 '11 at 11:13
add comment

3 Answers

up vote 1 down vote accepted

I always test the model in model builder first to see if it runs successfully or not before using the code. One possible issue is that your point shp has a x field that you did you Kriging analysis on then you tried to add another x field to do your field calculation on. Alternativly, you could just skip the two Add Field and Calculate tools with the Add XY coordinates tool. The code below worked on my end.

Dim gp As Object


' Create the Geoprocessor object
Set gp = CreateObject("esriGeoprocessing.GPDispatch.1")


' Check out any necessary licenses
gp.CheckOutExtension "spatial"


' Load required toolboxes...
gp.AddToolbox "C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx"
gp.AddToolbox "C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx"
gp.AddToolbox "C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx"



' Local variables...


Dim point2 As String
Dim Temp As String
Dim Temp__2_ As String
Dim Kriging_poin As String
Dim Output_variance_of_prediction_raster As String
Dim point2_shp As String
Dim RasterT_Kriging_shp As String
Dim kriging_poin__2_ As String
Dim Output_Features As String



point2 = "C:\Temp\New File Geodatabase.gdb\point2"
Temp = "C:\Temp"
Temp__2_ = "C:\Temp"
Kriging_poin = "C:\Temp\Kriging_poin"
Output_variance_of_prediction_raster = ""
point2_shp = "C:\Temp\point2.shp"
RasterT_Kriging_shp = "C:\Temp\RasterT_Kriging.shp"
kriging_poin__2_ = "C:\Temp\kriging_poin"
Output_Features = "C:\Temp\RasterT_Kriging.shp"


' Process: Feature Class To Shapefile (multiple)...
gp.FeatureClassToShapefile_conversion "'C:\Temp\New File Geodatabase.gdb\point2'", Temp__2_

' Process: Kriging...

gp.Kriging_sa point2_shp, "x", Kriging_poin, "Spherical 51.968338", "51.968338", "VARIABLE 12", Output_variance_of_prediction_raster


' Process: Raster to Point...
gp.RasterToPoint_conversion kriging_poin__2_, RasterT_Kriging_shp, "Value"


' Process: Add XY Coordinates...
gp.AddXY_management RasterT_Kriging_shp
share|improve this answer
    
Thank you for your support!! –  blueman010112 Jun 16 '11 at 3:32
    
Oh no....I can run it in Model Builder but I am stuck with VBA again.... ' Process: Add XY Coordinates... gp.AddXY_management Output_point_features same error at the Line –  blueman010112 Jun 16 '11 at 3:44
    
I fix the error myself thank you!! –  blueman010112 Jun 16 '11 at 6:37
add comment

I know there is an answer already, but I want to add the following link from the ARCGIS 9.3.1 HELP.

in this post there are solutions in VBA, and in PYTHON.

for anyone looking for this solution in the future, this is the solution to the question above in python:

# Calculate x and y centroid fields using the geometry property Centroid
import arcgisscripting, sys
gp = arcgisscripting.create()

inputFC = sys.argv[1]

gp.AddField_management(inputFC, "xCentroid", "DOUBLE", 18, 11)
gp.AddField_management(inputFC, "yCentroid", "DOUBLE", 18, 11)

# Centroid property returns a string with x and y separated by a space
xExpression = "float(!SHAPE.CENTROID!.split()[0])"
yExpression = "float(!SHAPE.CENTROID!.split()[1])"

gp.CalculateField_management(inputFC, "xCentroid", xExpression, "PYTHON")
gp.CalculateField_management(inputFC, "yCentroid", yExpression, "PYTHON")
share|improve this answer
add comment

ET Tools has "Easycalculate" that you can download (free), There are many functions in it. You should go there to get this...

'=========================  

'point_Get_X.cal
'Author: Ianko Tchoukanski
'http://www.ian-ko.com
'=========================
On Error Resume Next
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pPoint As IPoint
Dim dX As Double
Dim bSrefFromMap As Boolean
'=======================
'Adjust the parameter below
'True ==> the coordinates will be calculated in the projection of the Map
'False ==> the coordinates will be calculated in the projection of the data
bSrefFromMap = True
'=======================
If (Not IsNull([Shape])) Then
Set pPoint = [Shape]
If (bSrefFromMap) Then
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap
pPoint.Project pMap.SpatialReference
End If
dX = pPoint.X
else
dX = 0
End If

esri_field_calculator_splitter
dX

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.