Create and display geometry object from WKB

2177
8
Jump to solution
08-01-2019 11:41 PM
RobertKnapik
New Contributor III

Is it possible to convert WKB geometry to geometry object and display it in ArcGIS Pro/ArcGIS Online etc.or save in geodatabase? I found in documentation something like this below, but how to use it?

FromWKB

I have a WKB string: 

'010300002084080000010000000E0000007B14AEC74699244152B81E851B741D410AD7A3F0429924410000000002741D41713D0A571A992441333333331E741D419A9999990F99244100000000E0731D4100000080CF98244152B81E850B741D419A999919CB9824411F85EB51ED731D413D0AD723CA982441713D0AD7ED731D415C8FC2F5C49824411F85EB51CE731D410AD7A3F0B798244152B81E85D7731D4166666666AD982441C3F5285C97731D41E17A142E8598244166666666B2731D419A999999A3982441EC51B81E84741D415C8FC2F5F1982441B81E85EB51741D417B14AEC74699244152B81E851B741D41'

Thanks

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

I think the bigger issue than the extra "0" line at the start and trailing newline character is the fact that the site if outputting EWKB and EWKT and not WKB and WKT.  I find working with EWKB frustrating at best, so in this case I would simply get the results as EWKT since it is easier to break it down into a valid WKT string.

The following works with Python3:

>>> from urllib.request import urlopen
>>> import arcpy
>>>
>>> url = r"http://uldk.gugik.gov.pl/?request=GetParcelById&id=141201_1.0001.1867/2&result=geom_wkt"
>>> req = urlopen(url)
>>> EWKT = req.read().decode().split("\n")[1]
>>> EWKT
'SRID=2180;POLYGON((674979.39 482566.88,674977.47 482560.5,674957.17 482567.55,674951.8 482552,674919.75 482562.88,674917.55 482555.33,674917.07 482555.46,674914.48 482547.58,674907.97 482549.88,674902.7 482533.84,674882.59 482540.6,674897.8 482593.03,674936.98 482580.48,674979.39 482566.88))'
>>> SRID, WKT = EWKT.split(";")
>>> poly = arcpy.FromWKT(WKT, arcpy.SpatialReference(int(SRID.split("=")[1])))
>>> poly.area
2379.763699999483
>>> ‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

8 Replies
NeilAyres
MVP Frequent Contributor

I have a WKB string:

Indeed this looks like a text string, not a byte array type as stated in the help.

How are you getting the WKB object?

0 Kudos
RobertKnapik
New Contributor III
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I think the bigger issue than the extra "0" line at the start and trailing newline character is the fact that the site if outputting EWKB and EWKT and not WKB and WKT.  I find working with EWKB frustrating at best, so in this case I would simply get the results as EWKT since it is easier to break it down into a valid WKT string.

The following works with Python3:

>>> from urllib.request import urlopen
>>> import arcpy
>>>
>>> url = r"http://uldk.gugik.gov.pl/?request=GetParcelById&id=141201_1.0001.1867/2&result=geom_wkt"
>>> req = urlopen(url)
>>> EWKT = req.read().decode().split("\n")[1]
>>> EWKT
'SRID=2180;POLYGON((674979.39 482566.88,674977.47 482560.5,674957.17 482567.55,674951.8 482552,674919.75 482562.88,674917.55 482555.33,674917.07 482555.46,674914.48 482547.58,674907.97 482549.88,674902.7 482533.84,674882.59 482540.6,674897.8 482593.03,674936.98 482580.48,674979.39 482566.88))'
>>> SRID, WKT = EWKT.split(";")
>>> poly = arcpy.FromWKT(WKT, arcpy.SpatialReference(int(SRID.split("=")[1])))
>>> poly.area
2379.763699999483
>>> ‍‍‍‍‍‍‍‍‍‍‍‍‍
BruceHarold
Esri Regular Contributor

Hi Joshua, I never heard of EWKB/T, who needs that surprise, but like you show, easily handled.  I'll request it be supported as a geometry encoding in Data Interoperablity.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The Extended WKB/T formats are defined by PostGIS, but there are other packages that support them.  From Chapter 4. Using PostGIS: Data Management and Queries :

4.1.2. PostGIS EWKB, EWKT and Canonical Forms

OGC formats only support 2D geometries, and the associated SRID is *never* embedded in the input/output representations.

PostGIS extended formats are currently superset of OGC one (every valid WKB/WKT is a valid EWKB/EWKT) but this might vary in the future, specifically if OGC comes out with a new format conflicting with our extensions. Thus you SHOULD NOT rely on this feature!

PostGIS EWKB/EWKT add 3DM, 3DZ, 4D coordinates support and embedded SRID information.

0 Kudos
RobertKnapik
New Contributor III

Thanks!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If my response answered your question, please mark it correct to close out the question.  Thanks.

0 Kudos
BruceHarold
Esri Regular Contributor

Hi

The data is not well formed, it contains hex-encoded WKB but is prepended with a 0 and newline character.

I attach the source for a Data Interoperability ETL tool that reads the geometry after discarding the leading junk.

You can use this with FME too.