ArcGIS开发&读取txt坐标数据,并创建面shapefile

功能实现

之前有小伙伴说,他有坐标数据,想生成shapefile数据。后面我联系了他,但一直没有给数据,所示只好作罢。最近由于项目的需要,又有这样的需求。那么我这里有一个txt文件,都是坐标数据,如下图所示,其中每一行对应一个面。现在需要使用arcpy将这些数据创建成面。

在这其中,需要使用到的是,逐行读取txt文件,然后分割字符。注意这里使用的逗号。那么我们在使用arcpy的时候,会去创建一个投影信息,然后创建相应的shapefile文件,为这个shapefile创建字段。最后通过字段来,一一填入数据。具体的实现代码如下所示。

1#coding:utf-8
 2import os
 3
 4import arcpy
 5
 6txt_filename = 'C:/Users/qrb_PC/Desktop/gltf/data.txt'
 7path='C:/Users/qrb_PC/Desktop/gltf/'
 8outputname="polygontest.shp"
 9dir=path+outputname;
10in_file = open(txt_filename,'r');
11
12spatRef = arcpy.SpatialReference(4326)
13createFC = arcpy.CreateFeatureclass_management(os.path.dirname(dir), os.path.basename(dir), "POLYGON", "", "", "",
14                                               spatRef)
15#创建字段
16arcpy.AddField_management(createFC, "index", "TEXT", 50)
17# 左下角
18arcpy.AddField_management(createFC, "minX", "TEXT", 50)
19arcpy.AddField_management(createFC, "minY", "TEXT", 50)
20
21# 右上角
22arcpy.AddField_management(createFC, "maxX", "TEXT", 50)
23arcpy.AddField_management(createFC, "maxY", "TEXT", 50)
24
25cur = arcpy.InsertCursor(createFC)
26
27for line in in_file:
28    p = line.split(',');
29    array = arcpy.Array()
30
31    index=p[0]
32    minX=p[1]
33    minY=p[2]
34    maxX=p[3]
35    maxY=p[4]
36
37    pointLB = arcpy.Point()
38    pointLB.X=minX
39    pointLB.Y=minY
40
41    pointRB = arcpy.Point()
42    pointRB.X=maxX
43    pointRB.Y=minY
44
45    pointRU = arcpy.Point()
46    pointRU.X=maxX
47    pointRU.Y=maxY
48
49    pointLU = arcpy.Point()
50    pointLU.X=minX
51    pointLU.Y=maxY
52
53    array.append(pointLB)
54    array.append(pointRB)
55    array.append(pointRU)
56    array.append(pointLU)
57
58    row = cur.newRow()
59    row.shape = array
60    row.index = index
61    row.minX=minX
62    row.minY=minY
63
64    row.maxX=maxX
65    row.maxY=maxY
66
67    cur.insertRow(row)
68
69
70print 'finished'

我们来看一下实现的效果。其中方格就是我们对应的面数据。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值