If you are looking to convert DEMs to contour lines:
For converting scanned topo maps to vector contour lines, Arcscan is one of the easiest (and most sophisticated) programs out there. However, there are several opensource alternatives, including a promising GRASS approach:
Trace vector contours from a scanned map.
Additional information can be found here:
If you load your LineString gpx/geojson
ogr2ogr -f "PostgreSQL" PG:"dbname=" linestring.geojson
and DEM tiff into PostGIS (replace XXXX with EPSG code, eg for WGS84 use 4326)
raster2pgsql -d -C -I -M -s XXXX -t auto dem.tiff dem
you can then use ST_Segmentize to add extra vertices at the resolution of your DEM (gdalinfo's Pixel Size), then ST_DumpPoints to convert the LineString to a bunch of points,
CREATE TABLE line_points AS select (ST_DumpPoints(ST_Segmentize(wkb_geometry, 5))).*, 0.0 AS ele from line;
then ST_Value to get the elevation at each point,
CREATE INDEX ON line_points USING gist (geom);
UPDATE line_points SET ele = (SELECT ST_Value(dem.rast, geom) AS ele FROM dem WHERE ST_Intersects(line_points.geom, dem.rast) ORDER BY ele LIMIT 1);
then update the geometry to include the elevation
UPDATE line_points SET geom = ST_MakePoint(ST_X(geom), ST_Y(geom), ele);
then ST_MakeLine to reconstruct the points with elevation back into a LineString.
CREATE AGGREGATE array_accum (anyelement)
(
sfunc = array_append,
stype = anyarray,
initcond = '{}'
);
CREATE TABLE linez AS SELECT ST_MakeLine(array_accum(geom)) AS geom FROM (SELECT * FROM line_points ORDER BY path) AS line;
Export back to GeoJSON/CSV/etc:
ogr2ogr -f GeoJSON linez.geojson PG: linez
This could be optimised to generate multiple profiles in bulk, so you're only calling each SQL statement once.
Best Answer
There may be better and shorter ways, but I found this webpage with detailed step-by-step instructions that worked perfectly:
https://medium.com/@hantatsang/qgis-create-dem-digital-elevation-model-with-grass-plug-in-f4ec191d08e0
Then I just used the Profile tool on the DEM created by this process.
Although the link above gives more detail and screenshots, at the suggestion of Simbamangu, here are the basic steps. They are based on a project which has a shapefile vector layer containing contour lines that has an attribute giving the contour elevation of each line. QGIS must be run with the GRASS component enabled (QGIS 3.18.18 with GRASS 7.4.0 was used in this case).
Create a new GRASS project (Plugins > GRASS > New Mapset) and follow prompts.
Make sure the shapefile vector layer is selected.
Set the Extent (bounding box) and Resolution in the Region tab of the GRASS Tools dialog box. If there is no GRASS Tools dialog box displaying on the screen, switch it on under Plugins > GRASS > Open GRASS Tools. The lower the resolution value, the higher quality the DEM map will be but the processing time can be increased significantly in Step xx below.
In the Modules tab of the GRASS Tools dialog box, import your contour shapefile into GRASS using the v.in.ogr.qgis module. Search for the module by typing the module name v.in.ogr.qgis into the Filter field. Click on the module name to open it in a new tab, and fill in the form. For "Loaded layer" field, select the contour shapefile vector layer containing contour lines that you want to import into GRASS. For "Name for output vector map" field, type in the layer name that you would like.
Click on the "Run" button to start importing the vector contours. Once it finishes, click on the "View output" button so that the results show up in the Layers Panel.
In the Modules tab of the GRASS Tools dialog box, create a raster contours layer using the v.to.rast.attr module. Search for the module by typing the module name into the Filter field. Click on the module name to open the form in a new tab, and fill in the form. For "Name of input vector map", select the output vector map layer that was imported in the previous step. For Attribute field, select the attribute that holds the contour’s elevation data. For "Name for output raster map", type in the the map name you would like.
Click on the "Run" button to create the raster contours layer. Once it finishes, click on the "View output" button so that the results show up in the Layers Panel.
In the Modules tab of the GRASS Tools dialog box, create the DEM map using the r.surf.contour module. Search for the module by typing the module name into the Filter field. Click on the module name to open the form in a new tab, and fill in the form. For "Name of input raster map containing contours" field, select the output raster map that you created in the previous step. In "Name for output raster map" field, type in the map name that you would like.
Click on the "Run" button to create the DEM map. Once it finishes, click on the "View output" button so that the results show up in the Layers Panel. NOTE: This module will run extremely slowly taking several hours if the resolution is set too high (i.e., a low value) in Step #3 above. If you change the resolution in the Region tab, you must repeat the steps starting at Step #4 above. Obviouly, the resolution affects the quality of the final DEM.
To permanently save the DEM map as a GeoTiff for use outside of GRASS, right click on the DEM layer you created in the step above using "Save As".
Install the Profile tool to obtain profiles using the DEM created above.