Six Stars

Manipulate strings, replace a blank space on two

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.

3 ACCEPTED SOLUTIONS

Accepted Solutions
Twelve Stars

Re: Manipulate strings, replace a blank space on two

Ah, sorry. I assumed you might know some Java. OK do the following....

 

1) Create a routine called MyRoutine (you can change the name if you choose later, once you understand this).

2) Replace the code that is automatically generated with the below...

package routines;


public class MyRoutine {

	public static String prepareCoords(String data){
	      String[] tokens = data.split(" ");
	      String returnVal = "";

	      for(int i = 0; i<tokens.length;i++){
		   if(i%2==1){
			returnVal = returnVal+tokens[i]+",";
		   }else{
			returnVal = returnVal+tokens[i]+" ";
		   }	
	     }
	     return returnVal;
	}
}

3) Save the routine.

4) Now, in your job you would use the routine like below. I will assume that you are using it in a tMap or similar and that your data is in a column called "MyData" and the row is "row1".....

routines.MyRoutine.prepareCoords(row1.MyData)

The above code would do in one of your output columns in a tMap

Rilhia Solutions
Eight Stars

Re: Manipulate strings, replace a blank space on two

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 ?

Twelve Stars

Re: Manipulate strings, replace a blank space on two

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;
	}
}

 

 

Rilhia Solutions
16 REPLIES
Twelve Stars

Re: Manipulate strings, replace a blank space on two

Create a routine to do this. I've knocked the below up which *should* work. You will need to test it first...

public static String prepareCoords(String data){
      String[] tokens = data.split(" ");
      String returnVal = "";

      for(int i = 0; i<tokens.length;i++){
	   if(i%2==1){
		returnVal = returnVal+tokens[i]+",";
	   }else{
		returnVal = returnVal+tokens[i]+" ";
	   }	
     }
     return returnVal;
}
Rilhia Solutions
Six Stars

Re: Manipulate strings, replace a blank space on two

Thank you rhall_2_0 

 

There is several error message displayed in the interface:

 

public static String prepareCoords(String data){

- syntax error on token(s), misplaced construct(s)

- syntax error on token(s), misplaced construct(s)

- Syntax error on token "String", @ expected

 

String[] tokens = data.split(" ");

- syntax error on token(s), misplaced construct(s)

 

String returnVal = "";

- syntax error on token(s), misplaced construct(s)

 

And the last one:

printscreen.png

I am not familiar at all with java and the routines, but I have found how to use it so I will be able to test it I think.

Twelve Stars

Re: Manipulate strings, replace a blank space on two

Ah, sorry. I assumed you might know some Java. OK do the following....

 

1) Create a routine called MyRoutine (you can change the name if you choose later, once you understand this).

2) Replace the code that is automatically generated with the below...

package routines;


public class MyRoutine {

	public static String prepareCoords(String data){
	      String[] tokens = data.split(" ");
	      String returnVal = "";

	      for(int i = 0; i<tokens.length;i++){
		   if(i%2==1){
			returnVal = returnVal+tokens[i]+",";
		   }else{
			returnVal = returnVal+tokens[i]+" ";
		   }	
	     }
	     return returnVal;
	}
}

3) Save the routine.

4) Now, in your job you would use the routine like below. I will assume that you are using it in a tMap or similar and that your data is in a column called "MyData" and the row is "row1".....

routines.MyRoutine.prepareCoords(row1.MyData)

The above code would do in one of your output columns in a tMap

Rilhia Solutions
Six Stars

Re: Manipulate strings, replace a blank space on two

Perfect thank you very much.

 

I combine this with a StringHandling.LEFT and a concatenation to add at the end of the result the first couple of coordinats (that is a prerequisite of WKT).

I will just have to tansform my string into a geometry to set a projection and the work will be done.

 

Thanks again for your help.

Eight Stars

Re: Manipulate strings, replace a blank space on two

Hi,

 

You could use a tReplace component:

Capture.PNG

Eric

Six Stars

Re: Manipulate strings, replace a blank space on two

Hello,

 

I may need some help again, a detail have been forgotten...

 

In the string submit at the beginning, I didn(t see that the X and Y are reversed. In fact when i have this as a 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

 

The result might be:
-1.355741 45.979904,-1.355603 45.979665,-1.35542 45.979396,-1.355198 45.979095,-1.354722 45.978451

 

Is there a way to reverse order of data inside each couple of coordinates?

Eight Stars

Re: Manipulate strings, replace a blank space on two

Hi,

 

With a tReplace:

Capture.PNG

 

-First expression (To add a space at the end of your line):

pattern is "(.*)"

replace is "$1 "

-Second expression (To reverse the coordinates and replace the space with a comma)

pattern is "(.*?)( )(.*?)( )"

replace is "$3 $1,"

-Last expression (To remove the comma at the end of the line):

pattern is "(.*),"

replace is "$1"

 

Let me know it it meets your requirements

 

Eric

Eight Stars

Re: Manipulate strings, replace a blank space on two

For first expression pattern should be "(^.*)" (Otherwise you will get an extra space at the end of the line)

Six Stars

Re: Manipulate strings, replace a blank space on two

Hello Eric,

 

I have duplicate the job and test this solution.

Here is the result:

-0.447106,44.994297 44.994234,-0.446109,44.994333 -0.446381,44.994321,-0.445707,44.994443 -0.446009,44.99437,-0.444517,44.994516 -0.445193,44.9945,-0.443701,44.994416 -0.444318,44.99447,-0.443013,44.994124 -0.44327,44.994384,-0.44093,44.993959 -0.441486,44.994033,-0.440272, -0.440493,44.993887

 

The original data was: 

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 (Bad copy/paste)

 

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

 

For more efficience, I suggest to simplify the original data into:

Y1 X1 Y2 X2 Y3 X3...

 

As you can see, the result is :

X1(comma)Y2(space)Y1(comma)X3(comma)Y3...

or

X1,Y2 Y1,X3,Y3...

and the expected is:

X1 Y1,X2 Y2, X3 Y3...

 

If i am not cear enought tell me of course.

 

Thank you for helping me anyway

Eight Stars

Re: Manipulate strings, replace a blank space on two

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.

Six Stars

Re: Manipulate strings, replace a blank space on two

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

Eight Stars

Re: Manipulate strings, replace a blank space on two

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 ?

Twelve Stars

Re: Manipulate strings, replace a blank space on two

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;
	}
}

 

 

Rilhia Solutions
Six Stars

Re: Manipulate strings, replace a blank space on two

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).

Six Stars

Re: Manipulate strings, replace a blank space on two

Yes your routine works too. 

Thanks a lot.

Six Stars

Re: Manipulate strings, replace a blank space on two

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.

 temp1.PNG

 

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":

 

temp2.PNG

 

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.