Feat: 地震の震度分布画像生成 / Chg: 震度分布画像のズームレベルを8から9へ / Chg: 震度分布画像の生成条件を震源と位置が存在するへ変更 / Feat: 震度分布画に全タイルが埋まる機能 / Feat: 震度分布画像の震源がタイル単位で中央になる機能 / Feat: 震度分布画像で欠けているタイルが描画される機能 / Chg: 震度分布画像のアセットを並列に取得 / Feat: 震度分布画像のアセットの位置が中央基準になる機能 / Chg: 震度分布画像の震源のアセットに縁取りを追加 / Chg: 震度分布画像の震度ではないアセットを拡大 / Chg: ユーズの分割の案内文を太字に / Del: 最大震度が不明な場合に投稿するかどうかのconfigを削除
This commit is contained in:
@@ -153,12 +153,12 @@ function getEdge<T extends any[]>(arr: T, property: keyof T[number]) {
|
||||
|
||||
export default async function generateImage(message: any) {
|
||||
if (
|
||||
message.earthquake.hypocenter === undefined &&
|
||||
message.earthquake.hypocenter === undefined ||
|
||||
message.points === undefined
|
||||
)
|
||||
return "input_lack";
|
||||
|
||||
const ZOOM_LEVEL = 8;
|
||||
const ZOOM_LEVEL = 9;
|
||||
|
||||
// タイル・地点取得
|
||||
const tiles: {
|
||||
@@ -278,23 +278,120 @@ export default async function generateImage(message: any) {
|
||||
scale: number;
|
||||
}))[] = [];
|
||||
|
||||
// タイルのXYの各最大、最小を取得
|
||||
const tileXCount = getEdge(tiles, "tileX");
|
||||
const tileYCount = getEdge(tiles, "tileY");
|
||||
// タイルの幅、最大最小
|
||||
let tileXCount = getEdge(tiles, "tileX");
|
||||
let tileYCount = getEdge(tiles, "tileY");
|
||||
let xSize = tileXCount.most - tileXCount.least + 1;
|
||||
let ySize = tileYCount.most - tileYCount.least + 1;
|
||||
|
||||
// タイルサイズ決定
|
||||
// タイルサイズ仮計算
|
||||
let tileSize: number;
|
||||
|
||||
const xSize = tileXCount.most - tileXCount.least + 1;
|
||||
const ySize = tileYCount.most - tileYCount.least + 1;
|
||||
xSize = tileXCount.most - tileXCount.least + 1;
|
||||
ySize = tileYCount.most - tileYCount.least + 1;
|
||||
|
||||
if (xSize > ySize) {
|
||||
tileSize = Math.round(WIDTH / xSize);
|
||||
} else {
|
||||
tileSize = Math.round(HEIGHT / ySize);
|
||||
}
|
||||
|
||||
// 全画面
|
||||
if (WIDTH - xSize * tileSize > 10) {
|
||||
const requireTilesX = Math.ceil(WIDTH / tileSize);
|
||||
const halfTilesX = Math.ceil(requireTilesX / 2);
|
||||
|
||||
tileXCount = {
|
||||
least: tileXCount.least - halfTilesX,
|
||||
most: tileXCount.most + halfTilesX,
|
||||
}
|
||||
}
|
||||
|
||||
if (HEIGHT - ySize * tileSize > 10) {
|
||||
const requireTilesY = Math.ceil(HEIGHT / tileSize);
|
||||
const halfTilesY = Math.ceil(requireTilesY / 2);
|
||||
|
||||
tileYCount = {
|
||||
least: tileYCount.least - halfTilesY,
|
||||
most: tileYCount.most + halfTilesY,
|
||||
}
|
||||
}
|
||||
|
||||
// 震源を中心とする
|
||||
const epicenterPosition = positions.filter(position => position.type === "epicenter")[0];
|
||||
if (!epicenterPosition)
|
||||
return "input_lack";
|
||||
|
||||
const epicenterTile = tiles[epicenterPosition.tileIndex];
|
||||
if (!epicenterTile)
|
||||
return "input_lack";
|
||||
|
||||
const leftPx = (epicenterTile.tileX - tileXCount.least) * tileSize + epicenterPosition.innerX;
|
||||
const rightPx = xSize * tileSize - leftPx;
|
||||
const topPx = (epicenterTile.tileY - tileYCount.least) * tileSize + epicenterPosition.innerY;
|
||||
const bottomPx = ySize * tileSize - topPx;
|
||||
|
||||
if (leftPx > rightPx) {
|
||||
const leftTiles = epicenterTile.tileX - tileXCount.least;
|
||||
tileXCount = {
|
||||
...tileXCount,
|
||||
most: epicenterTile.tileX + leftTiles,
|
||||
}
|
||||
} else {
|
||||
const rightTiles = tileXCount.most - epicenterTile.tileX;
|
||||
tileXCount = {
|
||||
...tileXCount,
|
||||
least: epicenterTile.tileX - rightTiles,
|
||||
}
|
||||
}
|
||||
|
||||
if (topPx > bottomPx) {
|
||||
const topTiles = epicenterTile.tileY - tileYCount.least;
|
||||
tileYCount = {
|
||||
...tileYCount,
|
||||
most: epicenterTile.tileY + topTiles,
|
||||
}
|
||||
} else {
|
||||
const bottomTiles = tileYCount.most - epicenterTile.tileY;
|
||||
tileYCount = {
|
||||
...tileYCount,
|
||||
least: epicenterTile.tileY - bottomTiles,
|
||||
}
|
||||
}
|
||||
|
||||
// タイルサイズ再計算
|
||||
xSize = tileXCount.most - tileXCount.least + 1;
|
||||
ySize = tileYCount.most - tileYCount.least + 1;
|
||||
|
||||
if (xSize > ySize) {
|
||||
tileSize = Math.round(WIDTH / xSize);
|
||||
} else {
|
||||
tileSize = Math.round(HEIGHT / ySize);
|
||||
}
|
||||
|
||||
// 欠けているタイルを取得
|
||||
for (let xIndex = 0; xIndex < xSize; xIndex++) {
|
||||
const itX = xIndex + tileXCount.least;
|
||||
|
||||
for (let yIndex = 0; yIndex < ySize; yIndex++) {
|
||||
const itY = yIndex + tileYCount.least;
|
||||
|
||||
const itTile = tiles.filter(tile => tile.tileX === itX && tile.tileY === itY)[0];
|
||||
if (itTile)
|
||||
continue;
|
||||
|
||||
tiles.push({
|
||||
tileX: itX,
|
||||
tileY: itY,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// アセット
|
||||
const assets: Record<string, Buffer<ArrayBuffer>> = {}
|
||||
const assets: Record<string, {
|
||||
buffer: Buffer<ArrayBuffer>;
|
||||
size: number;
|
||||
}> = {}
|
||||
const assetsList = [
|
||||
"scales/10",
|
||||
"scales/20",
|
||||
@@ -309,15 +406,20 @@ export default async function generateImage(message: any) {
|
||||
"epicenter",
|
||||
];
|
||||
|
||||
assetsList.forEach(name => {
|
||||
const asset = readFileSync(`${import.meta.dirname}/assets/${name}.png`);
|
||||
|
||||
await Promise.all(assetsList.map(async name => {
|
||||
const key = name.split("/").at(-1);
|
||||
if (!key)
|
||||
return;
|
||||
|
||||
assets[key] = asset;
|
||||
});
|
||||
const asset = readFileSync(`${import.meta.dirname}/assets/${name}.png`);
|
||||
const assetMeta = await sharp(asset).metadata();
|
||||
const assetSize = assetMeta.width;
|
||||
|
||||
assets[key] = {
|
||||
buffer: asset,
|
||||
size: assetSize,
|
||||
}
|
||||
}));
|
||||
|
||||
// 各画像処理
|
||||
await Promise.all(tiles.map(async (tile, index) => {
|
||||
@@ -371,9 +473,9 @@ export default async function generateImage(message: any) {
|
||||
|
||||
compounds.push({
|
||||
...about,
|
||||
input: asset,
|
||||
top: Math.round(top + position.innerY * tileResizedSize),
|
||||
left: Math.round(left + position.innerX * tileResizedSize),
|
||||
input: asset.buffer,
|
||||
top: Math.round(top + position.innerY * tileResizedSize - asset.size / 2),
|
||||
left: Math.round(left + position.innerX * tileResizedSize - asset.size / 2),
|
||||
});
|
||||
});
|
||||
}));
|
||||
@@ -424,6 +526,4 @@ export default async function generateImage(message: any) {
|
||||
|
||||
const buffer = await result.png().toBuffer();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
writeFileSync("result.png", await generateImage(JSON.parse(readFileSync(`${import.meta.dirname}/data.json`, "utf8"))));
|
||||
}
|
||||
Reference in New Issue
Block a user