ctucx.git: gpx-map

render gpx files to tiles and display them on a map

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 {
  description = "render gpx files to tiles and display them on a map";

  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.url     = "github:NixOS/nixpkgs/nixos-23.11";
  };

  outputs = { self, nixpkgs, flake-utils }: {

    overlay = final: prev: {

      datamaps  = final.stdenv.mkDerivation {
        name = "datamaps";

        src = final.fetchFromGitHub {
          owner  = "e-n-f";
          repo   = "datamaps";
          rev    = "76e620adabbedabd6866b23b30c145b53bae751e";
          sha256 = "1rdqbyfmgidiv4aqy1s6llls304dxbg5226c7k622smd2rnda2jk";
        };

        buildInputs = with final; [ pkg-config libpng ];

        installPhase = ''
          mkdir -p $out/bin;
          cp {encode,render,merge,enumerate} $out/bin;
        '';
      };

      gpx-map = final.mkYarnPackage {
        name = "gpx-map";
        src  = self;

        packageJSON = ./package.json;
        yarnLock    = ./yarn.lock;
        yarnNix     = ./yarn.nix;
        doDist      = false;

        buildPhase   = ''
          export NODE_OPTIONS=--openssl-legacy-provider
          yarn build
        '';

        installPhase = ''
          mkdir -p $out
          cp -r deps/gpx-map/{index.html,bundle.js} $out
        '';
      };

      generateTilesFromGPX = (
        let
          parse-gpx = final.stdenv.mkDerivation rec {
            name = "parse-gpx";
            src  = ./parse-gpx.pl;

            dontUnpack = true;

            nativeBuildInputs = [ final.makeWrapper ];
            buildInputs       = [ final.perl ];

            installPhase = ''mkdir -p $out/bin; cp $src $out/bin/parse-gpx; chmod +x $out/bin/parse-gpx;'';
            postFixup    = ''wrapProgram $out/bin/parse-gpx --prefix PERL5LIB : "${with final.perlPackages; makePerlPath [ XMLParser ]}"'';
          };

         makeTile = final.writeShellScript "makeTile.sh" ''
           mkdir -p tiles/$2/$3
           echo "rendering $1 $2 $3 $4 $5 $6"

           if [ $2 -gt 13 ];
           then
             ${final.datamaps}/bin/render -g -t0 -L4 -c 'ff8800' -S 'ff8800' $1 $2 $3 $4 | ${final.pngquant}/bin/pngquant 256 > tiles/$2/$3/$4.png
           else
             ${final.datamaps}/bin/render -g -t0 -L7 -c 'ff8800' -S 'ff8800' $1 $2 $3 $4 | ${final.pngquant}/bin/pngquant 256 > tiles/$2/$3/$4.png
           fi
         '';

        in final.writeShellScriptBin "generateTilesFromGPX" ''
          # strict mode
          set -euo pipefail
          IFS=$'\n\t'

          TMP_DIR=`mktemp -d -t tiles.XXXXXXXXXX`

          trap "{ rm -rf "$TMP_DIR"; }" SIGINT SIGTERM ERR EXIT

          if [ ! -d "$1" ]; then
            echo "$1 does not exist."
            exit 1
          fi

          if [ -z "$(ls -A $1)" ]; then
            echo "$1 is empty."
            exit 1
          fi

          if [ ! -d "$2" ]; then
            echo "$2 does not exist."
            exit 1
          fi

          if [ ! -z "$(ls -A $2)" ]; then
            echo "$2 is not empty."
            exit 1
          fi

          GPX_DIR=`realpath $1`
          TILES_DIR=`realpath $2`

          cd $TMP_DIR

          find $GPX_DIR -name '*.gpx' -print0 | xargs -0 ${parse-gpx}/bin/parse-gpx | ${final.datamaps}/bin/encode -z16 -m8 -o $TMP_DIR/gpx.dm
          ${final.datamaps}/bin/enumerate -s -Z6 -z16 ./gpx.dm | xargs -L1 -P3 ${makeTile}

          mv $TMP_DIR/tiles/* $TILES_DIR/
        ''
      );

    };

  } // (flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = import nixpkgs {
        inherit system;
        overlays = [ self.overlay ];
      };

    in rec {

      packages.generateTilesFromGPX = pkgs.generateTilesFromGPX;
      packages.datamaps             = pkgs.datamaps;
      packages.gpx-map               = pkgs.gpx-map;
 
      devShells.default = pkgs.mkShell {
        nativeBuildInputs = with pkgs; [ yarn yarn2nix php datamaps generateTilesFromGPX ];
        shellHook = ''
          export NODE_OPTIONS=--openssl-legacy-provider
          alias serve="yarn build && php -S localhost:8080"
        '';
      };

      apps.default = {
        type = "app";
        program = "${pkgs.generateTilesFromGPX}/bin/generateTilesFromGPX";
      };

    }
  ));
}