利用arcpy在arcgis中创建渔网工具

本次学习,我们在arcgis中利用arcpy来创建渔网工具,其中利用到CreateFishnet_management函数,该函数有多个参数。可以参考一下arcgis的帮助文档。

http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#//00170000002q000000http://resources.arcgis.com/zh-cn/help/main/10.1/index.html#//00170000002q000000

CreateFishnet_management (out_feature_class, origin_coord, y_axis_coord, cell_width, cell_height, number_rows, number_columns, {corner_coord}, {labels}, {template}, {geometry_type})

参数 说明 数据类型

out_feature_class

包含由矩形单元组成的渔网的输出要素类。

Feature Class

origin_coord

渔网的起始枢轴点。

Point

y_axis_coord

Y 轴坐标用于定向渔网。按照原点坐标与 y 轴坐标的连线所定义的角度旋转渔网。

Point

cell_width

确定每个单元的宽度。如果要使用行数参数值自动计算宽度,则将该值设置为零,这样在运行工具时便会计算宽度。

Double

cell_height

确定每个单元的高度。如果要使用列数参数值自动计算高度,则将该值设置为零,这样在运行工具时便会计算高度。

Double

number_rows

确定渔网所含的行数。如果要使用单元宽度参数值自动计算行数,则将该值设置为零,这样在运行工具时便会计算行数。

Long

number_columns

确定渔网所含的列数。如果要使用单元高度参数值自动计算列数,则将该值设置为零,这样在运行工具时便会计算列数。

Long

corner_coord

(可选)

由 X 坐标和 Y 坐标值设置的渔网的对角。

Point

labels

(可选)

指定是否在每个渔网单元中心创建包含标注点的点要素类。

  • LABELS —创建带标注点的新要素类。这是默认设置。
  • NO_LABELS —不创建标注点要素类。
Boolean

template

(可选)

指定渔网的范围。可通过指定坐标或使用模板数据集来输入范围。

  • 左 - X 最小值
  • 右 - X 最大值
  • 下 - Y 最小值
  • 上 - Y 最大值
Extent

geometry_type

(可选)

确定输出渔网单元是折线要素还是面要素。

  • POLYLINE —输出是折线要素类。每个单元都由四个线要素定义。
  • POLYGON —输出是面要素类。每个单元都由一个面要素定义。

而在本次实现的代码中,我们设置的参数分别为,输出要素为"fishnet"、起始的原点为坐下拐点,确定渔网的方向使用的是对坐下角拐点Y值加10,接着就是渔网的宽度和高度了。由于开发的工具中使用的是角度,所以需要将角度转为弧度。接下来参数为渔网的对角,这里使用的是图框可视范围的右上角拐点。渔网组成单元,这里是polyline.即纵横的经纬网组成。

同时这里针对经纬线判断情况,在创建的字段LINE中,根据线的起点和终点坐标值的x、y值来填入是经线,还是纬线。最后将线类型输入到LINE字段中,具体实现代码如下。

#coding=utf-8
import arcpy
import sys
import traceback

def dms_to_dd(deg, mins, secs):
    """Convert degrees, minutes and seconds into decimal degrees

    Keyword arguments:
    deg     -- Degrees
    mins    -- minutes
    secs    -- seconds
    """
    return deg + (mins / 60.0) + (secs / 3600.0)

def func(degrees,minutes,seconds,out_feature_class):
   try:
    arcpy.env.workspace = r'in_memory'
    arcpy.env.scratchWorkspace = r'in_memory'
    arcpy.env.overwriteOutput = True

    degrees = int(degrees)
    minutes = int(minutes)
    seconds = int(seconds)
    #out_feature_class = arcpy.GetParameterAsText(3)  # Output feature class

    mxd = arcpy.mapping.MapDocument("CURRENT")
    data_frame = mxd.activeDataFrame
    sr = data_frame.spatialReference
    arcpy.env.outputCoordinateSystem = sr
    extent = data_frame.extent
    lower_left = extent.lowerLeft
    upper_right = extent.upperRight

    interval_param = dms_to_dd(degrees, minutes, seconds)
    arcpy.AddMessage("范围计算完成.")

    #构建渔网坐标值,使用Round函数,使数值落到整经度和纬度上
    origin_coord = str(round(lower_left.X)) + ' ' + str(round(lower_left.Y))
    corner_coord = str(round(upper_right.X)) + ' ' + str(round(upper_right.Y))
    orient_coord = str(round(lower_left.X)) + ' ' + str(round(lower_left.Y + 10))  # Add 10 to Y for orientation
    fishnet = arcpy.CreateFishnet_management("fishnet", origin_coord, orient_coord, interval_param, interval_param,
                                             corner_coord=corner_coord, geometry_type="POLYLINE")
    arcpy.AddMessage("渔网创建完成.")

    #创建横竖线和刻度值字段
    arcpy.AddField_management(fishnet, "LINE", "TEXT", field_length=20)
    arcpy.AddField_management(fishnet, "DMS", "TEXT", field_length=50)
    arcpy.AddMessage("横竖线和刻度值字段创建完成.")

    line_codeblock ="""def line_orientation(line):
        firstX = line.firstPoint.X
        firstY = line.firstPoint.Y
        lastX = line.lastPoint.X
        lastY = line.lastPoint.Y
        if abs(firstX - lastX) < abs(firstY - lastY):
            return "Longitude"
        else:
            return "Latitude"
            """



    arcpy.CalculateField_management(fishnet, "LINE", "line_orientation (!Shape!)",
                                    "PYTHON_9.3", line_codeblock)
    arcpy.AddMessage("line_codeblock创建完成.")


    arcpy.AddMessage("Fields calculated.")


    densify_distance = interval_param/3.00
    arcpy.Densify_edit(fishnet, 'DISTANCE', densify_distance)
    arcpy.AddMessage("Densify complete.")
    arcpy.CopyFeatures_management(fishnet, out_feature_class)


   except:
       tb = sys.exc_info()[2]
       tbinfo = traceback.format_tb(tb)[0]

       pymsg = ('PYTHON ERRORS:\nTraceback info:\n' + tbinfo + '\nError Info: \n'
                + str(sys.exc_info()[1]))
       msgs = 'ArcPy ERRORS:\n' + arcpy.GetMessage(2) + '\n'
       arcpy.AddError(msgs)
       print pymsg
   finally:
       arcpy.Delete_management('in_memory')

根据之前写的博客,我们可以将代码打包成如下的工具。

最后,在arcgis desktop中使用后,生成的渔网shapefile文件如下所示。其中bou2_4p4为创建的结果。


                                                                           更多内容,请关注公众号

                                                                  

猜你喜欢

转载自blog.csdn.net/u010608964/article/details/87110946
今日推荐