Page 1 of 1

WKB to WKT transformation

Posted: Fri Sep 14, 2018 11:47 am
by julonska
Dear all,

I am currently developing an application for a customer to restore or display routes that have already been calculated.
Therefore, I need to store somehow either WKB, WKT or the plain coordinates. Since storing WKT either than plain coordinates would be quite tricky (huge amount of data), I planned to store WKB.

Storing issue is therefore solved, but unfortunately I cannot convert my stored WKB back to WKT (example WKB is attached) to draw the route in my JavaScript frontend.
Since I could not solve the issue with JavaaScript, I thought about transforming it within my backend written in Java (JTS library). I have tried this with several different routes but always received an "invalid" WKB (not the expected HEX format) which resulted in an IllegalArgumentException.

Is there any possibility to receive a valid WKB in HEX format?
Or does PTV deliver a Java class for the transformation from this WKB I got from the xRoute to a LineString (WKT)?

Many thanks!

Julian Ulonska

Re: WKB to WKT transformation

Posted: Fri Sep 14, 2018 12:50 pm
by Oliver Heilig
Hello Julian,

your data is not directly WKB, but base64 encoded. You must convert it to a byte array first. Then you can load and write it to WKT. Her a sample using .NET and NetToplogySuite https://github.com/NetTopologySuite/NetTopologySuite. The same sould apply to Java using JTS https://github.com/locationtech/jts.

Code: Select all

var wkb64 = "AAAAAAIAAAB...";
var wkbArray = System.Convert.FromBase64String(wkb64);
var reader = new NetTopologySuite.IO.WKBReader();
var geometry = reader.Read(wkbArray);
var writer = new NetTopologySuite.IO.WKTWriter();
var wkt = writer.Write(geometry); // returns LINESTRING (6.28257322 50.895717408, 6.282400207 50.895799252, ...
Oli

Re: WKB to WKT transformation

Posted: Fri Sep 14, 2018 1:24 pm
by julonska
Hello Oli,

thanks for your fast reply.
Actually this fixed the first problem with illegal argument. Nevertheless, I receive either a ParseException due to "Unknown WKB type 77" or, if I change the tpe during runtime with the debugger, a Java heap space exception.

Not sure if this is a problem with the Java class. Attached you can find my byte array..

Code: Select all

        
String wkb64 = "010200
byte[] wkbArray = Base64.getDecoder().decode(wkb64);

Geometry geometry = new WKBReader().read(wkbArray);
WKTWriter wktWriter = new WKTWriter();
String wkt = wktWriter.write(geometry)

Re: WKB to WKT transformation

Posted: Fri Sep 14, 2018 4:34 pm
by Oliver Heilig
Hello Julian,
  • It looks like you are using the hex/string representation for wkb64, and not the base64 encoding as input.
  • The wkb-response from xRoute is a base64 encoded string (like in your first attachment).
  • The bytes in your second attachment is no valid WKB. The first byte must either be 0 or 1.
xRoute wkb response (base64 string) -> base64 decode (byte array) -> WkbReader (line geometry)

Re: WKB to WKT transformation

Posted: Mon Sep 17, 2018 5:44 am
by julonska
Good morning,

thank you very much! Finally it worked :)