feat(proj): init

This commit is contained in:
vl.arkhangelskii
2026-09-21 03:53:15 +03:00
commit 3e34f391b3
258 changed files with 20968 additions and 0 deletions
@@ -0,0 +1,147 @@
using System.Text.Json.Nodes;
using Microsoft.Extensions.Options;
using TileServer.Application.Configuration;
using TileServer.Application.Extracts;
using TileServer.Application.Tiles;
using TileServer.Domain;
using TileServer.Domain.Exceptions;
using TileServer.Domain.Extracts;
namespace TileServer.Application.Styles;
public sealed class StyleService(
IStyleCatalog catalog,
IExtractCatalog extracts,
ITileStore tileStore,
IOptions<TileServerOptions> options) : IStyleService
{
public IReadOnlyList<StyleListItem> List(string baseUrl)
=> catalog.List()
.Select(s => new StyleListItem(s.Name, Combine(baseUrl, $"/api/v1/styles/{s.Name}")))
.ToArray();
public JsonObject Get(string name, string extractId, string baseUrl)
{
var styleName = ResourceName.Require(name, nameof(name));
var extract = ResolveExtract(extractId);
var style = catalog.GetRequired(styleName);
var tilesUrl = Combine(baseUrl, $"/api/v1/tiles/{extract.Id}/{{z}}/{{x}}/{{y}}.pbf?v=3");
BindRuntimeUrls(style, extract, tilesUrl);
return style;
}
public JsonObject Bind(JsonObject style, string extractId, string tilesUrl)
{
var extract = ResolveExtract(extractId);
var clone = JsonNode.Parse(style.ToJsonString())?.AsObject()
?? throw new DomainValidationException("Style must be a JSON object.");
BindRuntimeUrls(clone, extract, tilesUrl);
return clone;
}
private Extract ResolveExtract(string extractId)
=> string.IsNullOrWhiteSpace(extractId)
? extracts.GetAll().FirstOrDefault(e => e.Enabled)
?? throw new ResourceNotFoundException("Extract", "(none configured)")
: extracts.GetRequired(extractId);
public Task SaveAsync(string name, JsonObject style, CancellationToken ct)
{
var styleName = ResourceName.Require(name, nameof(name));
if (style["version"] is null)
{
style["version"] = 8;
}
return catalog.SaveAsync(styleName, style, ct);
}
private void BindRuntimeUrls(JsonObject style, Extract extract, string tilesUrl)
{
var glyphs = options.Value.GlyphsUrl;
var metadata = tileStore.GetMetadata(extract.Id);
var minZoom = metadata?.MinZoom ?? extract.MinZoom;
var maxZoom = metadata?.MaxZoom ?? extract.MaxZoom;
style["glyphs"] = glyphs;
ReplaceOpenSansFonts(style);
style["sources"] ??= new JsonObject();
if (style["sources"] is not JsonObject sources)
{
throw new DomainValidationException("Style 'sources' must be a JSON object.");
}
if (sources.Count == 0)
{
sources["openmaptiles"] = CreateVectorSource(tilesUrl, minZoom, maxZoom);
return;
}
foreach (var property in sources.ToList())
{
if (property.Value is JsonObject source &&
string.Equals(source["type"]?.GetValue<string>(), "vector", StringComparison.OrdinalIgnoreCase))
{
ApplyVectorSource(source, tilesUrl, minZoom, maxZoom);
}
}
}
private static JsonObject CreateVectorSource(string tilesUrl, int minZoom, int maxZoom)
{
var source = new JsonObject
{
["type"] = "vector"
};
ApplyVectorSource(source, tilesUrl, minZoom, maxZoom);
return source;
}
private static void ApplyVectorSource(JsonObject source, string tilesUrl, int minZoom, int maxZoom)
{
source["type"] = "vector";
source["tiles"] = new JsonArray(tilesUrl);
source["minzoom"] = minZoom;
source["maxzoom"] = maxZoom;
source["scheme"] = "xyz";
source["attribution"] = "© OpenStreetMap contributors";
source.Remove("url");
source.Remove("bounds");
}
private static void ReplaceOpenSansFonts(JsonObject style)
{
if (style["layers"] is not JsonArray layers)
{
return;
}
foreach (var node in layers)
{
if (node is not JsonObject layer || layer["layout"] is not JsonObject layout || layout["text-font"] is not JsonArray fonts)
{
continue;
}
for (var i = 0; i < fonts.Count; i++)
{
var name = fonts[i]?.GetValue<string>();
if (name == "Open Sans Regular")
{
fonts[i] = "Noto Sans Regular";
}
else if (name == "Open Sans Bold")
{
fonts[i] = "Noto Sans Bold";
}
else if (name == "Open Sans Italic")
{
fonts[i] = "Noto Sans Italic";
}
}
}
}
private static string Combine(string baseUrl, string path)
=> string.IsNullOrEmpty(baseUrl) ? path : $"{baseUrl.TrimEnd('/')}{path}";
}