Home

Awesome

geojson2svg

Converts GeoJSON to an SVG string, given the SVG viewport size and map extent. geojson2svg can be used on the client side (in the browser) or server side (with Node.js).

Check world map, SVG scaled map and color coded map examples to demonstrate that its very easy to convert GeoJSON into map.

🛠 Installation
:car: Usage
:popcorn: Basic Example
🔌 API
✈️ Migration from 1.x to 2.x
📌 Important points
📋 Changelog
🪪 License
🔗 Related useful articles

🛠 Installation

:car: Usage

:popcorn: Basic Example

const {GeoJSON2SVG} = require('geojson2svg');

const converter = new GeoJSON2SVG({
  mapExtent: {left: -180, bottom: -90, right: 180, top: 90},
  viewportSize: {width: 200, height: 100},
  attributes: ['properties.class' , 'properties.foo'],
  r: 2
});
const geojsonData = {
  type: 'FeatureCollection',
  features: [{
    type: 'Feature',
    id: 'pt-1',
    geometry: {type:'Point',coordinates:[50, 50]},
    properties: {foo: 'val-1', class: 'point-tree'}
  }, {
    type: 'Feature',
    geometry: {
      type: 'LineString',
      coordinates: [[10, 10],[15, 20],[30, 10]]
    },
    properties: {id: 'ln-1', foo: 'val-2', class: 'line-road', bar: 'val'}
  }, {
    type: 'Feature',
    id: 'pg-1',
    geometry: {
      type: 'LineString',
      coordinates: [[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]
    },
    properties: {id: 'not-used', foo: 'val-3', class: 'polygon-pond'}
  }]
});

const svgStr = converter.convert(geojsonData);
console.log(svgStr);

// output
// [
//   '<path d="M127.77777777777777,22.22222222222222 m-2,0 a2,2 0 1,1 4,0 a2,2 0 1,1 -4,0" class="point-tree" foo="val-1" id="pt-1"/>',
//   '<path d="M105.55555555555556,44.44444444444444 108.33333333333333,38.888888888888886 116.66666666666666,44.44444444444444" class="line-road" foo="val-2" id="ln-1"/>',
//   '<path d="M116.66666666666666,44.44444444444444 122.22222222222221,27.77777777777778 111.11111111111111,27.77777777777778 105.55555555555556,38.888888888888886 116.66666666666666,44.44444444444444" class="polygon-pond" foo="val-3" id="pg-1"/>'
// ]

convert function returns an array of SVG elements' strings.

✈️ Migration from 1.x to 2.x

🔌 API

Initializing the instance

const converter = new GeoJSON2SVG(options);

Here are all options available for initializing the instance.

Instance method

.convert(geojson, options)

The options 'attributes', 'r' and 'callback' can also be given in convert function's option. Example:

let svgStrings = convertor.convert(geojson,
  {
    "attributes": ...,
    "r": ...,
    "callback": function
  }
);

📌 Important points

📋 Changelog

Check here

🪪 License

This project is licensed under the terms of the MIT license.

🔗 Related useful articles