0

EDIT: Solved it myself. Beginners error... :/

I am writing a python Toolbox that should use gdal_translate to convert an ASCII Gridded XYZ-File into a lower resolution (and doing afterwards a couple of other things).

For that I have written the following snippet:

filepath=r'X:\some_path\to\file\with_only_ascii_and_no_whitespaces\file.xyz'
try:
    inf=filepath
    outf=filepath + '__dgm20.xyz'
    result = subprocess.check_output(
        [
            'gdal_translate', 
            '-of', 
            'XYZ', 
            '-tr',
            '20',
            '20', 
            inf.encode(locale.getpreferredencoding()), 
            outf.encode(locale.getpreferredencoding())
        ], 
        stderr=subprocess.STDOUT
    )
except subprocess.CalledProcessError as err:
    arcpy.AddWarning(unicode(err))
    arcpy.AddWarning(err.cmd)
    arcpy.AddError(err.output)
else:
    arcpy.AddMessage(filepath + '__dgm20.xyz')
    arcpy.AddMessage(result)

When I run this from the python console in cmd.exe or the ArcGIS python console, everything is fine, my file will be created. But when I run it from within a python toolbox (after retrieving filepath from a parameter and normalizing it with os.path) I get this error:

ERROR 1: At line 3999971, found 1 tokens. Expected 3 at least

The processed file (filepath) ist totally fine, I think.

So, why is this happening? What is it I can't figure out?

0

I solved my problem myself. But sadly it wasn't caused by the code snippet I provided.

The indentation was the problem. The csv hasn't been fully written down, when i tried to start working with it in gdal_translate.

filepath=r'X:\some_path\to\file\with_only_ascii_and_no_whitespaces\file.xyz'
with open(filepath, 'r') as xyzfile:
    with open(filepath + '.txt', 'wb') as csvfile:
        dialect = csv.Sniffer().sniff(xyzfile.read(2048), " ")
        xyzfile.seek(0)
        df = pandas.read_csv(xyzfile, dialect=dialect, header=None)
        df = df.dropna(axis=1, how='all')
        f = df.columns.values.tolist()
        df = df.sort_values(by=[f[1], f[0]])
        df.to_csv(csvfile, index=None, header=['x','y','z'])

        try: #here starts the above snippet
            inf=filepath + '.txt'
            outf=filepath + '__dgm20.xyz'
            [...]

correct is:

filepath=r'X:\some_path\to\file\with_only_ascii_and_no_whitespaces\file.xyz'
with open(filepath, 'r') as xyzfile:
    with open(filepath + '.txt', 'wb') as csvfile:
        [...]
try:
    inf=filepath + '.txt'
    outf=filepath + '__dgm20.xyz'
    result = subprocess.[...]

Sorry for any caused loss of time.

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.