Skip to content

Instantly share code, notes, and snippets.

@TimMcCauley
Created November 8, 2021 20:44
Show Gist options
  • Save TimMcCauley/b72d430733c3e283a6b71e7e09aee40a to your computer and use it in GitHub Desktop.
Save TimMcCauley/b72d430733c3e283a6b71e7e09aee40a to your computer and use it in GitHub Desktop.
import subprocess
from osgeo import gdal
unreferenced_pdf = '851724.603697945_6734206.62182539_852044.603697945_6734631.62182539.pdf'
a_ullr = unreferenced_pdf.replace('.pdf', '').split('_')
#Open your Unreferenced PDF
src = gdal.Open(unreferenced_pdf)
ulx = a_ullr[2]
uly = a_ullr[3]
lrx = a_ullr[0]
lry = a_ullr[1]
print(ulx, uly, lrx, lry)
create_cmd = f'gdal_create -outsize 20 20 -a_srs EPSG:3857 -a_ullr {ulx} {uly} {lrx} {lry} -burn 10 out.tif'
process = subprocess.Popen(create_cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
#Open the Tiff to obtain its data from
geoTiff = gdal.Open('out.tif')
#Obtain its Projection system and its Geotransform
coords = geoTiff.GetProjection()
gt = geoTiff.GetGeoTransform()
print(coords, gt)
src.SetProjection(coords)
src.SetGeoTransform(gt)
#Instantiate a PDF driver and save your Referenced copy
pdf_driver = gdal.GetDriverByName('PDF')
dst = pdf_driver.CreateCopy('referenced_pdf.pdf', src, 1)
translate_cmd = 'gdal_translate -of GTiff -co TILED=YES -co TFW=YES referenced_pdf.pdf referenced_geotiff.tiff'
process = subprocess.Popen(translate_cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment