Skip to content

Commit

Permalink
fix: improved algorithm to get the correct time's sky definition. upd…
Browse files Browse the repository at this point in the history
…ate sky when move map if location is not specified. (#10)
  • Loading branch information
JinIgarashi committed Aug 9, 2024
1 parent 81a7d24 commit 44d43cc
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-wombats-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@watergis/maplibre-gl-sky": patch
---

fix: improved algorithm to get the correct time's sky definition. update sky when move map if location is not specified.
8 changes: 4 additions & 4 deletions packages/maplibre-gl-sky/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import { SkyControl } from '@watergis/maplibre-gl-sky';
const map = new Map();

const sky = new SkyControl();
sky.addToMap(map);
sky.addTo(map);
```

- add specific sky color

```ts
const sky = new SkyControl();
sky.addToMap(map, {
sky.addTo(map, {
timeType: 'sunset'
});
```
Expand All @@ -37,7 +37,7 @@ sky.addToMap(map, {

```ts
const sky = new SkyControl();
sky.addToMap(map, {
sky.addTo(map, {
date: new Date()
});
```
Expand All @@ -51,7 +51,7 @@ const newOptions = defaultSkyOptions;
// edit your options for `newOptions`

const sky = new SkyControl(newOptions);
sky.addToMap(map);
sky.addTo(map);
```

## License
Expand Down
2 changes: 1 addition & 1 deletion packages/maplibre-gl-sky/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
>
<div id="map"></div>
<div id="overlay">
<p id="time-buttons" class="buttons"></p>
<p id="time-buttons" class="buttons has-addons"></p>
</div>
<script type="module" src="./index.ts"></script>
</body>
Expand Down
3 changes: 3 additions & 0 deletions packages/maplibre-gl-sky/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const map = new Map({
map.addControl(new NavigationControl({ visualizePitch: true }), 'top-right');
map.addControl(new maplibregl.AttributionControl({ compact: false }), 'bottom-right');

const sky = new SkyControl();
sky.addTo(map);

const timeButtons = document.getElementById('time-buttons');

const autoBtn = document.createElement('button');
Expand Down
79 changes: 59 additions & 20 deletions packages/maplibre-gl-sky/src/lib/SkyControl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Map, SkySpecification } from 'maplibre-gl';
import SunCalc from 'suncalc';
import { AddToOptions, AvailableTimeTypes, Options, SkyTimeType } from './interfaces';
import { defaultSkyOptions } from './defaultOptions';
import { AddToOptions, AvailableTimeTypes, Options, SkyTimeType, TimeType } from './interfaces';
import { defaultSkyOptions } from './constants';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);

export class SkyControl {
private map?: Map;
Expand All @@ -29,10 +31,14 @@ export class SkyControl {
this.map?.setSky(currentSky);
});
}
}

private timeToSec(date: dayjs.Dayjs) {
return date.hour() * 60 * 60 + date.minute() * 60 + date.second();
if (!options?.location) {
this.map.on('moveend', () => {
const currentSky = this.getSky(options);
if (!currentSky) return;
this.map?.setSky(currentSky);
});
}
}

private getSky(options?: AddToOptions) {
Expand Down Expand Up @@ -62,24 +68,40 @@ export class SkyControl {
private getSkySpecByTime(lng: number, lat: number, date?: Date) {
const skyOptions = this.options.skyOptions;
if (!skyOptions) return;
// convert to UTC
let currentDate: dayjs.Dayjs;
if (!date) {
date = new Date();
currentDate = dayjs.utc();
} else {
currentDate = dayjs(date).utc();
}
const times = SunCalc.getTimes(date, lng, lat);
const times = SunCalc.getTimes(currentDate.toDate(), lat, lng);

const currentDate = dayjs(date);
const currentTimeSec = this.timeToSec(currentDate);
// add 1 day before and 1 day after
const timesBefore: { type: TimeType; date: dayjs.Dayjs }[] = [];
const timesToday: { type: TimeType; date: dayjs.Dayjs }[] = [];
const timesAfter: { type: TimeType; date: dayjs.Dayjs }[] = [];
AvailableTimeTypes.forEach((timeType) => {
const targetTime = dayjs(times[timeType]).utc();
timesToday.push({ type: timeType, date: targetTime });
const before = targetTime.add(-1, 'day');
timesBefore.push({ type: timeType, date: before });

const after = targetTime.add(1, 'day');
timesAfter.push({ type: timeType, date: after });
});
let availableTImes = [...timesBefore, ...timesToday, ...timesAfter];
availableTImes = availableTImes.sort(
(a, b) => a.date.toDate().getTime() - b.date.toDate().getTime()
);

let beforeTime: SkyTimeType = {};
let afterTime: SkyTimeType = {};

AvailableTimeTypes.forEach((timeType) => {
let targetTime = dayjs(times[timeType]);
let targetTimeSec = this.timeToSec(targetTime);
if (targetTime.isSame(currentDate, 'day') && targetTimeSec < currentTimeSec) {
targetTime = targetTime.add(-1, 'day');
targetTimeSec = this.timeToSec(targetTime);
}
availableTImes.forEach((time) => {
const targetTime = time.date;
const timeType = time.type;
// console.log(timeType, targetTime.toISOString(), currentDate.toISOString())

if (targetTime.isBefore(currentDate)) {
const sky = skyOptions[timeType] as SkySpecification;
Expand Down Expand Up @@ -117,11 +139,28 @@ export class SkyControl {

// console.log(beforeTime, afterTime);

const activeTime = afterTime?.sky ?? beforeTime?.sky;
if (activeTime) {
return activeTime as SkySpecification;
const beforeDiff = beforeTime.date
? currentDate.toDate().getTime() - beforeTime.date.toDate().getTime()
: -1;
const afterDiff = afterTime.date
? afterTime.date.toDate().getTime() - currentDate.toDate().getTime()
: -1;

let activeTime: SkySpecification;
if (beforeDiff > -1 && afterDiff > -1) {
if (afterDiff > beforeDiff) {
activeTime = afterTime.sky as SkySpecification;
} else {
activeTime = beforeTime.sky as SkySpecification;
}
} else if (beforeTime && !afterTime) {
activeTime = beforeTime.sky as SkySpecification;
} else if (afterTime && !beforeTime) {
activeTime = afterTime.sky as SkySpecification;
} else {
return skyOptions.solarNoon;
activeTime = skyOptions.solarNoon;
}

return activeTime;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Options } from './interfaces';
import { Options } from '../interfaces';

export const defaultSkyOptions: Options = {
skyOptions: {
Expand Down
1 change: 1 addition & 0 deletions packages/maplibre-gl-sky/src/lib/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './defaultOptions';
1 change: 1 addition & 0 deletions packages/maplibre-gl-sky/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './SkyControl';
export * from './interfaces';
export * from './constants';

0 comments on commit 44d43cc

Please sign in to comment.