Skip to main content
Solved

How to plot a line from a text file when all the Y coordinates are in a row followed by all the X coordinates in a row?

  • June 23, 2020
  • 5 replies
  • 37 views

paul_m
Contributor
Forum|alt.badge.img+18

I have a series of text files that I am trying to create lines from. Each row in the text file has the number of vertices followed by the Y coordinates for all the vertices and then the x coordinates for all the vertices (see example below)

3,41.1380602942863,41.1378946727126,41.1378187626848,-111.856708809171,-111.857316620769,-111.857869454434

Does anyone know how I could break these up into an X,Y format to use something like the LineBuilder to create lines?

Thanks!

Best answer by ebygomm

One possible way to get pairs of coordinates

Split into a list by the comma, count the length of the list, explode the list into parts, discard the first item in the list (the number of coordinates), create an x attribute if the element index is less than or equal to half the element count -1, create a y attribute if the element index is more than half of the element count -1, use a modulo counter to assign a coordinate index, aggregate together to get coordinate pairs

You could then create points and use a point connector make lines

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

5 replies

ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • Best Answer
  • June 23, 2020

One possible way to get pairs of coordinates

Split into a list by the comma, count the length of the list, explode the list into parts, discard the first item in the list (the number of coordinates), create an x attribute if the element index is less than or equal to half the element count -1, create a y attribute if the element index is more than half of the element count -1, use a modulo counter to assign a coordinate index, aggregate together to get coordinate pairs

You could then create points and use a point connector make lines


Forum|alt.badge.img+2
  • 194 replies
  • June 23, 2020

Split, explode, filter, count, rename XY, and merge.

 

From there, use a VertexCreator and a LineBuilder to generate the lines.

 

 

 

 


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • June 23, 2020

In reality though, I'd probably use a pythoncaller to create the geometries, something like this

import fme
import fmeobjects

def processFeature(feature):
    #get value, split on comma to get list and remove first value, convert to floats
    mylist = [float(i) for i in feature.getAttribute('value').split(',')]
    #get num coordinates
    numcoords = int(mylist[0])
    #get first n items in list (excluding first)
    y = mylist[1:numcoords+1:]
    #get last n items in list
    x = mylist[numcoords+1::]
    #create list of tuples
    coords = list(zip(x,y))
    #set geometry
    feature.setGeometry(fmeobjects.FMELine(coords))

ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • June 24, 2020

One possible way to get pairs of coordinates

Split into a list by the comma, count the length of the list, explode the list into parts, discard the first item in the list (the number of coordinates), create an x attribute if the element index is less than or equal to half the element count -1, create a y attribute if the element index is more than half of the element count -1, use a modulo counter to assign a coordinate index, aggregate together to get coordinate pairs

You could then create points and use a point connector make lines

x and y need to be the other way round of course, now I've noticed you say y coords first but same principle


paul_m
Contributor
Forum|alt.badge.img+18
  • Author
  • Contributor
  • 11 replies
  • June 24, 2020

One possible way to get pairs of coordinates

Split into a list by the comma, count the length of the list, explode the list into parts, discard the first item in the list (the number of coordinates), create an x attribute if the element index is less than or equal to half the element count -1, create a y attribute if the element index is more than half of the element count -1, use a modulo counter to assign a coordinate index, aggregate together to get coordinate pairs

You could then create points and use a point connector make lines

Thank you, your response helped a ton! I had multiple rows, so I made a few adjustments:

  1. Added a Counter before the List Exploder
  2. Used the Counter and the coordinate index (Modulo Counter) in the Aggregate group by parameter
  3. Added a Vertex Creator, Sorter (sort by _count and _element_index), and Line Builder (group by Counter)