@hyperviz/weather
Version:
Weather visualization module using OffscreenCanvas and Web Workers
180 lines (133 loc) • 3.66 kB
Markdown
A high-performance, worker-based rendering module for weather visualization using OffscreenCanvas.
- High-performance rendering using worker threads
- Reduced main thread load through OffscreenCanvas
- Layers for integration with OpenLayers maps
- Data caching using IndexedDB
- Built-in weather data services
- Wind
- Precipitation
- Temperature
- Solar Radiation
- Cloud Coverage
```bash
npm install @hyperviz/weather
```
or
```bash
yarn add @hyperviz/weather
```
```typescript
import { WindLayer, initializeWorkerSystem } from "@hyperviz/weather";
import Map from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import OSM from "ol/source/OSM";
// Initialize worker system
initializeWorkerSystem({
poolSize: 2, // Number of workers (default: available CPU cores)
});
// Create OpenLayers map
const map = new Map({
target: "map",
layers: [
new TileLayer({
source: new OSM(),
}),
],
view: new View({
center: [14135027.5, 4511307.6], // Seoul
zoom: 7,
}),
});
// Create wind layer
const windLayer = new WindLayer({
maxSpeed: 15,
particleDensity: 1.0,
fadeOpacity: 0.95,
colorScale: ["rgba(0, 191, 255, 0.8)"],
lineWidth: 1.5,
});
// Add layer to map
windLayer.addToMap(map);
```
```typescript
import { WeatherService } from "@hyperviz/weather";
// Create weather service
const weatherService = new WeatherService({
apiKey: "your-api-key", // Optional
updateInterval: 300, // Update every 5 minutes (seconds)
});
// Map boundary coordinates
const bounds: [[number, number], [number, number]] = [
[], // Southwest
[], // Northeast
];
// Fetch weather data
weatherService.fetchWeatherData(bounds).then((weatherData) => {
// Set weather data to layer
windLayer.setWeatherData(weatherData);
});
// Start automatic updates
weatherService.startAutoUpdate(bounds, (weatherData) => {
windLayer.setWeatherData(weatherData);
});
```
```typescript
import { generateMockWeatherData } from "@hyperviz/weather";
// Generate mock weather data
const mockData = generateMockWeatherData({
bounds: [
[],
[],
],
resolution: 0.5, // Grid resolution in degrees
type: "wind", // or 'precipitation', 'temperature', 'cloud', 'solar'
});
// Set mock data to layer
windLayer.setWeatherData(mockData);
```
```typescript
// Start animation
windLayer.startAnimation();
// Pause animation
windLayer.stopAnimation();
// Change animation speed
windLayer.setOptions({ animationSpeed: 0.5 }); // 0.5x speed
```
```typescript
// Clean up when done
windLayer.dispose();
weatherService.stopAutoUpdate();
cleanupWorkerSystem();
```
Weather data is stored using IndexedDB, allowing use of the last cached data in offline environments.
- OpenLayers (ol)
- @hyperviz/worker
- Chrome 69+
- Firefox 79+
- Safari 16.4+
- Edge 79+
Works in all modern browsers that support the OffscreenCanvas API.
See the `examples/` directory for working examples, or check our [examples documentation](https://github.com/heartyoh/hyperviz/blob/main/docs/examples.md) for detailed explanations:
- Basic weather maps
- Weather data integration
- Advanced visualization options
- Layer customization
For comprehensive API documentation, see the [API reference](https://github.com/heartyoh/hyperviz/blob/main/docs/api.md).
MIT