@ixnode/geo-sphere
Advanced tools
Comparing version
@@ -14,2 +14,4 @@ import { TypeBoundingBox, TypeBoundingBoxType, TypeCountryKey, TypeFeatureMap } from "../types/types"; | ||
private readonly coordinateConverter; | ||
private width; | ||
private height; | ||
/** | ||
@@ -21,2 +23,4 @@ * The constructor of WorldMapSvg. | ||
constructor(options?: BoundingBoxOptions); | ||
setWidth(width: number): void; | ||
setHeight(height: number): void; | ||
/** | ||
@@ -47,2 +51,10 @@ * Calculates the bounding box from the given feature map. | ||
/** | ||
* Fixes given latitude and longitude values to dimensions. | ||
* | ||
* @param boundingBox | ||
* @param wantedWidth | ||
* @param wantedHeight | ||
*/ | ||
private adjustBoundingBox; | ||
/** | ||
* Calculates the bounding box from the given point. | ||
@@ -49,0 +61,0 @@ * |
@@ -85,7 +85,5 @@ import { InterfaceGeoJson, TypeBoundingBox } from "../types/types"; | ||
* @param country | ||
* @param width | ||
* @param height | ||
*/ | ||
generateSVG(geoJSON: InterfaceGeoJson, boundingBox: TypeBoundingBox, country: string | null, width: number, height: number): TypeSvgContent; | ||
generateSVG(geoJSON: InterfaceGeoJson, boundingBox: TypeBoundingBox, country: string | null): TypeSvgContent; | ||
} | ||
export {}; |
{ | ||
"name": "@ixnode/geo-sphere", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"description": "A Node.js package for rendering interactive world maps with Mercator projection, customizable languages, and advanced interactivity features such as zoom, pan, and click events.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs", |
import {GeometryChecker} from "./GeometryChecker"; | ||
import {boundingBoxEuropeProj4326} from "../config/config"; | ||
import {boundingBoxEuropeProj4326, defaultMapHeight, defaultMapWidth} from "../config/config"; | ||
import { | ||
@@ -32,2 +32,6 @@ TypeBoundingBox, | ||
private width: number|null = null; | ||
private height: number|null = null; | ||
/** | ||
@@ -40,2 +44,10 @@ * The constructor of WorldMapSvg. | ||
public setWidth(width: number) { | ||
this.width = width; | ||
} | ||
public setHeight(height: number) { | ||
this.height = height; | ||
} | ||
/** | ||
@@ -180,2 +192,51 @@ * Calculates the bounding box from the given feature map. | ||
/** | ||
* Fixes given latitude and longitude values to dimensions. | ||
* | ||
* @param boundingBox | ||
* @param wantedWidth | ||
* @param wantedHeight | ||
*/ | ||
private adjustBoundingBox = ( | ||
boundingBox: TypeBoundingBox, | ||
wantedWidth: number, | ||
wantedHeight: number | ||
) => { | ||
const {longitudeMin, longitudeMax, latitudeMin, latitudeMax} = boundingBox; | ||
const width = longitudeMax - longitudeMin; | ||
const height = latitudeMax - latitudeMin; | ||
const currentAspectRatio = width / height; | ||
const wantedAspectRatio = wantedWidth / wantedHeight; | ||
const longitudeCenter = (longitudeMin + longitudeMax) / 2; | ||
const latitudeCenter = (latitudeMin + latitudeMax) / 2; | ||
let adjustedWidth = width; | ||
let adjustedHeight = height; | ||
if (currentAspectRatio > wantedAspectRatio) { | ||
adjustedHeight = width / wantedAspectRatio; | ||
} else if (currentAspectRatio < wantedAspectRatio) { | ||
adjustedWidth = height * wantedAspectRatio; | ||
} | ||
const newLongitudeMin = longitudeCenter - adjustedWidth / 2; | ||
const newLongitudeMax = longitudeCenter + adjustedWidth / 2; | ||
const newLatitudeMin = latitudeCenter - adjustedHeight / 2; | ||
const newLatitudeMax = latitudeCenter + adjustedHeight / 2; | ||
return { | ||
longitudeMin: newLongitudeMin, | ||
latitudeMin: newLatitudeMin, | ||
longitudeMax: newLongitudeMax, | ||
latitudeMax: newLatitudeMax, | ||
width: newLongitudeMax - newLongitudeMin, | ||
height: newLatitudeMax - newLatitudeMin, | ||
}; | ||
}; | ||
/** | ||
* Calculates the bounding box from the given point. | ||
@@ -315,46 +376,33 @@ * | ||
): TypeBoundingBox { | ||
/* Extract the current bounding box. */ | ||
let longitudeMin = boundingBox.longitudeMin; | ||
let latitudeMin = boundingBox.latitudeMin; | ||
let longitudeMax = boundingBox.longitudeMax; | ||
let latitudeMax = boundingBox.latitudeMax; | ||
/* Calculate the distance of the current bounding box. */ | ||
const longitudeDistance = longitudeMax - longitudeMin; | ||
const latitudeDistance = latitudeMax - latitudeMin; | ||
/* Adjust bounding box to given width and height. */ | ||
boundingBox = this.adjustBoundingBox(boundingBox, width, height); | ||
/* Calculate the center point of the current bounding box. */ | ||
const longitudeCenter = (longitudeMin + longitudeMax) / 2; | ||
const latitudeCenter = (latitudeMin + latitudeMax) / 2; | ||
/* No gap given. */ | ||
if (factorGapLongitude <= 0 || factorGapLatitude <= 0) { | ||
return boundingBox; | ||
} | ||
/* Calculate the ratio of the width and height to be output and the current element. */ | ||
const aspectRatioOutput = width / height; | ||
const aspectRatioElement = longitudeDistance / latitudeDistance; | ||
/* Calculate the distance of the current bounding box. */ | ||
const longitudeDistance = boundingBox.longitudeMax - boundingBox.longitudeMin; | ||
const latitudeDistance = boundingBox.latitudeMax - boundingBox.latitudeMin; | ||
/* Adjustment of width and height based on the ratio. */ | ||
let longitudeDistanceAdjusted: number = aspectRatioOutput < aspectRatioElement ? longitudeDistance : latitudeDistance * aspectRatioOutput; | ||
let latitudeDistanceAdjusted: number = aspectRatioOutput < aspectRatioElement ? longitudeDistance / aspectRatioOutput : latitudeDistance; | ||
const gapLongitude = longitudeDistance * factorGapLongitude / 2; | ||
const gapLatitude = latitudeDistance * factorGapLatitude / 2; | ||
const longitudeDistanceLeft = longitudeDistanceAdjusted / 2 * (aspectRatioOutput < aspectRatioElement ? 1 : 1); | ||
const latitudeDistanceTop = latitudeDistanceAdjusted / 2; | ||
const longitudeMin = boundingBox.longitudeMin - gapLongitude; | ||
const longitudeMax = boundingBox.longitudeMax + gapLongitude; | ||
const longitudeDistanceRight = longitudeDistanceAdjusted - longitudeDistanceAdjusted; | ||
const latitudeDistanceBottom = latitudeDistanceAdjusted - latitudeDistanceTop; | ||
const latitudeMin = boundingBox.latitudeMin - gapLatitude; | ||
const latitudeMax = boundingBox.latitudeMax + gapLatitude; | ||
const zoomGapBoundingBoxLongitude = factorGapLongitude * longitudeDistance; | ||
const zoomGapBoundingBoxLatitude = factorGapLatitude * latitudeDistance; | ||
return { | ||
longitudeMin: longitudeMin, | ||
longitudeMax: longitudeMax, | ||
const longitudeMinCalc = longitudeCenter - longitudeDistanceLeft - zoomGapBoundingBoxLongitude; | ||
const latitudeMinCalc = latitudeCenter - latitudeDistanceTop - zoomGapBoundingBoxLatitude; | ||
latitudeMin: latitudeMin, | ||
latitudeMax: latitudeMax, | ||
const longitudeMaxCalc = longitudeCenter + longitudeDistanceRight + zoomGapBoundingBoxLongitude; | ||
const latitudeMaxCalc = latitudeCenter + latitudeDistanceBottom + zoomGapBoundingBoxLatitude; | ||
return { | ||
longitudeMin: longitudeMinCalc, | ||
latitudeMin: latitudeMinCalc, | ||
longitudeMax: longitudeMaxCalc, | ||
latitudeMax: latitudeMaxCalc, | ||
width: longitudeMaxCalc - longitudeMinCalc, | ||
height: latitudeMaxCalc - latitudeMinCalc | ||
width: longitudeMax - longitudeMin, | ||
height: latitudeMax - latitudeMin, | ||
}; | ||
@@ -361,0 +409,0 @@ } |
@@ -204,4 +204,2 @@ /* Import configuration. */ | ||
* @param country | ||
* @param width | ||
* @param height | ||
*/ | ||
@@ -211,9 +209,7 @@ public generateSVG( | ||
boundingBox: TypeBoundingBox, | ||
country: string|null, | ||
width: number, | ||
height: number | ||
country: string|null | ||
): TypeSvgContent { | ||
const viewBoxLeft = boundingBox.longitudeMin; | ||
const viewBoxTop = -boundingBox.latitudeMax; | ||
const viewBoxWidth = (boundingBox.longitudeMax - boundingBox.longitudeMin) * width / height; | ||
const viewBoxWidth = (boundingBox.longitudeMax - boundingBox.longitudeMin); | ||
const viewBoxHeight = boundingBox.latitudeMax - boundingBox.latitudeMin; | ||
@@ -220,0 +216,0 @@ |
@@ -52,3 +52,3 @@ /* Import configuration. */ | ||
private boundingBox: BoundingBox = new BoundingBox(); | ||
private boundingBox: BoundingBox; | ||
@@ -90,2 +90,5 @@ private geoJson2Path: GeoJson2Path; | ||
this.language = options.language ?? this.propertyLanguageDefault; | ||
this.boundingBox = new BoundingBox(); | ||
this.boundingBox.setWidth(this.width); | ||
this.boundingBox.setHeight(this.height); | ||
@@ -168,3 +171,5 @@ /* Build GeoJson2Path with configuration. */ | ||
*/ | ||
public generateSvgByCountry(country: string|null): TypeSvgContent { | ||
public generateSvgByCountry( | ||
country: string|null | ||
): TypeSvgContent { | ||
let boundingType = this.boundingBox.getBoundingType(this.country, this.countryKey, this.zoomCountry); | ||
@@ -191,3 +196,3 @@ | ||
return this.geoJson2Path.generateSVG(this.data, boundingBox, country, this.width, this.height); | ||
return this.geoJson2Path.generateSVG(this.data, boundingBox, country); | ||
} | ||
@@ -194,0 +199,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43099893
0.02%41653
0.16%