TILES: QGIS

With the program QGIS it is possible to project OSM data, shapefiles and GeoTiff files on the correct coordinates. There are also many options for how this data is displayed, like the color, width of lines and much more.

We use the high functionality, but don’t worry, you don’t have to learn how the program works. QGIS provides a python API that allows you to control all functions through code.

In principle the code works as follows: We activate the appropriate layers we want to export, then we set the image size, deactivate some functions like anti-aliasing and export the image. This is done one after the other with each image that corresponds exactly to a tile, i.e. 1° longitude and 1° latitude.

Don’t be afraid, you don’t have to piece together the source code yourself. In the finished program you select your desired Tiles and export the script: A simple batch file you just need to execute.

Lets take a deeper look into the source code:

With the following part, we store all layers into an array, deactivate all of them and activate only layers that start with “landuse”. In this layer farms, meadows, quarrys and buildings are displayed.

alllayers = []
for alllayer in QgsProject.instance().mapLayers().values():
    alllayers.append(alllayer)
    
root = QgsProject.instance().layerTreeRoot()
for alllayer in alllayers:
    node = root.findLayer(alllayer.id())
    node.setItemVisibilityChecked(Qt.Unchecked)
    
#save all layers starting with "landuse" into a list
layers = []
for layer in QgsProject.instance().mapLayers().values():
    if layer.name().startswith("landuse"):
        layers.append(layer)

Now we create a new layout. Layouts are custom settings for exporting and visualisation.

project = QgsProject().instance()
layout = QgsPrintLayout(project)
layout.initializeDefaults()
    
#select all pages and change the first one to the right dimension
pages = layout.pageCollection()
pages.page(0).setPageSize(QgsLayoutSize(scale,scale,QgsUnitTypes.LayoutPixels))
    
#create a map inside the layout
map = QgsLayoutItemMap(layout)
map.setRect(0, 0, scale, scale)
map.setExtent(QgsRectangle(xMin, yMin, xMax, yMax))
map.attemptMove(QgsLayoutPoint(0,0,QgsUnitTypes.LayoutPixels))
map.attemptResize(QgsLayoutSize(scale,scale,QgsUnitTypes.LayoutPixels))
layout.addLayoutItem(map)
    
#create exporter with settings
exporter = QgsLayoutExporter(layout)
settings = QgsLayoutExporter.ImageExportSettings()
settings.imageSize = (QSize(scale,scale))
    
#disable Antialiasing
context = QgsLayoutRenderContext(layout)
context.setFlag(context.FlagAntialiasing, False)
settings.flags = context.flags()

The last part of thy python script is to export the layout as a *.png image. Later we use this images in WorldPainter to create the Minecraft map.

#create image
ret = exporter.exportToImage(path + 'image_exports/' + tile + '/' + tile + '_landuse.png', settings)
print('File: "' + path + 'image_exports/' + tile + '/' + tile + '_landuse.png" saved')
assert ret==0

Finally, the code is transferred to QGIS via a batch file when the program starts.

"C:\Program Files\QGIS 3.10\bin\qgis-bin.exe" --project "\QGIS\MinecraftEarthTiles.qgz" --code "\python\N41W070.py"

The project file “MinecraftEarthTiles.qgz” can be downloaded from this website soon. The *.py files are created by the MinecraftTileSelector.