GithubHelp home page GithubHelp logo

tadiraman / geojson2svg Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gagan-bansal/geojson2svg

0.0 2.0 0.0 676 KB

Converts geojson to svg string given svg viewport size and maps extent.

License: MIT License

JavaScript 99.76% CSS 0.24%

geojson2svg's Introduction

geojson2svg

Converts geojson to svg string given svg viewport size and maps extent. Check world map and color coded map examples to demostrate that its very easy to convert geojson into map.

Installation

Using in node.js or with browserify

npm install geojson2svg

For including in html page standard way, download file dist/geojson2svg.min.js

<script type="text/javascipt" src="path/to/geojson2svg.min.js"></script>

This creates a global variable 'geojson2svg'

Usage

Using in node.js or with browserify

var geojson2svg = require('geojson2svg');
var converter = geojson2svg(viewportSize,options);
var svgString = converter.convert(geojson,options);

Using in browser standard way

var converter = geojson2svg(viewportSize,options);
var svgString = converter.convert(geojson,options);

viewportSize is object containing width and height in pixels - {width: 200, height: 100}

convert function returns array of svg element string

Options

  • mapExtent: {"left": coordinate, "bottom": coordinate, "right": coordinate, "top": coordinate}. Coordinates should be in same projection as of geojson. Default maps extent are of Web Mercator projection (EPSG:3857). Default extent values are:
    {
      left: -20037508.342789244,
      right: 20037508.342789244,
      bottom: -20037508.342789244,
      top: 20037508.342789244
    }
  • output: 'svg'|'path' default is 'svg'

    'svg' - svg element string is returned like '<path d="M0,0 20,10 106,40"/>'

    'path' - path 'd' value is returned 'M0,0 20,10 106,40' a linestring

  • explode: true | false, default is false. Should multigeojson be exploded to many svg elements or not.

  • attributes: json object containing attribute(key) and values(value) for all svg elements. These attributes would be added to svg string. If option is like

    {"attributes": {"class": "mapstyle"}}

    returned string would be

    '<path class="mapstyle" d="M0,0 20,10 106,40"/>'

  • pointAsCircle: true | false, default is false. For point geojson return circle element for option: { "pointAsCircel": true } output svg string would be:

    '<cirlce cx="30" cy="40" r="1" />'

  • r: radius of point svg element

  • callback: function, accept function that will be called on every geojson conversion with output string as one input variable e.g:

    { "callback": function(svgString) {
      // do something with svgString
    }}
    

    Suitable callback function could be render svgString.

The options 'attributes', 'r' and 'callback' can also be given in convert function

var svgString = convertor.convert(geojson, 
  {
    "attributes": ...,
    "r": ...,
    "callback": function
  }

mapExtent is critical option default are the extents of Web Mercator projection ('EPSG:3857') or also known as Spherical Mercator. This projection is used by many web mapping sites (Google / Bing / OpenStreetMap). In case your source data is in geographic coordinates, it can be converted on the fly to Web Mercator Projection using reproject or proj4js. Check my world map example for detail.

Examples

Converts geojson LineString to svg element string:

var converter = geojson2svg({width: 200, height: 100},
  {
    mapExtent: {left: -180, bottom: -90, right: 180, top: 90},
    output: 'svg' 
  }
);
var svgString = converter.convert(
  {type:'LineString',coordinates:[[10,10],[15,20],[30,10]]}
);
//svgString: ['<path d="M105.55555555555556,44.44444444444444 108.33333333333333,38.888888888888886 116.66666666666666,44.44444444444444" />']

Converts geojson Polygon to svg path data 'd' string:

var converter = geojson2svg({width: 200, height: 100},
  {    
    mapExtent: {left: -180, bottom: -90, right: 180, top: 90},
    output: 'path'
  }
);
var pathData = converter.convert(
  {
    "type": "Polygon", 
    "coordinates": [
      [[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]] 
    ]
  }
);
// pathData: ['M116.66666666666666,44.44444444444444 122.22222222222221,27.77777777777778 111.11111111111111,27.77777777777778 105.55555555555556,38.888888888888886 116.66666666666666,44.44444444444444Z']

Developing

Once you run

npm isntall

then for running test

npm run test

to create build

npm run build

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

geojson2svg's People

Contributors

gagan-bansal avatar tadiraman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.