Python - Field Calculator Expression - Writing Bytearray to Text Field (WKB)

777
4
Jump to solution
05-26-2021 01:33 PM
Labels (3)
ColeAndrews
Occasional Contributor III

Need a little python help. This is for use in field calculator. Goal is to write the WKB representation of geometry objects (in a feature class) to a text field, for the purpose of exporting to csv.

ArcPy supports writing WKB of geometry objects. See: https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/geometry.htm#P_GUID-BC466D06-360C-42BD-B198-D...

The basic expression to get the WKB is !Shape.WKB!, although that returns a bytearray. I believe it needs to be encoded and converted to a string. The output should look like this image:

ColeAndrews_0-1622060899503.png

The text in that image exports to csv as: "\001\006\000\000\000\001\000\000\000\001\003\000\000\000\001\000\000\000\012\023\000\000\314\276\362 . . . "

0 Kudos
1 Solution

Accepted Solutions
ColeAndrews
Occasional Contributor III

Confirmed, I took the string output of (!Shape.WKB!).hex() and input in to a few non-esri databases using spatial SQL functions. The databases can properly parse the hexidecimal WKB in to GEOMETRY/GEOGRAPHY data.

View solution in original post

0 Kudos
4 Replies
ColeAndrews
Occasional Contributor III
0 Kudos
DanPatterson
MVP Esteemed Contributor

 

 

from arcpy.da import SearchCursor
in_fc = r"C:\arcpro_npg\npg\Project_npg\tests.gdb\sq"
with SearchCursor(in_fc, ['SHAPE@WKB']) as cursor:
    shps = [row[0] for row in cursor]
s0 = shps[0]
# --- first shape
s0
bytearray(b'\x01\x03\x00\x00\x00\x02\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\xa4O\x12A\x00\x00\x00@\xd0\x12SA\x00\x00\x00\x00\xa8O\x12A\x00\x00\x00\x00\xd2\x12SA\x00\x00\x00\x00\xa8O\x12A\x00\x00\x00\x80\xd2\x12SA\x00\x00\x00\x00\xa0O\x12A\x00\x00\x00\x80\xd2\x12SA\x00\x00\x00\x00\x88O\x12A\x00\x00\x00\x00\xd2\x12SA\x00\x00\x00\x00\x80O\x12A\x00\x00\x00\x00\xd0\x12SA\x00\x00\x00\x00\xa4O\x12A\x00\x00\x00@\xd0\x12SA\x04\x00\x00\x00\x00\x00\x00\x00\x8cO\x12A\x00\x00\x00\xc0\xd0\x12SA\x00\x00\x00\x00\x94O\x12A\x00\x00\x00\xc0\xd1\x12SA\x00\x00\x00\x00\x9cO\x12A\x00\x00\x00\xc0\xd0\x12SA\x00\x00\x00\x00\x8cO\x12A\x00\x00\x00\xc0\xd0\x12SA')

# ---- as hex
s0.hex()
'0103000000020000000700000000000000a44f124100000040d012534100000000a84f124100000000d212534100000000a84f124100000080d212534100000000a04f124100000080d212534100000000884f124100000000d212534100000000804f124100000000d012534100000000a44f124100000040d012534104000000000000008c4f1241000000c0d012534100000000944f1241000000c0d1125341000000009c4f1241000000c0d0125341000000008c4f1241000000c0d0125341'

 

then 

Well-known text representation of geometry - Wikipedia

if you can test with your inputs

Other options

from_WKB.png

 


... sort of retired...
ColeAndrews
Occasional Contributor III

@DanPatterson , thanks for the response. Trying to simplify this as a field expression. Looks like I just need to use the hex function on the bytearray. If my empty field is called "WKB_String", then  [WKB_String] = (!Shape.WKB!).hex() is populating the field. I would have thought the syntax was  [WKB_String] = hex.(!Shape.WKB!), but that throws an error that bytearray cannot be converted to integer. Does this image look like it is accomplishing the same end result as your script?

ColeAndrews_0-1622124863774.png

 

0 Kudos
ColeAndrews
Occasional Contributor III

Confirmed, I took the string output of (!Shape.WKB!).hex() and input in to a few non-esri databases using spatial SQL functions. The databases can properly parse the hexidecimal WKB in to GEOMETRY/GEOGRAPHY data.

0 Kudos