# 3.1.量测

## 前言

对于几何信息，其长度或面积是一个非常重要的属性。对于在ArcPy中获取线几何的长度或者面几何的面积可以通过使用lengh/getLength()和area/getArea()来实现。

## 长度

`lengh`是所有几何的基属性，不管是点还是线、面都有，但是只有线状要素能够正确获取其长度信息。lengh的属性值是以当前要素类文件的单位为基础的，因此如果我们使用的是地理坐标系，那么其返回的结果就是度。

这一般不是我们想要的结果，我们可能更希望获取的长度是以米或千米为单位的。这有两种解决方案，一是给其添加一个投影信息；二是使用`getLength`方法。`getLength`方法可以选择测度的类型与单位，其定义如下：

`getLength ({measurement_type}, {units})`

| 参数                | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | 数据类型   |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ |
| measurement\_type | PLANAR 测量值反映出的是地理数据在 2D 表面上的投影（也就是说，平面测量不考虑地球的曲率）。如有必要，可选择 GEODESIC、GREAT\_ELLIPTIC、LOXODROME 以及 PRESERVE\_SHAPE 测量类型作为替代类型。GEODESIC —椭球体（椭圆体）地球表面上任意两点间长度最短的线。要确定两城市间最短的飞机飞行路径，便会用到大地测量线。如果基于一个球体而非一个椭圆体，则这种线又称为大圆线。GREAT\_ELLIPTIC —椭球体（椭圆体）上的线，定义为通过椭球体中心以及某线段的起点和终点的平面与椭球体相交产生的线。当使用球体时，此形状又称为大圆。LOXODROME —斜航线并非两点之间的最短距离，而是定义固定方位或方向的线。大圆路径经常被分解成一系列斜航线，以简化导航过程。它又称为等角航线 (rhumb line)。PLANAR —平面测量使用 2D 笛卡尔算法计算长度与面积。此选项仅适用于在投影坐标系中执行测量，并且该坐标系的 2D 平面将成为测量的基础。PRESERVE\_SHAPE —该类型可计算地球椭圆体表面几何的面积或长度，其中几何已在投影或地理坐标系中定义。此选项保留了几何在坐标系中的形状。(默认值为 GEODESIC) | String |
| units             | 计算长度时将采用的单位。CENTIMETERS —厘米，DECIMETERS —公寸，FEET —英尺，INCHES —英寸，KILOMETERS —千米，METERS —米，MILES —英里，MILLIMETERS —毫米，NAUTICALMILES —海里，YARDS —码。                                                                                                                                                                                                                                                                                                                                                                                                                 | String |

*引用自：*[*https://desktop.arcgis.com/zh-cn/arcmap/10.3/analyze/arcpy-classes/geometry.htm*](https://desktop.arcgis.com/zh-cn/arcmap/10.3/analyze/arcpy-classes/geometry.htm)

一般而言，其测量类型我们都是GEODESIC 即可，units默认是与投影坐标系的单位一致的，而我们可以看到units选项中是没有度这个单位的，因此对于只有地理坐标系的数据，其返回值是以米为单位的。

来看一段代码：

```
def calc_length():
    shp_file_path = ur'./data/ReadData/polyline.shp'
    length_total = 0
    get_length_total = 0
    with arcpy.da.SearchCursor(shp_file_path, ['Shape@']) as cursor:
        for row in cursor:
            geometry = row[0]
            length_total += geometry.length
            get_length_total += geometry.getLength()
​
    print(u"总长度：")
    print("length_total:" + str(length_total))
    print("get_length_total:" + str(get_length_total))
```

**polyline.shp**的地理坐标系是WGS84，不具备投影坐标系，因此其在控制台的输出结果如下：

```
总长度：
length_total:62.0501943117
get_length_total:4206322.33691
```

对于多线几何，其整体被视为一个几何，因此通过lengh/getLength()得到就是几何中各个部件的长度之和。

请注意，对于两个点，如果直接使用公式：

计算的结果并不是正确的答案，两者之间存在有一定的误差。这时候，我们可以将其构建为一个线几何，然后再对其长度进行求解。

## 面积

面积与长度相似，在几何基类属性上，其存在有`area`属性和`getArea`方法，但是只有面几何才能获取面积，其他几何获取的结果将为空。与同样`length`/`getLength`相似，`area`的单位是以当前要素类文件的单位为基础的，而`getArea`则是可以进行选择的，其与`getLength`具有两个相同意义的可选参数：

```
getArea ({measurement_type}, {units})
```

其中measurement\_type与`getLength`中的measurement\_type是一样的，但是`units`参数不同，毕竟面积单位是长度单位的平方。单位的其可选值为：ACRES、ARES、HECTARES、SQUARECENTIMETERS、SQUAREDECIMETERS、SQUAREINCHES、SQUAREFEET、SQUAREKILOMETERS、SQUAREMETERS、SQUAREMILES、SQUAREMILLIMETERS、SQUAREYARDS。当然，默认的返回值也变为了平方米。

示例：

```
def calc_area():
    shp_file_path = ur'./data/ReadData/polygon.shp'
    area_total = 0
    get_area_total = 0
    with arcpy.da.SearchCursor(shp_file_path, ['Shape@']) as cursor:
        for row in cursor:
            geometry = row[0]
            area_total += geometry.area
            get_area_total += geometry.getArea()
​
    print(u"总面积：")
    print("area_total:" + str(area_total))
    print("get_area_total:" + str(get_area_total))
```

polygon.shp的地理坐标系是WGS84，不具备投影坐标系，因此其在控制台的输出结果如下：

```
总面积：
area_total:7480.55653785
get_area_total:6.51956142652e+13
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zxyao.gitbook.io/arcpy/3.-ji-he-cao-zuo/3.1.-liang-ce.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
