AnsweredAssumed Answered

Raster to ASCII inserts extra spaces in header?!

Question asked by gabrisch on Sep 6, 2016
Latest reply on Sep 18, 2016 by lpinner

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"

Outcomes