moduleNSWTopomoduleDEMincludeGDALGlob,Logdefget_dem(temp_dir,dem_path)txt_path=temp_dir/"dem.txt"vrt_path=temp_dir/"dem.vrt"cutline=@map.cutline(**margin)Dir.chdir(@source?@source.parent:Pathname.pwd)dolog_update"%s: examining DEM"%@namegdal_rasters@pathdo|index,total|log_update"%s: examining DEM file %i of %i"%[@name,index,total]endend.tapdo|rasters|raise"no elevation data found at specified path"ifrasters.none?log_update"%s: extracting DEM raster"%@nameend.group_bydo|path,info|@epsg?Projection.epsg(@epsg):Projection.new(info.dig("coordinateSystem","wkt"))end.map.with_indexdo|(projection,rasters),index|raise"DEM data not in planar projection with metre units"unlessprojection.metres?paths,resolutions=rasters.mapdo|path,info|rx,ry=info["geoTransform"].values_at(1,2)nextpath,Vector[rx,ry].normend.sort_by(&:last).transposetxt_path.writepaths.reverse.join(?\n)@mm_per_px||=@map.to_mm(resolutions.first)indexed_dem_path=temp_dir/"dem.#{index}.tif"OS.gdalbuildvrt"-overwrite","-allow_projection_difference","-a_srs",projection,"-input_file_list",txt_path,vrt_pathOS.gdalwarp"-t_srs",@map.projection,"-tr",@mm_per_px,@mm_per_px,"-r","bilinear","-cutline","GeoJSON:/vsistdin?buffer_limit=-1","-crop_to_cutline",vrt_path,indexed_dem_pathdo|stdin|stdin.putscutline.to_jsonendindexed_dem_pathend.tapdo|dem_paths|txt_path.writedem_paths.join(?\n)OS.gdalbuildvrt"-overwrite","-input_file_list",txt_path,vrt_pathOS.gdal_translatevrt_path,dem_pathendenddefblur_dem(dem_path,blur_path)half=(3*@smooth/@mm_per_px).ceilcoeffs=(-half..half).mapdo|n|n*@mm_per_px/@smoothend.mapdo|x|Math::exp(-x**2)endvrt=OS.gdalbuildvrt"/vsistdout/",dem_pathxml=REXML::Document.newvrtxml.elements.each("//ComplexSource|//SimpleSource")do|source|kernel_filtered_source=source.parent.add_element("KernelFilteredSource")source.elements.each("SourceFilename|OpenOptions|SourceBand|SourceProperties|SrcRect|DstRect")do|element|kernel_filtered_source.add_elementelementendkernel=kernel_filtered_source.add_element("Kernel","normalized"=>1)kernel.add_element("Size").text=coeffs.sizekernel.add_element("Coefs").text=coeffs.join?\ssource.parent.deletesourceendlog_update"%s: smoothing DEM raster"%@nameOS.gdal_translate"/vsistdin?buffer_limit=-1",blur_pathdo|stdin|stdin.writexmlendendendend