Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/OpenHistoricalMap/maplibre-gl-dates
MapLibre GL JS plugin for filtering the map based on a date
https://github.com/OpenHistoricalMap/maplibre-gl-dates
datetime maplibre-gl-js openhistoricalmap
Last synced: about 1 month ago
JSON representation
MapLibre GL JS plugin for filtering the map based on a date
- Host: GitHub
- URL: https://github.com/OpenHistoricalMap/maplibre-gl-dates
- Owner: OpenHistoricalMap
- License: cc0-1.0
- Created: 2024-08-04T06:33:36.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-11-22T01:01:03.000Z (about 1 month ago)
- Last Synced: 2024-11-22T02:17:31.092Z (about 1 month ago)
- Topics: datetime, maplibre-gl-js, openhistoricalmap
- Language: JavaScript
- Homepage:
- Size: 169 KB
- Stars: 4
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-maplibre - maplibre-gl-dates - enabled map by date. Optimized for OpenHistoricalMap vector tiles. (Other Plugins / Vue)
README
# MapLibre GL Dates
This is a plugin for [MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js/) for filtering the map based on a date. The plugin is designed for use with [OpenHistoricalMap](https://www.openhistoricalmap.org/) data.
## Requirements
This plugin requires [MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js/) v3.0.0 and above.
This plugin is able to manipulate both the deprecated [legacy filter syntax](https://maplibre.org/maplibre-style-spec/deprecations/#other-filter) and the newer [expression syntax](https://maplibre.org/maplibre-style-spec/expressions/) defined in the MapLibre Style Specification.
The stylesheet must be backed by a vector tileset, such as [OpenHistoricalMap’s official vector tileset](https://wiki.openstreetmap.org/wiki/OpenHistoricalMap/Reuse#Vector_tiles_and_stylesheets), that includes the following properties in each tile layer:
Property | Type | Description
----|----|----
`start_date` | String | The date the feature came into existence as a date string.
`start_decdate` | Number | The date the feature came into existence as a decimal year.
`end_date` | String | The date the feature went out of existence as a date string.
`end_decdate` | Number | The date the feature went out of existence as a decimal year.A date string is a date in `YYYY`, `YYYY-MM`, or `YYYY-MM-DD` format, similar to ISO 8601-1 format. A decimal year is a floating-point number such that each integer represents midnight on New Year’s Day. An implementation of decimal year conversion functions is available [for PL/pgSQL](https://github.com/OpenHistoricalMap/DateFunctions-plpgsql/).
All properties are optional, but the plugin will only have an effect if one or more of these properties is present in the tileset. For performance reasons, if a given feature has a `start_decdate` or `end_decdate` property, this plugin prefers it over the `start_date` or `end_date` property.
Regardless of the data type, all dates are interpreted according to the proleptic Gregorian calendar. As there is no Year Zero, the value 1.0 falls on New Year’s Day of 1 CE, the value 0.0 falls on 1 BCE, the value -1.0 falls on 2 BCE, etc.
## Installation
This plugin is available as [an NPM plugin](https://www.npmjs.com/package/@openhistoricalmap/maplibre-gl-dates). To install it, run the following command:
```bash
npm install @openhistoricalmap/maplibre-gl-dates
```## Usage
After creating an instance of `maplibregl.Map`, register an event listener for the `styledata` event that filters the map:
```js
map.once('styledata', function (event) {
map.filterByDate('2013-04-14');
});
```If you set the `hash` option to a string when creating the `Map`, you can have this code respond to a `date` parameter in the URL hash:
```js
map.once('styledata', function (event) {
let params = new URLSearchParams(location.hash.substring(1));
let date = params.get('date') || new Date();
map.filterByDate(date);
});
```And you can add a window event listener for whenever the hash changes, in order to update the filter:
```js
addEventListener('hashchange', function (event) {
let oldParams = new URLSearchParams(new URL(event.oldURL).hash.substring(1));
let newParams = new URLSearchParams(new URL(event.newURL).hash.substring(1));
let oldDate = oldParams.get('date') || new Date();
let newDate = newParams.get('date') || new Date();
if (oldDate !== newDate) {
map.filterByDate(newDate);
}
});
```## API
This plugin adds a single method to each instance of `maplibregl.Map`.
### `filterByDate`
Filters the map’s features by a date.
Parameters:
Parameter | Type | Description
----|----|----
`date` | `Date` or date string | The date to filter by.A date string is defined as a date in `YYYY`, `YYYY-MM`, or `YYYY-MM-DD` format, similar to ISO 8601-1 format. If the date is only given to year precision, every feature overlapping that year is included; likewise, if the date is given to month precision, every feature overlapping that month is included. Negative years are supported as described in “[Requirements](#requirements)”.
## Feedback
Please submit bug reports and feature requests to [OpenHistoricalMap’s central issue tracker](https://github.com/OpenHistoricalMap/issues/issues/), noting “maplibre-gl-dates” somewhere in the title or description.