Last active
November 27, 2023 16:40
-
-
Save jarnaldich/1f8f3eaec3e0fa3e3735063a7e3aeddc to your computer and use it in GitHub Desktop.
[Prolog as a build tool] Example for using Prolog to build something #prolog #make
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- use_module(library(http/http_open)). | |
:- use_module(library(http/http_client)). | |
:- use_module(library(uri)). | |
:- use_module(library(zip)). | |
:- use_module(library(pcre)). | |
:- initialization(main, main). | |
download(URL, File) :- | |
print_message(informational, ['Downloading', File]), | |
setup_call_cleanup( | |
open(File, write, OF, [type(binary)]), | |
setup_call_cleanup( | |
http_open(URL, In, []), | |
copy_stream_data(In, OF), | |
close(In)), | |
close(OF)). | |
unzip_members(Z) :- | |
zipper_file_info(Z, FileName, _Attrs), | |
format("~w~n", [FileName]), | |
setup_call_cleanup( | |
zipper_open_current(Z, In, [type(binary)]), | |
setup_call_cleanup( | |
open(FileName, write, OF, [type(binary)]), | |
copy_stream_data(In, OF), | |
close(OF)), | |
close(In)), | |
( zipper_goto(Z, next) -> unzip_members(Z)). | |
unzip(File) :- | |
zip_open(File, read, Zipper, []), | |
with_zipper(Zipper, ( | |
zipper_goto(Zipper, first), | |
unzip_members(Zipper))), | |
zip_close(Zipper). | |
unarchive(File) :- | |
setup_call_cleanup( | |
archive_open(File, Archive, [close_parent(true)]), | |
( repeat, | |
( archive_next_header(Archive, Path) | |
-> format('~w~n', [Path]), | |
setup_call_cleanup( | |
% Setup | |
archive_open_entry(Archive, Stream), | |
% Call | |
setup_call_cleanup( | |
open(Path, write, OF, [type(binary)]), | |
copy_stream_data(Stream, OF), | |
close(OF)), | |
% Cleanup | |
close(Stream)), | |
fail | |
; ! | |
) | |
), | |
archive_close(Archive)). | |
unzip(Target) :- | |
bath_zip(Target), | |
re_replace('.zip', '', Target, ZipFile), | |
setup_call_cleanup( | |
zip_open(Target, read, Zipper, []), ( | |
zipper_goto(Zipper, first), | |
zipper_open_current(Zipper, In, [type(binary)]), | |
setup_call_cleanup( | |
open(ZipFile, write, OF, [type(binary)]), | |
copy_stream_data(In, OF), | |
close(OF))), | |
zip_close(Zipper)). | |
tile(X) :- member(X, ['D3', 'D4', 'D5', 'D6', 'D7', | |
'E3', 'E4', 'E5', 'E6', 'E7', 'E8', | |
'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', | |
'G2', 'G3', 'G5', 'G6', 'G7', 'G8', | |
'H2', 'H3', 'H8' ]). | |
bath_zip(X) :- tile(T), atom_concat(T, "_2022.tif.zip", X). | |
bath_tiff(X) :- tile(T), atom_concat(T, "_2022.tif", X). | |
build_bath_zip(Target, Status) :- | |
bath_zip(Target), | |
exists_file(Target), | |
Status=already_done. | |
build_bath_zip(Target, Status) :- | |
bath_zip(Target), | |
atom_concat('https://downloads.emodnet-bathymetry.eu/v11/', Target, Url), | |
download(Url, Target), | |
Status=built. | |
build_bath_tiff(Target, Status) :- | |
bath_tiff(Target), | |
exists_file(Target), | |
Status=already_done. | |
build_bath_tiff(Target, Status) :- | |
% Dependency | |
bath_tiff(Target), | |
atom_concat(Target, ".zip", ZipTarget), | |
build_bath_zip(ZipTarget, _S), | |
% Proc | |
unzip(ZipTarget), | |
Status=built. | |
:- cd('e:/public/RomanEmpire'), pwd(). | |
% go(Res) :- findall(Tiff, build_bath_tiff(Tiff, _), Res). | |
% main :- concurrent_forall(bath_tiff(Tiff), build_bath_tiff(Tiff, _)). | |
vrt() :- | |
exists_file('bath_tiles.vrt'); | |
setup_call_cleanup( | |
open('bath_tiles.txt', write, OF, []), | |
forall(bath_tiff(Tiff), writeln(OF, Tiff)), | |
close(OF)), | |
swritef(Cmd, 'gdalbuildvrt -input_file_list bath_tiles.txt bath_tiles.vrt'), | |
shell(Cmd, 0). | |
cog() :- | |
exists_file('bath_tiles.tif'); | |
vrt(), | |
format(atom(Cmd), 'gdal_translate -of GTiff -co TILED=YES -co BIGTIFF=YES -co COMPRESS=LZW -co PREDICTOR=3 bath_tiles.vrt bath_tiles.tif', []), | |
shell(Cmd, 0), | |
shell('gdaladdo -r average bath_tiles.tif'). | |
provinces() :- | |
exists_file('provinces.geojson'); | |
download( | |
'https://raw.githubusercontent.com/klokantech/roman-empire/master/data/provinces.geojson', | |
'provinces.geojson'). | |
main :- provinces(), cog(). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment