"Copy" Image coordinates to another image that is nd.array
Save result array (rf_array
) as in the following lines:
rf_array = df_join['Prediction'].values # returns numpy.ndarray
rf_array = rf_array.reshape(img.shape[0], img.shape[1])
# rf_array = rf_array.reshape(869, 1202)
with rasterio.open('path/to/new.tif',
'w',
driver='GTiff',
height=rf_array.shape[0],
width=rf_array.shape[1],
count=1,
dtype=rf_array.dtype,
crs=img.crs,
nodata=None, # change if data has nodata value
transform=img.transform) as new_file:
new_file.write(rf_array, 1)
You must define 2 elements in order to have a geolocated image
The first is the geotransform that converts the row/column coordinates into X/Y coordinates. In your case this will be done using SetGeotransform. The geotransform is a vector with X coordinate of the origin, the size in X from column value, change in X from row value , the Y coordinate of origin, the change in Y by column value, the size in Y by row value. As you can see, this is not the same order as in the affine transform, which is : a = width of a pixel b = row rotation (typically zero) c = x-coordinate of the upper-left corner of the upper-left pixel d = column rotation (typically zero) e = height of a pixel (typically negative) f = y-coordinate of the of the upper-left corner of the upper-left pixel
So in your case the geotransform will be:
dataset.SetGeoTransform([208810,10,0,7583530,0,-10,])
so that
Xgeo = GT(0) + colval*GT(1) + rowval*GT(2)
Ygeo = GT(3) + colval*GT(4) + rowval*GT(5)
The second is the coordinate system corresponding to your image
You could define it based on the EPSG code, e.g.
srs = osr.SpatialReference()
srs.ImportFromEPSG(your_EPSG_code)
dataset.SetProjection(srs.ExportToWkt())
or get it from another dataset
dataset.SetProjection(inputdataset.GetProjection())