#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    WKT text to rows


    I have polygon boundaries defined by latitude longitude pairs as below in a csv file
    "POLYGON(x1 y1,x2 y2,x3 y3,x4,y4)",name1,abc,100</B>
    "POLYGON(x1 y1,x2 y2,x3 y3,x4 y4,x5 y5,x6 y6)",name2,pqr,101

    Can someone help .I need output in another csv file as below:
    100,name1,abc,x1,y1
    100,name1,abc,x2,y2
    100,name1,abc,x3,y3
    100,name1,abc,x4,y4

    101,name2,pqr,x1,y1
    101,name2,pqr,x2,y2
    101,name2,pqr,x3,y3
    101,name2,pqr,x4,y4
    101,name2,pqr,x5,y5
    101,name2,pqr,x6,y6
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    Function reform processes a stripped line of input at a time.
    Code:
    def group(L, size = 2):
        assert (len(L) % size) == 0
        for i in range(0, len(L), size):
            yield L[i:i+size] # An itertools function providing this functionality exists
    
    def reform(s):
        '''
            command to run doctests:   python -m doctest p.py
    
            >>> print(reform('"POLYGON(x1 y1,x2 y2,x3 y3,x4,y4)",name1,abc,100</B>'))
            100,name1,abc,x1,y1
            100,name1,abc,x2,y2
            100,name1,abc,x3,y3
            100,name1,abc,x4,y4
            >>>
            >>> print(reform('"POLYGON(x1 y1,x2 y2,x3 y3,x4 y4,x5 y5,x6 y6)",name2,pqr,101'))
            101,name2,pqr,x1,y1
            101,name2,pqr,x2,y2
            101,name2,pqr,x3,y3
            101,name2,pqr,x4,y4
            101,name2,pqr,x5,y5
            101,name2,pqr,x6,y6
            >>>
        '''
        string_fields = s.split('"') # double quote field
        assert 3 == len(string_fields)
        comma_fields = string_fields[-1].split(',')
        assert 4 == len(comma_fields)
        polygon = string_fields[1]
        assert polygon.startswith('POLYGON(')
        assert polygon[-1] == ')'
        comma_fields[-1] = comma_fields[-1].split('<')[0] # discard '</B>'
        head = ','.join(comma_fields[i] for i in (-1, 1, 2))
        dirty_coordinates = polygon[1+polygon.index('('):-1]
        clean_coordinates = ' '.join(dirty_coordinates.split(','))
        format = head + ',{},{}'
        return '\n'.join(format.format(*xy) for xy in group(clean_coordinates.split()))
    Last edited by b49P23TIvg; January 16th, 2014 at 01:54 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0
    Superb :-) ..it is working great.

IMN logo majestic logo threadwatch logo seochat tools logo