# 3.2.几何分析

## 前言

阅读ArcGIS官网关于Geometry的[文档](https://desktop.arcgis.com/zh-cn/arcmap/10.3/analyze/arcpy-classes/geometry.htm)，我们可以看到其有许多种方法，比如`buffer(distance)`、`contains(second_geometry)`、`disjoint(second_geometry)`等，其中很多是用来进行几何分析的，这些用来进行几何分析的方法，大多都带有图解。这些基础的几何空间分析方法，是用来进行各种空间分析的基础。使用这些方法，我们还可以用来实现自定义的空间分析。

以touches方法为例。touches中文直译为接触，顾名思义，touches方法是用来判断两个几何的边界是否相互接触的，其返回值是布尔值。注意，当且仅当两个几何相接触时才返回True。如果两个几何相交或重叠、包含，都将返回False。其返回**True**的图解如图1所示。

![图 1 (图源自ArcGIS官网)](/files/-M857cSX_32ZpKR7UVZ-)

其中，对于`geom1.touches(geom2)`而言，基础几何表示的是`geom1`，比较几何表示的是`geom2`，这对于ArcPy几何的所有分析方法都是适用的。

## 实例

我们先构建以下几个几何，分别是

点：\[100,30]

线：\[100,30]、\[110,30]

多边形1：\[90,30]、\[100,30]、\[100,20]、\[90,20]

多边形2：\[95,25]、\[105,25]、\[105,15]、\[95,15]

其示意图如图2所示。

![图 2](/files/-M857nGAcRiTKyw96MVW)

上述的四个几何对应的ArcPy代码如下：

```
pt = arcpy.Point(100, 30)
line = arcpy.Polyline(arcpy.Array([arcpy.Point(100, 30), arcpy.Point(10, 30)]))
polygon1 = arcpy.Polygon(arcpy.Array([arcpy.Point(90, 30), arcpy.Point(100, 30),
                                      arcpy.Point(100, 20), arcpy.Point(90, 20)]))
polygon2 = arcpy.Polygon(arcpy.Array([arcpy.Point(95, 25), arcpy.Point(105, 25),
                                      arcpy.Point(105, 15), arcpy.Point(95, 15)]))
```

我们使用`print`语句在控制台上输出两者相互touches的结果，其后面的注释为控制台上应该输出的内容。

```
print(u'pt——line')
print(pt.touches(line))  # True
print(line.touches(pt))  # True
​
print(u'pt——polygon1')
print(pt.touches(polygon1))  # False
print(polygon1.touches(pt))  # True
​
print(u'pt——polygon2')
print(pt.touches(polygon2))  # False
print(polygon2.touches(pt))  # False
​
print(u'line——polygon1')
print(line.touches(polygon1))  # True
print(polygon1.touches(line))  # True
​
print(u'line——polygon2')
print(line.touches(polygon2))  # False
print(polygon2.touches(line))  # False
​
print(u'polygon1——polygon2')
print(polygon1.touches(polygon2))  # False
print(polygon2.touches(polygon1))  # False
```

请注意，我们这里交换了所谓的基础几何与比较几何的位置，请看点pt与多边形polygon1的结果。按照我们的思维，touches的结果在交换了基础几何与比较几何的位置后应当是一致的，但是，点与多边形的输出结果是相反的，这是为何呢？因为这里的line、polygon1和polygon是几何，但是**pt不是一个几何**！在前面“shapefile的新建与要素插入”中有提到，ArcPy中点要素的几何创建的正确规范应该使用的是`arcpy.PointGeometry(arcpy.Point(x,y))`，`arcpy.Point(x,y)`仅仅是用来创建一个几何中的节点（或者是称之为折点）的。如果将pt的声明改为`pt = arcpy.PointGeometry(arcpy.Point(100, 30))`，那么`pt.touches(polygon1)`和`polygon1.touches(pt)`的输出结果都将会为`True`。

那为什么pt也能使用touches方法呢？因为通过`arcpy.Point(x,y)`创建的Point对象同样具有touches方法。尽管我们在Point的[文档](https://desktop.arcgis.com/zh-cn/arcmap/10.3/analyze/arcpy-classes/point.htm)中可以看到其文档说明与图解和Geometry下是完全一致的，但是其结果并不相同。这是我们在使用ArcPy中需要注意的，一定要声明一个点几何，最好是使用`arcpy.PointGeometry`而不是`arcpy.Point`。

更多的几何分析方法的使用，请自行参考官方文档。


---

# 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.2.-ji-he-fen-xi.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.
