11

How can I convert WKB to WKT form?

I stored coordinates in WKB but now I want to display it using WKT form.

2
  • 5
    Hi tina, you'll need to provide more details to make this question answerable, such as what software are you attempting to do this with, or attempting to do encoding / decoding directly?
    – scw
    Nov 24, 2010 at 8:22
  • 2
    Consider changing the question title to "Converting WKB to WKT"
    – underdark
    Nov 24, 2010 at 9:00

6 Answers 6

17

If you want to use PostGIS to do the conversion, you can choose from the following functions:

bytea WKB = ST_AsBinary(geometry);
text WKT = ST_AsText(geometry);
geometry = ST_GeomFromWKB(bytea WKB, SRID);
geometry = ST_GeometryFromText(text WKT, SRID);

More: http://postgis.net/docs/manual-1.5/ch04.html#OpenGISWKBWKT

3
  • no i am not using any exisitng db,. i want to apply spatial db ..
    – tina
    Nov 24, 2010 at 9:48
  • "i want to apply spatial db" - Does that mean you want to use PostGIS? Then the above functions should help.
    – underdark
    Nov 24, 2010 at 9:50
  • This helped me solve it for SQL Server. In SQL Server the function is STAsText. Aug 29, 2013 at 17:58
8

If you can work with Java, the JTS Topology Suite can perform such translation by using the classes com.vividsolutions.jts.io.WKBReader and com.vividsolutions.jts.geom.Geometry. A code like this should get the geometry in Well-Known Text format:

import com.vividsolutions.jts.io.WKBReader;
import com.vividsolutions.jts.geom.Geometry;

String wkbString = "0101000000cdcccccc170d2241b81e859bcb405241";
// geometry in WKB format to be translated to WKT
byte[] aux = WKBReader.hexToBytes(wkbString);
Geometry geom = new WKBReader().read(aux);
String wktString = geom.toText();
// geometry in WKT format
System.out.printf("WKB = %s\n", wkbString);
System.out.printf("WKT = %s\n", wktString);
1

In Sql Server Spatial

select geometry::STGeomFromWKB(0x01030000000100000005000000E968ED3E66CF1D41BD6859D5EEE15941E968ED3E66CF1D41844B22F8E1E959417156F69608241F41844B22F8E1E959417156F69608241F41BD6859D5EEE15941E968ED3E66CF1D41BD6859D5EEE15941,3857).ToString();
0
1

Using R, you can convert between WKB and WKT using the packages rgeos and wkb. Example:

# load packages
library(rgeos)
library(wkb)

# create raw vector representing WKB geometry
wkb <- hex2raw("0101000000cdcccccc170d2241b81e859bcb405241")

# convert WKB to Spatial object
obj <- readWKB(wkb)

# convert Spatial object to WKT
wkt <- writeWKT(obj, byid = TRUE)
1

If you don't want to install any tools, online conversions between WKB, WKT and GeoJSON formats can be performed using Online conversion between geometric formats.

0

In Kotlin and PostGIS Java, net.postgis.jdbc.PGgeometry can take WKB representation:

val wkb = "010100000033333333333324409a99999999992440"
val geometry = PGgeometry(wkb)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.