3

I have a QGIS project file, it has a point layer in GPKG format and online map layers. Coordinate System of online map layers is WGS 84 / Pseudo-Mercator. Point layer's default coordinate system is WGS84 4326. I try to modify coordinate system of point layer using QGIS C++ API and QgsQuick based on user's input with EPSG code. The code below creates new project. When I open the project and create new points on online map layer, it is OK. But when I open the project again, the points are at wrong place, like middle of Africa. I think there is a problem about on-the-fly projection about the project file. I wrote this C++ code from an answer about creating project with PyQGIS.

Is there a way to fix this problem? Should I set the extent, project coordinate system before saving the project?

You can see the project's source code here.

QString ProjectModel::addNewProject(QString name, QString epsgcode )
{
    // create CRS with EPSG
    QgsCoordinateReferenceSystem my_crs = QgsCoordinateReferenceSystem( "EPSG:" + epsgcode );
    if ( !(my_crs.isValid()) ) {
        return "crs_not_found";
    }

    // new project folder path name
    QString new_path = dataDir() + "/" + name;

    // folder path name to directory
    QDir dir( new_path );

    if ( !(dir.exists()) ) {
        // Copy sample qgs project and point layer to destination path
        copy_survey_project( name );
        // New project path
        QString projectName = new_path + "/" + name + ".qgs";
        // initialize project and clear project
        mProject = QgsProject::instance();
        mProject->clear();
        // Read the copied qgs project
        mProject->read( projectName );
        // Point layer
        QgsMapLayer *myLayer = mProject->mapLayersByName("survey")[0];

        if( myLayer && myLayer->isValid() ){
            // Set new CRS to the layer
            myLayer->setCrs( my_crs );
            // Save the project
            mProject->write( projectName );
            mProject->clear();
        }
        return "ok";
    }
    else{
        return "fileExists";
    }
}

1 Answer 1

-1

I found my answer in the source codes of Input app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.