Raster to ASCII inserts extra spaces in header?!

1336
3
09-06-2016 11:11 AM
GerryGabrisch
Occasional Contributor III

I am testing a Python script where an ESRI shapefile is converted to a raster (based on FID).  The resulting raster is converted to ASCII for further processing.  Interestingly the tool worked great then started giving errors.  The source of the error is that the output ASCII file is now inserting extra spaces in the header of the ASCII file where earlier in the day it was not. Here is an example of the problematic ASCII output.

ncols         17
nrows         41
xllcorner     1193433.4604796
yllcorner     659603.16649559
cellsize      10
NODATA_value  -9999

The column of numbers have extra spaces resulting in a left justified column of data when only one space should exist between the names (ncols, nrows...) and the data (17, 41).

What would cause this behavior?  Am I stuck with using regular expressions to purge the extra white spaces? 

try:
    print "Split polygon by percent area from the top down..."
    import sys, traceback, os
    import arcpy
    
    arcpy.env.overwriteOutput = True

    ########################################################################################
    #The Input Feature Class or projected data to Divide into Percents...
    inFC = r"Z:\GISpublic\GerryG\PythonScripts\SplitPolygonByPercent\testData\testdata.shp"
    #The rasterized version of the polygon...
    outRas = r"Z:\GISpublic\GerryG\PythonScripts\SplitPolygonByPercent\outRas"
    #The cell size of the outRas-smaller is better but takes longer to process..
    outCellSize = 10
    #Useer defined precent by area divisions...will split a polygons into 60%, 20%, and 20%
    divisions = [60,25,15]
    #ASCII version of the raster data....
    outASCII = r"Z:\GISpublic\GerryG\PythonScripts\SplitPolygonByPercent\testdata.asc"
    #ASCII version of the split by area data
    splitASCII = r"Z:\GISpublic\GerryG\PythonScripts\SplitPolygonByPercent\Split.asc"
    #The Output Raster....
    FinalRaster = r"Z:\GISpublic\GerryG\PythonScripts\SplitPolygonByPercent\PercentArea"
    
    ##########################################################################################
    
    #Holds the total area of each percentage....
    areaPercents = []
    #Get the polygon area...
    rows = arcpy.SearchCursor(inFC)
    shapeName = arcpy.Describe(inFC).shapeFieldName
    for row in rows:
        feat = row.getValue(shapeName)
        polygonArea = feat.area
    #Calculate the areas for each percent division    
    for item in divisions:
        areaPercents.append(polygonArea* item/100.0)    
    print "Converting polygon to ESRI GRID..."
    arcpy.PolygonToRaster_conversion(inFC, "FID",outRas,"", "", outCellSize)
    print "Converting ESRI GRID to ASCII..."
    arcpy.RasterToASCII_conversion(outRas, outASCII)
    print "Create the new ascii file"
    fw = open(splitASCII, 'a')
    #Keep track of the line number to ensure the header gets written correctly to
    #the new .asc file
    linecounter = 1
    cell_area_counter = 0
    thispercentcounter = 0
    with open(outASCII, 'r') as f:
        for line in f:
            #The first 6 lines are header-just copy that over to the new asc file.
            if linecounter <=6:
                fw.write(line)
            #The remaining lines have data...convert them to percent values..
            else:
                #Strip those pesky blank spaces at the start and ends of the line
                line = line.strip()
                linelist = line.split(" ")
                newlinelist = []
                for item in linelist:
                    if item =="-9999":
                        newlinelist.append(item)
                    else:
                        #thispercentcounter +=1
                        cell_area_counter = cell_area_counter + (outCellSize*outCellSize)
                        if cell_area_counter <areaPercents[thispercentcounter]:
                            newlinelist.append(str(divisions[thispercentcounter]))
                            
                        else:
                            thispercentcounter +=1
                            cell_area_counter = 0
                            print  thispercentcounter
                            newlinelist.append(str(divisions[thispercentcounter]))
                            
                fw.write(" ".join(newlinelist)+"\n")
            linecounter+=1
    print "Creating output raster..."
    arcpy.ASCIIToRaster_conversion(splitASCII,FinalRaster, "INTEGER")
    
    
    
    #Take out the trash....    
    os.remove(outASCII)
    os.remove(splitASCII)
    arcpy.Delete_management(outRas)
   
        
    print "Done"
except:
    print "error"
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    print pymsg + "\n"

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

Is the error with your script? or are you saying the tool is introducing the error?

In any event, check for lines where you have added a newline character that perhaps isnt needed, for example, is the '\n' needed in this line?

fw.write(" ".join(newlinelist)+"\n")

0 Kudos
GerryGabrisch
Occasional Contributor III

Dan, no, the original output to ASCII from ArcMap was corrupted somehow. As a test  I could  take  that original exported ASCII and convert it back to a GRID and I would get the error.  Restarting the desktop seems to have had  a magical effect on the output ASCII and I am not getting errors anymore.

FYI fw.write(" ".join(newlinelist)+"\n"), I  already had that.  Thanks again for your help.

0 Kudos
Luke_Pinner
MVP Regular Contributor

The left justified (fixed width) columns in the ASCII grid header lines are perfectly normal and are not corrupt.  Here's an extract of one of my outputs:

ncols         1015
nrows         584
xllcorner     1649685.4143459
yllcorner     -2394132.3096793
cellsize      25
NODATA_value  -9999
1 5 13 24 33 40 47 56 66 etc...‍‍‍‍‍‍‍

If your script is expecting only a single space, then you need to modify your script to parse correctly.

BTW If you're parsing ASCII grids, you're "doing it wrong"™. If you explain a little more about what you are trying to do, I think we can suggest some alternatives.

0 Kudos