VBA Script for calculating a polyline's X,Y coordinate of centroid

3167
6
07-02-2010 08:20 AM
DarrenMurphy1
New Contributor
To: The ESRI Community at Large
From: Darren Murphy
Date: 7/2/2010
Subject:VB Script for calculating a polyline's X,Y coordinate of centroid

Thanks in advance for all of the help.

I'm creating a model using ESRI's ModelBuilder, and I'm trying to calculate the X,Y coordinate of Centroid for a polyline feature class within the "Calculate Field" systems tool environment.

I realize that you can do it by right-clicking the field of interest and selecting the X,Y Coordinate of Centroid. However, I would like to able to automate this process when I run the model.

I would prefer to use VBA but I'm open to VBScript or Python.

Thanks,
Darren Murphy
GIS Analyst
District of Columbia Government
Office of the Chief Technology Officer
Jefferson Middle School
801 7th Street, SW
Washington, DC  20024
Phone: 202-727-6421
eFax: 202-727-7922
Email: darren.murphy@dc.gov
Website: http://dcgis.dc.gov
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus
0 Kudos
DaleHoneycutt
Occasional Contributor III
You can also use the Feature to Point tool.  This will create a point feature class of the line centroids.  If you need access to the centroid coordinates, you can use the Add XY Coordinates tool and then use Join Field to join the centroid coordinates back to the original line features.  When using the Feature To Point tool, be sure to check the "Inside" option!
0 Kudos
CynthiaGaines
New Contributor
It used to be really east to find sample VBA code for this by searching the desktop help, but I was just looking for this and couldn't find it anywhere...until I clicked on the "Help" button right in the Field Calculator tool (at least in 9.3).  This is from that help document.

Dim Output As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
Output = pPoint.X
0 Kudos
FabianHeredia
New Contributor II
Calida Calida!!!!
0 Kudos
DarrenMurphy1
New Contributor
Hello Mr. Honeycutt,

Unfortunately, the "Feature to Point" tool doesn't give me the exact midpoint (polyline, with geometry).  Like I said in my previous email, I'm looking for a script (preferably VBA) that I can use in ModelBuilder's "Calculate Field" System Tool under the Data Management toolbox.

I need to able to perform a spatial join on another point feature class (city block) to make any appropriate updates. Therefore, utmost precision is needed.

Therefore, I need to the same functionality of the ArcInfo's (Ver. 9.3.1) Calculate Geometry, X,Y Coordinate of Centroid function. I need this script for my Geoprocessing model.

Thanks,
Darren Murphy
GIS Analyst
District of Columbia Government
Office of the Chief Technology Officer
Jefferson Middle School
801 7th Street, SW
Washington, DC 20024
Phone: 202-727-6421
eFax: 202-727-7922
Email: darren.murphy@dc.gov
Website: http://dcgis.dc.gov
0 Kudos
RichardFairhurst
MVP Honored Contributor
If you are relying on the field calculator in ModelBuilder you should not use VBA, since there is no support for VBA in the field calculator once you upgrade to ArcGIS 10 (or beyond) and VB Script at ArcGIS 10 cannot do geometry calculations.  You should use Python (ModelBuilder supports Python in the Field Calculator, at least by 9.3).  That way you can not only use the code in ModelBuilder, but also in a python script (which can then be run in task scheduler) and it should port to future versions of ArcGIS.

The two expressions below should get you the Centroid X and Y coordinates in Python with the Field Calculator.

float(!SHAPE.CENTROID!.split()[0])

float(!SHAPE.CENTROID!.split()[1])
0 Kudos