Skip to main content
Solved

Formatting coordinate string and adding comma's

  • December 29, 2017
  • 3 replies
  • 112 views

jeroen
Contributor
Forum|alt.badge.img+15

I'm working on a workflow that uses the PythonCaller to extract coordinates from a source file of my choice and puts them in a results attribute. My plan is to replace the existing geometry with the one extracted using the GeometryReplacer with the OGC WKT encoding.

As an example:

This format needs to go into the GeometryReplacer

LINESTRING(161274.467 453784.372,161277.253 453785.924,161279.958 453787.131,161280.921 453787.508)

What i got is something like this, but it could be longer or shorter but always has a x and y coordinate but rounding after the comma could be different:

161274.467 453784.372 161277.253 453785.924 161279.958 453787.131 161280.921 453787.508 

How can you replace every second space with a comma without making it too static?This example contains four coordinates but it could have 8, 100 or more. After this stap i use LINESTRING(@Value(result)) to add the linestring text.

Best answer by takashi

Hi @JeroenR, if you prefer to use Python script, this code snippet might help you. Assuming that the attribute "poslist" stores the space separated coordinates.

    src = feature.getAttribute('poslist').split()
    result = ','.join(['%s %s' % c for c in zip(src[0::2], src[1::2])])        
    feature.setAttribute('result', result)

The StringReplacer with a regular expression could also work.

  • Mode: Replace Regular Expression
  • Text To Replace: (\S+\s+\S+)\s+
  • Replacement Text: \1,

However, if the goal is to create a line geometry from the coordinates with the GeometryReplacer, GML format could also be used without replacing spaces with commas.

<gml:LineString>
  <gml:posList>@Value(poslist)</gml:posList>
</gml:LineString>
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.

3 replies

takashi
Celebrity
  • 7843 replies
  • Best Answer
  • December 29, 2017

Hi @JeroenR, if you prefer to use Python script, this code snippet might help you. Assuming that the attribute "poslist" stores the space separated coordinates.

    src = feature.getAttribute('poslist').split()
    result = ','.join(['%s %s' % c for c in zip(src[0::2], src[1::2])])        
    feature.setAttribute('result', result)

The StringReplacer with a regular expression could also work.

  • Mode: Replace Regular Expression
  • Text To Replace: (\S+\s+\S+)\s+
  • Replacement Text: \1,

However, if the goal is to create a line geometry from the coordinates with the GeometryReplacer, GML format could also be used without replacing spaces with commas.

<gml:LineString>
  <gml:posList>@Value(poslist)</gml:posList>
</gml:LineString>

takashi
Celebrity
  • 7843 replies
  • December 29, 2017

Hi @JeroenR, if you prefer to use Python script, this code snippet might help you. Assuming that the attribute "poslist" stores the space separated coordinates.

    src = feature.getAttribute('poslist').split()
    result = ','.join(['%s %s' % c for c in zip(src[0::2], src[1::2])])        
    feature.setAttribute('result', result)

The StringReplacer with a regular expression could also work.

  • Mode: Replace Regular Expression
  • Text To Replace: (\S+\s+\S+)\s+
  • Replacement Text: \1,

However, if the goal is to create a line geometry from the coordinates with the GeometryReplacer, GML format could also be used without replacing spaces with commas.

<gml:LineString>
  <gml:posList>@Value(poslist)</gml:posList>
</gml:LineString>
Corrected the Python codes example.

 

 


jeroen
Contributor
Forum|alt.badge.img+15
  • Author
  • Contributor
  • 97 replies
  • December 29, 2017

Hi @JeroenR, if you prefer to use Python script, this code snippet might help you. Assuming that the attribute "poslist" stores the space separated coordinates.

    src = feature.getAttribute('poslist').split()
    result = ','.join(['%s %s' % c for c in zip(src[0::2], src[1::2])])        
    feature.setAttribute('result', result)

The StringReplacer with a regular expression could also work.

  • Mode: Replace Regular Expression
  • Text To Replace: (\S+\s+\S+)\s+
  • Replacement Text: \1,

However, if the goal is to create a line geometry from the coordinates with the GeometryReplacer, GML format could also be used without replacing spaces with commas.

<gml:LineString>
  <gml:posList>@Value(poslist)</gml:posList>
</gml:LineString>

 

Thanks a million! the GML trick did it :) I did not want any extra python cause of  support reasons. If needed now i can get coordinates in the WKT format with geometry extractor but for now this is not needed.