diff --git a/app/(main)/ClientComponents/Global.tsx b/app/(main)/ClientComponents/Global.tsx index a375488..0870a33 100644 --- a/app/(main)/ClientComponents/Global.tsx +++ b/app/(main)/ClientComponents/Global.tsx @@ -1,11 +1,8 @@ -import { countryCodeMapping, reverseCountryCodeMapping } from "@/lib/geo"; -import { countryCoordinates } from "@/lib/geo-limit"; +import { countryCodeMapping } from "@/lib/geo"; import { GetNezhaData } from "@/lib/serverFetch"; -import * as turf from "@turf/turf"; -import DottedMap from "dotted-map/without-countries"; +import { geoEqualEarth, geoPath } from "d3-geo"; import { geoJsonString } from "../../../lib/geo-json-string"; -import { mapJsonString } from "../../../lib/map-string"; import GlobalInfo from "./GlobalInfo"; interface GlobalProps { @@ -29,113 +26,67 @@ export default async function ServerGlobal() { } export async function Global({ countries = [] }: GlobalProps) { - const map = new DottedMap({ map: JSON.parse(mapJsonString) }); + const width = 900; + const height = 500; + const projection = geoEqualEarth() + .scale(180) + .translate([width / 2, height / 2]) + .rotate([0, 0]); // 调整旋转以优化显示效果 + + const path = geoPath().projection(projection); + + const geoJson = JSON.parse(geoJsonString); const countries_alpha3 = countries .map((code) => countryCodeMapping[code]) .filter((code) => code !== undefined); - const geoJson = JSON.parse(geoJsonString); - - countries_alpha3.forEach((countryCode) => { - const feature = geoJson.features.find( - (f: any) => f.properties.iso_a3 === countryCode, - ); - - if (feature) { - if (countryCode === "RUS") { - // 获取俄罗斯的多个边界 - const bboxList = feature.geometry.coordinates.map((polygon: any) => - turf.bbox({ type: "Polygon", coordinates: polygon }), - ); - - const spacing = 20; // 单位为千米 - const options = { units: "kilometers" }; - - bboxList.forEach((bbox: any) => { - // @ts-expect-error ignore - const pointGrid = turf.pointGrid(bbox, spacing, options); - - // 过滤出位于当前多边形内部的点 - const pointsWithin = turf.pointsWithinPolygon(pointGrid, feature); - - if (pointsWithin.features.length === 0) { - const centroid = turf.centroid(feature); - const [lng, lat] = centroid.geometry.coordinates; - map.addPin({ - lat, - lng, - svgOptions: { color: "#FF4500", radius: 0.3 }, - }); - } else { - pointsWithin.features.forEach((point: any) => { - const [lng, lat] = point.geometry.coordinates; - map.addPin({ - lat, - lng, - svgOptions: { color: "#FF4500", radius: 0.3 }, - }); - }); - } - }); - } - // 获取国家的边界框 - const bbox = turf.bbox(feature); - - const spacing = 40; // 单位为千米,值越小点越密集 - const options = { units: "kilometers" }; - // @ts-expect-error ignore - const pointGrid = turf.pointGrid(bbox, spacing, options); - - // 过滤出位于国家多边形内部的点 - const pointsWithin = turf.pointsWithinPolygon(pointGrid, feature); - - // 如果没有点在多边形内部,则使用国家的中心点 - if (pointsWithin.features.length === 0) { - const centroid = turf.centroid(feature); - const [lng, lat] = centroid.geometry.coordinates; - map.addPin({ - lat, - lng, - svgOptions: { color: "#FF4500", radius: 0.3 }, - }); - } else { - pointsWithin.features.forEach((point: any) => { - const [lng, lat] = point.geometry.coordinates; - map.addPin({ - lat, - lng, - svgOptions: { color: "#FF4500", radius: 0.3 }, - }); - }); - } - } else { - // 如果找不到feature,使用countryCoordinates中的坐标 - const alpha2Code = reverseCountryCodeMapping[countryCode]; - if (alpha2Code && countryCoordinates[alpha2Code]) { - const coordinates = countryCoordinates[alpha2Code]; - map.addPin({ - lat: coordinates.lat, - lng: coordinates.lng, - svgOptions: { color: "#FF4500", radius: 0.3 }, - }); - } - } - }); - - const finalMap = map.getSVG({ - radius: 0.35, - color: "#D1D5DA", - shape: "circle", - }); + const filteredFeatures = geoJson.features.filter( + (feature: any) => feature.properties.iso_a3 !== "", + ); return (
- World Map with Highlighted Countries +
+ + + + + + + + {/* @ts-ignore */} + {filteredFeatures.map((feature, index) => { + const isHighlighted = countries_alpha3.includes( + feature.properties.iso_a3, + ); + return ( + + ); + })} + + +
); } diff --git a/bun.lockb b/bun.lockb index 93e0b56..099cd7f 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 92d3c28..3003c40 100644 --- a/package.json +++ b/package.json @@ -23,12 +23,15 @@ "@radix-ui/react-tooltip": "^1.1.4", "@trivago/prettier-plugin-sort-imports": "^4.3.0", "@turf/turf": "^7.1.0", + "@types/d3-geo": "^3.1.0", "@types/luxon": "^3.4.2", "@typescript-eslint/eslint-plugin": "^8.15.0", "caniuse-lite": "^1.0.30001684", "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "country-flag-icons": "^1.5.13", + "d3-geo": "^3.1.1", + "d3-selection": "^3.0.0", "dotted-map": "^2.2.3", "eslint-plugin-simple-import-sort": "^12.1.1", "flag-icons": "^7.2.3",