Skip to main content
Solved

Formatting coordinate string and adding comma's

  • December 29, 2017
  • 3 replies
  • 90 views

jeroen
Contributor
Forum|alt.badge.img+8

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>
View original
Did this help you find an answer to your question?

3 replies

takashi
Influencer
  • 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
Influencer
  • December 29, 2017
takashi wrote:

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+8
  • Author
  • Contributor
  • December 29, 2017
takashi wrote:

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.

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings