0

How can I export WKB (geometry) to text file using arcpy (ArcGIS)?

When I use the examples that I found on the Internet, it doesn't work. Below it's the code I'd wrote:

## Import arcpy module

import sys
import string
import os
import arcpy

## Global variables:

mxd = arcpy.mapping.MapDocument("current")

IN_FC = arcpy.GetParameterAsText(0)
OUT_Addre = arcpy.GetParameterAsText(1)
OUT_Name = arcpy.GetParameterAsText(2)

## Set progressor
lines = arcpy.GetCount_management(IN_FC)
number = int(fc.getOutput(0))

arcpy.SetProgressor("default","Exporting WKB geometry",0,number,1)

## for row in input
with arcpy.da.SearchCursor(IN_FC, ["OID@", "SHAPE@WKB"]) as cursor:
    for row in cursor: 
## First feature
        elem = IN_FC.getFeatures().next()
        geom = elem.geometry()

## Export into wkb format
        wkb = geom.asWkb()

## Import to ogr geometry
        from osgeo import ogr
        geom_ogr = ogr.CreateGeometryFromWkb(wkb)

## Import to Shapely geometry

        from shapely.wkb import loads
        line = loads(wkb)
        ls = wkb.encode('hex')

## output

        output_file = open(OUT_Addre & OUT_Name & ".txt", 'w')
        output_file.write('\n' & ls.encode('utf-8'))
        output_file.close()

Some errors message:

import ogr or wkb = geom.asWkb() "doesn't exist"

1
  • Luke, I added the whole code I tried to write.
    – Machado
    Commented Sep 10, 2017 at 14:14

2 Answers 2

3

Use SHAPE@WKT to get the geometry in text form. WKB is a binary format.

If you specifically need WKB serialized to a string, then you should look at any of the ways in Python to convert type bytearray to string. You could write the object to hexadecimal values:

with arcpy.da.SearchCursor(IN_FC, ["OID@", "SHAPE@WKB"]) as cursor:
    for row in cursor: 
        wkb = row[1]
        output_file.write(wkb.encode('hex') + '\n')  

Then the question becomes why don't you just serialize the geometry with a common GIS format. You mention shapefile and WKB in the comments, so some more specific information about the software that needs this input could help. If shapefile is a valid format, you could just do:

arcpy.CopyFeatures_management (in_FC, 'out.shp')

In your example you have a for row in cursor iterator combined with a .next(). You don't need both. The iterator will advance to the next row for you. If you only needed the first feature then you could use next without the iterator.

7
  • WKB could be exported as hexadecimal or base64 strings, but that wouldn't be a GIS question.
    – Vince
    Commented Sep 10, 2017 at 3:54
  • Marc, I've already used SHAPE@WKT, but the type of geometry description I need is in WKB format. After that I will have to insert in another sowftware.
    – Machado
    Commented Sep 10, 2017 at 14:17
  • Vince, I didn't even know this format until I needed to use it in other software that the shapefile input is by this format (WKB).
    – Machado
    Commented Sep 10, 2017 at 14:20
  • 1
    But the WK in WKB and WKT stands for "well known"! Commented Sep 11, 2017 at 3:16
  • Thanks Marc for your answer. Well, this software is EPANET and, to my knowledge, it uses background QGIS. So, my demand was to convert the polyline geometry to text file to import into this EPANET.
    – Machado
    Commented Sep 11, 2017 at 12:47
1

My final code was:

# Import arcpy module

import sys
import string
import os
import arcpy
import binascii

# Global variables:

mxd = arcpy.mapping.MapDocument("current")

IN_FC = arcpy.GetParameterAsText(0)

OUT_Addre = arcpy.GetParameterAsText(1)

OUT_Name = arcpy.GetParameterAsText(2)

with arcpy.da.SearchCursor(IN_FC, ["OID@", "SHAPE@WKB"]) as cursor:

# output
    for row in cursor:
        wkb = row[1]
        output_file = open(OUT_Addre + os.sep + OUT_Name + ".txt", 'a')
        output_file.write(binascii.hexlify(wkb) + '\n')
        output_file.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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