0

I have numpy array that I know bbox for and can transform like this:

import rasterio.plot
from rasterio.transform import from_bounds

t = from_bounds(minx, miny, maxx, maxy, sorted_np[3,:,:,7].shape[0], sorted_np[3,:,:,7].shape[1])

rasterio.plot.show(sorted_np[3,:,:,7],transform=t)

enter image description here

I want to open() this into rasterio transformed as above.


Using the comment below, tried writing new dataset as:

new = rasterio.open('new.tif', 
'w', driver='GTiff', 
height=sorted_np[3,:,:,7].shape[0], 
width=sorted_np[3,:,:,7].shape[1], 
dtype=sorted_np[3,1,1,7].dtype, 
count=1, crs='+proj=latlong', transform=t)

new.write(sorted_np[3,:,:,7], 1)
new.close()

When I go:

rasterio.plot.show(new)

I get error:

TypeError: Image data cannot be converted to float

FYI:

print(sorted_np[3,1,1,7].dtype) 

float32

print(sorted_np[3,:,:,7])

[[0.2168 0.2067 0.1924 ... 0.2053 0.196  0.1746]
 [0.2237 0.2218 0.2158 ... 0.2035 0.197  0.1734]
 [0.2251 0.2266 0.2251 ... 0.2018 0.1956 0.181 ]
 ...
 [0.1611 0.1637 0.1227 ... 0.1167 0.1206 0.1187]
 [0.1486 0.1661 0.1788 ... 0.1136 0.1175 0.1193]
 [0.1652 0.166  0.1952 ... 0.1141 0.1172 0.1203]]

It would be nice to not have to write a temporary tif file to do this because that seems inefficient.

  • Have you tried adapting the examples in the documentation for writing a dataset? – user2856 Aug 9 '18 at 5:34
  • Your question is not clear. What do you have, what do you want, what have you tried so far and why is that not sufficient? – bugmenot123 Aug 9 '18 at 8:48
  • OK fair comment. I thought maybe rasterio could hold the data array as a variable with spatial attributes without have to store it on disk. Anyhow I have my TIFF with data in it, wondering why it won't plot now? – Ben Aug 10 '18 at 5:09
  • 1
    Ahh ok, I understand now. Not yet, but support for that looks like it's on the way github.com/mapbox/rasterio/issues/798 Have you considered xarray? – user2856 Aug 10 '18 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.