-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinitiate.js
More file actions
173 lines (156 loc) · 4.59 KB
/
Copy pathinitiate.js
File metadata and controls
173 lines (156 loc) · 4.59 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os from "os";
import fs from "fs";
import path from "path";
import { generateStyle, generateMBTiles } from "./generate_resources.js";
import { requestOnlineTiles } from "./download_resources.js";
import { handleError } from "./utils.js";
const MBTILES_REGEXP = /mbtiles:\/\/(\S+?)(?=[/"]+)/gi;
export const initiateRendering = async (
style,
styleDir,
sourceDir,
apiKey,
mapboxStyle,
monthYear,
overlay,
bounds,
minZoom,
maxZoom,
outputDir,
outputFilename,
) => {
console.log("Initiating rendering...");
const workBegun = new Date().toISOString();
const tempDir = path.join(os.tmpdir(), "mapgl-tile-renderer-temp");
if (!fs.existsSync(tempDir)) {
try {
fs.mkdirSync(tempDir, { recursive: true });
} catch (error) {
return handleError(error, "creating the temporary directory");
}
}
let stylePath = null;
let styleObject = null;
// If the style is self-hosted, let's read the style from the file.
if (style === "self") {
stylePath = path.resolve(process.cwd(), styleDir);
try {
styleObject = JSON.parse(fs.readFileSync(stylePath, "utf-8"));
} catch (error) {
return handleError(error, "reading the style file");
}
}
// If the style is not self-hosted, let's generate everything that we need to render tiles.
if (style !== "self") {
// Download tiles from the online source
try {
await requestOnlineTiles(
style,
apiKey,
mapboxStyle,
monthYear,
bounds,
minZoom,
maxZoom,
tempDir,
);
} catch (error) {
return handleError(error, "downloading tiles from the online source");
}
// Save the overlay GeoJSON to a file, if provided
if (overlay) {
try {
fs.writeFileSync(`${tempDir}/sources/overlay.geojson`, overlay);
} catch (error) {
return handleError(error, "saving the overlay GeoJSON to a file");
}
console.log(`Overlay GeoJSON saved to file!`);
}
// Set the tileSize of the online source. Mapbox Raster API provides 512px tiles.
let tileSize;
if (style === "mapbox" || style === "mapbox-satellite") {
tileSize = 512;
} else {
tileSize = 256;
}
// Generate and save a stylesheet from the online source and overlay source.
if (styleObject === null) {
try {
styleObject = generateStyle(style, overlay, tileSize);
fs.writeFileSync(
`${tempDir}/style.json`,
JSON.stringify(styleObject, null, 2),
);
console.log("Stylesheet generated and saved!");
} catch (error) {
return handleError(error, "generating and saving the stylesheet");
}
}
sourceDir = `${tempDir}/sources`;
styleDir = tempDir;
}
const localMbtilesMatches = JSON.stringify(styleObject).match(MBTILES_REGEXP);
if (localMbtilesMatches && !sourceDir) {
const msg =
"Stylesheet has local mbtiles file sources, but no sourceDir is set";
return handleError(new Error(msg), "checking for local mbtiles sources");
}
if (localMbtilesMatches) {
localMbtilesMatches.forEach((name) => {
const mbtileFilename = path.normalize(
path.format({
dir: sourceDir,
name: name.split("://")[1],
ext: ".mbtiles",
}),
);
if (!fs.existsSync(mbtileFilename)) {
const msg = `Mbtiles file ${path.format({
name,
ext: ".mbtiles",
})} in stylesheet is not found in: ${path.resolve(sourceDir)}`;
return handleError(
new Error(msg),
"checking for local mbtiles sources",
);
}
});
}
try {
let metadata = await generateMBTiles(
styleObject,
styleDir,
sourceDir,
bounds,
minZoom,
maxZoom,
tempDir,
outputDir,
outputFilename,
);
console.log(
`\x1b[32m${outputFilename}.mbtiles has been successfully generated!\x1b[0m`,
);
// if successful, return the metadata
return {
style: style,
status: metadata.status,
errorCode: metadata.errorCode,
errorMessage: metadata.errorMessage,
filename: metadata.filename,
filesize: metadata.filesize,
numberOfTiles: metadata.numberOfTiles,
numberOfAttempts: 1,
workBegun,
workEnded: new Date().toISOString(),
// if status = success, set expiration for one year
expiration:
metadata.status === "success"
? new Date(Date.now() + 31556952000).toISOString()
: null,
};
} catch (error) {
return handleError(error, "generating MBTiles file");
}
};
export default initiateRendering;