arcpy.NumPyArrayToRaster

2823
6
10-04-2011 07:15 PM
SarahBurns
New Contributor
I would like to use arcpy.NumPyArrayToRaster to import a numpy array and use in arcGIS10 as a raster.
This function works fine except I did not enter the lower left corner (in map units) and the x and y cell size, therefore my image was upside down!
Is there a simple command I can run in python on my numpy array to find out what the lower left corner is??
thanks
Tags (2)
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus
>>> import numpy as np
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> b = np.array(a)
>>> b
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> b.shape
(3, 3)
>>> 

the array's shape will give you the number of rows and columns, but not its coordinates nor its cell size.
0 Kudos
PhilMorefield
Occasional Contributor III
I would like to use arcpy.NumPyArrayToRaster to import a numpy array and use in arcGIS10 as a raster.
This function works fine except I did not enter the lower left corner (in map units) and the x and y cell size, therefore my image was upside down!
Is there a simple command I can run in python on my numpy array to find out what the lower left corner is??
thanks


If your numpy array was originially a raster, you can export that raster to an ASCII file and get the coordinates of the lower left corner from the output('xllcorner' and 'yllcorner').

Note that, based on my own experience, you have to input coordinates for the lower left corner as a Point object:

corner = arcpy.Point(xllcorner, yllcorner)
myRaster = arcpy.NumpyArrayToRaster(myArray, corner, cellSizeX, cellSizeY)

Also note that you can quite easily flip a numpy array:

flipped_array = numpy.flip_ud(myArray).
0 Kudos
SarahBurns
New Contributor
Thanks for the feedback, especially the point/corner idea.
I eventually found lower left corner (print LONmin() LATmin() for netcdf file) and cell size (simply by printing all the lats and lons in python and then minusing one from the other, not the prettiest method but it gave me what I needed)
but I seem to still be producing an upside down raster!
I have tried numpy.flipud and from the documentation this appears to be the best method but the results are strange lines.
Maybe to solve this problem I need to go back to the original array?
Is there some way I should write my array to a file that will make it easier to import into arcmap as a raster?
I am currently using
savetxt("myfile.txt", myarray)

any feedback will be greatly appreciated.
0 Kudos
PhilMorefield
Occasional Contributor III
Thanks for the feedback, especially the point/corner idea.
I eventually found lower left corner (print LONmin() LATmin() for netcdf file) and cell size (simply by printing all the lats and lons in python and then minusing one from the other, not the prettiest method but it gave me what I needed)
but I seem to still be producing an upside down raster!
I have tried numpy.flipud and from the documentation this appears to be the best method but the results are strange lines.
Maybe to solve this problem I need to go back to the original array?
Is there some way I should write my array to a file that will make it easier to import into arcmap as a raster?
I am currently using
savetxt("myfile.txt", myarray)

any feedback will be greatly appreciated.



Now that you mention it, I recall getting weird results with np.flipud as well. The solution I found was to re-declare my data as an array, like so:

myArrayFlipped = np.array(np.flipud(myArray))

If worst comes to worse you could flip the array yourself with some slicing, but yuck. What a pain.

Good luck!
0 Kudos
GraziaZulian
New Contributor II
anyway here
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000026000000.htm
some more and more complete examples could be helpful.
0 Kudos