Spaces:
Running
Running
File size: 6,869 Bytes
618aeaa | 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | const DEFAULT_MAPBOX_STYLE_ID = "mapbox/light-v11";
const DEFAULT_CITY_TILE_STYLES = [
{ id: "light", label: "浅色", styleId: "mapbox/light-v11" },
{ id: "streets", label: "街道", styleId: "mapbox/streets-v12" },
{ id: "outdoors", label: "地形", styleId: "mapbox/outdoors-v12" },
{ id: "satellite", label: "卫星", styleId: "mapbox/satellite-streets-v12" },
{ id: "custom", label: "自定义", styleId: "" },
];
const OSM_PROVIDER = {
name: "osm",
attribution: "© OpenStreetMap",
tileSize: 256,
};
const CARTO_LIGHT_PROVIDER = {
name: "cartoLight",
attribution: "© OpenStreetMap © CARTO",
tileSize: 256,
};
const LOCAL_CITY_TILES_ATTRIBUTION = "© Mapbox © OpenStreetMap";
function normalizeProviderName(name) {
return String(name || "osm").replace(/[-_\s]/g, "").toLowerCase();
}
function normalizeMapboxStyleId(styleId = DEFAULT_MAPBOX_STYLE_ID) {
const cleaned = String(styleId || DEFAULT_MAPBOX_STYLE_ID)
.replace(/^styles\/v1\//, "")
.replace(/^\/+|\/+$/g, "");
const stylePath = cleaned.includes("/") ? cleaned : `mapbox/${cleaned}`;
return stylePath.split("/").map((part) => encodeURIComponent(part)).join("/");
}
function normalizeStyleOption(style) {
const id = String(style?.id || "").trim();
const label = String(style?.label || id).trim();
const styleId = String(style?.styleId || style?.mapboxStyleId || "").trim();
if (!id || !label || (id !== "custom" && !styleId)) return null;
return { id, label, styleId };
}
function normalizeTileBounds(bounds) {
if (!bounds || typeof bounds !== "object") return null;
const normalized = {
z: Number(bounds.z),
minX: Number(bounds.minX),
maxX: Number(bounds.maxX),
minY: Number(bounds.minY),
maxY: Number(bounds.maxY),
};
if (Object.values(normalized).some((value) => !Number.isInteger(value))) return null;
if (normalized.minX > normalized.maxX || normalized.minY > normalized.maxY) return null;
return normalized;
}
function tileIsInsideBounds(bounds, { z, x, y }) {
if (!bounds) return true;
return z === bounds.z
&& x >= bounds.minX
&& x <= bounds.maxX
&& y >= bounds.minY
&& y <= bounds.maxY;
}
export function normalizeCityTileStyles(runtimeConfig = {}) {
const configured = Array.isArray(runtimeConfig.mapboxStyles)
? runtimeConfig.mapboxStyles.map(normalizeStyleOption).filter(Boolean)
: [];
const stylesById = new Map();
for (const style of configured) stylesById.set(style.id, style);
for (const style of DEFAULT_CITY_TILE_STYLES) {
if (!stylesById.has(style.id)) stylesById.set(style.id, style);
}
return Array.from(stylesById.values());
}
function resolveMapboxStyleId(special, runtimeConfig) {
if (special.mapboxStyleId) return special.mapboxStyleId;
const styles = normalizeCityTileStyles(runtimeConfig);
const activeStyleId = special.activeMapStyleId
|| runtimeConfig.activeMapStyleId
|| runtimeConfig.defaultMapStyleId
|| runtimeConfig.defaultMapboxStyleKey
|| runtimeConfig.defaultStyleId
|| "";
if (!activeStyleId) {
return runtimeConfig.mapboxStyleId
|| runtimeConfig.styleId
|| runtimeConfig.mapbox?.styleId
|| styles[0]?.styleId
|| DEFAULT_MAPBOX_STYLE_ID;
}
if (activeStyleId === "custom") {
return runtimeConfig.customMapboxStyleId
|| runtimeConfig.mapboxStyleId
|| runtimeConfig.styleId
|| runtimeConfig.mapbox?.styleId
|| DEFAULT_MAPBOX_STYLE_ID;
}
return styles.find((style) => style.id === activeStyleId)?.styleId
|| runtimeConfig.mapboxStyleId
|| runtimeConfig.styleId
|| runtimeConfig.mapbox?.styleId
|| DEFAULT_MAPBOX_STYLE_ID;
}
export function resolveCityTileProvider(special = {}, runtimeConfig = {}) {
const providerName = normalizeProviderName(
special.tileProvider
|| runtimeConfig.cityTileProvider
|| runtimeConfig.tileProvider
|| runtimeConfig.provider,
);
if (providerName === "mapbox" || providerName === "mapboxstatic" || providerName === "mapboxtiles") {
const token = special.mapboxAccessToken
|| special.mapboxToken
|| runtimeConfig.mapboxAccessToken
|| runtimeConfig.mapboxToken
|| runtimeConfig.mapbox?.accessToken
|| runtimeConfig.mapbox?.token
|| "";
if (!token) return { ...CARTO_LIGHT_PROVIDER, fallbackReason: "missing-mapbox-token" };
return {
name: "mapboxStatic",
attribution: "© Mapbox © OpenStreetMap",
tileSize: Number(special.tileSize || runtimeConfig.tileSize || 256) || 256,
mapboxStyleId: normalizeMapboxStyleId(resolveMapboxStyleId(special, runtimeConfig)),
mapboxAccessToken: token,
retina: special.retinaTiles ?? runtimeConfig.retinaTiles ?? true,
};
}
if (providerName === "carto" || providerName === "cartolight" || providerName === "cartopositron") {
return { ...CARTO_LIGHT_PROVIDER };
}
if (providerName === "local" || providerName === "localcitytiles" || providerName === "localtiles") {
const baseUrl = String(
special.localTileBaseUrl
|| special.tileBaseUrl
|| runtimeConfig.localTileBaseUrl
|| runtimeConfig.localCityTileBaseUrl
|| "",
).replace(/\/+$/g, "");
if (!baseUrl) return { ...CARTO_LIGHT_PROVIDER, fallbackReason: "missing-local-tile-base-url" };
return {
name: "localCityTiles",
attribution: special.localTileAttribution || runtimeConfig.localTileAttribution || LOCAL_CITY_TILES_ATTRIBUTION,
tileSize: Number(special.tileSize || runtimeConfig.tileSize || 256) || 256,
baseUrl,
tileBounds: normalizeTileBounds(special.localTileBounds || runtimeConfig.localTileBounds),
version: String(special.localTileVersion || runtimeConfig.localTileVersion || "").trim(),
};
}
return { ...OSM_PROVIDER };
}
export function buildCityTileUrl(provider, { z, x, y }) {
if (provider?.name === "localCityTiles") {
if (!tileIsInsideBounds(provider.tileBounds, { z, x, y })) return "";
const versionSuffix = provider.version ? `?v=${encodeURIComponent(provider.version)}` : "";
return `${provider.baseUrl}/${z}/${x}/${y}.png${versionSuffix}`;
}
if (provider?.name === "mapboxStatic") {
const scaleSuffix = provider.retina === false ? "" : "@2x";
const params = new URLSearchParams({ access_token: provider.mapboxAccessToken });
return `https://api.mapbox.com/styles/v1/${provider.mapboxStyleId}/tiles/${provider.tileSize}/${z}/${x}/${y}${scaleSuffix}?${params.toString()}`;
}
if (provider?.name === "cartoLight") {
const scaleSuffix = provider.retina === false ? "" : "@2x";
return `https://a.basemaps.cartocdn.com/light_all/${z}/${x}/${y}${scaleSuffix}.png`;
}
return `https://tile.openstreetmap.org/${z}/${x}/${y}.png`;
}
export function runtimeCityTileConfig() {
return globalThis.CITYBOOK_MAP_CONFIG || {};
}
|