简体   繁体   中英

How to convert TXT file with XY coordinates to point feature?

I have a txt file with the following format:

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

I am trying to use Python 2.7 with ArcPY to generate a point shapefile or feature from this document. My Python skills are beginner at best, but am enjoying the frustrations of learning it. However, this problem has me stumped at how to approach it.

My understanding of this is:

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

Where the result would look similar to this:

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 

I have tried capturing the information to a list and have successfully isolated the XY values into them, but am unsure as how to move the info into a shapefile or table.

# 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()

When I run this in PyCharm, the result is this:

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']

Also, why does the list have the \\n at the end of the objects?

There are a few issues here. First, the \\n are newlines, which are part of the strings in your file. You can get rid of them by using the str.strip() method, which will remove leading and trailing whitespace characters.

Second, your logic regarding finding where the Name instances are is on the right path. You are trying to build a csv, so take that as a hint to use the csv module.

Third, be sure to use the with context manager for file handling, it removes the need for you to close the file, even when an error is encountered

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'}]        

Then you can write this using the csv module to a file like so:

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)  

With the output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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