1

I am trying to read a text file that has some funky formatting and manipulate some of the strings and export a csv file. The import text is a list of polygons and coordinates and the export is a list of coordinates with unique IDs associated with them. I am stuck at trying to make the code flexible for various lengths of polygons and various amounts of polygons. Example of list:

['poly', '1', '317806.6570985045', '4312355.239299678', '317808.2079078924', '4312354.675368992', '317806.1871562657', '4312348.754096784', '317804.4953642061', '4312349.365021695', 'poly', '2', '317811.4975035638', '4312361.724502574', '317810.651607534', '4312362.006467917', '317809.3357692654', '4312358.199935783', '317810.3226479669', '4312357.917970439']

Example of output csv:

poly1A, 317806.6570985045, 4312355.239299678
poly1B, 317808.2079078924, 4312354.675368992
poly1C, 317806.1871562657, 4312348.754096784
poly2A, 317811.4975035638, 4312361.724502574

So far, I have cleaned up the input .txt and have a list of the relevant info. I loop through the list and count the number of times 'poly' appears to know how many polygons there will be. I'm stuck at how to loop through the list and count the number of coordinate sets between polys to know where to cut them out of the list into a new list while maintaining flexibility for different size polygons. My code so far:

with open("Polys.txt", "r") as in_file, open("csvout.csv", 'w') as out_file:
    lines_list = in_file.readlines()
    del lines_list[0:5]
    #print(lines_list[])
    poly_list = []
    for i in range(len(lines_list)):
        poly_list.append(lines_list[i].strip('\n'))
    poly_list_strip = []
    for i in range(len(poly_list)):
        poly_list_strip.append(poly_list[i].strip())
    poly_list_untab =[]
    for i in range(len(poly_list_strip)):
        poly_list_untab.append(poly_list_strip[i].split())
    #print(poly_list_untab)
    flat_poly_list = [val for sublist in poly_list_untab for val in sublist]
    poly_index = [i for i, x in enumerate(flat_poly_list) if x == 'poly']
    poly_comb = flat_poly_list[]
CDJB
  • 14,043
  • 5
  • 29
  • 55
RageonAF
  • 13
  • 2

1 Answers1

0

You could use the following approach, if I understand the desired output correctly. list_to_csv(l, n) is called with l referring to the list of strings in the question, and n referring to the length of the polygons. If you want to account for different sized polygons within the same list then you need some way of identifying that.

import string
import csv

chars = string.ascii_uppercase

def isplit(iterable,splitters):
    """https://stackoverflow.com/a/4322780/12366110"""
    return [list(g) for k,g in itertools.groupby(iterable,lambda x:x in splitters) if not k]

def chunks(lst, n):
    """https://stackoverflow.com/a/312464/12366110"""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

def list_to_csv(l, n):
    split = [(f"poly{x[0]}{chars[i]}", *chunk) for x in isplit(l, 'poly') for i, chunk in enumerate(chunks(x[1:], n))]
    with open('csvout.csv','w',newline='') as f:
        csv_out=csv.writer(f)
        csv_out.writerow(['name', *[f"coord{x}" for x in range(n)]])
        for row in split:
            csv_out.writerow(row)

Usage:

>>> l = ['poly', '1', '317806.6570985045', '4312355.239299678', '317808.2079078924', '4312354.675368992', '317806.1871562657', '4312348.754096784', '317804.4953642061', '4312349.365021695', 'poly', '2', '317811.4975035638', '4312361.724502574', '317810.651607534', '4312362.006467917', '317809.3357692654', '4312358.199935783', '317810.3226479669', '4312357.917970439']
>>> list_to_csv(l, 2)

Output:

name,coord0,coord1
poly1A,317806.6570985045,4312355.239299678
poly1B,317808.2079078924,4312354.675368992
poly1C,317806.1871562657,4312348.754096784
poly1D,317804.4953642061,4312349.365021695
poly2A,317811.4975035638,4312361.724502574
poly2B,317810.651607534,4312362.006467917
poly2C,317809.3357692654,4312358.199935783
poly2D,317810.3226479669,4312357.917970439
CDJB
  • 14,043
  • 5
  • 29
  • 55