Georeferencing an image that I know four corner points, CRS and their coordinates with GDAL or Rasterio in Python

georeferencinggeotiff-tiffpythonrasterio

I have some scan files with spatial information.
My Original Image
Original Image
In the example image, the rectangular area drawn with the blue line is my area containing spatial information. In the four corners of the rectangle, there is information that gives the coordinate value of the corner it is in.
Top Left Of Raster
Top Left
Top Right Of Raster
Top Right
Bottom Left Of Raster
Bottom Left
Bottom Right Of Raster
Bottom Right

As you can see, the coordinates of the EPSG:2320 Coordinate System are written on all 4 corners.
I tried to make georeference at Python code below. But it gave me a very different result. In the program it is going to the real-world domain of the raster. However, it rotates and inverts the image even though I enter the position of the corners in the image and the coordinate information in the real world correctly.

My code:

    """ PINDEX CORNER X,Y(IMAGE) AND EPSG:2320 COORDÄ°NATES 

                    IN IMAGE  | EPSG:2320
                    ----------|----------------------
    TOP LEFT:       796, 569  | 443893.7, 4420713.72
    TOP RIGHT       5005, 598 | 444428.05,4420709.81
    BOTTOM RIGHT    4965,6065 | 444423,4420015.85
    BOTTOM LEFT     754, 6035 | 443888.6,4420019.76
    """

    import rasterio as rio
    from rasterio.transform import from_gcps
    from rasterio.control import GroundControlPoint

    file_path="ciktilar/"+i

    tl = GroundControlPoint(796, 569, 443893.7, 4420713.72)   #top left
    bl = GroundControlPoint(5005, 598, 444428.05,4420709.81)    #top right
    br = GroundControlPoint(4965,6065, 444423,4420015.85)     #bottom right
    tr = GroundControlPoint(754, 6035, 443888.6,4420019.76)    #bottom left
    gcps = [tl, bl, br, tr]

    transform = from_gcps(gcps)
    crs = 'epsg:2320'

    with rio.open(file_path, 'r+') as ds:
        ds.crs = crs
        ds.transform = transform

My Result GeoTIF
My Result GeoTIF
The result image was added to the GIS program and taken from the screen.
How do I do the correct Georeference?
Any ideas?

Best Answer

You have two alternatives:

  1. Change the order of pixel coordinates into row-column order for rasterio as documented in https://rasterio.readthedocs.io/en/latest/topics/transforms.html#using-ground-control-points
    tl = GroundControlPoint(569, 796, 443893.7, 4420713.72)   #top left
        bl = GroundControlPoint(598, 5005, 444428.05,4420709.81)    #top right
        br = GroundControlPoint(6065, 4965, 444423,4420015.85)     #bottom right
        tr = GroundControlPoint(6035, 754, 443888.6,4420019.76)    #bottom left
  1. Keep the order of pixel coordinates as they are but add ground control points with GDAL, for example https://gdal.org/programs/gdal_translate.html#cmdoption-gdal_translate-gcp

gdal_translate -gcp 796 569 443893.7 4420713.72 -gcp 5005 598 444428.05 420709.81 -gcp 4965 6065 444423 4420015.85 -gpc 754 6035 443888.6 420019.76 input.tif with_gcp.tif

Then you can use gdalwarp

gdalwarp -t_srs epsg:2320 with_gcp.tif final.tif

Related Question