phayes / geoPHP

Advanced geometry operations in PHP
https://geophp.net
Other
863 stars 263 forks source link

convert wkb to wkt via php #160

Open sanabadai opened 5 years ago

sanabadai commented 5 years ago

hi i have this wkb string: 0xE61000000104060000004A5429C0E349424010806D3742964D4040038179E2494240BC8752993B964D407543AEF8E1494240BB1A3C373B964D40D29E7A81DF494240BDC654F23B964D409EE3FDE0E04942406CEB561143964D404A5429C0E349424010806D3742964D4001000000020000000001000000FFFFFFFF0000000003

i want to convert this to wkt via php? can i do this? please help me! thanks.

BathoryPeter commented 5 years ago

geoPHP::load($wkbString)->out('wkt');

But keep in mind, that geoPHP's WKB adapter is buggy and sometimes crashes. I had completely rewrite the WKB adapter in my fork supporting every OGC type and EWKB too. Give a try: https://github.com/funiq/geoPHP

sanabadai commented 5 years ago

i have this require 'geoPHP.inc'; $dbString1='0xE61000000104060000004A5429C0E349424010806D3742964D4040038179E2494240BC8752993B964D407543AEF8E1494240BB1A3C373B964D40D29E7A81DF494240BDC654F23B964D409EE3FDE0E04942406CEB561143964D404A5429C0E349424010806D3742964D4001000000020000000001000000FFFFFFFF0000000003'; geoPHP::load($dbString1)->out('wkt');

but give me error: Fatal error: Uncaught Error: Call to a member function out() on boolean in E:\xampp\htdocs\golbahar\test\test.php:4 Stack trace: #0 {main} thrown in E:\xampp\htdocs\golbahar\test\test.php on line 4

BathoryPeter commented 5 years ago

Because it is an invalid WKB. At first, remove the unneeded 0x prefix, then we get a hex string. Then look at the first byte: E6100000 = 4326 in decimal, so it is an EWKB and the geometry is in EPSG:4326. Its nice, but the first 10 bit (endianness and geometry type) is missing. From the EPSG code, we know that the string is little endian coded, and we can try to guess the type mask. For that, the have to investigate in the remaining part. Geometries are built from UInt32 and Double (64 bit) values, thus the remaining part must be divisible by 32. The string is 248 character which is 31 UInt32 value, hooray. Its too long to be a single point, so it must be some collection. Then the next UInt32 value (01040600) is the number of geometries. But if we convert 01040600 to decimal, we get a huge number: 394241. Its certainly bad.

At this point, we have to stop guessing. Your string is not a WKB and cannot be recover.