Arcgis' Python's Arcpy's point, line, and surface object creation processing and creation of geometric objects by reading latitude and longitude coordinates in txt through pandas

foreword

This section will introduce the creation and processing of point-line-area objects. There are three classes for creating objects, which are Point, Multipoint, PointGeometryand . The class for creating 线objects is Polyline, and the class for creating objects is Polygon.

1. Creation of point objects - Point

Point objects are often used in conjunction with cursors. Point features will return a single point object rather than an array of point objects. The other feature types (polygon, polyline, and multipoint) return an array of point objects, and when the features have multiple parts, an array of arrays of point objects.

**Points are not geometry classes, but are commonly used to construct geometry. ** That is, other geometric classes are constructed through point objects, whether it is a line or a surface. In fact, they are all constructed through points, which should be known to those who have studied gis.

insert image description here

insert image description here

# 创建点对象(经度,纬度,高程,路径测量值,ID)参数都是可选的
point = arcpy.Point(120.34, 31.34, 50)

print "Point properties:"
print "ID:{0}".format(point.ID)  # 不填ID则默认从0开始
print "X:{0}".format(point.X)
print "Y:{0}".format(point.Y)
print "Z:{0}".format(point.Z)
Point properties:
ID:0
X:120.34
Y:31.34
Z:50.0

Second, the creation of geometric single point objects - PointGeometry

Passing a Point to PointGeometry creates a geometric single point, which is the point that can be seen in arcgis.

insert image description here

insert image description here
insert image description here

Our following code is to create three geometric single-point objects through three Point objects, that is, the created geometric single-point objects can be viewed and displayed in arcgis. code show as below:

import arcpy

pointList = [[1, 2], [3, 5], [7, 3]]  # 坐标的列表对象

point = arcpy.Point()  # 创建一个空的point对象
pointGeometryList = []  # 用来存放pointGeometry对象

for pt in pointList:
    point.X = pt[0]
    point.Y = pt[1]
    pointGeometry = arcpy.PointGeometry(point)
    pointGeometryList.append(pointGeometry)

# 使用CopyFeatures_management工作生成要素类
print pointGeometryList
arcpy.CopyFeatures_management(pointGeometryList, r'E:\arcpy_study\我得学城\几何对象文件\几何点对象创建.shp')
print 'done!'

The feature class is shown in the figure:
insert image description here
insert image description here

3. Creation of geometric multipoint objects - Multipoint

A Multipoint object is simply an ordered collection of points. An array containing multiple Point objects can be passed to Multipoint as a parameter to create multiple geometric single points at one time.

insert image description here

insert image description here
insert image description here

The following code stores multiple Point objects in an array to create geometric multi-point objects:

import arcpy

pointList = [[1, 2], [3, 5], [7, 3]]  # 坐标的列表对象

# 用于存储多点对象
# pt = [<Point (1.0, 2.0, #, #)>, <Point (3.0, 5.0, #, #)>, <Point (7.0, 3.0, #, #)>]
# pt是包含三个点对象的列表
pt = [arcpy.Point(*coords) for coords in pointList]

# 将多点对象转换为MultiPoint能识别的地理数组
# g_pt = <geoprocessing array object object at 0x0714D200>
g_pt = arcpy.Array(pt)

# 使用array创建几何多点对象
mp = arcpy.Multipoint(g_pt)

arcpy.CopyFeatures_management(mp, r'E:\arcpy_study\我得学城\几何对象文件\几何多点对象创建.shp')
print 'done!'

The effect is as shown in the figure:
insert image description here
insert image description here

Fourth, the creation of line objects - Polyline

We use an array containing multiple Point objects to create a Polyline object, and the resulting polyline is formed by connecting these points.

insert image description here
insert image description here

The sample code is as follows:

import arcpy

# 三维数组,相当于两条折线,一个二维数组一条折线
feature_info = [[[1, 2], [2, 4], [3, 7]],
                [[6, 8], [5, 7], [7, 2], [9, 5]]]

# 用来存放线对象
features = []

for feature in feature_info:
    pt = [arcpy.Point(*coords) for coords in feature]
    print pt
    pl = arcpy.Polyline(arcpy.Array(pt))
    features.append(pl)

print features

arcpy.CopyFeatures_management(features, r'E:\arcpy_study\我得学城\几何对象文件\线对象创建.shp')
print 'done!'

The effect is as follows:
insert image description here

insert image description here

Five, the creation of surface objects - Polygon

We use an array containing multiple Point objects to create a Polygon object, and the resulting polyline is formed by connecting these points end to end.

insert image description here

insert image description here

Sample code:

import arcpy

# 三维数组,相当于两条折线,一个二维数组一条折线
feature_info = [[[1, 2], [2, 4], [3, 7]],
                [[6, 8], [5, 7], [7, 2], [9, 5]]]

# 用来存放线对象
features = []

for feature in feature_info:
    pt = [arcpy.Point(*coords) for coords in feature]
    print pt
    pl = arcpy.Polygon(arcpy.Array(pt))
    features.append(pl)

print features

arcpy.CopyFeatures_management(features, r'E:\arcpy_study\我得学城\几何对象文件\面对象创建.shp')
print 'done!'

The effect is as follows:
insert image description here

insert image description here

6. Read the coordinates in txt to create geometric objects

We use the pandas library to read the latitude and longitude coordinates in the txt file. The TXT file is as follows, and the file is named 经纬度.txt:

1 118.897901 32.099258
2 118.898200 32.099075
3 118.901593 32.096570
4 118.904799 32.094134
5 118.904801 32.094180

Note here, I am using Arcgis, and the python version that comes with it is 2.7, but if you install pandas directly, the latest pandas does not support python2, you can only lower the version, and lower the version of pandas to 0.23.4 and below OK.

code show as below:

视频教程地址:https://www.bilibili.com/video/BV1ab4y1h7dv/?p=8&spm_id_from=pageDriver&vd_source=5f425e0074a7f92921f53ab87712357b

Guess you like

Origin blog.csdn.net/qq_47188967/article/details/131548704