Skip to content

Instantly share code, notes, and snippets.

@LucHermitte
Created January 28, 2021 15:55
Show Gist options
  • Save LucHermitte/7ea9363965c88eb7962461618f25c4a9 to your computer and use it in GitHub Desktop.
Save LucHermitte/7ea9363965c88eb7962461618f25c4a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ******************************************************************************
#
# Name: copy-projection.py (forked from gdalcopyproj.py)
# Project: GDAL Python Interface
# Purpose: Duplicate the geotransform and projection metadata from
# one raster dataset to another, which can be useful after
# performing image manipulations with other software that
# ignores or discards georeferencing metadata.
# Author: Schuyler Erle, [email protected]
#
# ******************************************************************************
# Copyright (c) 2005, Frank Warmerdam
# Copyright (c) 2009-2011, Even Rouault <even dot rouault at spatialys.com>
# Copyright (c) 2020-2021, Luc Hermitte, CSGroup France, handle missing
# projection, or missing geotransform, or missing GCP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ******************************************************************************
import sys
import logging
import click
from osgeo import gdal
@click.command()
@click.version_option('1.0.0')
@click.argument('reference_filename', type=click.Path(exists=True))
@click.argument('updated_filename', type=click.Path(exists=True))
def main(reference_filename, updated_filename):
"""
Copy Projection/GetGeoTransform/GCP from reference_filename to updated_filename.
"""
try:
logging.info("Copying Projection/GetGeoTransform/GCP from %s to %s",
reference_filename, updated_filename)
dataset_reference = gdal.Open(reference_filename)
if not dataset_reference:
raise RuntimeError("Cannot open "+reference_filename+" with GDAL")
dataset_updated = gdal.Open(updated_filename, gdal.GA_Update)
if not dataset_reference:
raise RuntimeError("Cannot open "+updated_filename+" with GDAL for modifications")
projection = dataset_reference.GetProjection()
geotransform = dataset_reference.GetGeoTransform()
gcp_count = dataset_reference.GetGCPCount()
if projection:
dataset_updated.SetProjection(projection)
else:
logging.info("No projection found in %s", reference_filename)
if geotransform and geotransform != (0, 1, 0, 0, 0, 1):
dataset_updated.SetGeoTransform(geotransform)
else:
logging.info("No geotransformation found in %s", reference_filename)
if gcp_count:
dataset_updated.SetGCPs(dataset_reference.GetGCPs(), dataset_reference.GetGCPProjection())
else:
logging.info("No GCP found in %s", reference_filename)
except Exception as ex:
logging.exception('Error: %s', ex)
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment