Results 1 to 11 of 11

Thread: Converting coordinates system QGraphicScene to Latitude,Lognitude

  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Converting coordinates system QGraphicScene to Latitude,Lognitude

    I have been reviewing the forum posts on converting coordinate systems but I am still confused.

    I am trying to convert the coordinate system of a QGraphicScene (physical) into geographic (latitude, longitude). This is so I can move my airplane object by latitude, longitude.

    Currently I have code that creates the graphic item and will move it within the physical points but I’m not sure how/where to set the Window coordinates to get it to move on latitude, longitude. If you notice in my code I tried on the painter object and that didn’t work.

    Can anyone help?

    Qt Code:
    1. //-----------------------------------------------------------------------------------
    2.  
    3. class CMapView : public QGraphicsView
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. CMapView(QWidget * parent = 0);
    9. };
    10.  
    11. CMapView::CMapView(QWidget * parent)
    12. : QGraphicsView(parent)
    13. {
    14. setDragMode( ScrollHandDrag );
    15. }
    16.  
    17.  
    18. //-----------------------------------------------------------------------------------
    19.  
    20. CFlightPath : public QMainWindow
    21. {
    22. Q_OBJECT
    23.  
    24. public:
    25. CFlightPath();
    26.  
    27. private slots:
    28.  
    29. protected:
    30. void placeAircraft();
    31.  
    32. QGraphicsScene * scene;
    33. CMapView * view;
    34. };
    35.  
    36. CFlightPath::CFlightPath()
    37. {
    38. scene = new QGraphicsScene(0,0,400,400);
    39.  
    40. placeAircraft();
    41.  
    42. view = new CMapView();
    43. view->setScene(scene);
    44.  
    45. setCentralWidget(view);
    46.  
    47. setWindowTitle("Flight Path");
    48.  
    49. mainClose = false;
    50. }
    51.  
    52. void CFlightPath::placeAircraft()
    53. {
    54. CAircraft * airplane = new CAircraft();
    55. ownship->setPos( 200, 300 );
    56.  
    57. scene->addItem( airplane );
    58. }
    59.  
    60. //-----------------------------------------------------------------------------------
    61.  
    62. class CAircraft : public QObject, public QGraphicsItem
    63. {
    64. Q_OBJECT
    65.  
    66. public:
    67. CAircraft();
    68.  
    69. QRectF boundingRect() const;
    70. void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
    71.  
    72. private slots:
    73. void moveAircraft();
    74.  
    75. private:
    76. QPainterPath shape;
    77.  
    78. QTimer * updateTimer;
    79.  
    80. float xPos, yPos, deg, prevDeg;
    81. int count;
    82. };
    83.  
    84.  
    85. CAircraft::CAircraft()
    86. {
    87. double localX = -24.0;
    88. double localY = -14.0;
    89.  
    90. QPolygonF aircraft;
    91.  
    92. aircraft << QPointF( (localX+24.0), (localY+0.0) )
    93. << QPointF( (localX+25.0), (localY+1.0) )
    94. << QPointF( (localX+26.0), (localY+2.0) )
    95. << QPointF( (localX+27.0), (localY+3.0) )
    96. << QPointF( (localX+27.0), (localY+9.0) )
    97. << QPointF( (localX+32.0), (localY+9.0) )
    98. << QPointF( (localX+32.0), (localY+8.0) )
    99. << QPointF( (localX+35.0), (localY+8.0) )
    100. << QPointF( (localX+35.0), (localY+9.0) )
    101. << QPointF( (localX+38.0), (localY+9.0) )
    102. << QPointF( (localX+38.0), (localY+8.0) )
    103. << QPointF( (localX+41.0), (localY+8.0) )
    104. << QPointF( (localX+41.0), (localY+9.0) )
    105. << QPointF( (localX+47.0), (localY+9.0) )
    106. << QPointF( (localX+48.0), (localY+10.0) )
    107. << QPointF( (localX+47.0), (localY+11.0) )
    108. << QPointF( (localX+36.0), (localY+13.0) )
    109. << QPointF( (localX+27.0), (localY+16.0) )
    110. << QPointF( (localX+26.0), (localY+31.0) )
    111. << QPointF( (localX+30.0), (localY+31.0) )
    112. << QPointF( (localX+31.0), (localY+32.0) )
    113. << QPointF( (localX+25.0), (localY+34.0) )
    114. << QPointF( (localX+23.0), (localY+34.0) )
    115. << QPointF( (localX+17.0), (localY+32.0) )
    116. << QPointF( (localX+18.0), (localY+31.0) )
    117. << QPointF( (localX+22.0), (localY+31.0) )
    118. << QPointF( (localX+21.0), (localY+16.0) )
    119. << QPointF( (localX+12.0), (localY+13.0) )
    120. << QPointF( (localX+1.0), (localY+11.0) )
    121. << QPointF( (localX+0.0), (localY+10.0) )
    122. << QPointF( (localX+1.0), (localY+9.0) )
    123. << QPointF( (localX+7.0), (localY+9.0) )
    124. << QPointF( (localX+7.0), (localY+8.0) )
    125. << QPointF( (localX+10.0), (localY+8.0) )
    126. << QPointF( (localX+10.0), (localY+9.0) )
    127. << QPointF( (localX+13.0), (localY+9.0) )
    128. << QPointF( (localX+13.0), (localY+8.0) )
    129. << QPointF( (localX+16.0), (localY+8.0) )
    130. << QPointF( (localX+16.0), (localY+9.0) )
    131. << QPointF( (localX+21.0), (localY+9.0) )
    132. << QPointF( (localX+21.0), (localY+3.0) )
    133. << QPointF( (localX+22.0), (localY+2.0) )
    134. << QPointF( (localX+23.0), (localY+1.0) );
    135.  
    136. shape.addPolygon( aircraft );
    137.  
    138. setFlag(ItemIgnoresTransformations, true);
    139.  
    140. updateTimer = new QTimer();
    141. updateTimer->setInterval( 50 );
    142. connect( updateTimer, SIGNAL(timeout()), this, SLOT(moveAircraft()));
    143. updateTimer->start();
    144.  
    145. xPos = 200.0;
    146. yPos = 300.0;
    147. deg = 270.0;
    148. prevDeg = 0.0;
    149. count = 0;
    150. };
    151.  
    152. QRectF CAircraft::boundingRect() const
    153. {
    154. return QRectF(-24,-14,48,34);
    155. }
    156.  
    157. void CAircraft::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * /*widget*/)
    158. {
    159. painter->setWindow(-100,-100, 200, 200);
    160.  
    161. painter->fillPath( shape, Qt::darkGray );
    162. }
    163.  
    164. void CAircraft::moveAircraft()
    165. {
    166. float x, y;
    167.  
    168. if ( count < 50 )
    169. xPos -= 1.0;
    170. else if (count < 100 )
    171. yPos += 1.0;
    172. else if (count < 150 )
    173. xPos += 1.0;
    174. else if (count < 200 )
    175. yPos -= 1.0;
    176. else if (count < 250 )
    177. {
    178. yPos += 1.0;
    179. xPos -= 1.0;
    180. }
    181. else if (count < 300 )
    182. {
    183. yPos -= 1.0;
    184. xPos += 1.0;
    185. }
    186.  
    187. count++;
    188.  
    189. x = xPos;
    190. y = yPos;
    191.  
    192. setPos(x, y);
    193. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting coordinates system QGraphicScene to Latitude,Lognitude

    Why not simply make the scene extend from (-180,-90) to (180,90)? Or (-180*60,-90*60) to (180*60,90*60) to include minutes.

  3. #3
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting coordinates system QGraphicScene to Latitude,Lognitude

    But will then that make the actuall widget 180 by 360 pixels? Or if I size the widget to be say 400 by 600 will the proposed scene coordinates be used on top of them?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Converting coordinates system QGraphicScene to Latitude,Lognitude

    Quote Originally Posted by db View Post
    But will then that make the actuall widget 180 by 360 pixels?
    No. The view size is independent of the scene size.

    Or if I size the widget to be say 400 by 600 will the proposed scene coordinates be used on top of them?
    Consider scene coordinates as world coordinates and widget's coordinates as view (physical) coordinates. So if you have a scene that is 400 units wide shown in a widget that is 200 pixels wide, each two units of the scene will correspond to 1 pixel of the widget (unless you apply some scaling, of course).

  5. #5
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: (NEW ??) Converting coordinates system QGraphicScene to Latitude,Lognitude

    Sorry got busy on another item... thanks fpr the response.... so back to this one.

    What I think understand is that I make the scene mapping 90,-180,180,360 but after I create the view I set its size to 400,400 (or some size).

    Then when is set the position for a graphics item added to the scene I use the actual lat and lon values (not pixels).

    If this is true when I select a point with the mouse will the x,y be pixels or lat/lon?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: (NEW ??) Converting coordinates system QGraphicScene to Latitude,Lognitude

    Everything will be calculated in scene coordinates, so lat/long.

  7. #7
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting coordinates system QGraphicScene to Latitude,Lognitude

    Thanks... let me continue with this new thought... may have to provide you some more code

  8. #8
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: (Code attached) Converting coordinates system QGraphicScene to Latitude,Lognitude

    I tried you suggestion and it doesn’t seem to work. I must be missing something in the discussion. I have included my code for the project. Can you help? I attached a zip file.

    Should be able to build it and run (windows). Puts up a map gui and a flight plan gui. Either select a predefined set of points or enter your own., the click on “flying”. The version as is works the way I would expect it to work but it is using pixel per degree conversions instead of scene coordinates. If you change the PIXELFACTOR to 0 then the code will attempt to do the conversion in latitude,longitude.

    Under lat, lon approach I want the reseet the scene parameters with each new set of waypoints and all processing to be based on the lat, lon boundaries.

    Problems I am having are:

    1) It won’t reestablish the scene boundaries every time a new set of waypoints is supplied. (works in the pixel conversion approach).

    2) It won’t o zoom in on the scene with things staying center oriented; everything moves up and out of view, no longer accessable.

    3) Would like the scene mouse double click to return the mouse position in the scene lat, lon.

    4) Would like the scroll drag to work. When I do the mouse double click the scene drag stops working.

    5) Finally when I do this version under Linux, when you zoom in white lines begin to appear. As the object moves across the screen white spacing is left behind. Like some background isn’t updating

    Thanks for help
    Attached Files Attached Files

  9. #9
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: (Code attached) Converting coordinates system QGraphicScene to Latitude,Lognitude

    Hi All,

    Sorry to revive an old thread but I am trying to do the same thing and am a little confused... If I set up my scene as suggested by Master Wysota (90, -180, 180, 360) so I get a conversion to lat/lon coords, but I only want to view
    a very small area, for example a 250nm radius around a given lat/lon point, how do I then have the view "zoom in" to display only that area and maintain the lat/lon coord conversion for that area? I am trying to make a radar/sonar type of
    display using lat/lon points...

    Thank you in advance,
    tim

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: (Code attached) Converting coordinates system QGraphicScene to Latitude,Lognitude

    Calculate the rectangle containing your radar circle (world coordinates), about 6 degrees wide/high, and call QGraphicsView::fitInView()Alternatively, you can call QGraphicsView::centerOn() with the centre point of your radar and then scale to suit.

  11. #11
    Join Date
    Jul 2010
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Converting coordinates system QGraphicScene to Latitude,Lognitude

    Hi Chris,

    Thank you for your reply. I will try your suggetion...

    tim

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.