# GeoJSON2SVG [](https://badge.fury.io/js/geojson-to-svg) [](https://travis-ci.org/w8r/geojson2svg)
Render geojson into SVG using inline or external stylesheet, in Node or in the browser
- [World countries in WGS, ~4Mb GeoJSON, randomized style, processed in 280ms, compressed with SVGO](https://w8r.github.io/geojson2svg/demo/lands.svg)
- [Example of non-cartographic `GeoJSON` rendered with the lib in 18ms](https://w8r.github.io/geojson2svg/demo/markup.svg) ([GeoJSON source](https://raw.githubusercontent.com/w8r/geojson2svg/master/test/fixtures/data.json))
## Usage
via npm
```shell
npm install geojson-to-svg -S
```
CommonJS
```js
// ES5
var geojson2svg = require("geojson-to-svg"); // factory
// if you want to re-use the renderer
var Renderer = geojson2svg.Renderer;
// ES6
import geojson2svg, { Renderer } from "geojson-to-svg";
```
Alternatively, you can use one of the pre-built versions from the `dist/` directory.
## API
# **new geojson2svg.Renderer(** gj, styles, extent, projection, type, fonts, transform **)**
# **geojson2svg(** gj, styles, extent, projection, type, fonts, transform **)**
Constructor and factory. You may pass all the settings in one go to the renderer, but it's rather convenient to use the settings API:
# **geojson2svg().data(** FeatureCollection|Feature **)**
Stores the data for rendering, it will also be projected right away if you specified the projection function before calling this method.
# **geojson2svg().styles(** Object | Function **)**
Styles hashmap, you can define your own types, the type will be selected from feature properties. Also a function can be passed, which would provide styling based on the feature. The signature is `style (feature, canvasBBox, featureBBox) -> Object`
```js
geojson2svg()
.styles({ YourType: { fill: "blue", stroke: "red" } })
.data({ type: "Feature", properties: { type: "YourType" } })
.render();
```
# **geojson2svg().type(** String **)**
Which field in the feature properties should be used to select the style for the feature from the stylesheet.
```js
geojson2svg()
.type("myField")
.styles({ YourType: { fill: "blue", stroke: "red" } })
.data({ type: "Feature", properties: { myField: "YourType" } })
.render();
```
# **geojson.render(** GeoJSON(optional) **)**
Main rendering pipe, returns SVG string.
# **geojson2svg().fonts(** Array | Font **)**
Fonts measurements required for correct text and multi-line text rendering.
Must be in the same format that the CLI script provides, see
[font measurement](#font-measurement). You can provide several measurement
data objects at once in an array.
```js
geojson2svg()
.fonts({
name: '\"Custom font\", Standard-font, serif',
values : [{
"avg": 1.5294730846698468,
"height": 3.5390625,
"size": 3
}]
})
.render({
type: 'Feature',
properties: {
type: 'textbox',
fontFamily: '\"Custom font\", Standard-font, serif',
fontSize: 3
...
}
})
```
# **geojson2svg().extent(** [xMin, yMin, xMax, yMax] **)**
Forced canvas extent, e.g. SVG `viewBox`. It will overwrite what was calculated from the features coordinatesand styles.
# **geojson2svg().decorator(** string, (feature, rings, closed, bbox, featuresBbox) => string **)**
Registers a decorator for the special rendering of the `Polygon`/`LineString` path.
Basically it's a custom `` renderer, it has to return a valid `SVG` path.
```js
geojson2svg()
.decorator("YourType", function (
feature,
coordinates /* rings */,
closed,
bbox,
fBounds
) {
var path = "";
for (var i = 0, len = coords.length; i < len; i++) {
// randomize coords a little bit
var c = [coords[i][0] + Math.random(), coords[i][1] + Math.random()];
path += (i === 0 ? "M" : "L") + c[0] + " " + c[1];
// it's your responsibility to update the content bounds if you
// don't provide the forced extent through .extent()
geojson2svg.extendBBox(bbox, c);
geojson2svg.extendBBox(fBounds, c);
}
if (closed) path += "Z";
return path;
})
.data({
type: "Feature",
properties: { type: "YourType" },
geometry: {
type: "LineString",
coordinates: [
[0, 0],
[1, 1],
[2, 2],
],
},
})
.render();
// ''
```
# **geojson2svg().projection(** Function **)**
Projection function that will be used on every coordinate pair in every feature. See [w8r/geojson-project](https://github.com/w8r/geojson-project). Difference between this one and
`.decorator()` is that this one can be used to project the input geometries (think geographical projections),
and decorators are used to adjust the styling of the paths to apply some kind of a pattern, double-up the lines etc.
```js
geojson2svg()
.projection(function (coord) {
return [coord[0] + 1, coord[1] + 1];
})
.data({ type: "Feature", geometry: { coordinates: [0, 0], type: "Point" } })
.render();
//
```
# **geojson2svg.transform(** function **)**
Transform features on the run, for instance transforming a feature geometry
from one type to another. Signature of the function should be
`function (feature) -> feature`
## Supported types
- `Polygon`, `MultiPolygon`
will be rendered into single `` element, for `MultiPolygon`
`fill-rule` is automatically set into `nonzero`, so that with the correct
ring orientation it would handle the overlaps nicely
- `LineString`, `MultiLineString`
will be rendered into single `` element
- `Point`
will be rendered into a `` element
- `MultiPoint`
will be rendered into a group
```xml
...
```
- `GeometryCollection`
will be rendered as a group, whereas it's geometries will be rendered as
separate features and styles for them will be selected from
`feature.properties.geometriesTypes`, an Array, which should have a respective
value for the geometries included into the collection. This will be assigned
as a `type` in the properties of split features
```xml
```
and the type values for the styles to be selected should be defined as
`properties: { geometriesTypes: ['MyPoint', 'MyLine', 'MyPolygon'] }`
By default, renderer will apply a geometry type as class name to the rendered
elements and try to use `feature.properties.className` to assign individual `CSS`
class to the element on top of that.
### Special types
Some special types (though still GeoJSON-standard compliant) are used in this
library:
- # **Textbox** (`textbox`)
Textbox is used to place text on canvas. The geometry of it can be defined by
a rectangle (`Polygon`) or a point (`Point`). Text must be provided as a
string in properties, or, preferably, if it's a multiline text, split it into
an array of strings. `geojson2svg` is a pure text renderer, so it's not
supposed to calculate the positioning and layout for text, though if used
in the browser and not provided with enough font data, it would try to render
the text within the given rectangle. It is **strongly** recommended to provide
`fontFamily`, `fontSize` and `lineHeight` explicitly, if you want the renderer
to do the text formatting for you. If geometry type is `Point`, text will
simply be centered.
- # **Symbol** (`symbol`)
You can think of the symbol as of an icon. It provides a very basic support
for putting icons on the canvas, symbols are rendered as a combination of
[SVG Symbol](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/symbol)
and a `