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.

WKB to WKT transformation

Postby julonska » Fri Sep 14, 2018 11:47 am

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 17 times
julonska
 
Posts: 5
Joined: Thu Aug 09, 2018 1:37 pm

Re: WKB to WKT transformation

Postby Oliver Heilig » Fri Sep 14, 2018 12:50 pm

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.

csharp code
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
PTV GROUP - Germany

https://github.com/oliverheilig/
User avatar
Oliver Heilig
 
Posts: 107
Joined: Tue May 13, 2014 12:10 pm
Location: Karlsruhe, Germany

Re: WKB to WKT transformation

Postby julonska » Fri Sep 14, 2018 1:24 pm

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 10 times
julonska
 
Posts: 5
Joined: Thu Aug 09, 2018 1:37 pm

Re: WKB to WKT transformation

Postby Oliver Heilig » Fri Sep 14, 2018 4:34 pm

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
PTV GROUP - Germany

https://github.com/oliverheilig/
User avatar
Oliver Heilig
 
Posts: 107
Joined: Tue May 13, 2014 12:10 pm
Location: Karlsruhe, Germany

Re: WKB to WKT transformation

Postby julonska » Mon Sep 17, 2018 5:44 am

Good morning,

thank you very much! Finally it worked :)
julonska
 
Posts: 5
Joined: Thu Aug 09, 2018 1:37 pm


Return to Generic questions