Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to gather coordinates from a .tif image and insert them to PyQt code
Forum Update on Monday, May 27th 2025

How to gather coordinates from a .tif image and insert them to PyQt code

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 952 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john_hobbyist
    wrote on 4 Feb 2021, 18:13 last edited by john_hobbyist 2 Apr 2021, 18:59
    #1

    Hello, I use this code here: https://github.com/kklmn/OrthoView/blob/master/OrthoView.py As I see this part here (line 109):

    class MyToolBar(mpl_qt.NavigationToolbar2QT):
        def set_message(self, s):
            try:
                parent = self.parent()
            except TypeError:  # self.parent is not callable
                parent = self.parent
    
            try:
                sstr = s.split()
    #            print(sstr, len(sstr))
                while len(sstr) > 5:  # when 'zoom rect' is present
                    del sstr[0]
    #            print(sstr)
                x, y = float(sstr[0][2:]), float(sstr[1][2:])
                if parent.canTransform():
                    xC, yC = parent.beamPosRectified
                    if not parent.buttonStraightRect.isChecked():
                        xP, yP = parent.transformPoint((x, y))
                        x0, y0 = (xP-xC)/parent.zoom, (yP-yC)/parent.zoom
                        s = u'image: x={0:.1f} px, y={1:.1f} px\nplate: '\
                            'x={2:.2f} mm, y={3:.2f} mm'.format(x, y, x0, y0)
                    else:
                        x0, y0 = (x-xC)/parent.zoom, (y-yC)/parent.zoom
                        s = 'plate: x={0:.2f} mm, y={1:.2f} mm'.format(x0, y0)
                else:
                    s = u'image: x={0:.1f}, y={1:.1f}'.format(x, y)
            except Exception as e:
                pass
    #            print(e)
    
            if self.coordinates:
                self.locLabel.setText(s)
    

    is involved with the x,y coordinates. I need to do the following: to get the coordinates from the .tif image I load and put them inside the code, so that instead x,y coordinates to show the real coordinates of the .tif. I am thinking of using the gdal package but I do not know which lines I should change on the above code and how to achieve this transformation, can you help me? Thank you...

    1 Reply Last reply
    0
    • J Offline
      J Offline
      john_hobbyist
      wrote on 4 Feb 2021, 21:09 last edited by
      #2

      I am trying to reproduce this interesting code from here: https://stackoverflow.com/questions/50191648/gis-geotiff-gdal-python-how-to-get-coordinates-from-pixel by giving random test values on x, y:

      # open the dataset and get the geo transform matrix
      ds = gdal.Open('final.tif') 
      xoffset, px_w, rot1, yoffset, px_h, rot2 = ds.GetGeoTransform()
      
      x = 100000
      y = 100000
      
      # supposing x and y are your pixel coordinate this 
      # is how to get the coordinate in space.
      posX = px_w * x + rot1 * y + xoffset
      posY = rot2 * x + px_h * y + yoffset
      
      # shift to the center of the pixel
      posX += px_w / 2.0
      posY += px_h / 2.0
      

      but I get this error:

      free(): invalid pointer
      Aborted (core dumped)
      

      Any idea, how to proceed?

      M 1 Reply Last reply 4 Feb 2021, 21:19
      0
      • J john_hobbyist
        4 Feb 2021, 21:09

        I am trying to reproduce this interesting code from here: https://stackoverflow.com/questions/50191648/gis-geotiff-gdal-python-how-to-get-coordinates-from-pixel by giving random test values on x, y:

        # open the dataset and get the geo transform matrix
        ds = gdal.Open('final.tif') 
        xoffset, px_w, rot1, yoffset, px_h, rot2 = ds.GetGeoTransform()
        
        x = 100000
        y = 100000
        
        # supposing x and y are your pixel coordinate this 
        # is how to get the coordinate in space.
        posX = px_w * x + rot1 * y + xoffset
        posY = rot2 * x + px_h * y + yoffset
        
        # shift to the center of the pixel
        posX += px_w / 2.0
        posY += px_h / 2.0
        

        but I get this error:

        free(): invalid pointer
        Aborted (core dumped)
        

        Any idea, how to proceed?

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 4 Feb 2021, 21:19 last edited by mrjj 2 Apr 2021, 21:19
        #3

        @john_hobbyist
        Hi
        Do you have a TIF called final.tif and in a location where it actually finds it?

        else i guess
        ds = gdal.Open('final.tif')
        will result in a invalid ds and
        doing
        rot2 = ds.GetGeoTransform()
        might crash it.

        Purely guessing. sadly i dont know gdal.

        1 Reply Last reply
        1
        • J Offline
          J Offline
          john_hobbyist
          wrote on 4 Feb 2021, 21:25 last edited by
          #4

          @mrjj: instead of

          ds = gdal.Open('final.tif')
          

          I have the full path of the .tif file

          ds = gdal.Open('/home/john/Desktop/UAV_image.tif')
          

          The path is correct, in other code it works just fine

          M 1 Reply Last reply 4 Feb 2021, 21:29
          0
          • J john_hobbyist
            4 Feb 2021, 21:25

            @mrjj: instead of

            ds = gdal.Open('final.tif')
            

            I have the full path of the .tif file

            ds = gdal.Open('/home/john/Desktop/UAV_image.tif')
            

            The path is correct, in other code it works just fine

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 4 Feb 2021, 21:29 last edited by
            #5

            @john_hobbyist
            Hi
            Ok, so its not that.

            so it doesn't say at which line, the error comes?

            J 1 Reply Last reply 4 Feb 2021, 21:33
            1
            • M mrjj
              4 Feb 2021, 21:29

              @john_hobbyist
              Hi
              Ok, so its not that.

              so it doesn't say at which line, the error comes?

              J Offline
              J Offline
              john_hobbyist
              wrote on 4 Feb 2021, 21:33 last edited by john_hobbyist 2 Apr 2021, 23:03
              #6

              @mrjj It only prints this, nothing else:

              free(): invalid pointer
              Aborted (core dumped)
              

              I won't stuck on this searching if there is a better solution to my question on the initial post...

              1 Reply Last reply
              0
              • J Offline
                J Offline
                john_hobbyist
                wrote on 4 Feb 2021, 23:29 last edited by john_hobbyist 2 Apr 2021, 23:30
                #7

                Can I run gdalinfo inside the python code (not on terminal) and get the data I need of the coordinates of the .tif's corners in order to use them on x,y (I don't know how!!) ?

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 5 Feb 2021, 05:53 last edited by
                  #8

                  Please ask in a forum specialized to gdal and image processing. I don't see how this is related to Qt in any way.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1

                  1/8

                  4 Feb 2021, 18:13

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved