Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved How to change (longitude, latitude) to QGraphicsItem(x,y) ?

    General and Desktop
    3
    4
    170
    Loading More Posts
    • 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.
    • sonichy
      sonichy last edited by

      I have already get the point(longitude, latitude) of gpx file, how to draw them on QGrahpicsScene ?
      替代文字

      QFile file(filePath);
      if(!file.open(QFile::ReadOnly))
          return;
      QDomDocument doc;
      if (!doc.setContent(&file)) {
          file.close();
          return;
      }
      file.close();
      
      QTreeWidgetItem *TWI_root = new QTreeWidgetItem(ui->treeWidget);
      TWI_root->setText(0, QFileInfo(filePath).fileName());
      TWI_root->setIcon(0, QIcon::fromTheme("application-gpx+xml", QIcon(":/icon.png")));
      
      QDomElement root = doc.documentElement();
      QDomNodeList DNL = root.elementsByTagName("trkpt");
      QPainterPath PP;
      for (int i=0; i<DNL.count(); i++) {
          QDomNode DN = DNL.at(i);
          if (DN.isElement()) {
              QDomElement DE = DN.toElement();
              qreal lon = DE.attribute("lon").toDouble();
              qreal lat = DE.attribute("lat").toDouble();
              QTreeWidgetItem *TWI_point = new QTreeWidgetItem(TWI_root);
              TWI_point->setText(0, QString("%1").arg(lon, 0, 'f', 14) + "," + QString("%1").arg(lat, 0, 'f', 14));
              TWI_point->setIcon(0, QIcon(":/marker.png"));
              QGraphicsEllipseItem *GEI = new QGraphicsEllipseItem(lon, lat, 5, 5);
              GEI->setFlags(QGraphicsItem::ItemIsSelectable);
              scene->addItem(GEI);
              if (i == 0)
                  PP.moveTo(lon, lat);
              else
                  PP.lineTo(lon, lat);
          }
      }
      QPen pen(Qt::blue, 1);
      QGraphicsPathItem *GPI = scene->addPath(PP, pen);
      GPI->setFlags(QGraphicsItem::ItemIsSelectable);
      

      https://github.com/sonichy

      Pl45m4 1 Reply Last reply Reply Quote 0
      • Pl45m4
        Pl45m4 @sonichy last edited by

        @sonichy

        Hi,

        do you want to translate your lat / long value to coordinates (x, y values) of your scene?

        https://stackoverflow.com/questions/14034455/translating-lat-long-to-actual-screen-x-y-coordinates-on-a-equirectangular-map


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        sonichy 1 Reply Last reply Reply Quote 2
        • sonichy
          sonichy @Pl45m4 last edited by

          @Pl45m4 The method is not fit.
          The key problem is the number is too small, I find another way : Differential Amplification. The shape is correct.

          QDomElement root = doc.documentElement();
          QDomNodeList DNL = root.elementsByTagName("trkpt");
          QPainterPath PP;
          qreal lonc=0, latc=0;
          for (int i=0; i<DNL.count(); i++) {
              QDomNode DN = DNL.at(i);
              if (DN.isElement()) {
                  QDomElement DE = DN.toElement();
                  qreal lon = DE.attribute("lon").toDouble();
                  qreal lat = DE.attribute("lat").toDouble();
                  if (i == 0) { // first point as reference point
                      lonc = lon;  
                      latc = lat;
                  } else { // Differential then Amplification
                      qreal x = (lon - lonc) * 10000; 
                      qreal y = - (lat - latc) * 10000; // direction is opposite, add -
                      PP.lineTo(x, y);
                  }
              }
          }
          QPen pen(Qt::blue, 1);
          QGraphicsPathItem *GPI = scene->addPath(PP, pen);
          GPI->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
          

          替代文字

          https://github.com/sonichy

          1 Reply Last reply Reply Quote 0
          • Kent-Dorfman
            Kent-Dorfman last edited by Kent-Dorfman

            @sonichy said in How to change (longitude, latitude) to QGraphicsItem(x,y) ?:

            QGraphicsEllipseItem

            correctly projecting geographic coordinates onto a flat view widget is a whole lot more complicated than you are considering. You need to consider the shape of the reference spheroid and datum standard used in taking the original readings, then it's usually easiest to deal with calculations once they have been converted from arc-degrees to UTM, thus then allowing the use of the pythagorean theorem to calculate points. See great circle algorithm. You're mapping a curved surface onto a flat projection so you have to consider orthographic distortion also....lots involved in doing it correctly...look up gdal package to automate some of it.

            1 Reply Last reply Reply Quote 2
            • First post
              Last post