WKB to WKT transformation

Deals with generic topics such as logging, framework and so on. If you are not sure where to place your topic just put it here.
Post Reply
julonska
Posts: 9
Joined: Thu Aug 09, 2018 1:37 pm

WKB to WKT transformation

Post 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
Attachments
example_wkb.txt
Example WKB
(2.35 KiB) Downloaded 336 times
User avatar
Oliver Heilig
Posts: 154
Joined: Tue May 13, 2014 12:10 pm
Location: Karlsruhe, Germany
Contact:

Re: WKB to WKT transformation

Post 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
Oliver Heilig
Chief Developer Logistic Services
PTV GROUP - Germany

https://github.com/oliverheilig/
julonska
Posts: 9
Joined: Thu Aug 09, 2018 1:37 pm

Re: WKB to WKT transformation

Post 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)
Attachments
example_wkb.txt
Byte Array from WKB
(11.7 KiB) Downloaded 327 times
User avatar
Oliver Heilig
Posts: 154
Joined: Tue May 13, 2014 12:10 pm
Location: Karlsruhe, Germany
Contact:

Re: WKB to WKT transformation

Post 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)
Oliver Heilig
Chief Developer Logistic Services
PTV GROUP - Germany

https://github.com/oliverheilig/
julonska
Posts: 9
Joined: Thu Aug 09, 2018 1:37 pm

Re: WKB to WKT transformation

Post by julonska »

Good morning,

thank you very much! Finally it worked :)
Post Reply