2

I am trying to take a plain JPEG with no spatial references, apply ground control points (GCPs) to it, and output a georeferenced GeoTIFF or JPEG with world files.

I found some code that seemed like it might work for my task, and adapted it to my situation. The code produces an object of type osgeo.gdal.Dataset, and my understanding is that I want to save this object as a georeferenced image, but what I'm trying doesn't output a georeferenced file.

Here's what my code looks like:

from osgeo import gdal 
import osr

inputImage = "Example_image.jpg"
outputImage = "image_gdal.jpg"

dataset = gdal.Open(inputImage) 
I = dataset.ReadAsArray(0,0,dataset.RasterXSize,dataset.RasterYSize)

outdataset = gdal.GetDriverByName('GTiff') 
output_SRS = osr.SpatialReference() 
output_SRS.ImportFromEPSG(2193) 
outdataset = outdataset.Create(outputImage,dataset.RasterXSize,dataset.RasterYSize,I.shape[0]) 
for nb_band in range(I.shape[0]):
    outdataset.GetRasterBand(nb_band+1).WriteArray(I[nb_band,:,:])

# Creating ground control points (not sure if I got the order of variables right):
gcp_list = [] 
gcp_list.append(gdal.GCP(176.412984, -310.977264, 1681255.524654, 6120217.357425))
gcp_list.append(gdal.GCP(160.386905, -141.487145, 1681158.424227, 6120406.821253))
gcp_list.append(gdal.GCP(433.204947, -310.547238, 1681556.948690, 6120335.658359))

outdataset.SetProjection(output_SRS.ExportToWkt()) 
wkt = outdataset.GetProjection() 
outdataset.SetGCPs(gcp_list,wkt)

# Another attempt to export the image:
gdal.Warp("output_image.tif", outdataset, dstSRS='EPSG:2193', format='GTiff')

outdataset = None

The above code produces a JPEG file called "image_gdal.jpg", however this JPEG does not have any spatial reference associated with it.

In the second-last line of the above code I tried another strategy for generating a georeferenced output file (this time a TIFF file), but the file does not get created at all, although I don't get any errors when running the script.

The "Example_image.jpg" is located here: https://imgur.com/a/9ThHtOz. It'd be great if you could run my code and help me troubleshoot it.

I'm very new to working with geospatial data in Python, and I might be overlooking something fundamental, or might have multiple small errors in the script that prevent it from working properly.

  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Please check out our short tour to learn about our focussed Q&A format. – PolyGeo May 6 at 3:23
  • I would learn the workflow first with gdal_translate (set the ground control points) and gdalwarp binaries (create north-up georeferenced image). Your ground control points seem to be outside the image (negative "lines" value). It is OK for GDAL but it that what you mean? – user30184 May 6 at 6:26
  • @user30184 Would you by any chance be able to write up a script showing how I could use gdal_translate and gdalwarp for my purpose? I'm afraid I don't know where to start. Also, I obtained the ground control points by georeferencing a spatially identical (but temporally different) image in ArcGIS, so I'm guessing it's probably ok that some of the references are negative/outside the image – Kat May 6 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.