Thursday, January 30, 2025

Images Georeferencing World Files

 World Files are georeferencing files for images that describe the location, scale, and orientation of an image such geoTiff with .tfw as its world file, JPEG with .jgw as its world file and PNG with .pgw as its world file.

Image world files (.tfw, .jgw, .pgw) are text files that contains geographic information for a raster image. They are used to georeference the image, which means to position the image correctly on a map or in a mapping system.

The .tfw world file is a text file used to georeference the GeoTIFF raster images, like the orthomosaic and the DSM. The .jgw world file is a text file used to georeference the JPEG raster images. The .pgw world file is a text file used to georeference the PNG raster images.

Any of the world files have similar structure which contain 6-line in the text file:

  • Line 1: pixel size in the x-direction in map units (GSD).
  • Line 2: rotation about y-axis.
  • Line 3: rotation about x-axis.
  • Line 4: pixel size in the y-direction in map in map units (GSD).
  • Line 5: x-coordinate of the upper left corner of the image.
  • Line 6: y-coordinate of the upper left corner of the image.

*GSD: Ground Sampling Distance.

As pixels are considered as square lines 1 and 4 are the same.


Georeferencing a JPEG image in AutoCAD using its .jgw World Files

This one used case of the worl files in AutoCAD. The Visual LISP program below adopted from Insert georeferenced image with world file in AutoCAD will select an image that has a jgw world file and georeferencing it appropraitely.

(vl-load-com)
 (defun ss->lst (ss / e n out)
   (setq n -1)
   (while (setq e (ssname ss (setq n (1+ n)))) (setq out (cons (vlax-ename->vla-object e) out)))
 )
 (defun _writefile (filename lst / file result)
   (cond ((and (eq 'str (type filename)) (setq file (open filename "w")))
   (foreach x lst
     (write-line
       (cond ((= (type x) 'str) x)
	     ((= (type x) 'int) (itoa x))
	     ((= (type x) 'real) (rtos x 2 6))
	     ((vl-prin1-to-string x))
       )
       file
     )
   )
   (close file)
   filename
  )
   )
 )
 (defun _readfile (filename / file result)
   (cond
     ((and (eq 'str (type filename)) (setq file (open filename "r")))
      (while (setq line (read-line file)) (setq result (cons (vl-string-trim " " line) result)))
      (close file)
      (reverse result)
     )
   )
 )
 (setq opt "ReadIt")
;  (initget 0 "ReadIt WriteIt")
;  (setq	opt (cond ((getkword (strcat "\nImage World File [ReadIt/WriteIt] <" opt ">: ")))
;		  (opt)
;	    )
; )
 (princ "\nSelect image(s): ")
 (setq pre (getvar 'dwgprefix))
 (if (and (setq ss (ssget '((0 . "image")))) (setq ss (ss->lst ss)))
   (foreach image ss
     (setq name    (vlax-get image 'name)
    hgt	    (vlax-get image 'height)
    wdth    (vlax-get image 'width)
    imhgt   (vlax-get image 'imageheight)
    imwdth  (vlax-get image 'imagewidth)
    rot	    (vlax-get image 'rotation)
    bpt	    (vlax-get image 'origin)
    imgpath (vl-filename-directory (vlax-get image 'imagefile))
    jgw	    (strcat imgpath "\\" name ".jgw")
     )
     (if (= opt "ReadIt")
(progn
  (if (and (or (setq jgw (findfile (strcat pre name ".jgw")))
	       (setq jgw (findfile (strcat imgpath "\\" name ".jgw")))
	       (setq jgw (getfiled (strcat "***Select <<" name ".jgw>>***") pre "jgw" 16))
	   )
	   (setq pre (strcat (vl-filename-directory jgw) "\\"))
	   (setq data (mapcar 'atof (_readfile jgw)))
	   (> (length data) 5)
	   (setq l1 (car data))
	   (setq mvpt (list (nth 4 data) (nth 5 data) 0.0))
      )
    (progn (vla-put-imageheight image (* hgt l1))
	   (vla-put-imagewidth image (* wdth l1))
	   (vla-put-rotation image (cadr data))
	   (setq rot (vlax-get image 'rotation))
	   (setq bpt (polar bpt (+ (/ pi 2.) rot) (* hgt l1)))
	   (vlax-invoke image 'move bpt mvpt)
	   (princ (strcat "\njgw File Read - " jgw))
    )
    (princ "\njgw file NOT found or not correctly formatted!")
  )
)
(progn (setq bpt (polar bpt (+ (/ pi 2.) rot) imhgt))
       (if (setq jgw (_writefile
		       (strcat imgpath "\\" name ".jgw")
		       (list (/ imhgt hgt)
			     rot
			     (strcat "-" (rtos (abs rot) 2 6))
			     (strcat "-" (rtos (abs (/ imwdth wdth)) 2 6))
			     (rtos (car bpt) 2 6)
			     (rtos (cadr bpt) 2 6)
		       )
		     )
	   )
	(print jgw)
	(princ "\nError writing file...")
       )
)
     )
   )
 )
 (princ)

Save the script in a lisp file or just copy and paste it into the vlisp window as shown below. Then run the program and select the image you want to georeference in the AutoCAD current window. this will move, scale, rotate and orient the image to its real-world coordinates position. Zoom to Extend to see the result.


Thank you for reading.

No comments:

Post a Comment