繁体   English   中英

如何将带有 XY 坐标的 TXT 文件转换为点特征?

[英]How to convert TXT file with XY coordinates to point feature?

我有一个格式如下的txt文件:

Spatial Reference: 43006
Name: Jones Tract
424564.620666, 4396443.55267
425988.30892, 4395630.01652
426169.09473, 4395426.63249
426214.291182, 4395268.4449

Name: Lewis Tract
427909.158152, 4393935.14955
428587.104939, 4393731.76552
428700.096071, 4393528.38148
428745.292523, 4393347.59567

Name: Adams Tract
424180.450819, 4393957.74778
424361.236629, 4393709.16729
424655.013571, 4393641.37261
424858.397607, 4393776.96197

我正在尝试将 Python 2.7 与 ArcPY 结合使用以从此文档生成点 shapefile 或要素。 我的 Python 技能充其量只是初学者,但我很享受学习它的挫败感。 然而,这个问题让我不知道如何解决它。

我对此的理解是:

OPEN text file
READ LINES with NAME
GET INDEX of NAME
CREATE LIST from INDEX NAME to NAME-1
APPEND NAME to OBJECT in LIST
OUTPUT to SHAPEFILE

结果与此类似:

NAME X Y
JONES 424564.620666 4396443.55267
JONES 425988.30892 4395630.01652
JONES 426169.09473 4395426.63249
JONES 426214.291182 4395268.4449
LEWIS 427909.158152 4393935.14955
LEWIS 428587.104939 4393731.76552
LEWIS 428700.096071 4393528.38148
LEWIS 428745.292523 4393347.59567
ADAMS 424180.450819 4393957.74778
ADAMS 424361.236629 4393709.16729
ADAMS 424655.013571 4393641.37261
ADAMS 424858.397607 4393776.96197 

我尝试将信息捕获到列表中,并成功地将 XY 值隔离到其中,但不确定如何将信息移动到 shapefile 或表格中。

# Opens txt file with tract data
iFile = open("U:/Data/Property_Module_7.txt", "r")

# Generates a list from the txt file
list1 = iFile.readlines()

# Returns index of lines with "Name"
for n, i in enumerate(list1):
    if i.startswith("Name"):
        print(n)

# lists for each coordinate between name indices
    jones = list1[2:24]
    smith = list1[25:43]
    adams = list1[44:73]
    jefferson = list1[74:90]
    lewis = list1[91:108]
    river = list1[109:]

    print(adams)
iFile.close()

当我在 PyCharm 中运行它时,结果是这样的:

1 
24 
43 
73 
90 
108 
['424180.450819, 4393957.74778\n', '424361.236629,
4393709.16729\n', '424655.013571, 4393641.37261\n', '424858.397607, 4393776.96197\n', '425106.978096, 4394025.54246\n', '425287.763906, 4394048.14068\n', '425513.746168, 4394093.33714\n', '425762.326657, 4394115.93536\n', '426010.907146, 4394183.73004\n', '425943.112467, 4393889.9531\n', '425920.514241, 4393709.16729\n', '425852.719562, 4393641.37261\n', '425943.112467, 4393550.97971\n', '425852.719562, 4393257.20276\n', '425739.728431, 4393099.01518\n', '425626.7373, 4392895.63114\n', '425581.540847, 4392669.64888\n', '425423.353263, 4392443.66662\n', '425355.558585, 4392285.47904\n', '425129.576322, 4391969.10387\n', '424926.192286, 4392104.69323\n', '424677.611797, 4392330.67549\n', '424406.433082, 4392511.4613\n', '424225.647272, 4392692.24711\n', '424112.656141, 4392827.83647\n', '424112.656141, 4392963.42582\n', '424135.254367, 4393279.80099\n', '424180.450819, 4393957.74778\n', '\n']

另外,为什么列表的对象末尾有 \\n ?

这里有几个问题。 首先, \\n是换行符,它们是文件中字符串的一部分。 您可以使用str.strip()方法去除它们,该方法将删除前导和尾随空白字符。

其次,关于查找Name实例所在位置的逻辑在正确的路径上。 您正在尝试构建一个 csv,因此将其作为使用csv模块的提示。

第三,一定要使用with上下文管理器进行文件处理,它消除了您关闭文件的需要,即使遇到错误

columns = ('Name', 'X', 'Y')
lines = []

with open('somefile.txt') as fh:
    for line in fh:
        if line.startswith('Name'):
            # the name looks like it's always the second
            # non-space string, so unpack it like so

            _, name, *_ = line.split(' ')
            # iterate over the next few lines until a blank is hit
            while True:
                try: 
                    line = next(fh).strip()
                except StopIteration:
                    break

                if not line:
                    break
                lines.append(dict(zip(columns, [name, *(x.strip() for x in line.split(','))])))


print(lines)
[{'Name': 'Jones', 'X': '424564.620666', 'Y': '4396443.55267'}, {'Name': 'Jones', 'X': '425988.30892', 'Y': '4395630.01652'}, {'Name': 'Jones', 'X': '426169.09473', 'Y': '4395426.63249'}, {'Name': 'Jones', 'X': '426214.291182', 'Y': '4395268.4449'}, {'Name': 'Lewis', 'X': '427909.158152', 'Y': '4393935.14955'}, {'Name': 'Lewis', 'X': '428587.104939', 'Y': '4393731.76552'}, {'Name': 'Lewis', 'X': '428700.096071', 'Y': '4393528.38148'}, {'Name': 'Lewis', 'X': '428745.292523', 'Y': '4393347.59567'}, {'Name': 'Adams', 'X': '424180.450819', 'Y': '4393957.74778'}, {'Name': 'Adams', 'X': '424361.236629', 'Y': '4393709.16729'}, {'Name': 'Adams', 'X': '424655.013571', 'Y': '4393641.37261'}, {'Name': 'Adams', 'X': '424858.397607', 'Y': '4393776.96197'}]        

然后您可以使用 csv 模块将其写入文件,如下所示:

import csv

with open('someotherfile.csv', 'w') as fh:
    writer = csv.DictWriter(fh, fieldnames = ['Name', 'X', 'Y'])
    writer.writerow({c: c for c in columns})
    writer.writerows(lines)  

随着输出

cat someotherfile.csv

Name,X,Y
Jones,424564.620666,4396443.55267
Jones,425988.30892,4395630.01652
Jones,426169.09473,4395426.63249
Jones,426214.291182,4395268.4449
Lewis,427909.158152,4393935.14955
Lewis,428587.104939,4393731.76552
Lewis,428700.096071,4393528.38148
Lewis,428745.292523,4393347.59567
Adams,424180.450819,4393957.74778
Adams,424361.236629,4393709.16729
Adams,424655.013571,4393641.37261
Adams,424858.397607,4393776.96197

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM