# -*-coding:utf-8-*-
import arcpy
import fileinput
import os
# 探矿权坐标格式举例
# 111.0846,31.1530
# 111.1002,31.1530
# 111.1002,31.1500
# 111.1027,31.1500
# 111.1027,31.1344
# 111.1127,31.1344
# 111.1127,31.1300
# 111.1157,31.1300
# 111.1157,31.1225
# 111.0942,31.1225
# 111.0942,31.1400
# 111.0927,31.1400
# 111.0927,31.1500
# 111.0846,31.1500
# 0,0
# 生成的shp图形存放目录
arcpy.env.workspace = r"F:\shp"
fc = "tk.shp"
# 如果工作空间下不存在该FeatureClass,那么新建FeatureClass
isexist = arcpy.Exists(fc)
if not isexist:
print fc + " 要素类不存在!"
exit()
# 创建插入游标,txt坐标文件名称就是项目名称
# cursor = arcpy.da.InsertCursor(fc, ["XMMC", "DKID", "DKMC", "MJ", "SHAPE@"])
cursor = arcpy.da.InsertCursor(fc, ["XMMC", "SHAPE@"])
# 遍历所有坐标文件
dirpath = r"F:\2018tk\\"
txtlist = os.listdir(dirpath)
txtname = ""
try:
# 遍历文件夹目录下所有的txt文件
for txtpath in txtlist:
txtname = txtpath.split(".")[0]
txt = fileinput.input(dirpath + txtpath)
rowstr = txt.readline()
fieldlist = []
# 外圈坐标,即外圈矿山
_xypolylist = []
# 挖空坐标,即挖空矿山
_wkpolylist = []
# 临时坐标
_tempxylist = arcpy.Array()
# 坐标点的编号
bh = 0
# 遍历单个txt坐标文件的坐标值
print txtname.decode("gbk")
while rowstr:
# 如果出现空行,则直接跳过
if rowstr.strip() == "" or rowstr.strip() == r"\n":
rowstr = txt.readline()
continue
fieldlist = rowstr.split(",")
# 如果以0,0或者-1,0开头,标明该行是一个矿山(外圈或者内圈)结束
if rowstr.replace("\n", "") == "0,0":
_xytemppolygon = arcpy.Polygon(_tempxylist)
_xypolylist.append(_xytemppolygon)
_tempxylist.removeAll()
print rowstr.replace("\n", "")  # 一行末尾有换行符
rowstr = txt.readline()
bh = 0
continue
# 挖空矿山的标识是-1
elif rowstr.replace("\n", "") == "-1,0":
_wktemppolygon = arcpy.Polygon(_tempxylist)
_wkpolylist.append(_wktemppolygon)
_tempxylist.removeAll()
print rowstr.replace("\n", "")  # 一行末尾有换行符
rowstr = txt.readline()
bh = 0
continue
# 读取坐标值
pnt = arcpy.Point()
# 依次为坐标点编号、纵坐标、横坐标
bh = bh + 1
x = fieldlist[0].format("000.0000")
y = fieldlist[1].replace("\n", "").format("00.0000")
degrex = x[0:3]
minix = x[4:6]
secdx = x[6:8]
degrey = y[0:2]
miniy = y[3:5]
secdy = y[5:7]
_x = ("%.3f" % (float(degrex) + float(minix) / 60 + float(secdx) / 3600))
_y = ("%.3f" % (float(degrey) + float(miniy) / 60 + float(secdy) / 3600))
pnt.ID = bh
pnt.X = _x
pnt.Y = _y
_tempxylist.append(pnt)
print "第{0}个坐标是:{1},{2}".format(bh, _x, _y)
rowstr = txt.readline()
xypolynum = len(_xypolylist)
wkpolynum = len(_wkpolylist)
# 如果外圈矿山只有1个,挖空矿山1个或者多个,则执行裁剪,即交集取反
if xypolynum == 1 and wkpolynum >= 1:
poly = _xypolylist[0]
for j in range(wkpolynum):
poly = poly.symmetricDifference(_wkpolylist[j])
cursor.insertRow([txtname, poly])
print txtname.decode("gbk") + " is finished!"
continue
# 对于多个外圈矿山,1个或者多个挖空矿山,无法判断对哪个外圈矿山挖空
if xypolynum > 1 and wkpolynum >= 1:
print (txtname + ",无法判断挖空矿山!")
continue
# 遍历形成外圈地块
for i in range(xypolynum):
cursor.insertRow([txtname, _xypolylist[i]])
print txtname.decode("gbk") + " is finished!"
except Exception as err:
# print (err.args[0]).decode("gbk")
print "请检查坐标格式是否正确,或者坐标存在自相交!"
else:
print "全部生成!"
del cursor
效果如下:
测试数据如下:
111.1500,31.2537
111.1627,31.2617
111.1812,31.2301
111.1605,31.2300
0,0
111.1529,31.2501
111.1601,31.2501
111.1558,31.2530
111.1525,31.2526
-1,0
111.1648,31.2313
111.1738,31.2310
111.1735,31.2335
111.1648,31.2338
-1,0

转载于:https://www.cnblogs.com/apromise/p/10330757.html

使用python读取txt坐标文件生成挖空矿山_探矿批量相关推荐

  1. python读取坐标文本文件_使用python读取txt坐标文件生成挖空矿山_探矿批量

    # -*-coding:utf-8-*- import arcpy import fileinput import os # 探矿权坐标格式举例 # 111.0846,31.1530 # 111.10 ...

  2. python导入txt文件并绘图-Python读取txt数据文件,并作图

    import re #Errors=['验证通过!','身份证号码位数不对!','身份证号码出生日期超出范围或含有非法字符!','身份证号码校验错误!','身份证地区非法!'] def checkId ...

  3. python读取txt数据-Python读取txt数据文件,并作图

    import re #Errors=['验证通过!','身份证号码位数不对!','身份证号码出生日期超出范围或含有非法字符!','身份证号码校验错误!','身份证地区非法!'] def checkId ...

  4. python读取txt文件为dataframe,python批量读取txt文件为DataFrame

    我们有时候会批量处理同一个文件夹下的文件,并且希望读取到一个文件里面便于我们计算操作.比方我有下图一系列的txt文件,我该如何把它们写入一个txt文件中并且读取为DataFrame格式呢? 首先我们要 ...

  5. python读取txt文件的行数_python读取txt文件符合条件的行数-女性时尚流行美容健康娱乐mv-ida网...

    女性时尚流行美容健康娱乐mv-ida网 mvida时尚娱乐网 首页 美容 护肤 化妆技巧 发型 服饰 健康 情感 美体 美食 娱乐 明星八卦 首页 > 高级搜索 python 关系网图 pyth ...

  6. Python——读取txt文件每一行数据

    Python--读取txt文件每一行数据生成列表 一.例子1 (间隔符:逗号) 1.1 文件 1.2 代码 1.3 结果 二.例子2 (间隔符:空格) 2.1 文件 2.2 代码 2.3 结果 参考文 ...

  7. python删除重复值所在的行数_使用python读取txt文件的内容,并删除重复的行数方法...

    注意,本文代码是使用在txt文档上,同时txt文档中的内容每一行代表的是图片的名字. #coding:utf-8 import shutil readDir = "原文件绝对路经" ...

  8. python读取txt文件并画图

    1,使用python读取txt文件 已知txt文件内容如下: 0 01 12 43 94 165 256 36 请以第一列为x轴,第二列为y轴画图 步骤如下: 1)使用readlines读取文件 2) ...

  9. python读取txt文件并写入excel-Python读取txt内容写入xls格式excel中的方法

    由于xlwt目前只支持xls格式,至于xlsx格式,后面会继续更新 import xlwt import codecs def Txt_to_Excel(inputTxt,sheetName,star ...

最新文章

  1. 如何用python创建一个下载网站-使用Python下载文件的简单示例
  2. 汇编语言(十四)之判断字符串是否包含数字
  3. stream分组计数_Java Stream:第2部分,计数始终是计数吗?
  4. Android studio的sdk tools下没有LLDB的解决办法
  5. 2020年产品经理生存报告
  6. 鸿蒙系统空城计,鸿蒙系统到底什么时候用在手机上?
  7. 【转载】 C#中使用int.TryParse方法将字符串转换为整型Int类型
  8. sudo: unable to resolve host 枝桠
  9. 关于直线,折线切割平面的问题
  10. ubuntu 16.04 镜像下载
  11. termux安装gcc
  12. Incorrect argument type to variable ‘max_allowed_packet‘解决方法
  13. DNS服务器ttl修改不生效,中科三方DNS专家手把手教学——定位域名解析不生效问题...
  14. 让logo设计更有设计感的几个方法
  15. 3、集成逻辑门电路的功能和参数调试
  16. AI万物生成技术,颠覆内容传统创作模式 | iBrandUp 职位内推
  17. python break函数用法_Python break用法详解
  18. 关于耳机与电脑连接断断续续问题的解决办法
  19. oracle打开scott用户_Oracle解锁scott账户
  20. 【人月神话】第一章:焦油坑

热门文章

  1. 封神-核心功能 | 钉钉告警+数据网关
  2. 预览速度提升30倍,这是什么黑科技?(天猫618之3D渲染篇)
  3. 从DevOps到AIOps,阿里如何实现智能化运维?
  4. 零基础学Java编程的三个重点学习内容
  5. 从零点五开始用Unity做半个2D战棋小游戏(五)
  6. 科技感的动态设计方法-2
  7. 游戏AI——行为树理论及实现
  8. php socket(服务端与客户端)demo
  9. 解决goldengate复制进程应用缓慢一例
  10. Oracle约束的关键字Enable/Disable/Validate/Novalidate