I have georeferenced tiff, gdalinfo output:
Driver: GTiff/GeoTIFF
Files: generated.tiff
generated.tiff.aux.xml
Size is 6941, 4886
Coordinate System is `'
GCP Projection =
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4326"]]
GCP[ 0]: Id=1, Info=
(0,0) -> (0.01,0.05886,0)
GCP[ 1]: Id=2, Info=
(6941,0) -> (0.07941,0.05886,0)
GCP[ 2]: Id=3, Info=
(6941,4886) -> (0.07941,0.01,0)
GCP[ 3]: Id=4, Info=
(0,4886) -> (0.01,0.01,0)
Metadata:
AREA_OR_POINT=Area
Software=paint.net 4.0
Image Structure Metadata:
INTERLEAVE=BAND
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 4886.0)
Upper Right ( 6941.0, 0.0)
Lower Right ( 6941.0, 4886.0)
Center ( 3470.5, 2443.0)
There is second file containing a map marker image - called marker1.png (36x60 pixels).
I want to overlay marker1.png on top of the above generated.tiff - so that its top left corner is located at coordinates 0.037,0.025 of the geotiff file. Visually the result should look like a google map with a single marker on top of it.
How would I go about achieving that?
I have managed to partially implement this, but I am not sure whether this is the right path.
import gdal
gdal.UseExceptions()
s = gdal.Open('generated.tiff')
drv = gdal.GetDriverByName("VRT")
vrt = drv.CreateCopy('test.vrt', s, 0)
band = vrt.GetRasterBand(1)
source_path = 'marker1.png'
source_band = 1
x_size = 36
y_size = 60
x_block = 36
y_block = 1
x_offset = 0
y_offset = 0
x_source_size = 36
y_source_size = 60
dest_x_offset = 2000
dest_y_offset = 2000
x_dest_size = 36
y_dest_size = 60
simple_source = '<SimpleSource><SourceFilename relativeToVRT="1">%s</SourceFilename>' % source_path + \
'<SourceBand>%i</SourceBand>' % source_band + \
'<SourceProperties RasterXSize="%i" RasterYSize="%i" DataType="Byte" BlockXSize="%i" BlockYSize="%i"/>' % (x_size, y_size, x_block, y_block) + \
'<SrcRect xOff="%i" yOff="%i" xSize="%i" ySize="%i"/>' % (x_offset, y_offset, x_source_size, y_source_size) + \
'<DstRect xOff="%i" yOff="%i" xSize="%i" ySize="%i"/></SimpleSource>' % (dest_x_offset, dest_y_offset, x_dest_size, y_dest_size)
band.SetMetadata({'source_0': simple_source}, "new_vrt_sources")
band.SetMetadataItem("NoDataValue", '1')
p = gdal.GetDriverByName("PNG")
p.CreateCopy('result.png', vrt, 0)
vrt = None
This uses pixel coordinates instead of geographical ones (but that conversion is easy), however the marker images show up as black blobs (but with right dimensions) - looks like the palette might be wrong?
I tried multiple different approaches, none worked properly - colors were wrong, transparency was wrong or incorrect.
Finally I just did it with the help of PIL, with the code below. Its just a few lines, its actually readable (as opposed to anything I could think up using gdal) and most importantly - it works.
Of course, it can be improved.
from PIL import Image, ImageFont, ImageDraw
from osgeo import gdal,ogr
image = 'generated.tiff'
src_ds = gdal.Open(image)
gt = src_ds.GetGeoTransform() # used to convert geographical coordinates to pixel coordinates
font = ImageFont.truetype("sans-serif.ttf", 16)
img = Image.open(image)
def add_marker (gt, watermark, font, img, mx, my, text):
px = int((mx - gt[0]) / gt[1]) #x pixel
py = int((my - gt[3]) / gt[5]) #y pixel
wmark = Image.open(watermark)
draw = ImageDraw.Draw(wmark)
draw.text((12, 10), text, (0, 0, 0), font=font)
img.paste(wmark, (px, py), wmark)
add_marker(gt, 'marker1.png', font, img, 0.012, 0.0132, "1")
img.save("result.png", "PNG")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With