geojson2svg
Advanced tools
Comparing version 1.2.9 to 1.3.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [1.3.0](https://github.com/gagan-bansal/geojson2svg/compare/v1.2.9...v1.3.0) (2022-01-23) | ||
### Features | ||
* svg output precision option added ([b474cd7](https://github.com/gagan-bansal/geojson2svg/commit/b474cd743df54fbe9498a43ae849e2169e93f0a0)) | ||
### [1.2.9](https://github.com/gagan-bansal/geojson2svg/compare/v1.2.8...v1.2.9) (2021-11-27) | ||
@@ -7,0 +14,0 @@ |
@@ -55,7 +55,14 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.geojson2svg = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
var multi = require('multigeojson'); | ||
function getCoordString(coords,res,origin) { | ||
//origin - svg image origin | ||
var coordStr = coords.map(function(coord) { | ||
return (coord[0] - origin.x)/res + ',' + (origin.y - coord[1])/res; | ||
function getCoordString(coords,res,origin, precision) { | ||
//origin - svg image origin | ||
var convertedCoords = coords.map(function(coord) { | ||
return [(coord[0] - origin.x)/res, (origin.y - coord[1])/res]; | ||
}); | ||
var coordStr = convertedCoords.map(function (coord) { | ||
if (precision) { | ||
return coord[0].toFixed(precision) + ',' + coord[1].toFixed(precision); | ||
} else { | ||
return coord[0] + ',' + coord[1]; | ||
} | ||
}); | ||
return coordStr.join(' '); | ||
@@ -75,5 +82,5 @@ } | ||
var r = opt && opt.r ? opt.r : 1; | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
? opt.pointAsCircle : false; | ||
var coords = getCoordString([geom.coordinates],res,origin); | ||
var coords = getCoordString([geom.coordinates],res,origin,opt.precision); | ||
if (pointAsCircle) { | ||
@@ -83,3 +90,3 @@ return [coords]; | ||
return [ | ||
'M' + coords | ||
'M' + coords | ||
+ ' m'+ -r+ ',0'+ ' a'+r+','+ r + ' 0 1,1 '+ 2*r + ','+0 | ||
@@ -99,5 +106,5 @@ + ' a'+r+','+ r + ' 0 1,1 '+ -2*r + ','+0 | ||
} | ||
function lineString(geom,res,origin,otp) { | ||
var coords = getCoordString(geom.coordinates,res,origin); | ||
var path = 'M'+ coords; | ||
function lineString(geom,res,origin,opt) { | ||
var coords = getCoordString(geom.coordinates,res,origin,opt.precision); | ||
var path = 'M'+ coords; | ||
return [path]; | ||
@@ -115,3 +122,3 @@ } | ||
var mainStr,holes,holeStr; | ||
mainStr = getCoordString(geom.coordinates[0],res,origin); | ||
mainStr = getCoordString(geom.coordinates[0],res,origin,opt.precision); | ||
if (geom.coordinates.length > 1) { | ||
@@ -123,3 +130,3 @@ holes = geom.coordinates.slice(1,geom.coordinates.length); | ||
for(var i=0;i<holes.length; i++) { | ||
path += ' M' + getCoordString(holes[i],res,origin); | ||
path += ' M' + getCoordString(holes[i],res,origin,opt.precision); | ||
} | ||
@@ -168,3 +175,3 @@ } | ||
this.options = options || {}; | ||
this.viewportSize = this.options.viewportSize || | ||
this.viewportSize = this.options.viewportSize || | ||
{width: 256, height: 256}; | ||
@@ -184,3 +191,3 @@ this.mapExtent = this.options.mapExtent || | ||
var yres = (extent.top - extent.bottom)/size.height; | ||
if (fitTo) { | ||
if (fitTo) { | ||
if (fitTo.toLowerCase() === 'width') { | ||
@@ -240,3 +247,3 @@ return xres; | ||
if (property.type === 'dynamic') { | ||
var val, key = property.key ? property.key | ||
var val, key = property.key ? property.key | ||
: property.property.split('.').pop() | ||
@@ -258,6 +265,6 @@ try { | ||
} | ||
var id = opt.attributes.id || feature.id || | ||
(feature.properties && feature.properties.id | ||
var id = opt.attributes.id || feature.id || | ||
(feature.properties && feature.properties.id | ||
? feature.properties.id : null); | ||
if (id) opt.attributes.id = id | ||
if (id) opt.attributes.id = id; | ||
return this.convertGeometry(feature.geometry,opt); | ||
@@ -293,3 +300,3 @@ }; | ||
var svg = {}; | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
? opt.pointAsCircle : false; | ||
@@ -303,4 +310,4 @@ if((type == 'Point' || type == 'MultiPoint') && pointAsCircle) { | ||
if(type == 'Polygon' || type == 'MultiPolygon') { | ||
svg['fill-rule'] == 'evenodd'; | ||
} | ||
svg['fill-rule'] == 'evenodd'; | ||
} | ||
} | ||
@@ -314,3 +321,3 @@ for (var key in attributes) { | ||
function jsonToSvgElement(json,type,opt) { | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
? opt.pointAsCircle : false; | ||
@@ -330,7 +337,7 @@ var ele ='<path'; | ||
//taken from http://stackoverflow.com/a/6394168/713573 | ||
function index(prev,cur, i, arr) { | ||
function index(prev,cur, i, arr) { | ||
if (prev.hasOwnProperty(cur)) { | ||
return prev[cur]; | ||
return prev[cur]; | ||
} else { | ||
throw new Error(arr.slice(0,i+1).join('.') + ' is not a valid property path'); | ||
throw new Error(arr.slice(0,i+1).join('.') + ' is not a valid property path'); | ||
} | ||
@@ -337,0 +344,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(f){"object"==typeof exports&&"undefined"!=typeof module?module.exports=f():"function"==typeof define&&define.amd?define([],f):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).geojson2svg=f()}(function(){return function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var p="function"==typeof require&&require;if(!f&&p)return p(i,!0);if(u)return u(i,!0);throw(p=new Error("Cannot find module '"+i+"'")).code="MODULE_NOT_FOUND",p}p=n[i]={exports:{}},e[i][0].call(p.exports,function(r){return o(e[i][1][r]||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}({1:[function(require,module,exports){var singles,multies,multigeojson;singles=["Point","LineString","Polygon"],multies=["MultiPoint","MultiLineString","MultiPolygon"],multigeojson={explode:function(g){return-1<multies.indexOf(g.type)&&g.coordinates.map(function(part){var single={};return single.type=g.type.replace("Multi",""),single.coordinates=part,g.crs&&(single.crs=g.crs),single})},implode:function(gs){var sameType=gs.every(function(g){return-1<singles.indexOf(g.type)}),crs=gs[0].crs||0,sameCrs=gs.every(function(g){return(g.crs||0)==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}},void 0!==module&&module.exports?module.exports=multigeojson:window&&(window.multigeojson=multigeojson)},{}],2:[function(require,module,exports){var multi=require("multigeojson");function getCoordString(coords,res,origin){return coords.map(function(coord){return(coord[0]-origin.x)/res+","+(origin.y-coord[1])/res}).join(" ")}function point(geom,res,coords,pointAsCircle){var r=pointAsCircle&&pointAsCircle.r?pointAsCircle.r:1,pointAsCircle=!(!pointAsCircle||!pointAsCircle.hasOwnProperty("pointAsCircle"))&&pointAsCircle.pointAsCircle,coords=getCoordString([geom.coordinates],res,coords);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 lineString(geom,res,origin,otp){return["M"+getCoordString(geom.coordinates,res,origin)]}function polygon(geom,res,origin,opt){var holes,path="M"+getCoordString(geom.coordinates[0],res,origin);if(holes=1<geom.coordinates.length?geom.coordinates.slice(1,geom.coordinates.length):holes)for(var i=0;i<holes.length;i++)path+=" M"+getCoordString(holes[i],res,origin);return[path+="Z"]}module.exports={Point:point,MultiPoint:function(paths,res,origin,opt){var explode=!(!opt||!opt.hasOwnProperty("explode"))&&opt.explode,paths=multi.explode(paths).map(function(single){return point(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ")]},LineString:lineString,MultiLineString:function(paths,res,origin,explode){return explode=!(!explode||!explode.hasOwnProperty("explode"))&&explode.explode,paths=multi.explode(paths).map(function(single){return lineString(single,res,origin)[0]}),explode?paths:[paths.join(" ")]},Polygon:polygon,MultiPolygon:function(paths,res,origin,explode){return explode=!!explode.hasOwnProperty("explode")&&explode.explode,paths=multi.explode(paths).map(function(single){return polygon(single,res,origin)[0]}),explode?paths:[paths.join(" ").replace(/Z/g,"")+"Z"]}}},{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(g2svg,module,exports){var extend=g2svg("./extend.js"),converter=g2svg("./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)};function valueAt(obj,path){return path.split(".").reduce(function(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")},obj)}g2svg.prototype.calResolution=function(extent,yres,fitTo){var xres=(extent.right-extent.left)/yres.width,yres=(extent.top-extent.bottom)/yres.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(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,opt){if(feature||feature.geometry){opt=extend(this.options,opt||{});opt.attributes&&opt.attributes instanceof Array?(id=opt.attributes,opt.attributes=id.reduce(function(sum,property){if("string"==typeof property){var key=property.split(".").pop();try{val=valueAt(feature,property)}catch(e){val=!1}val&&(sum[key]=val)}else if("object"==typeof property&&property.type&&property.property)if("dynamic"===property.type){var val,key=property.key||property.property.split(".").pop();try{val=valueAt(feature,property.property)}catch(e){val=!1}val&&(sum[key]=val)}else"static"===property.type&&property.value&&(sum[property.property]=property.value);return sum},{})):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,paths){if(converter[geom.type]){var opt=extend(this.options,paths||{}),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()?paths.map(function(path){return function(path,type,attributes,opt){var key,svg={},pointAsCircle=!(!opt||!opt.hasOwnProperty("pointAsCircle"))&&opt.pointAsCircle;"Point"!=type&&"MultiPoint"!=type||!pointAsCircle?(svg={d:path},"Polygon"!=type&&"MultiPolygon"!=type||svg["fill-rule"]):(svg.cx=path.split(",")[0],svg.cy=path.split(",")[1],svg.r=opt&&opt.r?opt.r:"1");for(key in attributes)svg[key]=attributes[key];return svg}(path,geom.type,opt.attributes,opt)}).map(function(json){return function(json,type,pointAsCircle){var key,pointAsCircle=!(!pointAsCircle||!pointAsCircle.hasOwnProperty("pointAsCircle"))&&pointAsCircle.pointAsCircle,ele="<path";"Point"!=type&&"MultiPoint"!=type||!pointAsCircle||(ele="<circle");for(key in json)ele+=" "+key+'="'+json[key]+'"';return ele+="/>"}(json,geom.type,opt)}):paths}},module.exports=g2svg},{"./converter.js":2,"./extend.js":3}],5:[function(require,module,exports){var g2svg=require("./instance.js");module.exports=function(options){return new g2svg(options)}},{"./instance.js":4}]},{},[5])(5)}); | ||
!function(f){"object"==typeof exports&&"undefined"!=typeof module?module.exports=f():"function"==typeof define&&define.amd?define([],f):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).geojson2svg=f()}(function(){return function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var p="function"==typeof require&&require;if(!f&&p)return p(i,!0);if(u)return u(i,!0);throw(p=new Error("Cannot find module '"+i+"'")).code="MODULE_NOT_FOUND",p}p=n[i]={exports:{}},e[i][0].call(p.exports,function(r){return o(e[i][1][r]||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}({1:[function(require,module,exports){var singles,multies,multigeojson;singles=["Point","LineString","Polygon"],multies=["MultiPoint","MultiLineString","MultiPolygon"],multigeojson={explode:function(g){return-1<multies.indexOf(g.type)&&g.coordinates.map(function(part){var single={};return single.type=g.type.replace("Multi",""),single.coordinates=part,g.crs&&(single.crs=g.crs),single})},implode:function(gs){var sameType=gs.every(function(g){return-1<singles.indexOf(g.type)}),crs=gs[0].crs||0,sameCrs=gs.every(function(g){return(g.crs||0)==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}},void 0!==module&&module.exports?module.exports=multigeojson:window&&(window.multigeojson=multigeojson)},{}],2:[function(require,module,exports){var multi=require("multigeojson");function getCoordString(coords,res,origin,precision){return coords.map(function(coord){return[(coord[0]-origin.x)/res,(origin.y-coord[1])/res]}).map(function(coord){return precision?coord[0].toFixed(precision)+","+coord[1].toFixed(precision):coord[0]+","+coord[1]}).join(" ")}function point(geom,res,origin,coords){var r=coords&&coords.r?coords.r:1,pointAsCircle=!(!coords||!coords.hasOwnProperty("pointAsCircle"))&&coords.pointAsCircle,coords=getCoordString([geom.coordinates],res,origin,coords.precision);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 lineString(geom,res,origin,opt){return["M"+getCoordString(geom.coordinates,res,origin,opt.precision)]}function polygon(geom,res,origin,opt){var holes,path="M"+getCoordString(geom.coordinates[0],res,origin,opt.precision);if(holes=1<geom.coordinates.length?geom.coordinates.slice(1,geom.coordinates.length):holes)for(var i=0;i<holes.length;i++)path+=" M"+getCoordString(holes[i],res,origin,opt.precision);return[path+="Z"]}module.exports={Point:point,MultiPoint:function(paths,res,origin,opt){var explode=!(!opt||!opt.hasOwnProperty("explode"))&&opt.explode,paths=multi.explode(paths).map(function(single){return point(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ")]},LineString:lineString,MultiLineString:function(paths,res,origin,opt){var explode=!(!opt||!opt.hasOwnProperty("explode"))&&opt.explode,paths=multi.explode(paths).map(function(single){return lineString(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ")]},Polygon:polygon,MultiPolygon:function(paths,res,origin,opt){var explode=!!opt.hasOwnProperty("explode")&&opt.explode,paths=multi.explode(paths).map(function(single){return polygon(single,res,origin,opt)[0]});return explode?paths:[paths.join(" ").replace(/Z/g,"")+"Z"]}}},{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(g2svg,module,exports){var extend=g2svg("./extend.js"),converter=g2svg("./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)};function valueAt(obj,path){return path.split(".").reduce(function(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")},obj)}g2svg.prototype.calResolution=function(extent,yres,fitTo){var xres=(extent.right-extent.left)/yres.width,yres=(extent.top-extent.bottom)/yres.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(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,opt){if(feature||feature.geometry){opt=extend(this.options,opt||{});opt.attributes&&opt.attributes instanceof Array?(id=opt.attributes,opt.attributes=id.reduce(function(sum,property){if("string"==typeof property){var key=property.split(".").pop();try{val=valueAt(feature,property)}catch(e){val=!1}val&&(sum[key]=val)}else if("object"==typeof property&&property.type&&property.property)if("dynamic"===property.type){var val,key=property.key||property.property.split(".").pop();try{val=valueAt(feature,property.property)}catch(e){val=!1}val&&(sum[key]=val)}else"static"===property.type&&property.value&&(sum[property.property]=property.value);return sum},{})):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,paths){if(converter[geom.type]){var opt=extend(this.options,paths||{}),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()?paths.map(function(path){return function(path,type,attributes,opt){var key,svg={},pointAsCircle=!(!opt||!opt.hasOwnProperty("pointAsCircle"))&&opt.pointAsCircle;"Point"!=type&&"MultiPoint"!=type||!pointAsCircle?(svg={d:path},"Polygon"!=type&&"MultiPolygon"!=type||svg["fill-rule"]):(svg.cx=path.split(",")[0],svg.cy=path.split(",")[1],svg.r=opt&&opt.r?opt.r:"1");for(key in attributes)svg[key]=attributes[key];return svg}(path,geom.type,opt.attributes,opt)}).map(function(json){return function(json,type,pointAsCircle){var key,pointAsCircle=!(!pointAsCircle||!pointAsCircle.hasOwnProperty("pointAsCircle"))&&pointAsCircle.pointAsCircle,ele="<path";"Point"!=type&&"MultiPoint"!=type||!pointAsCircle||(ele="<circle");for(key in json)ele+=" "+key+'="'+json[key]+'"';return ele+="/>"}(json,geom.type,opt)}):paths}},module.exports=g2svg},{"./converter.js":2,"./extend.js":3}],5:[function(require,module,exports){var g2svg=require("./instance.js");module.exports=function(options){return new g2svg(options)}},{"./instance.js":4}]},{},[5])(5)}); |
{ | ||
"name": "geojson2svg", | ||
"version": "1.2.9", | ||
"version": "1.3.0", | ||
"description": "Converts geojson to svg/path string given svg viewport size and maps extent.", | ||
@@ -13,3 +13,6 @@ "main": "src/main.js", | ||
"release": "standard-version", | ||
"watch": "watchify src/main.js --debug --standalone geojson2svg -o dist/geojson2svg.js -v" | ||
"release:minor": "standard-version --release-as minor", | ||
"release:patch": "standard-version --release-as patch", | ||
"release:major": "standard-version --release-as major", | ||
"watch": "mocha -w" | ||
}, | ||
@@ -37,3 +40,3 @@ "keywords": [ | ||
"jsdom": "^18.1.1", | ||
"mocha": "^9.1.3", | ||
"mocha": "^9.1.4", | ||
"parse-svg-path": "~0.1.2", | ||
@@ -40,0 +43,0 @@ "standard-version": "^9.3.2", |
@@ -69,3 +69,3 @@ # geojson2svg | ||
``` | ||
or include in your html file | ||
or include in your html file | ||
```html | ||
@@ -109,3 +109,5 @@ <script type="text/javascript" src="path/to/parse-svg.min.js"></script> | ||
* **explode:** true | false, default is false. Should multigeojson be exploded to many svg elements or not. | ||
* **precision** a number, precision of output svg coordinates. Default is false. | ||
* **explode:** true | false, default is false. Should multigeojson be exploded to many svg elements or not. | ||
* **attributes:** Attributes which are required to attach as SVG attributes from features can be passed here as list of path in feature or json object for static attributes, like shown here | ||
@@ -118,7 +120,7 @@ | ||
or **static** ``` {"attributes": {"class": "mapstyle"}}``` | ||
outut: ```'<path class="mapstyle" d="M0,0 20,10 106,40"/>'``` | ||
or **dynamic** and **static** both | ||
{attributes: [ | ||
@@ -139,7 +141,7 @@ { | ||
output: ``` [ '<path d="M128,128 128.00638801979818,127.99361198020182" id="fooVal-1" baz="bazVal-1" bar="barStatic"/>'] ``` | ||
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. | ||
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. | ||
@@ -160,6 +162,6 @@ * **pointAsCircle:** true | false, default is false. For point geojson return circle element for option: | ||
Callback function could be used to render SVG string. | ||
The options **'attributes'**, **'r'** and **'callback'** can also be given in **convert** function | ||
``` | ||
var svgStrings = convertor.convert(geojson, | ||
The options **'attributes'**, **'r'** and **'callback'** can also be given in **convert** function | ||
``` | ||
var svgStrings = convertor.convert(geojson, | ||
{ | ||
@@ -186,3 +188,3 @@ "attributes": ..., | ||
mapExtent: {left: -180, bottom: -90, right: 180, top: 90}, | ||
output: 'svg' | ||
output: 'svg' | ||
} | ||
@@ -199,3 +201,3 @@ ); | ||
{ | ||
viewportExtent: {width: 200, height: 100}, | ||
viewportExtent: {width: 200, height: 100}, | ||
mapExtent: {left: -180, bottom: -90, right: 180, top: 90}, | ||
@@ -207,5 +209,5 @@ output: 'path' | ||
{ | ||
"type": "Polygon", | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]] | ||
[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]] | ||
] | ||
@@ -220,6 +222,6 @@ } | ||
Once you run | ||
```npm install``` | ||
then for running test | ||
then for running test | ||
@@ -226,0 +228,0 @@ ```npm run test``` |
//converter.js | ||
var multi = require('multigeojson'); | ||
function getCoordString(coords,res,origin) { | ||
//origin - svg image origin | ||
var coordStr = coords.map(function(coord) { | ||
return (coord[0] - origin.x)/res + ',' + (origin.y - coord[1])/res; | ||
function getCoordString(coords,res,origin, precision) { | ||
//origin - svg image origin | ||
var convertedCoords = coords.map(function(coord) { | ||
return [(coord[0] - origin.x)/res, (origin.y - coord[1])/res]; | ||
}); | ||
var coordStr = convertedCoords.map(function (coord) { | ||
if (precision) { | ||
return coord[0].toFixed(precision) + ',' + coord[1].toFixed(precision); | ||
} else { | ||
return coord[0] + ',' + coord[1]; | ||
} | ||
}); | ||
return coordStr.join(' '); | ||
@@ -22,5 +29,5 @@ } | ||
var r = opt && opt.r ? opt.r : 1; | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
? opt.pointAsCircle : false; | ||
var coords = getCoordString([geom.coordinates],res,origin); | ||
var coords = getCoordString([geom.coordinates],res,origin,opt.precision); | ||
if (pointAsCircle) { | ||
@@ -30,3 +37,3 @@ return [coords]; | ||
return [ | ||
'M' + coords | ||
'M' + coords | ||
+ ' m'+ -r+ ',0'+ ' a'+r+','+ r + ' 0 1,1 '+ 2*r + ','+0 | ||
@@ -46,5 +53,5 @@ + ' a'+r+','+ r + ' 0 1,1 '+ -2*r + ','+0 | ||
} | ||
function lineString(geom,res,origin,otp) { | ||
var coords = getCoordString(geom.coordinates,res,origin); | ||
var path = 'M'+ coords; | ||
function lineString(geom,res,origin,opt) { | ||
var coords = getCoordString(geom.coordinates,res,origin,opt.precision); | ||
var path = 'M'+ coords; | ||
return [path]; | ||
@@ -62,3 +69,3 @@ } | ||
var mainStr,holes,holeStr; | ||
mainStr = getCoordString(geom.coordinates[0],res,origin); | ||
mainStr = getCoordString(geom.coordinates[0],res,origin,opt.precision); | ||
if (geom.coordinates.length > 1) { | ||
@@ -70,3 +77,3 @@ holes = geom.coordinates.slice(1,geom.coordinates.length); | ||
for(var i=0;i<holes.length; i++) { | ||
path += ' M' + getCoordString(holes[i],res,origin); | ||
path += ' M' + getCoordString(holes[i],res,origin,opt.precision); | ||
} | ||
@@ -73,0 +80,0 @@ } |
@@ -7,3 +7,3 @@ var extend = require('./extend.js'); | ||
this.options = options || {}; | ||
this.viewportSize = this.options.viewportSize || | ||
this.viewportSize = this.options.viewportSize || | ||
{width: 256, height: 256}; | ||
@@ -23,3 +23,3 @@ this.mapExtent = this.options.mapExtent || | ||
var yres = (extent.top - extent.bottom)/size.height; | ||
if (fitTo) { | ||
if (fitTo) { | ||
if (fitTo.toLowerCase() === 'width') { | ||
@@ -79,3 +79,3 @@ return xres; | ||
if (property.type === 'dynamic') { | ||
var val, key = property.key ? property.key | ||
var val, key = property.key ? property.key | ||
: property.property.split('.').pop() | ||
@@ -97,6 +97,6 @@ try { | ||
} | ||
var id = opt.attributes.id || feature.id || | ||
(feature.properties && feature.properties.id | ||
var id = opt.attributes.id || feature.id || | ||
(feature.properties && feature.properties.id | ||
? feature.properties.id : null); | ||
if (id) opt.attributes.id = id | ||
if (id) opt.attributes.id = id; | ||
return this.convertGeometry(feature.geometry,opt); | ||
@@ -132,3 +132,3 @@ }; | ||
var svg = {}; | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
? opt.pointAsCircle : false; | ||
@@ -142,4 +142,4 @@ if((type == 'Point' || type == 'MultiPoint') && pointAsCircle) { | ||
if(type == 'Polygon' || type == 'MultiPolygon') { | ||
svg['fill-rule'] == 'evenodd'; | ||
} | ||
svg['fill-rule'] == 'evenodd'; | ||
} | ||
} | ||
@@ -153,3 +153,3 @@ for (var key in attributes) { | ||
function jsonToSvgElement(json,type,opt) { | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle') | ||
? opt.pointAsCircle : false; | ||
@@ -169,7 +169,7 @@ var ele ='<path'; | ||
//taken from http://stackoverflow.com/a/6394168/713573 | ||
function index(prev,cur, i, arr) { | ||
function index(prev,cur, i, arr) { | ||
if (prev.hasOwnProperty(cur)) { | ||
return prev[cur]; | ||
return prev[cur]; | ||
} else { | ||
throw new Error(arr.slice(0,i+1).join('.') + ' is not a valid property path'); | ||
throw new Error(arr.slice(0,i+1).join('.') + ' is not a valid property path'); | ||
} | ||
@@ -176,0 +176,0 @@ } |
@@ -49,2 +49,11 @@ 'use strict'; | ||
}); | ||
it('Output svg coordinates precision', function() { | ||
var geojson = {type:'LineString',coordinates:[[10,10],[15,20],[30,10]]}; | ||
var expSVGs = ['<path d="M105.556,44.444 108.333,38.889 116.667,44.444"/>']; | ||
var converter = geojson2svg(testData.options); | ||
var actualSVGs = converter.convert(geojson, {precision: 3}); | ||
expect(actualSVGs).to.be.an('array'); | ||
expect(actualSVGs.length).to.be.equal(1); | ||
expect(actualSVGs).to.be.deep.equal(expSVGs); | ||
}); | ||
it('Feature {output: "path",explode: false}', function() { | ||
@@ -51,0 +60,0 @@ var actualPaths = converter.convert(testData.feature.geojson, |
Sorry, the diff of this file is not supported yet
459503
26
3951
227