Making OsmAnd Overlays with QGIS (2024 Edition) (Shallow Thoughts)

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Wed, 03 Apr 2024

Making OsmAnd Overlays with QGIS (2024 Edition)

Several years ago I wrote about Making a Land Ownership Overlay with QGIS and Making Overlay Maps for OsmAnd. I've been using that land use overlay for years. But recently I needed to make several more overlays: land ownership for Utah for a hiking trip, one for the eclipse, and I wanted to refresh my New Mexico land ownership overlay since it was several years out of date. It turns out some things have changed, so here's an update, starting from the point where your intended overlay is loaded as a layer in QGIS.

Removing Black Borders

[QGIS layer properties, Fill button]

One thing I've long wanted to fix: my earlier overlay had a black border, added by QGIS, between each color. That looks fine in the QGIS window, but if you export it as an image then zoom in in a mapping app like OsmAnd, it gives you a big black DMZ between every two regions where you can't tell which side owns the spot you care about.

I had done a lot of web searching to try to find a way to eliminate the black line. Ifound several pages mentioning setting stroke color to transparent or stroke width to zero, I couldn't find any place that offered any settings related to stroke. But Tom Krajci finally found it for me (thanks, Tom!): click on the color next to Symbol, which brings up a color selection dialog. In the color selection dialog, click on Simple Fill.

[QGIS Simple Fill dialogn] That brings up the Simple Fill dialog, which has the needed stroke properties. Click on Stroke Color to get a dialog where you can change Opacity to zero; and/or set Stroke Style to No Line. (There's also Stroke width but it won't let you set that to zero; if you do, it reverts to Hairline.)

Exporting a GeoTiff

Okay, your layer is ready; how do you export it as GeoTiff? That has changed a little, but it hasn't really gotten any easier.

Raster→Conversion→Rasterize (Vector to Raster)... looked promising, but I couldn't find any way to make it work: it always exported a solid black image.

So back to what I used last time. The Print Composer I used in my earlier article is now Project→New Print Layout.... It has some lengthy documentation here but I didn't find that it answered the questions I needed answered. But here's a basic workflow.

[QGIS Print Layout dialog] First, make sure the layer you want to export is at full opacity. If not, right-click on the layer, choose Properties, choose Symbology, expand Layer Rendering near the bottom of the window, and use the Opacity slider.

Adjust the QGIS window so that the full layer you want as your overlay is showing in the window, with not too much whitespace around it. If you could adjust it so there's no whitespace, that would be ideal, but I haven't found a way to do that: you can't just resize the window smaller to match the layer, because QGIS resizes the layer too.

Then bring up Project→New Print Layout... In the Print Layout window, choose Add Item→Add Map, then drag from upper left to lower right, more or less, in the Print Layout window. It's not super important where you drag, and you can adjust it later with resize handles, but good luck resizing it to eliminate any whitespace. If you adjust the handles smaller, the image shrinks with them; if you adjust them larger, you can't see the "paper" size underneath. You can make the "paper" the same aspect ratio as your layer with Resize Layout to Content (scroll down in the Layout pane in the lower right of Print Layout). But adjusting Print Layout so that you get exactly your layer and nothing else (no white margin) doesn't seem possible.

Once you give up fiddling with resize handles and resign yourself to having a white margin, use Layout→Export as Image... (there's also a button for that on the toolbar) and save as GeoTiff. You'll probably want to un-check Open Layer After Exporting since you already have a vector version of the layer loaded.

Take a look at the saved tiff in an image viewer and make sure it's what you want. Depending on how much you fiddled with those Print Layout handles, it might end up weirdly offset or with pieces missing, in which case you can go back and try again.

Making an OsmAnd Overlay

Once you have a GeoTiff, the steps for making an OsmAnd overlay haven't changed at all, happily. Convert to map tiles and add some more zoom levels with

gdal_translate filename.tiff filename.mbtiles
gdaladdo -r nearest filename.mbtiles 2 4 8 16

Then turn that into an OsmAnd sqlitedb file with

mbtiles2osmand.py filename.mbtiles filename.sqlitedb
using Tarwirdur's excellent Python script mbtiles2osmand.

Finally, copy the sqlitedb file into OsmAnd's tiles folder, which for me means a command like

adb push filename.sqlitedb /storage/emulated/0/Android/data/net.osmand.plus/files/tiles/

Merging Several GeoTiffs

My Utah land ownership overlay worked great for my Utah trip. But for my eclipse overlay, I had saved several map images downloaded from eclipse sites (I asked if there was vector data and if I could share any subsequent work I did, but was told no, sadly), and georeferenced them and converted them into GeoTIFF using QGIS. But that gave me several separate GeoTiffs.

Doubtless I could go back into QGIS and find some way to merge the layers, then re-export as a new GeoTiff, but there's an easier way (I just love gdal!):

gdal_merge.py -o output.tif input1.tif input2.tif ...

Then I was able to run gdal_translate, gdaladdo and mbtiles2osmand.py to make a new overlay layer containing all the original GeoTiff files.

Tags: , , ,
[ 18:15 Apr 03, 2024    More mapping | permalink to this entry | ]

Comments via Disqus:

blog comments powered by Disqus