Ask A Cartographer

Centroid, Label Point, and LatLng Bounds

June 09 2009 | 2 comments

I'm working on the vector compliment for Tom Patterson's Natural Earth dataset and need to calculate and store onto each feature in my shapefile the:

a) Centroid XY (Long Lat)
b) Label Point XY (Long Lat), and
c) Bound Box MinX, MaxX, MinY, and MaxY (MinLong, MaxLong, MinLat, MaxLat).

These will make the files more useful for web mapping.

I can use Calculate Geometry in ArcMap to get each feature's Centroid XY, but there is no option for Bounding Box. I've seen a few web posts for a complicated procedure to determine the Label Point, is there an automated toolbox routine? Or VisualBasic code to use in the Field Calculator?


Mapping Center Answer:

a) Centroid XY (Long Lat)

This uses Calculate Geometry function ( You need to add two fields to the attribute table – one for the latitude coordinate and one for the longitude coordinate. Use a field of type double. In the Field Calculator dialog box, check Advanced. You'll see two empty text entry boxes. In the topmost box, the one labeled Pre-Logic VBA Script Code, copy and paste the code below (starting with "dim dbl…"). Then in the text box directly under the field name, type the variable "dblX" for the X-coordinate of the centroid or "dblY" for the X-coordinate of the centroid. Click OK.

For the X-coordinate of a polygon centroid:
Dim dblX As Double
Dim pArea As IArea
Set pArea = [Shape]
dblX = pArea.Centroid.X

For the Y-coordinate of a polygon centroid:
Dim dblY As Double
Dim pArea As IArea
Set pArea = [Shape]
dblY = pArea.Centroid.Y

b) Label Point XY (Long Lat)

I am not sure where you are getting this – if it came from a Workstation ArcInfo coverage, then the X and Y coordinates should already be there for you in the label point file. Maybe I am just not understanding this question. However, you could use geoprocessing ion annotation to figure out the x-y coodinates of a label but this requires the use of a geodatabase. 

c) Bound Box MinX, MaxX, MinY, and MaxY (MinLong, MaxLong, MinLat, MaxLat)

You would do this much like the you would the X-coordinate of the centroid of the polygon and the Y-coordinate of the polygon except you would use the following code in the Pre-Logic VBA Script Code box and the variables XMax, YMax, XMin, and YMin in the second box of the Advanced interface in the field calculator.

Dim dblX As Double
Dim pArea As IPolygon
Set pArea = [Shape]
coordval = pArea.Envelope.XMax (use YMax, XMin, and YMin in successive fields)

Just a quick side note - you really do have a lot of additional capability when you use a geodatabase, so you might give some serious thought to migrating from shapefiles to a file or personal geodatabase.

label point posted by nathaniel kelso on Jun 9 2009 7:36PM
About the label point:

Why do I need a "label point"? (1) The centroid calculated is not guaranteed to be inside the polygon while the label point is. I need to do point-in-polygon spatial joins to migrate attribute information from another data source, and (2) I actually want to know the best (optimal) point in the polygon or on my linear feature to place an automatic label (if one did not have maplex, eg result is returned out of non-spatial DB).

The origin of the shapefile features is Adobe Illustrator via MaPublisher so they do not have a label point built as part of their topology. Being that as it may, what is the easiest &/or best way to add one? Turn it into a coverage and build it via Catalog or is there a Toolbox command to run on the shapefile directly? Please describe geoprocessing ion annotation using geodatabase if that is the best approach.

To followup on your sidenote: The final files will be distributed in shapefile format but can be processed internally in geodatabase format.

Another sidenote: we plan on delivering two versions of the Centroid, Label Point, and LongLat bounds: full extent and optimal extent. So for the US the full extent would be almost the entire globe. While the optimal extent would be just the lower 48 states (sorry Alaska and Hawaii) thus excluding small U.S. administered territories overseas.
Additional items posted by Charlie Frye on Jun 10 2009 8:59AM
There are a few items here that I want to make sure are clear given how you expressed your expectations:

1. Shapefiles do not have topology. An editor can create data that is topologically correct and then save that data to a shapefile format. The result is a dataset that has it's topology implied because of it's editing/compilation process, but there is no topology written explicitly or in the form of rules to the shapefile format.

2. In the code above, you can subsitute the IArea.LabelPoint method for the IArea.Centroid method and get what you want.

3. You could also geoprocess your shapefile using the Feature to Point tool and check the "Inside" option when you run it. That will give you another dataset that you can add X and Y fields (type = DOUBLE or FLOAT) to then use the calculate Geometry tool to add the coordinate values. The last step would be to join that point dataset to the original polygon dataset and calculate new fields for X Y in the polygon dataset based on the joined point feature class's atttributes.

4. The centroid method given above gives you the centroid of the bounding envelope for each polygon.

5. In the first paragraph of the comment above you indicate wanting the "best" point within the polygon. A label point is not going to meet many potentially subjective measures for "best", but it will be inside, though often just barely so.

If you would like to post a comment, please login.

Contact Us | Legal | Privacy |