Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi all,
I try to transform an string into another one.
My source string have blank spaces, and i want to replace some of them by a comma. More precisely, i want to change just one on two.
An example could be more efficient:
source string ( a list of coordinates) :
45.979904 -1.355741 45.979665 -1.355603 45.979396 -1.35542 45.979095 -1.355198 45.978451 -1.354722
Desire result:
45.979904 -1.355741,45.979665 -1.355603,45.979396 -1.35542,45.979095 -1.355198,45.978451 -1.354722
As you can see, the first blank space is not replaced, but the second yes, the third one no and fourth yes...
Unfortunately, i didn't know how many couples of coordinates i will have.
Somebody have an idea?
Thank you.
I don't understand your input data. Is your input data "44.994234 -0.447106,44.994297 -0.446381,44.994321 -0.446109,44.994333 -0.446009,44.99437 -0.445707,44.994443 -0.445193,44.9945 -0.444517,44.994516 -0.444318,44.99447 -0.443701,44.994416 -0.44327,44.994384 -0.443013,44.994124 -0.441486,44.994033 -0.44093,44.993959 -0.440493,44.993887 -0.440272" ?
Indeed I thought there was no comma in your input data. Here I can see a comma.
Sorry mistake of copy/paste.
Original data is:
44.994234 -0.447106 44.994297 -0.446381 44.994321 -0.446109 44.994333 -0.446009 44.99437 -0.445707 44.994443 -0.445193 44.9945 -0.444517 44.994516 -0.444318 44.99447 -0.443701 44.994416 -0.44327 44.994384 -0.443013 44.994124 -0.441486 44.994033 -0.44093 44.993959 -0.440493 44.993887 -0.440272
So I get the output:
-0.447106 44.994234,-0.446381 44.994297,-0.446109 44.994321,-0.446009 44.994333,-0.445707 44.99437,-0.445193 44.994443,-0.444517 44.9945,-0.444318 44.994516,-0.443701 44.99447,-0.44327 44.994416,-0.443013 44.994384,-0.441486 44.994124,-0.44093 44.994033,-0.440493 44.993959,-0.440272 44.993887
Isn't the expected result ?
I've adjusted the routine I sent you last time. This one will handle the commas, rearrange the pairs and remove the extra comma at the end.
public class MyRoutine { public static String prepareCoords(String data){ String[] tokens = data.split(" "); String returnVal = ""; String pair = ""; for(int i = 0; i<tokens.length;i++){ if(i%2==1){ pair = tokens[i]+" "+pair; returnVal = returnVal+pair+","; pair=""; }else{ pair = tokens[i]; } } returnVal = returnVal.substring(0, returnVal.length()-1); return returnVal; } }
Yes it's work 🙂
I based the treplace on a wrong field.
I will post another message to explain how to finish to convert this kind of geometry to a real WKT.
Thanks again, weldone (and I will look at regex for next problems like that).
Yes your routine works too.
Thanks a lot.
As promised, a quick explanation of the end of my process to create a real WKT. It is quite difficult to find on internet so if it could help someone later...
You can use both solution proposed here to order the coordinates and the comma in the right place.
Complete expression:
"LINESTRING("+row6.line+")"
After that, you will have to generate the geometry, with the sentence "new Geometry(name_of_your_var). Do not forget to save the type of your geometry column as "geometry":
And finally, very important tip, you will have to add to your job a geographical component (I choose SGeomTxtInput) to load library or component (I am not sure of the term).
A little SProj will help you to declare the projection.
An output in postgis complete the job.
I have some trouble when repeating the manipulation, and in the PostGis output component, I choosed Update and Insert instead of Insert and Update and it works.
Hope it could help.