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.6 to 1.1.0

src/extend.js

175

dist/geojson2svg.js
!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){
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.deepmerge = factory();
}
}(this, function () {
function isMergeableObject(val) {
var nonNullObject = val && typeof val === 'object'
return nonNullObject
&& Object.prototype.toString.call(val) !== '[object RegExp]'
&& Object.prototype.toString.call(val) !== '[object Date]'
}
function emptyTarget(val) {
return Array.isArray(val) ? [] : {}
}
function cloneIfNecessary(value, optionsArgument) {
var clone = optionsArgument && optionsArgument.clone === true
return (clone && isMergeableObject(value)) ? deepmerge(emptyTarget(value), value, optionsArgument) : value
}
function defaultArrayMerge(target, source, optionsArgument) {
var destination = target.slice()
source.forEach(function(e, i) {
if (typeof destination[i] === 'undefined') {
destination[i] = cloneIfNecessary(e, optionsArgument)
} else if (isMergeableObject(e)) {
destination[i] = deepmerge(target[i], e, optionsArgument)
} else if (target.indexOf(e) === -1) {
destination.push(cloneIfNecessary(e, optionsArgument))
}
})
return destination
}
function mergeObject(target, source, optionsArgument) {
var destination = {}
if (isMergeableObject(target)) {
Object.keys(target).forEach(function (key) {
destination[key] = cloneIfNecessary(target[key], optionsArgument)
})
}
Object.keys(source).forEach(function (key) {
if (!isMergeableObject(source[key]) || !target[key]) {
destination[key] = cloneIfNecessary(source[key], optionsArgument)
} else {
destination[key] = deepmerge(target[key], source[key], optionsArgument)
}
})
return destination
}
function deepmerge(target, source, optionsArgument) {
var array = Array.isArray(source);
var options = optionsArgument || { arrayMerge: defaultArrayMerge }
var arrayMerge = options.arrayMerge || defaultArrayMerge
if (array) {
return Array.isArray(target) ? arrayMerge(target, source, optionsArgument) : cloneIfNecessary(source, optionsArgument)
} else {
return mergeObject(target, source, optionsArgument)
}
}
return deepmerge
}));
},{}],2:[function(require,module,exports){
//index.js

@@ -127,3 +52,3 @@ (function() {

},{}],3:[function(require,module,exports){
},{}],2:[function(require,module,exports){
//converter.js

@@ -150,6 +75,14 @@ var multi = require('multigeojson');

var r = opt && opt.r ? opt.r : 1;
var 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];
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle')
? opt.pointAsCircle : false;
var coords = getCoordString([geom.coordinates],res,origin);
if (pointAsCircle) {
return [coords];
} else {
return [
'M' + coords
+ ' m'+ -r+ ',0'+ ' a'+r+','+ r + ' 0 1,1 '+ 2*r + ','+0
+ ' a'+r+','+ r + ' 0 1,1 '+ -2*r + ','+0
];
}
}

@@ -210,4 +143,18 @@ function multiPoint(geom,res,origin,opt) {

},{"multigeojson":2}],4:[function(require,module,exports){
var merge = require('deepmerge'),
},{"multigeojson":1}],3:[function(require,module,exports){
// extend.js
// extend b to a with shallow copy
module.exports = function(a, b) {
var c = {}
Object.keys(a).forEach(function(key) {
c[key] = a[key]
})
Object.keys(b).forEach(function(key) {
c[key] = b[key]
})
return c
}
},{}],4:[function(require,module,exports){
var extend = require('./extend.js')
converter = require('./converter.js');

@@ -246,3 +193,3 @@

g2svg.prototype.convert = function(geojson,options) {
var opt = merge(this.options, options || {}, {clone: true});
var opt = extend(this.options, options || {});
var multiGeometries = ['MultiPoint','MultiLineString','MultiPolygon'];

@@ -273,6 +220,23 @@ var geometries = ['Point', 'LineString', 'Polygon'];

if(!feature && !feature.geometry) return;
var opt = merge(this.options, options || {}, {clone: true});
opt.attributes = opt.attributes || {};
opt.attributes.id = opt.attributes.id || feature.id ||
(feature.properties && feature.properties.id ? feature.properties.id : null);
var opt = extend(this.options, options || {});
if (opt.attributes && opt.attributes instanceof Array) {
var arr = opt.attributes
opt.attributes = arr.reduce(function(sum, path) {
var key = path.split('.').pop()
var val
try {
val = valueAt(feature, path)
} catch(e) {
val = false
}
if (val) sum[key] = val
return sum
}, {})
} else {
opt.attributes = opt.attributes || {};
}
var id = opt.attributes.id || feature.id ||
(feature.properties && feature.properties.id
? feature.properties.id : null);
if (id) opt.attributes.id = id
return this.convertGeometry(feature.geometry,opt);

@@ -282,3 +246,3 @@ };

if(converter[geom.type]) {
var opt = merge(this.options, options || {}, {clone: true});
var opt = extend(this.options, options || {});
var output = opt.output || 'svg';

@@ -296,3 +260,3 @@ var paths = converter[geom.type].call(this,geom,

svgEles = svgJsons.map(function(json) {
return jsonToSvgElement(json,geom.type);
return jsonToSvgElement(json,geom.type,opt);
});

@@ -307,7 +271,8 @@ return svgEles;

};
var pathToSvgJson = function(path,type,attributes,opt) {
function pathToSvgJson(path,type,attributes,opt) {
var svg = {};
var forcePath = opt && opt.hasOwnProperty('forcePath') ? opt.forcePath
: true;
if((type == 'Point' || type == 'MultiPoint') && !forcePath) {
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle')
? opt.pointAsCircle : false;
if((type == 'Point' || type == 'MultiPoint') && pointAsCircle) {
svg['cx'] = path.split(',')[0];

@@ -327,7 +292,8 @@ svg['cy'] = path.split(',')[1];

};
var jsonToSvgElement = function(json,type,opt) {
var forcePath = opt && opt.hasOwnProperty('forcePath') ? opt.forcePath
: true;
function jsonToSvgElement(json,type,opt) {
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle')
? opt.pointAsCircle : false;
var ele ='<path';
if((type == 'Point' || type == 'MultiPoint') && !forcePath) {
if((type == 'Point' || type == 'MultiPoint') && pointAsCircle) {
ele = '<circle';

@@ -340,7 +306,18 @@ }

return ele;
};
}
function valueAt(obj,path) {
//taken from http://stackoverflow.com/a/6394168/713573
function index(prev,cur, i, arr) {
if (prev.hasOwnProperty(cur)) {
return prev[cur];
} else {
throw new Error(arr.slice(0,i+1).join('.') + ' is not a valid property path');
}
}
return path.split('.').reduce(index, obj);
}
module.exports = g2svg;
},{"./converter.js":3,"deepmerge":1}],5:[function(require,module,exports){
},{"./converter.js":2,"./extend.js":3}],5:[function(require,module,exports){
var g2svg = require('./instance.js');

@@ -347,0 +324,0 @@ var geojson2svg = function(options) {

@@ -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(){function isMergeableObject(val){var nonNullObject=val&&"object"==typeof val;return nonNullObject&&"[object RegExp]"!==Object.prototype.toString.call(val)&&"[object Date]"!==Object.prototype.toString.call(val)}function emptyTarget(val){return Array.isArray(val)?[]:{}}function cloneIfNecessary(value,optionsArgument){var clone=optionsArgument&&optionsArgument.clone===!0;return clone&&isMergeableObject(value)?deepmerge(emptyTarget(value),value,optionsArgument):value}function defaultArrayMerge(target,source,optionsArgument){var destination=target.slice();return source.forEach(function(e,i){"undefined"==typeof destination[i]?destination[i]=cloneIfNecessary(e,optionsArgument):isMergeableObject(e)?destination[i]=deepmerge(target[i],e,optionsArgument):-1===target.indexOf(e)&&destination.push(cloneIfNecessary(e,optionsArgument))}),destination}function mergeObject(target,source,optionsArgument){var destination={};return isMergeableObject(target)&&Object.keys(target).forEach(function(key){destination[key]=cloneIfNecessary(target[key],optionsArgument)}),Object.keys(source).forEach(function(key){isMergeableObject(source[key])&&target[key]?destination[key]=deepmerge(target[key],source[key],optionsArgument):destination[key]=cloneIfNecessary(source[key],optionsArgument)}),destination}function deepmerge(target,source,optionsArgument){var array=Array.isArray(source),options=optionsArgument||{arrayMerge:defaultArrayMerge},arrayMerge=options.arrayMerge||defaultArrayMerge;return array?Array.isArray(target)?arrayMerge(target,source,optionsArgument):cloneIfNecessary(source,optionsArgument):mergeObject(target,source,optionsArgument)}return deepmerge})},{}],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(this.options,options||{},{clone:!0}),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("GeometryCollection"==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(this.options,options||{},{clone:!0});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(this.options,options||{},{clone:!0}),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(){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(){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)}()},{}],2:[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,pointAsCircle=opt&&opt.hasOwnProperty("pointAsCircle")?opt.pointAsCircle:!1,coords=getCoordString([geom.coordinates],res,origin);return pointAsCircle?[coords]:["M"+coords+" m"+-r+",0 a"+r+","+r+" 0 1,1 "+2*r+",0 a"+r+","+r+" 0 1,1 "+-2*r+",0"]}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:1}],3:[function(require,module,exports){module.exports=function(a,b){var c={};return Object.keys(a).forEach(function(key){c[key]=a[key]}),Object.keys(b).forEach(function(key){c[key]=b[key]}),c}},{}],4:[function(require,module,exports){function pathToSvgJson(path,type,attributes,opt){var svg={},pointAsCircle=opt&&opt.hasOwnProperty("pointAsCircle")?opt.pointAsCircle:!1;"Point"!=type&&"MultiPoint"!=type||!pointAsCircle?(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}function jsonToSvgElement(json,type,opt){var pointAsCircle=opt&&opt.hasOwnProperty("pointAsCircle")?opt.pointAsCircle:!1,ele="<path";"Point"!=type&&"MultiPoint"!=type||!pointAsCircle||(ele="<circle");for(var key in json)ele+=" "+key+'="'+json[key]+'"';return ele+="/>"}function valueAt(obj,path){function index(prev,cur,i,arr){if(prev.hasOwnProperty(cur))return prev[cur];throw new Error(arr.slice(0,i+1).join(".")+" is not a valid property path")}return path.split(".").reduce(index,obj)}var extend=require("./extend.js");converter=require("./converter.js");var 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=extend(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("GeometryCollection"==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=extend(this.options,options||{});if(opt.attributes&&opt.attributes instanceof Array){var arr=opt.attributes;opt.attributes=arr.reduce(function(sum,path){var val,key=path.split(".").pop();try{val=valueAt(feature,path)}catch(e){val=!1}return val&&(sum[key]=val),sum},{})}else opt.attributes=opt.attributes||{};var id=opt.attributes.id||feature.id||(feature.properties&&feature.properties.id?feature.properties.id:null);return id&&(opt.attributes.id=id),this.convertGeometry(feature.geometry,opt)}},g2svg.prototype.convertGeometry=function(geom,options){if(converter[geom.type]){var svgJsons,svgEles,opt=extend(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,opt)})):paths}},module.exports=g2svg},{"./converter.js":2,"./extend.js":3}],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.6",
"version": "1.1.0",
"description": "Converts geojson to svg/path string given svg viewport size and maps extent.",

@@ -25,3 +25,2 @@ "main": "src/main.js",

"dependencies": {
"deepmerge": "^1.2.0",
"multigeojson": "~0.0.1"

@@ -32,2 +31,3 @@ },

"chai": "~1.9.1",
"deepmerge": "^1.3.2",
"exorcist": "^0.1.6",

@@ -34,0 +34,0 @@ "jsdom": "^9.8.3",

@@ -60,4 +60,14 @@ # geojson2svg

* **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:** Attributes which are required to attach as SVG attributes from features can be passed here as list of path in feature, like
``` {"attributes": ["properties.foo", "properties.bar"]}```
output svg string would be:
``` <path foo="fooVal-1" bar="barVal-1" d="M0,0 20,10 106,40"/> ```
Note: If a feature does not have value at the mentioned path then the attribute key would not be attached to svg string and even error would not be thrown.
In case attribute(s) need be attach to all SVG string, a json object containing attribute(key) and values(value) can be passed. If option is like
``` {"attributes": {"class": "mapstyle"}}```

@@ -64,0 +74,0 @@

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

var merge = require('deepmerge'),
var extend = require('./extend.js')
converter = require('./converter.js');

@@ -35,3 +35,3 @@

g2svg.prototype.convert = function(geojson,options) {
var opt = merge(this.options, options || {}, {clone: true});
var opt = extend(this.options, options || {});
var multiGeometries = ['MultiPoint','MultiLineString','MultiPolygon'];

@@ -62,6 +62,23 @@ var geometries = ['Point', 'LineString', 'Polygon'];

if(!feature && !feature.geometry) return;
var opt = merge(this.options, options || {}, {clone: true});
opt.attributes = opt.attributes || {};
opt.attributes.id = opt.attributes.id || feature.id ||
(feature.properties && feature.properties.id ? feature.properties.id : null);
var opt = extend(this.options, options || {});
if (opt.attributes && opt.attributes instanceof Array) {
var arr = opt.attributes
opt.attributes = arr.reduce(function(sum, path) {
var key = path.split('.').pop()
var val
try {
val = valueAt(feature, path)
} catch(e) {
val = false
}
if (val) sum[key] = val
return sum
}, {})
} else {
opt.attributes = opt.attributes || {};
}
var id = opt.attributes.id || feature.id ||
(feature.properties && feature.properties.id
? feature.properties.id : null);
if (id) opt.attributes.id = id
return this.convertGeometry(feature.geometry,opt);

@@ -71,3 +88,3 @@ };

if(converter[geom.type]) {
var opt = merge(this.options, options || {}, {clone: true});
var opt = extend(this.options, options || {});
var output = opt.output || 'svg';

@@ -95,3 +112,4 @@ var paths = converter[geom.type].call(this,geom,

};
var pathToSvgJson = function(path,type,attributes,opt) {
function pathToSvgJson(path,type,attributes,opt) {
var svg = {};

@@ -115,3 +133,4 @@ var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle')

};
var jsonToSvgElement = function(json,type,opt) {
function jsonToSvgElement(json,type,opt) {
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle')

@@ -128,4 +147,15 @@ ? opt.pointAsCircle : false;

return ele;
};
}
function valueAt(obj,path) {
//taken from http://stackoverflow.com/a/6394168/713573
function index(prev,cur, i, arr) {
if (prev.hasOwnProperty(cur)) {
return prev[cur];
} else {
throw new Error(arr.slice(0,i+1).join('.') + ' is not a valid property path');
}
}
return path.split('.').reduce(index, obj);
}
module.exports = g2svg;

@@ -123,3 +123,32 @@ 'use strict';

});
it('add properties to svg: pass properties in constructor', function() {
it('add attributes to svg based on each feature properties:', function() {
var converter = geojson2svg(
{attributes: ['properties.foo', 'properties.bar', 'properties.baz']})
var svgStr = converter.convert({
type: 'FeatureCollection',
features: [{
type: 'Feature',
geometry: {type: 'LineString', coordinates: [[0,0], [1000,1000]]},
properties: {foo: 'fooVal-1', bar: 'barVal-1', baz: 'bazVal-1'}
}, {
type: 'Feature',
geometry: {type: 'LineString', coordinates: [[10,10], [100,100]]},
properties: {foo: 'fooVal-2', bar: 'barVal-2'}
}]
})
var svgEle1 = jsdom(svgStr[0]).firstChild.children[1].children[0];
expect(svgEle1).to.respondTo('getAttribute');
expect(svgEle1.getAttribute('foo')).to.be.equal('fooVal-1');
expect(svgEle1.getAttribute('bar')).to.be.equal('barVal-1');
expect(svgEle1.getAttribute('baz')).to.be.equal('bazVal-1');
var svgEle2 = jsdom(svgStr[1]).firstChild.children[1].children[0];
expect(svgEle2).to.respondTo('getAttribute');
expect(svgEle2.getAttribute('foo')).to.be.equal('fooVal-2');
expect(svgEle2.getAttribute('bar')).to.be.equal('barVal-2');
expect(svgEle2.getAttribute('baz')).to.be.null;
});
it('add given attributes in options to all svg elements: '
+ 'pass attributes in constructor', function() {
var converter = geojson2svg({attributes: {class: 'foo'}});

@@ -132,3 +161,4 @@ var output = converter.convert(

});
it('add properties to svg: pass properties in .convert', function() {
it('add given attributes in options to all svg elements: '
+ 'pass attributes in .convert', function() {
var converter = geojson2svg({attributes: {class: 'foo',id: 'foo-1'}});

@@ -135,0 +165,0 @@ var output = converter.convert(

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