0

I am building a QGIS web application with Python and Flask and would like advice on implementing QGIS.

Currently the application takes Shapefiles uploaded by the user from the browser, adds these to a QGIS project, creates a QGIS layout, exports this to PDF and serves the PDF back to the user.

I have managed to get this to work with the code below but I believe I am implementing QGIS incorrectly.

Currently the QGIS application is initiated in a Flask route but gives a warning about threading:

"WARNING: QApplication was not created in the main() thread."

I believe the QGIS application should be initiated elsewhere and I have studied Davidism's solution given here: Store large data or a service connection per Flask session.

Unfortunately I have been unable to build a working solution. I would stress that my background is as a GIS practitioner with limited software development experience and therefore I would appreciate any direction towards resources or tutorials that may help bridge the gap.

The project layout is based on the one detailed in the Flask tutorial.

@bp.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        project_name = request.form['project_name']  # accept user input from browser

        # Initiate the QGIS application. I suspect this should be implemented elsewhere.
        enable_gui = False
        qgis_application = QgsApplication([], enable_gui)
        qgis_application.initQgis()

        # Check if shapefilefiles were provided by user, save the files and return shapefile name.
        if request.files['files']:
            shapefile_name = save_shapefiles(request.files.getlist('files'))

        # Functions that create QGIS project and layout, add layers and export to PDF.
        project = qgis_project(project_name)
        layout = qgis_print_layout(project, shapefile_name)
        export_to_pdf(layout, f'flaskr\\pdfs\\{project_name}.pdf')

        # Serve PDF to user
        return serve_pdf(project_name)

    return render_template('index.html')

You can access the entire application on GitHub here.

0 Answers0