前言
前文写了个用arcgis工具箱来实现的创建,然后现在要重写,不用工具箱
思路
这下肯定思路就完全不一样了,要自己写,先梳理一下,要想创建一个格网需要的条件,查了一下arcpy创建格网的代码—官方文档给来的demo,从demo中可以看出,需要一个面的点集,数组的格式(三维数组):
其中最外面包裹起来的就是一个图层数据,其次第二层是一个面要素,最里面一层是面要素的拐点,那么我们现在需要的就是通过已知的点坐标来组合成这种数组。
import arcpy
# A list of features and coordinate pairs
feature_info = [
[[1, 2], [2, 4], [3, 7]],
[[6, 8], [5, 7], [7, 2], [9, 5]]
]
# A list that will hold each of the Polygon objects
features = []
for feature in feature_info:
# Create a Polygon object based on the array of points
# Append to the list of Polygon objects
features.append(
arcpy.Polygon(
arcpy.Array([arcpy.Point(*coords) for coords in feature])))
# Persist a copy of the Polyline objects using CopyFeatures
arcpy.CopyFeatures_management(features, "c:/geometry/polygons.shp")
有了创建面的方法,接下就是实现思路了:
- 算点对应的小方格面的拐点坐标:
1.1 小方格四个拐点,四个坐标,分别对应就是
UpperRight–右上(Xmax-Xp)%100+Xp,(Ymax-Yp)%100+Yp
可以推出其他三个点
UpperLeft–左上
LowerRight–右下
LowerLeft 0–左下
- 坐标算出来就将坐标做成创建面要素的数据格式
- 创建面要素
代码
# encoding: utf8
import arcpy
from arcpy import env
# 创建小方格
# coordinates: 小方格的点集坐标值
# outfile: 小方格图层的名称
def createpolygon(coordinates, outfile):
# 空间参考
outSR = arcpy.SpatialReference(3857)
features = []
for feature in coordinates:
# Create a Polygon object based on the array of points
# Append to the list of Polygon objects
features.append(
arcpy.Polygon(
arcpy.Array([arcpy.Point(*coords) for coords in feature])))
# 保存要素到工作空间
arcpy.CopyFeatures_management(features, outfile)
# 定义投影
arcpy.DefineProjection_management(outfile, outSR)
# --------------------------------
# 工作空间
environment = u'D:/学习/提取小方格/data.gdb'
env.workspace = environment
outfile = "envelope"
dataset = 'range'
extent = arcpy.Describe(dataset).extent
# 获取范围要素的坐标值
# Xmin, Ymin, Xmax, Ymax
Xmax = extent.XMax
Ymax = extent.YMax
# 读取点图层中的点要素获取坐标
pointfc = 'point'
coors = []
with arcpy.da.SearchCursor(pointfc, 'SHAPE@XY') as cursor:
for row in cursor:
UpperLeft = [(Xmax - row[0][0]) % 100 + row[0][0] - 100, (Ymax - row[0][1]) % 100 + row[0][1]]
UpperRight = [(Xmax - row[0][0]) % 100 + row[0][0], (Ymax - row[0][1]) % 100 + row[0][1]]
LowerRight = [(Xmax - row[0][0]) % 100 + row[0][0] - 100, (Ymax - row[0][1]) % 100 + row[0][1] - 100]
LowerLeft = [(Xmax - row[0][0]) % 100 + row[0][0], (Ymax - row[0][1]) % 100 + row[0][1] - 100]
coor = [UpperLeft, UpperRight, LowerLeft, LowerRight]
coors.append(coor)
print coors
# 样本数据
# coor = [[[1.1, 2.1], [2.1, 4.1], [3.1, 7.1]],
# [[6.2, 8.2], [5.2, 7.2], [7.2, 2.2], [9.2, 5.2]]]
createpolygon(coors, outfile)
最后
用地理坐标试一下呢?
用arcpy工具实现的:https://blog.csdn.net/Zzzpiu/article/details/118637469