Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

geojson2svg

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

geojson2svg - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

3

dist/geojson2svg.js

@@ -250,3 +250,4 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),o.geojson2svg=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

opt.attributes = opt.attributes || {};
opt.attributes.id = opt.attributes.id || feature.id || null;
opt.attributes.id = opt.attributes.id || feature.id ||
(feature.properties && feature.properties.id ? feature.properties.id : null);
return this.convertGeometry(feature.geometry,opt);

@@ -253,0 +254,0 @@ };

@@ -1,1 +0,1 @@

!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),o.geojson2svg=e()}}(function(){var define;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){!function(root,factory){"function"==typeof define&&define.amd?define(factory):"object"==typeof exports?module.exports=factory():root.deepmerge=factory()}(this,function(){return function deepmerge(target,src){var array=Array.isArray(src),dst=array&&[]||{};return array?(target=target||[],dst=dst.concat(target),src.forEach(function(e,i){"undefined"==typeof dst[i]?dst[i]=e:"object"==typeof e?dst[i]=deepmerge(target[i],e):-1===target.indexOf(e)&&dst.push(e)})):(target&&"object"==typeof target&&Object.keys(target).forEach(function(key){dst[key]=target[key]}),Object.keys(src).forEach(function(key){"object"==typeof src[key]&&src[key]&&target[key]?dst[key]=deepmerge(target[key],src[key]):dst[key]=src[key]})),dst}})},{}],2:[function(require,module,exports){!function(){function explode(g){return multies.indexOf(g.type)>-1?g.coordinates.map(function(part){var single={};return single.type=g.type.replace("Multi",""),single.coordinates=part,g.crs&&(single.crs=g.crs),single}):!1}function implode(gs){var sameType=gs.every(function(g){return singles.indexOf(g.type)>-1}),crs=gs[0].crs||0,sameCrs=gs.every(function(g){var gcrs=g.crs||0;return gcrs==crs});if(sameType&&sameCrs){var multi={};return multi.type="Multi"+gs[0].type,multi.coordinates=[],0!=crs&&(multi.crs=crs),gs.forEach(function(g){multi.coordinates.push(g.coordinates)}),multi}return!1}var singles=["Point","LineString","Polygon"],multies=["MultiPoint","MultiLineString","MultiPolygon"],multigeojson={explode:explode,implode:implode};"undefined"!=typeof module&&module.exports?module.exports=multigeojson:window&&(window.multigeojson=multigeojson)}()},{}],3:[function(require,module,exports){function getCoordString(coords,res,origin){var coordStr=coords.map(function(coord){return(coord[0]-origin.x)/res+","+(origin.y-coord[1])/res});return coordStr.join(" ")}function point(geom,res,origin,opt){var r=opt&&opt.r?opt.r:1,path="M"+getCoordString([geom.coordinates],res,origin)+" m"+-r+",0 a"+r+","+r+" 0 1,1 "+2*r+",0 a"+r+","+r+" 0 1,1 "+-2*r+",0";return[path]}function multiPoint(geom,res,origin,opt){var explode=opt&&opt.hasOwnProperty("explode")?opt.explode:!1,paths=multi.explode(geom).map(function(single){return point(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ")]}function lineString(geom,res,origin,otp){var coords=getCoordString(geom.coordinates,res,origin),path="M"+coords;return[path]}function multiLineString(geom,res,origin,opt){var explode=opt&&opt.hasOwnProperty("explode")?opt.explode:!1,paths=multi.explode(geom).map(function(single){return lineString(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ")]}function polygon(geom,res,origin,opt){var mainStr,holes;mainStr=getCoordString(geom.coordinates[0],res,origin),geom.coordinates.length>1&&(holes=geom.coordinates.slice(1,geom.coordinates.length));var path="M"+mainStr;if(holes)for(var i=0;i<holes.length;i++)path+=" M"+getCoordString(holes[i],res,origin);return path+="Z",[path]}function multiPolygon(geom,res,origin,opt){var explode=opt.hasOwnProperty("explode")?opt.explode:!1,paths=multi.explode(geom).map(function(single){return polygon(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ").replace(/Z/g,"")+"Z"]}var multi=require("multigeojson");module.exports={Point:point,MultiPoint:multiPoint,LineString:lineString,MultiLineString:multiLineString,Polygon:polygon,MultiPolygon:multiPolygon}},{multigeojson:2}],4:[function(require,module,exports){var merge=require("deepmerge"),converter=require("./converter.js"),g2svg=function(options){this.options=options||{},this.viewportSize=this.options.viewportSize||{width:256,height:256},this.mapExtent=this.options.mapExtent||{left:-20037508.342789244,right:20037508.342789244,bottom:-20037508.342789244,top:20037508.342789244},this.res=this.calResolution(this.mapExtent,this.viewportSize,this.options.fitTo)};g2svg.prototype.calResolution=function(extent,size,fitTo){var xres=(extent.right-extent.left)/size.width,yres=(extent.top-extent.bottom)/size.height;if(fitTo){if("width"===fitTo.toLowerCase())return xres;if("height"===fitTo.toLowerCase())return yres;throw new Error('"fitTo" option should be "width" or "height" ')}return Math.max(xres,yres)},g2svg.prototype.convert=function(geojson,options){var opt=merge(merge({},this.options),options||{}),svgElements=[];if("FeatureCollection"==geojson.type)for(var i=0;i<geojson.features.length;i++)svgElements=svgElements.concat(this.convertFeature(geojson.features[i],opt));else if("Feature"==geojson.type)svgElements=this.convertFeature(geojson,opt);else if("GeomtryCollection"==geojson.type)for(var i=0;i<geojson.geometries.length;i++)svgElements=svgElements.concat(this.convertGeometry(geojson.geometries[i],opt));else{if(!converter[geojson.type])return;svgElements=this.convertGeometry(geojson,opt)}return opt.callback&&opt.callback.call(this,svgElements),svgElements},g2svg.prototype.convertFeature=function(feature,options){if(feature||feature.geometry){var opt=merge(merge({},this.options),options||{});return opt.attributes=opt.attributes||{},opt.attributes.id=opt.attributes.id||feature.id||null,this.convertGeometry(feature.geometry,opt)}},g2svg.prototype.convertGeometry=function(geom,options){if(converter[geom.type]){var svgJsons,svgEles,opt=merge(merge({},this.options),options||{}),output=opt.output||"svg",paths=converter[geom.type].call(this,geom,this.res,{x:this.mapExtent.left,y:this.mapExtent.top},opt);return"svg"==output.toLowerCase()?(svgJsons=paths.map(function(path){return pathToSvgJson(path,geom.type,opt.attributes,opt)}),svgEles=svgJsons.map(function(json){return jsonToSvgElement(json,geom.type)})):paths}};var pathToSvgJson=function(path,type,attributes,opt){var svg={},forcePath=opt&&opt.hasOwnProperty("forcePath")?opt.forcePath:!0;"Point"!=type&&"MultiPoint"!=type||forcePath?(svg={d:path},("Polygon"==type||"MultiPolygon"==type)&&"evenodd"==svg["fill-rule"]):(svg.cx=path.split(",")[0],svg.cy=path.split(",")[1],svg.r=opt&&opt.r?opt.r:"1");for(var key in attributes)svg[key]=attributes[key];return svg},jsonToSvgElement=function(json,type,opt){var forcePath=opt&&opt.hasOwnProperty("forcePath")?opt.forcePath:!0,ele="<path";"Point"!=type&&"MultiPoint"!=type||forcePath||(ele="<circle");for(var key in json)ele+=" "+key+'="'+json[key]+'"';return ele+="/>"};module.exports=g2svg},{"./converter.js":3,deepmerge:1}],5:[function(require,module,exports){var g2svg=require("./instance.js"),geojson2svg=function(options){return new g2svg(options)};module.exports=geojson2svg},{"./instance.js":4}]},{},[5])(5)});
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),o.geojson2svg=e()}}(function(){var define;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){!function(root,factory){"function"==typeof define&&define.amd?define(factory):"object"==typeof exports?module.exports=factory():root.deepmerge=factory()}(this,function(){return function deepmerge(target,src){var array=Array.isArray(src),dst=array&&[]||{};return array?(target=target||[],dst=dst.concat(target),src.forEach(function(e,i){"undefined"==typeof dst[i]?dst[i]=e:"object"==typeof e?dst[i]=deepmerge(target[i],e):-1===target.indexOf(e)&&dst.push(e)})):(target&&"object"==typeof target&&Object.keys(target).forEach(function(key){dst[key]=target[key]}),Object.keys(src).forEach(function(key){"object"==typeof src[key]&&src[key]&&target[key]?dst[key]=deepmerge(target[key],src[key]):dst[key]=src[key]})),dst}})},{}],2:[function(require,module,exports){!function(){function explode(g){return multies.indexOf(g.type)>-1?g.coordinates.map(function(part){var single={};return single.type=g.type.replace("Multi",""),single.coordinates=part,g.crs&&(single.crs=g.crs),single}):!1}function implode(gs){var sameType=gs.every(function(g){return singles.indexOf(g.type)>-1}),crs=gs[0].crs||0,sameCrs=gs.every(function(g){var gcrs=g.crs||0;return gcrs==crs});if(sameType&&sameCrs){var multi={};return multi.type="Multi"+gs[0].type,multi.coordinates=[],0!=crs&&(multi.crs=crs),gs.forEach(function(g){multi.coordinates.push(g.coordinates)}),multi}return!1}var singles=["Point","LineString","Polygon"],multies=["MultiPoint","MultiLineString","MultiPolygon"],multigeojson={explode:explode,implode:implode};"undefined"!=typeof module&&module.exports?module.exports=multigeojson:window&&(window.multigeojson=multigeojson)}()},{}],3:[function(require,module,exports){function getCoordString(coords,res,origin){var coordStr=coords.map(function(coord){return(coord[0]-origin.x)/res+","+(origin.y-coord[1])/res});return coordStr.join(" ")}function point(geom,res,origin,opt){var r=opt&&opt.r?opt.r:1,path="M"+getCoordString([geom.coordinates],res,origin)+" m"+-r+",0 a"+r+","+r+" 0 1,1 "+2*r+",0 a"+r+","+r+" 0 1,1 "+-2*r+",0";return[path]}function multiPoint(geom,res,origin,opt){var explode=opt&&opt.hasOwnProperty("explode")?opt.explode:!1,paths=multi.explode(geom).map(function(single){return point(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ")]}function lineString(geom,res,origin,otp){var coords=getCoordString(geom.coordinates,res,origin),path="M"+coords;return[path]}function multiLineString(geom,res,origin,opt){var explode=opt&&opt.hasOwnProperty("explode")?opt.explode:!1,paths=multi.explode(geom).map(function(single){return lineString(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ")]}function polygon(geom,res,origin,opt){var mainStr,holes;mainStr=getCoordString(geom.coordinates[0],res,origin),geom.coordinates.length>1&&(holes=geom.coordinates.slice(1,geom.coordinates.length));var path="M"+mainStr;if(holes)for(var i=0;i<holes.length;i++)path+=" M"+getCoordString(holes[i],res,origin);return path+="Z",[path]}function multiPolygon(geom,res,origin,opt){var explode=opt.hasOwnProperty("explode")?opt.explode:!1,paths=multi.explode(geom).map(function(single){return polygon(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ").replace(/Z/g,"")+"Z"]}var multi=require("multigeojson");module.exports={Point:point,MultiPoint:multiPoint,LineString:lineString,MultiLineString:multiLineString,Polygon:polygon,MultiPolygon:multiPolygon}},{multigeojson:2}],4:[function(require,module,exports){var merge=require("deepmerge"),converter=require("./converter.js"),g2svg=function(options){this.options=options||{},this.viewportSize=this.options.viewportSize||{width:256,height:256},this.mapExtent=this.options.mapExtent||{left:-20037508.342789244,right:20037508.342789244,bottom:-20037508.342789244,top:20037508.342789244},this.res=this.calResolution(this.mapExtent,this.viewportSize,this.options.fitTo)};g2svg.prototype.calResolution=function(extent,size,fitTo){var xres=(extent.right-extent.left)/size.width,yres=(extent.top-extent.bottom)/size.height;if(fitTo){if("width"===fitTo.toLowerCase())return xres;if("height"===fitTo.toLowerCase())return yres;throw new Error('"fitTo" option should be "width" or "height" ')}return Math.max(xres,yres)},g2svg.prototype.convert=function(geojson,options){var opt=merge(merge({},this.options),options||{}),svgElements=[];if("FeatureCollection"==geojson.type)for(var i=0;i<geojson.features.length;i++)svgElements=svgElements.concat(this.convertFeature(geojson.features[i],opt));else if("Feature"==geojson.type)svgElements=this.convertFeature(geojson,opt);else if("GeomtryCollection"==geojson.type)for(var i=0;i<geojson.geometries.length;i++)svgElements=svgElements.concat(this.convertGeometry(geojson.geometries[i],opt));else{if(!converter[geojson.type])return;svgElements=this.convertGeometry(geojson,opt)}return opt.callback&&opt.callback.call(this,svgElements),svgElements},g2svg.prototype.convertFeature=function(feature,options){if(feature||feature.geometry){var opt=merge(merge({},this.options),options||{});return opt.attributes=opt.attributes||{},opt.attributes.id=opt.attributes.id||feature.id||(feature.properties&&feature.properties.id?feature.properties.id:null),this.convertGeometry(feature.geometry,opt)}},g2svg.prototype.convertGeometry=function(geom,options){if(converter[geom.type]){var svgJsons,svgEles,opt=merge(merge({},this.options),options||{}),output=opt.output||"svg",paths=converter[geom.type].call(this,geom,this.res,{x:this.mapExtent.left,y:this.mapExtent.top},opt);return"svg"==output.toLowerCase()?(svgJsons=paths.map(function(path){return pathToSvgJson(path,geom.type,opt.attributes,opt)}),svgEles=svgJsons.map(function(json){return jsonToSvgElement(json,geom.type)})):paths}};var pathToSvgJson=function(path,type,attributes,opt){var svg={},forcePath=opt&&opt.hasOwnProperty("forcePath")?opt.forcePath:!0;"Point"!=type&&"MultiPoint"!=type||forcePath?(svg={d:path},("Polygon"==type||"MultiPolygon"==type)&&"evenodd"==svg["fill-rule"]):(svg.cx=path.split(",")[0],svg.cy=path.split(",")[1],svg.r=opt&&opt.r?opt.r:"1");for(var key in attributes)svg[key]=attributes[key];return svg},jsonToSvgElement=function(json,type,opt){var forcePath=opt&&opt.hasOwnProperty("forcePath")?opt.forcePath:!0,ele="<path";"Point"!=type&&"MultiPoint"!=type||forcePath||(ele="<circle");for(var key in json)ele+=" "+key+'="'+json[key]+'"';return ele+="/>"};module.exports=g2svg},{"./converter.js":3,deepmerge:1}],5:[function(require,module,exports){var g2svg=require("./instance.js"),geojson2svg=function(options){return new g2svg(options)};module.exports=geojson2svg},{"./instance.js":4}]},{},[5])(5)});
{
"name": "geojson2svg",
"version": "1.0.1",
"version": "1.0.2",
"description": "Converts geojson to svg/path string given svg viewport size and maps extent.",

@@ -5,0 +5,0 @@ "main": "src/main.js",

@@ -96,2 +96,6 @@ # geojson2svg

**Assigning id to SVG path**
There are three ways for doing this. First and second, `.converter` reads it from `feature.properties.id` or `feature.id`. Third way, pass id along with attributes like `converter.convert(feature, {attributes: {id:'foo-1', class: 'bar'}})`. Preference order is first as id key in attributes then feature.id and last feature.properties.id.
### Examples

@@ -132,3 +136,3 @@ Converts geojson LineString to svg element string:

Check my blog [maps-on-blackboard](http://maps-on-blackboard.com/) for more detailed examples.
Check my blog [maps-on-blackboard](http://maps-on-blackboard.com/tag/geojson2svg/) for more detailed examples.
## Developing

@@ -135,0 +139,0 @@ Once you run

@@ -63,3 +63,4 @@ var merge = require('deepmerge'),

opt.attributes = opt.attributes || {};
opt.attributes.id = opt.attributes.id || feature.id || null;
opt.attributes.id = opt.attributes.id || feature.id ||
(feature.properties && feature.properties.id ? feature.properties.id : null);
return this.convertGeometry(feature.geometry,opt);

@@ -66,0 +67,0 @@ };

@@ -13,17 +13,17 @@ 'use strict';

var precision = testData.precision;
describe(testData.desc+ ': .convert()', function() {
var geojson2svg = require('../src/main.js');
var converter = geojson2svg(testData.options);
testData.geojsons.forEach(function(data) {
it(data.type+ ' {output: "path",explode: false,r:2}',function() {
describe(testData.desc+ ': .convert()', function() {
var geojson2svg = require('../src/main.js');
var converter = geojson2svg(testData.options);
testData.geojsons.forEach(function(data) {
it(data.type+ ' {output: "path",explode: false,r:2}',function() {
var options = {output:'path'};
options = merge(options,testData.options);
var actualPaths = converter.convert(data.geojson,options);
var actualPaths = converter.convert(data.geojson,options);
testPath(actualPaths,data.path,data.geojson.type,precision);
});
it(data.type + ' {output: "svg",explode: false,r:2}',function() {
var actualSVGs = converter.convert(data.geojson,testData.options);
testSVG(actualSVGs,data.svg,data.geojson.type,precision);
});
it(data.type + ' {output: "svg",explode: false,r:2}',function() {
var actualSVGs = converter.convert(data.geojson,testData.options);
testSVG(actualSVGs,data.svg,data.geojson.type,precision);
});
});
});
it('Feature {output: "path",explode: false}', function() {

@@ -105,2 +105,45 @@ var actualPaths = converter.convert(testData.feature.geojson,

});
it('add properties to svg: pass properties in constructor', function() {
var converter = geojson2svg({attributes: {class: 'foo'}});
var output = converter.convert(
{type:'LineString', coordinates: [[0,0], [1000,1000]]});
var outputEle = jsdom(output).firstChild.children[1].children[0];
expect(outputEle).to.respondTo('getAttribute');
expect(outputEle.getAttribute('class')).to.be.equal('foo');
});
it('add properties to svg: pass properties in .convert', function() {
var converter = geojson2svg({attributes: {class: 'foo',id: 'foo-1'}});
var output = converter.convert(
{type:'LineString', coordinates: [[0,0], [1000,1000]]},
{attributes: {class: 'foo',id: 'foo-1'}}
);
var outputEle = jsdom(output).firstChild.children[1].children[0];
expect(outputEle).to.respondTo('getAttribute');
expect(outputEle.getAttribute('class')).to.be.equal('foo');
expect(outputEle.getAttribute('id')).to.be.equal('foo-1');
});
it('add id to svg: as feature.id', function() {
var converter = geojson2svg({attributes: {class: 'foo'}});
var output = converter.convert({
type: 'Feature',
id: 'foo-1',
geometry: {type:'LineString', coordinates: [[0,0], [1000,1000]]}
});
var outputEle = jsdom(output).firstChild.children[1].children[0];
expect(outputEle).to.respondTo('getAttribute');
expect(outputEle.getAttribute('class')).to.be.equal('foo');
expect(outputEle.getAttribute('id')).to.be.equal('foo-1');
});
it('add id to svg: as feature.properties.id', function() {
var converter = geojson2svg({attributes: {class: 'foo'}});
var output = converter.convert({
type: 'Feature',
geometry: {type:'LineString', coordinates: [[0,0], [1000,1000]]},
properties: {id: 'foo-1', name: 'bar'}
});
var outputEle = jsdom(output).firstChild.children[1].children[0];
expect(outputEle).to.respondTo('getAttribute');
expect(outputEle.getAttribute('class')).to.be.equal('foo');
expect(outputEle.getAttribute('id')).to.be.equal('foo-1');
});
});

@@ -107,0 +150,0 @@ });

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc