2

I have a QGIS project with a lot of raster layers; and I want to build a Mapbox map based on this project; that would display those images on a website's map.

enter image description here

I need a way to export a group of raster layers from QGIS to a GeoJSON collection of polygons; that I could load in Mapbox.

I already had a look on GeoTIFF files and PGW files; but still, exporting every layer one by one will be painful - as I'll have to update the web project from QGIS quite often.

So I'm looking for a solution that would automate the job.

I would like to get a GeoJSON file that would look like this:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "layer_name": "AND17",
        "image": "images/png/world.png",
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              4.3813338557822,
              50.842573058675
            ],
            [
              4.418685805334,
              50.842573058675
            ],
            [
              4.418685805334,
              50.825893099756
            ],
            [
              4.3813338557822,
              50.825893099756
            ],
            [
              4.3813338557822,
              50.842573058675
            ]
          ]
        ]
      }
    }
  }

As you can see, it contains the georeferencing informations of the image, and its filename. If I can get that, i'm able to load and place the image on Mapbox.

Have you got any ideas of how I could do that; maybe using a plugin?

I've also seen that you can export a group to a *.qlr file. It seems that it contains some kind of geospatial datas :

<extent>
    <xmin>475442.96307834662729874</xmin>
    <ymin>6588302.91859502345323563</ymin>
    <xmax>483907.46308698022039607</xmax>
    <ymax>6594287.91860113199800253</ymax>
</extent>

Any tips ?

1 Answer 1

1

This script is assuming you want to save the bounds of all the raster layers.

import json

layers = [layer for layer in QgsProject.instance().mapLayers().values() if layer.type() == QgsMapLayerType.RasterLayer]
gj = {
  "type": "FeatureCollection",
  "features":[]
  }
for layer in layers:
    extent = layer.extent()
    feature = {
        "type": "Feature",
          "properties": {
            "layer_name": layer.name(),
            "image": layer.source(),
            },
              "geometry": {
                "type": "Polygon",
                "coordinates": [
                  [
                    [
                      extent.xMinimum(),
                      extent.yMinimum()
                    ],
                    [
                      extent.xMaximum(),
                      extent.yMinimum()
                    ],
                    [
                      extent.xMaximum(),
                      extent.yMaximum()
                    ],
                    [
                      extent.xMinimum(),
                      extent.yMaximum()
                    ],
                    [
                      extent.xMinimum(),
                      extent.yMinimum()
                    ]
                  ]
                ]
              }
            }
    gj['features'].append(feature)
    
    output_dir = r'C:\Work'
    with open('{}/image_bounds.geojson'.format(output_dir), 'w') as file: # Change the location
        file.write(json.dumps(gj))

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.