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"

  • Luke, I added the whole code I tried to write. – Machado Sep 10 '17 at 14:14
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.

  • WKB could be exported as hexadecimal or base64 strings, but that wouldn't be a GIS question. – Vince Sep 10 '17 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 Sep 10 '17 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 Sep 10 '17 at 14:20
  • 1
    But the WK in WKB and WKT stands for "well known"! – Kirk Kuykendall Sep 11 '17 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 Sep 11 '17 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, privacy policy and cookie policy

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