6

how can i convert wkb to wkt form ...

i stored coordinates in wkb but now can it will display to use in wkt form

  • 4
    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 '10 at 8:22
  • 2
    Consider changing the question title to "Converting WKB to WKT" – underdark Nov 24 '10 at 9:00
13

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

  • no i am not using any exisitng db,. i want to apply spatial db .. – tina Nov 24 '10 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 '10 at 9:50
  • This helped me solve it for SQL Server. In SQL Server the function is STAsText. – Iain Samuel McLean Elder Aug 29 '13 at 17:58
7

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();
  • 1
    does this add anything that isn't in the accepted answer (from >3 years ago)? – Evil Genius Jul 14 '14 at 15:10
  • 1
    yeah this is basically sql server specific, the accepted answer IMHO is based on PostGIS – varun Jul 14 '14 at 19:11
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 here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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