0

I have the georeferenced image with coordinates values like (475224.0, 4186282.0).The dimension of my image is (647, 2180). ie there are 647 columns and 2180 rows. I would like to take the coordinate values into a numpy array with size (647, 2180), so that I will get the coordinates of each pixel as an array. I code like below.

rr = rasterio.open(fname) #fname is the georefered image
col = rr.width
row = rr.height
coord = np.empty(shape=(col,row),dtype=rr.dtypes[0])

for i in range(0,col):
    for j in range(0,row):
        coord[i,j] = rr.transform*(i,j)

The problem is rr.transform*(i,j) will give values like (475224.0, 4186282.0). How to save it into a cell. For the above program, I'm getting error like below

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 3, in coord[i,j] = rr.transform*(i,j) ValueError: setting an array element with a sequence.

bibinwilson
  • 348
  • 2
  • 6
  • 20

1 Answers1

1

Assuming your rr.transform*() output is a valid Python tuple I think you are doing this a bit more complicated than it has to be. By default, numpy will handle tuples and lists equal when creating and/or assigning to np.array:s. Thus, a much simpler solution for you, would be to just add an extra dimension and assign your values directly:

rr = rasterio.open(fname) #fname is the georefered image
col = rr.width
row = rr.height
coord = np.empty(shape=(col,row,2)

for i in range(0,col):
    for j in range(0,row):
        coord[i,j] = rr.transform*(i,j)

As you can see, the only difference is that I add extra dimensions to fit the size of rr. Here I have hard coded the third dimension to 2. It is probably possible to find that dynamically, from the rr object instead. In the general case we are not limited to a tupleof two values.

JohanL
  • 6,671
  • 1
  • 12
  • 26