(十五)arcpy开发&利用arcpy将csv点数据转shapefile

本次我们来学习一下在arcpy中如何将.csv格式的数据转为shapefile。其中csv为点数据,这些点数据都是如下图所示。

现在利用arcpy首先要定义要投影,然后创建好字段。在遍历.csv文件。将数据填入到shapefile文件中。具体的实现代码如下图所示。

import arcpy, sys

try:

    path = "C:\\Users\\qin\\Desktop\\111"

    outputname = "polygontest.shp"
    spatRef = arcpy.SpatialReference(4326)

    createFC=arcpy.CreateFeatureclass_management(path, outputname, "POINT", "","","", spatRef)


    arcpy.AddField_management(path+"\\"+outputname, "NAME", "TEXT", 50)

    iflds = ["NAME","SHAPE@XY"]

    #iCur = arcpy.da.InsertCursor(createFC, iflds)
    iCur = arcpy.da.InsertCursor(path+"\\"+outputname, iflds)

    count = 1

    for ln in open(r"C:\Users\qin\Desktop\111\22.csv", 'r').readlines():
        lnstrip = ln.strip()
        if count > 1:
           dataList = ln.split(",")
           lat = dataList[1]

           lon = dataList[2]
           id = dataList[0]

           ivals = [id,(float(lon), float(lat)) ]
           iCur.insertRow(ivals)
        count += 1
    del iCur
except EnvironmentError as e:
    tbE = sys.exc_info()[2]
    print("Failed at Line {}\n".format(tbE.tb_lineno))
    print("Error: {}".format(str(e)))
except Exception as e:
    tbE = sys.exc_info()[2]
    print("Failed at Line {}\n".format(tbE.tb_lineno))
    print("Error: {}".format(e.message))

最后的实现结果如下图所示。

注意,在这其中创建字段使用如下的方式。

arcpy.AddField_management(createFC, "NAME", "TEXT", 50)

会报如下的错误。具体没有查到具体原因。

Error: Failed to execute. Parameters are not valid.
ERROR 000735: Input Table: Value is required
Failed to execute (AddField).

                                                                        更多内容,请关注公众号

                                                              

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

You may also like...