-
Notifications
You must be signed in to change notification settings - Fork 3
/
TileCompiler.java
117 lines (98 loc) · 3.63 KB
/
TileCompiler.java
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
package io.github.pfwikis;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import com.beust.jcommander.JCommander;
import io.github.pfwikis.run.Runner;
import lombok.Getter;
import lombok.extern.java.Log;
@Log
@Getter
public class TileCompiler {
private CLIOptions options;
private File geo;
private File targetDir;
private File targetRoot;
private File spriteDir;
public static void main(String[] args) throws IOException {
var options = new CLIOptions();
JCommander.newBuilder()
.addObject(options)
.build()
.parse(args);
new TileCompiler().run(options);
}
public void run(CLIOptions options) throws IOException {
this.options = options;
targetRoot = new File(options.isUseBuildShortcut()
?"../frontend/dist"
: "../frontend/public");
targetDir = new File(targetRoot, options.getDataPath());
spriteDir = new File(targetRoot, "sprites");
geo = new File("geo");
//delete old files before we start
Arrays.stream(targetRoot.listFiles())
.filter(f->f.getName().startsWith("data"))
.forEach(FileUtils::deleteQuietly);
FileUtils.deleteQuietly(geo);
//create target folders
geo.mkdirs();
targetDir.mkdirs();
spriteDir.mkdirs();
compileSprites();
compileLayers();
makeTiles();
//give maxZoom to vite
log.info("Writing .env.local");
Files.writeString(new File(targetRoot, "../.env.local").toPath(), "VITE_MAX_ZOOM="+options.getMaxZoom()+"\nVITE_DATA_PATH="+options.getDataPath());
}
private void makeTiles() throws IOException {
var layers = Arrays.stream(geo.listFiles())
.filter(f->f.getName().endsWith(".geojson"))
.flatMap(f->List.of("-L", f.getName().substring(0, f.getName().length()-8)+":"+f.toString()).stream())
.toList();
Runner.run("tippecanoe",
"-z"+options.getMaxZoom(),
"--no-tile-compression",
"-n", "golarion",
"-e", new File(targetDir, "golarion"),
"--force",
"--detect-shared-borders",
"--coalesce",
"-B", "0",
"--coalesce-densest-as-needed",
"--maximum-tile-bytes=200000",
"--maximum-tile-features=100000",
layers
);
var time = System.nanoTime();
//rename extensions for github pages gzipping
log.info("Renaming .pbf files to .pbf.json");
Files.walk(new File(targetDir, "golarion").toPath())
.map(Path::toFile)
.filter(f->f.getName().endsWith(".pbf"))
.toList()
.parallelStream()
.forEach(f->{
try {
FileUtils.moveFile(f, new File(f.getParentFile(), f.getName()+".json"));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
log.info("Renaming took "+TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()-time)+"s");
}
private void compileLayers() throws IOException {
new LayersCompiler(this).compile();
}
private void compileSprites() throws IOException {
log.info("Compiling Sprites");
Runner.run("spritezero", new File(spriteDir, "sprites"), "sprites/");
Runner.run("spritezero", new File(spriteDir, "sprites@2x"), "sprites/", "--retina");
}
}