获取ARCGIS中polygon的中心点坐标

 其实这个问题很早就遇到了 也没来得及总结 群里也有友友们在询问 这里集中解决一下啦
不妥之处 还请大家批评指正~~~

下面是arcgis help里的讲解(这段是别人整的 貌似有点问题 我先贴下 后面我提供了解决办法):
Adding the x,y coordinates of the centroid of a polygon layer to a new field
1. Optionally, start an edit session in ArcMap. Calculating a field is faster outside of an edit session, but

you won't be able to undo the calculation.
2. Open the attribute table of the layer of the layer you want to edit.
3. Right-click the field heading for the X field (if there is no X field you can add a new field by clicking

the options button and selecting the new field
option).
4. Click Calculate Values.
5. Check Advanced.
6. Type the following VBA statement in the first text box.
Dim dblX As DoubleDim pArea As IAreaSet pArea = [Shape]dblX = pArea.Centroid.X
7. Type the variable dblX in the text box directly under the X field name.
8. Click OK.
You can repeat the same process for updating a field with the Y coordinates for the centroid point of each

polygon in the layer.
? The property X returns a field type of double. For best results, your X field should also be a double field

type.

下面我简要的翻译下 不敢保证百分百正确哈 呵呵 其实这些小问题 大家也都能看得懂。。。。。。
在计算面的中心点坐标的时候 用的是字段计算器(当然了还有其他方法)
第一步 添加X Y字段 注意在对面图层添加字段的时候 图层不能是可编辑状态 也就是说Editor下面不要选择开始编辑状态即可
第二步 选择编辑 右键图层打开属性表
第三步 右键X字段表头 选择字段计算器
第四步 选中Advanced
第五步 编写VBA代码 :
Dim dblX As Double
Dim pArea As IArea
set pArea = [Shape]
dblX = pArea.Centroid.y

然后在下面一栏里直接填上“dblX” 点击OK 即可
为了取得更好的效果 建议在字段的声明类型与vba类型一致

下面我介绍另外一个方法 不用写VBA代码了 因为属性表里操作已经为大家准备好了
就是修改上面第三步的操作 不要选择字段计算器 选择计算几何要素(calculate Geometry)
然后在Property下拉列表框里选择相应的属性即可
显然这里默认的计算有面积 周长 中心坐标XY
其他操作就与上面的一样了 很明显这样的话不用书写VBA代码了 稍微简单一点 不过本人还是推荐大家用VBA 毕竟这样有助与

对AE的理解 对搞开发有帮助的 ~~~吼吼
对了 大家可以直接在HELP里查找 这里面对VBA代码说的也很详细的 我这里也贴出来一点供大家参考 不翻译了哈 很简单的
How to use Visual Basic code to calculate fields based on area, length, perimeter, etc.

You can use Visual Basic code in the Advanced box in the Field Calculator dialog to calculate fields using

geometric measurements. Although the easiest way
to perform these calculations is to use the Calculate Geometry dialog described above, there may be situations

where you want to use Visual Basic code to
perform these calculations in the Field Calculator instead. These examples show how to do this.

1. These code examples return a value of type 'double', so use them to calculate either an existing field of

type 'double' or a new field of type 'double'
you've added to the table.

2. Check Advanced. You'll see two empty text entry boxes.

3. Enter one of these four line code examples into the topmost box, the one labeled 'Pre-Logic VBA Script

Code'. Tip: you can select the code in this help
topic, right-click and choose Copy, and then paste it into the box.

To calculate area:

Dim Output as double

Dim pArea as Iarea

Set pArea = [shape]

Output = pArea.area

To calculate length or perimeter (depending on whether the features are lines or polygons):

Dim Output as double

Dim pCurve as ICurve

Set pCurve = [shape]

Output = pCurve.Length

To add the x coordinate of points:

Dim Output As Double

Dim pPoint As IPoint

Set pPoint = [Shape]

Output = pPoint.X

To add the x coordinate of polygon centroids:

Dim Output As Double

Dim pArea As IArea

Set pArea = [Shape]

Output = pArea.Centroid.X

4. Type the variable Output into the second text box. Do not enclose it in quotes or brackets.

5. Click OK

The units of the calculated values will be the units that your features are stored in, not the map units or

display units of the data frame you are currently
working with. So if your data is stored in feet, the calculated values will be in feet. If you want the

calculated data to be in different units than the
data's units, you could either add a conversion into the calculation expression, or (more simply) project your

data into a coordinate system that uses the
units you want the values to be in, and then perform the calculatio


最后我贴出来一些DESKTOP里常用的VBA代码吧 没有一一验证:
(下面代码转载自集思学院)

特点:
1推荐给不会使用AO的朋友
2可以保存为CAL文件以备下次方便使用

使用方法
1打开属性表,选择计算的字段,右点选择Calculate Values;
2.选择“是”,进入Field Calculator;
2选择Advance选项;
3 在Pre-Logic VBA Script Code编辑框中输入VBA代码;
4在下面编辑框中输入赋值部分.


1--点坐标X
VBA部分:
Dim pGeo As IGeometry
Set pGeo = [Shape]
Dim pPoint As IPoint
Set pPoint = pGeo
赋值部分:
pPoint.X

2--点坐标Y
VBA部分:
同上
赋值部分:
pPoint.Y

坐标值为文件存储的固有值,和是否使用On the Fly坐标表示无关。返回当前显示的坐标值参看8,9

3--多边形周长
VBA部分:
Dim pGeo As IGeometry
Set pGeo = [Shape]
Dim pPolygon As IPolygon
Set pPolygon = pGeo
赋值部分:
pPolygon.Length

4--多边形面积
VBA部分:
Dim pGeo As IGeometry
Set pGeo = [Shape]
Dim pPolygon As IPolygon
Set pPolygon = pGeo
Dim pArea As IArea
Set pArea = pPolygon
赋值部分:
pArea.Area

5--多边形重心X
VBA部分:
Dim pGeo As IGeometry
Set pGeo = [Shape]
Dim pPolygon As IPolygon
Set pPolygon = pGeo
Dim pArea As IArea
Set pArea = pPolygon
Dim pPoint As IPoint
Set pPoint = pArea.Centroid
赋值部分:
pPoint.X

6--多边形重心Y
VBA部分:
同上
赋值部分:
pPoint.Y

7--Polyline长度
VBA部分:
Dim pGeo As IGeometry
Set pGeo = [Shape]
Dim pPolyline As IPolyline
Set pPolyline = pGeo
Dim pCurve As IPolycurve
Set pCurve = pPolyline
赋值部分:
pCurve.Length

8--表示点坐标X
VBA部分:
Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pSpRef As ISpatialReference
Set pSpRef = pDoc.FocusMap.SpatialReference

Dim pClone As IClone
Set pClone = [Shape]
Dim pGeo As IGeometry
Set pGeo = pClone.Clone
Dim pPoint as IPoint
Set pPoint = pGeo

pGeo.Project pSpRef
赋值部分:
pPoint.X

9--表示点坐标Y
VBA部分:
同上
赋值部分:
pPoint.Y
......

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值