GeoStyler
GeoStyler is an open-source JavaScript library and framework for styling geospatial data, providing a robust set of modular React UI components for map styling in web applications. It is currently at version 18.4.2 and receives frequent updates, with minor releases for features and bug fixes occurring regularly. A core differentiator is its "micro packages" approach, allowing users to leverage standalone parsers for translating between various styling formats like SLD, OpenLayers, QGIS, and Mapbox without needing the full UI component suite. Internally, it uses its own `GeoStyler Style` definition as an exchange format for seamless conversion. This modularity enables developers to either integrate high-level, opinionated UI components like the `StyleEditor` or build custom interfaces using its fundamental building blocks. It also offers command-line and REST API tools for style conversion outside of a UI context.
Common errors
-
Error: Cannot find module 'react' or 'react-dom'
cause Missing peer dependencies required by GeoStyler's React components.fixInstall React and ReactDOM: `npm install react react-dom` along with their types if using TypeScript: `npm install @types/react @types/react-dom`. -
TypeError: (0 , geostyler_sld_parser__WEBPACK_IMPORTED_MODULE_2__.default) is not a constructor
cause Attempting to import a default export (like `SLDParser`) as a named export from a CommonJS context, or incorrect bundling of ESM default exports.fixEnsure you are using correct ESM `import SLDParser from 'geostyler-sld-parser';` and that your build tool is correctly configured for ESM. If in a strict CJS environment, you might need `const SLDParser = require('geostyler-sld-parser').default;` or similar, though ESM is preferred. -
ERR_OS_NOT_SUPPORTED: The current Node.js version is not supported. Please use Node.js >= 20.6.0.
cause The project's `node` engine requirement is not met, leading to runtime errors.fixUpdate your Node.js installation to version 20.6.0 or newer. Use `nvm install 20 && nvm use 20` or a similar method appropriate for your system. -
Property 'sldParser' does not exist on type 'IntrinsicAttributes & StyleProps'.
cause TypeScript error indicating that a prop like `sldParser` is not recognized by the `Style` component's prop types.fixEnsure all required parser packages are correctly installed and that the `Style` component in your `geostyler` version supports these specific parser props. If types are outdated, consider `npm update` or checking the documentation for prop name changes.
Warnings
- breaking GeoStyler updated its internal styling from LESS to plain CSS. If your application's build configuration previously assumed the existence of `.less` files or specific LESS tooling, you might need to adjust these configurations.
- breaking The `RuleTable` component's `onCloneRule` and `onRemoveRule` signatures changed. They now expect a numeric index instead of a `RuleRecord`. This impacts applications explicitly using `RuleTable` or interacting with these callbacks.
- breaking The `node` engine requirement for GeoStyler is `>=20.6.0`. Running the package with older Node.js versions may lead to unexpected errors or failures.
- gotcha GeoStyler relies on several peer dependencies, including `react`, `react-dom`, and `ol` (OpenLayers). These must be installed separately in your project, along with their respective `@types` packages for TypeScript.
- gotcha GeoStyler emphasizes a 'micro packages' approach. Style parsers (e.g., `geostyler-sld-parser`, `geostyler-openlayers-parser`) are separate npm packages and must be installed and imported individually, not directly from the main `geostyler` package.
- breaking Some parser packages (e.g., `geostyler-geojson-parser`) have introduced ESM builds as a breaking change, requiring adaptation of import paths and/or configurations if your application was relying on CommonJS or older module resolution.
Install
-
npm install geostyler -
yarn add geostyler -
pnpm add geostyler
Imports
- Style
import StyleEditor from 'geostyler';
import { Style } from 'geostyler'; - GeoStylerStyle
import { GeoStylerStyle } from 'geostyler';import { Style } from 'geostyler-style'; - SLDParser
import { SLDParser } from 'geostyler';import SLDParser from 'geostyler-sld-parser';
Quickstart
import React, { useState } from 'react';
import { Style } from 'geostyler';
import SLDParser from 'geostyler-sld-parser';
import OpenLayersParser from 'geostyler-openlayers-parser';
import { Style as GeoStylerCoreStyle } from 'geostyler-style';
const initialGeoStylerStyle: GeoStylerCoreStyle = {
name: 'My Initial Style',
rules: [
{
name: 'Orange Point Rule',
symbolizers: [
{ kind: 'Mark', wellKnownName: 'circle', color: '#FFA500', radius: 8 }
],
},
],
};
const MyStyleEditor: React.FC = () => {
const [currentStyle, setCurrentStyle] = useState<GeoStylerCoreStyle>(initialGeoStylerStyle);
const sldParser = new SLDParser();
const olParser = new OpenLayersParser();
// Note: For a fully functional setup, you might need to provide a data source
// and more comprehensive locale and editor configurations via the Style component's props.
// This example focuses on basic style management.
return (
<div style={{ width: '100%', height: 'calc(100vh - 20px)', padding: '10px' }}>
<h1>GeoStyler Style Editor</h1>
<Style
style={currentStyle}
onStyleChange={(newStyle: GeoStylerCoreStyle) => {
console.log('Style changed:', newStyle);
setCurrentStyle(newStyle);
}}
// The Style component often needs parsers to function correctly,
// enabling read/write operations for different formats.
sldParser={sldParser}
olParser={olParser}
// Other parsers can be added as props: qmlParser, mapboxStyleParser, etc.
// locale={{ /* custom locale */ }}
// data={{ /* data source */ }}
/>
<pre style={{ marginTop: '20px', backgroundColor: '#eee', padding: '10px' }}>
{JSON.stringify(currentStyle, null, 2)}
</pre>
</div>
);
};
export default MyStyleEditor;