(四)arcpy开发&利用arcpy实现arcgis中字段自动编号(pycharm导入arcpy站点包,字段创建、更新与写值)

一、问题来源

今天看到群里有一个小伙伴,要实现这样的一个功能,来看一下他的提问:

问下各位大神,如果图层里面有2000个小班,我需要将这2000小班在属性表里面编号依次为1 2 3 4……1998 1999 2000该怎么操作呢 。

于是下面有人说可以对字段的FID操作,FID+1,或者编写一个小工具。

二、Pycharm导入arcpy站点包

于是这里尝试着用python写一个工具。由于现在我是在新的电脑上开发,很多软件是新安装的,我这里使用的pycharm,需要将arcpy站点包导入到pycharm中,于是问题就来了。首先如下图所示,在写import arcpy找到不到站点包。于是网上找了相关资料,来解决这个问题。

试了多种方法,依旧如下图所示,没有将arcpy导入到External Libraries。

于是后面在创建新的python工程时,选择了Project interpreter下面有两个选择,可以用来创建虚拟环境,如果使用New environment using那么必须勾选上inherit global site-packges,下面的Make available to all projects可以选择性勾选,但建议大家最后一次性勾选,后面新建的工程就不用勾选了,然后在Location 设置相应的名字,以及Base interpreter中设置我们在安装arcgis desktop中的python路径。而如果选择的是黄色框内,则直接选择arcgis desktop的python路径即可。

这样新建的工程,我们在External Libraries中就可以看到arcpy站点包了,这个包将用于我们的工具开发

三、程序编写

按照要求,我们只需要创建一个编号(BH)的字段,然后读取FID,再将FID+1,所获得该值填入到BH字段中,说白了就是更新一下字段,就完事了。来看一下代码,是不是非常简单,几句话的事。

实现代码:


# coding:utf-8
import arcpy




def excute(shapePath):

   arcpy.AddField_management(shapePath, "BH", "TEXT", field_length=25)
   with   arcpy.da.UpdateCursor(shapePath, ["FID",'BH']) as cursor:
       for row in cursor:
           fid=row[0]
           row[1]=fid+1
           cursor.updateRow(row)
       del cursor

打包代码:

# coding:gbk
import arcpy

from OrderByID import excute


class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [Tool]


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "自动编号(测绘科技)"
        self.description = "自动编号(测绘科技)"
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        shapePath = arcpy.Parameter(
            displayName="需要修改的shapefile数据",
            name="shapePath",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input"
        )

        params = [shapePath]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        shapePath = parameters[0].valueAsText
        excute(shapePath)
        return

最后做出工具如下图所示。

将数据添加到选择框,运行即可。

来看一下最终结果,下图是处理之前的数据。

处理后的数据处理后的数据

至此,这个小功能就实现完了,当然也可以不用写代码那么麻烦,只不过这对于写程序来说是一个不小锻炼。


                                                                    更多内容,请关注公众号

                                                           

 

转载自:https://blog.csdn.net/u010608964/article/details/83445199

You may also like...