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 use variable of QVariantList format
Forum Updated to NodeBB v4.3 + New Features

how to use variable of QVariantList format

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 555 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.
  • R Offline
    R Offline
    RoApPr
    wrote on last edited by
    #1

    Good day to all!
    I want to read the format file .geojson and manually apply graphical data from it to a map with an arbitrary cartographic projection.
    I found the QGeoJson class in Qt, and following the example of geojson_viewer (Qt 5.15 -> location), I learned to read any geocoordinate file.
    However, the example uses Qt Quick, which I do not understand, which did not allow me to understand exactly how the file data is decrypted in the example.geojson - I don't see polygon drawing operations in the graphics area (and, accordingly, I don't see the decrypted data that the drawing is based on).
    Below I will give the code that I use to read the file.geojson. Using the debugger, I look at the variable modelList (see figure) and frankly do not understand how to get the coordinates of the corners of polygons from it. In addition to the fact that I have never encountered such complex data structures and do not understand how to get to the end of the chain to the final data, I also cannot see what lies there - Qt writes that the final field is "non-called" (what does it even mean?).
    Could you tell me how to extract from a variable
    modelList all coordinates of the polygon (and I have only polygons and multipolygons here)? The coordinates of any polygon will be enough - then I'll figure it out :)
    I am grateful in advance for any help!

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QString file_path = QCoreApplication::applicationDirPath();
        file_path += "/countries.geojson";
    
        // Reading GeoJSON file
        QFile loadedFile(file_path);
        if (!loadedFile.open(QIODevice::ReadOnly)) {
            qWarning() << "Error while opening the file: " << file_path;
            qWarning() << loadedFile.error() <<  loadedFile.errorString();
            return 1;
        }
    
        // Load the GeoJSON file using Qt's API
        QJsonParseError err;
        QJsonDocument loadDoc(QJsonDocument::fromJson(loadedFile.readAll(), &err));
        if (err.error) {
             qWarning() << "Parsing while importing the JSON document:\n" << err.errorString();
             return 2;
        }
    
        // Import geographic data to a QVariantList
        QVariantList modelList = QGeoJson::importGeoJson(loadDoc);
    
        return a.exec();
    }
    

    Снимок экрана 2022-09-18 181915.png

    JonBJ 1 Reply Last reply
    0
    • R RoApPr

      Good day to all!
      I want to read the format file .geojson and manually apply graphical data from it to a map with an arbitrary cartographic projection.
      I found the QGeoJson class in Qt, and following the example of geojson_viewer (Qt 5.15 -> location), I learned to read any geocoordinate file.
      However, the example uses Qt Quick, which I do not understand, which did not allow me to understand exactly how the file data is decrypted in the example.geojson - I don't see polygon drawing operations in the graphics area (and, accordingly, I don't see the decrypted data that the drawing is based on).
      Below I will give the code that I use to read the file.geojson. Using the debugger, I look at the variable modelList (see figure) and frankly do not understand how to get the coordinates of the corners of polygons from it. In addition to the fact that I have never encountered such complex data structures and do not understand how to get to the end of the chain to the final data, I also cannot see what lies there - Qt writes that the final field is "non-called" (what does it even mean?).
      Could you tell me how to extract from a variable
      modelList all coordinates of the polygon (and I have only polygons and multipolygons here)? The coordinates of any polygon will be enough - then I'll figure it out :)
      I am grateful in advance for any help!

      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          QString file_path = QCoreApplication::applicationDirPath();
          file_path += "/countries.geojson";
      
          // Reading GeoJSON file
          QFile loadedFile(file_path);
          if (!loadedFile.open(QIODevice::ReadOnly)) {
              qWarning() << "Error while opening the file: " << file_path;
              qWarning() << loadedFile.error() <<  loadedFile.errorString();
              return 1;
          }
      
          // Load the GeoJSON file using Qt's API
          QJsonParseError err;
          QJsonDocument loadDoc(QJsonDocument::fromJson(loadedFile.readAll(), &err));
          if (err.error) {
               qWarning() << "Parsing while importing the JSON document:\n" << err.errorString();
               return 2;
          }
      
          // Import geographic data to a QVariantList
          QVariantList modelList = QGeoJson::importGeoJson(loadDoc);
      
          return a.exec();
      }
      

      Снимок экрана 2022-09-18 181915.png

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @RoApPr
      If you are asking about the format/content of a GeoJson file itself you need to look at external specification documents.

      If you mean you want to see what is in the QGeoJson::importGeoJson()-returned QVariantList, you need to follow the description at https://doc.qt.io/qt-5/qgeojson.html#importing-geojson.

      The importer returns a QVariantList containing a single QVariantMap.

      Write some code to find that in the variant list and print its keys/values. If you are relying on the debugger window to be able to show you this I imagaine you will be disappointed.

      R 1 Reply Last reply
      0
      • R Offline
        R Offline
        RoApPr
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • JonBJ JonB

          @RoApPr
          If you are asking about the format/content of a GeoJson file itself you need to look at external specification documents.

          If you mean you want to see what is in the QGeoJson::importGeoJson()-returned QVariantList, you need to follow the description at https://doc.qt.io/qt-5/qgeojson.html#importing-geojson.

          The importer returns a QVariantList containing a single QVariantMap.

          Write some code to find that in the variant list and print its keys/values. If you are relying on the debugger window to be able to show you this I imagaine you will be disappointed.

          R Offline
          R Offline
          RoApPr
          wrote on last edited by
          #4

          @JonB, thank you for your answer, but I mean more that I don't know how to access the required field - I tried a number of options, but I get either an error or an empty object.
          I have given the image as an example of a real file that is being successfully read. I can't get a variable containing only information on "address" "modeList[0][0].value()[0][0].value()" (obviously, this is one of the incorrect access request forms, but, as I said earlier, I did not find the correct ones).

          JonBJ 1 Reply Last reply
          0
          • R RoApPr

            @JonB, thank you for your answer, but I mean more that I don't know how to access the required field - I tried a number of options, but I get either an error or an empty object.
            I have given the image as an example of a real file that is being successfully read. I can't get a variable containing only information on "address" "modeList[0][0].value()[0][0].value()" (obviously, this is one of the incorrect access request forms, but, as I said earlier, I did not find the correct ones).

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @RoApPr
            I don't know what to say. You have to write code per the docs. Start by showing you found the QVariantMap. Find how many elements it has, etc. Show code. And pretty obviously don't write code like modeList[0][0].value()[0][0].value(), pick out each item one at a time into intermediate variables so you can see how it goes.

            1 Reply Last reply
            1

            • Login

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