I am trying to create a raster from an ascii text file in python through the arcpy tool ASCIIToRaster_conversion(). I have the ascii text formatted as so:
NCOL 39
NROWS 54
XLLCORNER 2760.091202
YLLCORNER -1253.840905
CELLSIZE 2.0
NODATA_VALUE -99999
5.789899100000000232e+01 2.760091202000000067e+03 -1.253840905000000021e+03
6.423454300000000217e+01 2.762091202000000067e+03 -1.253840905000000021e+03
I have created the raster file through make x y event layer and the details all match up bar the nodata value, which is left blank.
The code used to create the ascii is:
xmin = np.min(myArray[:,1])
ymin = np.min(myArray[:,2])
cell_size = "2"
col = np.max(myArray[:,1]) - xmin
row = np.max(myArray[:,2]) - np.min(myArray[:,2])
header = "NCOL " + str(col) + "\n".format(myArray.shape[1])
header += "NROWS " + str(row) + "\n".format(myArray.shape[0])
header += "XLLCORNER " + str(xmin)+ "\n"
header += "YLLCORNER " + str(ymin)+ "\n"
header += "CELLSIZE " + str(cell_size) + "\n"
header += "NODATA_VALUE -99999"
np.savetxt("myGrid.asc", myArray, delimiter = " ",header = header, comments="")
The code used to open the ascii text and put it through the raster converter is:
Workspace= "D:/Documents/PhD/GIS/Odhran"
arcpy.ASCIIToRaster_conversion("mygrid.asc", Workspace, "INTEGER")
The associated error message is:
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.6\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "D:\Documents\Csv_to_raster_sub.py", line 12, in <module>
Workspace, "INTEGER")
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\conversion.py", line 2461, in ASCIIToRaster
raise e
ExecuteError: ERROR 010158: Unable to open file MYGRID.ASC.
ERROR 010067: Error in executing grid expression.
Failed to execute (ASCIIToRaster).
Sorry, but why are you specifying INTEGER when the data is floats
Sorry that was a typo the actual code has float specified
Ascii to raster will only deal with a one dimensional array I think.
Your myArray appears to have several dimensions. What is the shape of myArray?
[[ 57.898991 2760.091202 -1253.840905] [ 64.234543 2762.091202 -1253.840905] [ 58.50209 2764.091202 -1253.840905] ... [ 206.286987 2832.091202 -1147.840905] [ 181.628342 2834.091202 -1147.840905] [ 154.994568 2836.091202 -1147.840905]]
That looks like a "3 band" dataset.
I think that you will only be able to make a raster using AsciiToRaster using 1 band at a time.
So create a *.asc using list position 0, then 1 etc
This is what it looks like when you print it:
[[ 57.898991 2760.091202 -1253.840905] [ 64.234543 2762.091202 -1253.840905] [ 58.50209 2764.091202 -1253.840905] ... [ 206.286987 2832.091202 -1147.840905] [ 181.628342 2834.091202 -1147.840905] [ 154.994568 2836.091202 -1147.840905]]
- you are sending the output to an Esri grid named "D:/Documents/PhD/GIS/Odhran", correct?
- specify the full path to mygrid.asc
- these ASCII files are actually raster datasets and you could use CopyRaster ipo ASCIIToRaster
It may be failing because you are expressing the number using notation? Try make your numbers without this, have a look here.