Skip to content
Advertisement

WKB string to WKT, GeoJSON in Python

I have a number of WKB strings, couple of examples given below:

s1 = "0103000020E6100000010000000F000000BE63303EF1D551C078E289F14742454073F7B8FAD3D551C04B98F57E0F424540FC0EA22ED4D551C04ADE36890F424540ECEB53ACD5D551C07FE2CBCA0F424540D168512BD7D551C07F2DF9F80F42454043DB28ABD8D551C0A108AD13104245406D5A632BDAD551C01D42E31A104245408D378FABDBD551C0D9AB970E10424540AD23382BDDD551C008C1CFEE0F424540C61DEBA9DED551C077EC92BB0F4245403BD33327E0D551C02043F2740F424540E58DA0A2E1D551C0A4CA001B0F4245406245BE1BE3D551C09921DDAD0E424540C8E232ADF7D551C01C11F7CA4A424540BE63303EF1D551C078E289F147424540"

s2 = "0103000020E610000001000000060000009F64481FFD3A57C0E3AFF8C1EF7546407CEFEB30FD3A57C00AF4140AE375464018F55F07013B57C06018D414E37546407903F73F023B57C0BDF93E18E37546405F31E72D023B57C08D827D20F07546409F64481FFD3A57C0E3AFF8C1EF754640"

I want to convert them to WKT or GeoJSON type as I am able to do using the following online tool: https://rodic.fr/blog/online-conversion-between-geometric-formats/

Is there a way for me to do the same on Python?

Advertisement

Answer

You can use shapely for this:

from shapely import wkb, wkt
s1 = "0103000020E6100000010000000F000000BE63303EF1D551C078E289F14742454073F7B8FAD3D551C04B98F57E0F424540FC0EA22ED4D551C04ADE36890F424540ECEB53ACD5D551C07FE2CBCA0F424540D168512BD7D551C07F2DF9F80F42454043DB28ABD8D551C0A108AD13104245406D5A632BDAD551C01D42E31A104245408D378FABDBD551C0D9AB970E10424540AD23382BDDD551C008C1CFEE0F424540C61DEBA9DED551C077EC92BB0F4245403BD33327E0D551C02043F2740F424540E58DA0A2E1D551C0A4CA001B0F4245406245BE1BE3D551C09921DDAD0E424540C8E232ADF7D551C01C11F7CA4A424540BE63303EF1D551C078E289F147424540"
p = wkb.loads(bytes.fromhex(s1))
wkt.dumps(p)

[Out]:
POLYGON ((-71.3428492996744978 42.5178205416931974, -71.3410631948770941 42.5160978984063931, -71.3410755713084086 42.5160991208945944, -71.3411665744349079 42.5161069388895996, -71.3412578864110998 42.5161124436081010, -71.3413494013775988 42.5161156268016001, -71.3414410085913033 42.5161164865168999, -71.3415326021769971 42.5161150208074972, -71.3416240738140033 42.5161112322257964, -71.3417153163964883 42.5161051242256960, -71.3418062215904882 42.5160967047615941, -71.3418966834938857 42.5160859826912940, -71.3419865949740881 42.5160729722731006, -71.3432419773990887 42.5179074961071990, -71.3428492996744978 42.5178205416931974))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement