Skip to main content

渔网生成

语法

官方文档

import arcpy

# Process: 创建渔网
arcpy.CreateFishnet_management(
OUTPUT_FISH_SHP, # 包含由矩形单元组成的渔网的输出要素类
startXY, # 渔网的起始左上角坐标
Y 轴坐标, # 渔网生成的方向,起始坐标Y轴 + 10
NET_PIXEL_SPACING, # 渔网的宽度(默认正方形:25)
NET_PIXEL_SPACING, # 渔网的高度(默认正方形:25)
"", # 行数,如果设置了宽度和高度,不需要设置行列
"", # 列数,如果设置了宽度和高度,不需要设置行列
rightTop, # 渔网右上角坐标
"LABELS", # 指定是否在每个渔网单元中心创建包含标注点的点要素类,后缀名为 '{OUTPUT_FISH_SHP}_label.shp'
RIVER_RANGE_SHP, # 指定渔网的范围。可通过指定坐标或使用模板数据集来输入范围
"POLYLINE", # 确定输出渔网单元是折线要素还是面要素
)

使用示例

# -*- coding: utf-8 -*-
#
# @Author: CPS
# @email: 373704015@qq.com
# @Date: 2023-06-01 15:44:24.030528
# @Last Modified by: CPS
# @Last Modified time: 2023-06-01 15:44:24.031508
# @file_path "W:\CPS\MyProject\python-tools\gis-api\tools\工程文件导出后的py文件"
# @Filename "00.py"
# @Description: 功能描述
#

import os, sys
from os import path
import arcpy
from decimal import Decimal

reload(sys)
sys.setdefaultencoding("utf-8")


def get_shp_shap(shapefile_path, decimal_places=8):
"""
@Description 获取shp文件的左上角,右下角坐标

- param shapefile_path :{param} 要提取的shp文件绝对路径
- param decimal_places=8 :{int} 要保留的小数点,默认为6

@returns `{tuple[str]}` (xmin, ymin, xmax, ymax)

@example

```python
shp = "W:/CPS/MyProject/python-tools/gis-api/static/upload/mikeio/res/河道范围/河道范围.shp"
startX, startY, endX, endY = get_shp_shap(shp)

print("start: {0} {1}".format(startX, startY))
print("end: {0} {1}".format(endX, endY))
```

"""
# 使用 Describe 函数获取 Shapefile 的描述对象
desc = arcpy.Describe(shapefile_path)

# 获取 Shapefile 的扩展对象
ext = desc.extent

# 提取对角坐标,保留8位小数点
xmin = format(ext.XMin, ".{}f".format(decimal_places))
ymin = format(ext.YMin, ".{}f".format(decimal_places))
xmax = format(ext.XMax, ".{}f".format(decimal_places))
ymax = format(ext.YMax, ".{}f".format(decimal_places))

return (xmin, ymin, xmax, ymax)


def main():
# 像元间隔(一般100以内,默认25)
NET_PIXEL_SPACING = "25"

# 流向数据,支持gis认识的格式,最好使用.shp或者图层
WATER_DIR_DATA = ""

# 流向字段(角度),在数据文件中的流向字段,字段名,当前要求角度,dfsu文件默认弧度
WATER_DIR_FIELD = ""

# 河道【面】图层,用来明确网点裁剪后的区域
RIVER_RANGE_SHP = (
"W:/CPS/MyProject/python-tools/gis-api/static/upload/mikeio/res/河道范围/河道范围.shp"
)

# 图层文件
INPUT_SHP = (
"W:/CPS/MyProject/python-tools/gis-api/static/upload/mikeio/res/50年一遇_工程前.shp"
)

# 生成渔网点用来获取河道上的流向
filename, ext = path.splitext(path.basename(INPUT_SHP))
OUTPUT_FISH_SHP = "W:/CPS/MyProject/python-tools/gis-api/static/upload/mikeio/res/{}_Fishnet.shp".format(
filename
)
OUTPUT_FISH_SHP_POINT = "W:/CPS/MyProject/python-tools/gis-api/static/upload/mikeio/res/{}_Fishnet_label.shp".format(
filename
)

# 获取shp文件的左上角和右下角确定渔网范围
startX, startY, endX, endY = get_shp_shap(RIVER_RANGE_SHP)
startXY = "{} {}".format(startX, startY)
y_orient_coord = "{} {}".format(
startX, Decimal(startY).quantize(Decimal("0.00000000")) + 10
) # 使用Y坐标+10来确定方向
rightTop = "{} {}".format(endX, endY)

print(startX, startY, endX, endY)
print("其实原点: ", startXY)
print("生成方向: ", y_orient_coord)
print("右上角坐标: ", rightTop)

# Process: 创建渔网
arcpy.CreateFishnet_management(
OUTPUT_FISH_SHP, # 包含由矩形单元组成的渔网的输出要素类
startXY, # 渔网的起始左上角坐标
y_orient_coord, # 渔网的起始右下角坐标
NET_PIXEL_SPACING, # 渔网的宽度(默认正方形:25)
NET_PIXEL_SPACING, # 渔网的高度(默认正方形:25)
"", # 行数,如果设置了宽度和高度,不需要设置行列
"", # 列数,如果设置了宽度和高度,不需要设置行列
rightTop, # 渔网右上角坐标
"LABELS", # 指定是否在每个渔网单元中心创建包含标注点的点要素类
RIVER_RANGE_SHP, # 指定渔网的范围。可通过指定坐标或使用模板数据集来输入范围
"POLYLINE", # 确定输出渔网单元是折线要素还是面要素
)

# Process: 点裁剪
# arcpy.Clip_analysis(OUTPUT_FISH_SHP_POINT, RIVER_RANGE_SHP, v_OUTPUT_NAME__clip, "")


main()