def gen_geotiff(centerlat,centerlong,tilemaxlat,tileminlat,uid,f_name,f_tiff_name): from osgeo import gdal from osgeo import osr import numpy as np import os,sys import cv2 import csv f_name= uid+'_'+f_name+'.png' f_tiff_name = uid+'_'+f_tiff_name n_file = cv2.imread(f_name) orig_h,orig_w,orig_rgb = n_file.shape w,h = orig_h,orig_w if w<400: w_new = 600 h_new = 600 else: w_new = 600 h_new = 600 n_file = cv2.resize(n_file,(w_new,h_new)) image_size = (w_new,h_new) abc =0 with open('TileBounds.csv','rt') as f: data= csv.reader(f) for row in data: #print('row') #print(row) #print(row[0]) if abc ==0: PixelsPerLat = row[0] else: PixelsPerLong = row[0] abc=abc+1 #PixelsPerLat = abs(10980/(tilemaxlat - tileminlat)) LatsPerPixel = 1/float(PixelsPerLat) LongsPerPixel = 1/float(PixelsPerLong) #PixelsPerLong = abs(10980/(tilemaxlong - tileminlong)) newfieldminlat =centerlat-w*LongsPerPixel/2 newfieldmaxlat =centerlat+w*LongsPerPixel/2 newfieldminlong =centerlong-h*LatsPerPixel/2 newfieldmaxlong =centerlong+h*LatsPerPixel/2 lat = [newfieldminlat, newfieldmaxlat] lon = [newfieldminlong, newfieldmaxlong] #print(lat) #print(lon) b_pixels,g_pixels,r_pixels = cv2.split(n_file) nx = image_size[0] ny = image_size[1] xmin,ymin,xmax,ymax = [newfieldminlong, newfieldminlat, newfieldmaxlong, newfieldmaxlat] xres = (float(xmax)-float(xmin))/float(nx) yres = (float(ymax)-float(ymin))/float(ny) xmin = float(xmin) ymin = float(ymin) xmax = float(xmax) ymax = float(ymax) xres = float(xres) yres = float(yres) print('res') #print(xres) #print(yres) geotransform = (xmin,xres,0,ymax,0,-yres) dst_ds = gdal.GetDriverByName('GTiff').Create(f_tiff_name,ny,nx,3,gdal.GDT_Byte) dst_ds.SetGeoTransform(geotransform) srs = osr.SpatialReference() #srs.ImportFromEPSG(3857) srs.ImportFromEPSG(4326) dst_ds.SetProjection(srs.ExportToWkt()) dst_ds.GetRasterBand(1).WriteArray(r_pixels) dst_ds.GetRasterBand(2).WriteArray(g_pixels) dst_ds.GetRasterBand(3).WriteArray(b_pixels) dst_ds.FlushCache() dst_ds=None