前言
应用场景未知
思路
- 通过范围创建渔网–arcpy.CreateFishnet_management(一堆参数)
- 筛选渔网和点相交的渔网
- 导出所选的渔网
代码
# encoding: utf-8
import arcpy
from arcpy import env
environment = u'D:/学习/提取小方格/data.gdb'
env.workspace = environment
# 思路:
# 读取range创建格网100m*100m
# 读取点判断所处格网位置提取所处位置的格网
# get input spatialReference
dataset = 'range'
env.outputCoordinateSystem = arcpy.Describe(dataset).spatialReference
print '--------------获取创建渔网参数-----------------'
outFeatureClass = "fishnet100by100"
# 渔网的起始枢轴点
extent = arcpy.Describe(dataset).extent
originCoordinate = str(extent.XMin) + ' ' + str(extent.YMin)
# Y 轴坐标用于定向渔网。按照原点坐标与 y 轴坐标的连线所定义的角度旋转渔网。
yAxisCoordinate = str(extent.XMin) + ' ' + str(extent.YMin + 10)
# 创建渔网的宽高
cellSizeWidth = '100'
cellSizeHeight = '100'
# 创建渔网的行列数量--用来宽高可以不给
numRows = ''
numColumns = ''
# 由 X 坐标和 Y 坐标值设置的渔网的对角--就是xy最大值
oppositeCoorner = str(extent.XMax) + ' ' + str(extent.YMax)
# 是否要创建点
labels = 'NO_LABELS'
# 范围模板
templateExtent = dataset
# 渔网类型为面(还有线)
geometryType = 'POLYGON'
print '输出结果名称:{0} \n 输出渔网边长:{1}'.format(outFeatureClass, cellSizeHeight)
print '-------------开始创建渔网----------------'
arcpy.CreateFishnet_management(outFeatureClass, originCoordinate, yAxisCoordinate, cellSizeWidth, cellSizeHeight, numRows, numColumns, oppositeCoorner, labels, templateExtent, geometryType)
# 获取环境中的点要素
point = 'point'
selectFeature = 'fishnet100by100_lyr'
# 根据点要素和渔网进行空间选择
print '选择点所在的格网'
arcpy.MakeFeatureLayer_management('fishnet100by100', selectFeature)
arcpy.SelectLayerByLocation_management(selectFeature, 'intersect', point)
# 导出所选要素
result = 'result'
matchcount = int(arcpy.GetCount_management(selectFeature)[0])
if matchcount == 0:
print('no features matched spatial and attribute criteria')
else:
print '导出要素'
arcpy.CopyFeatures_management(selectFeature, result)
print 'success!!!'
参考:arcpy官方文档
最后
重写:不准用工具