-
Notifications
You must be signed in to change notification settings - Fork 49
/
geojson2svg.html
4 lines (3 loc) · 11.4 KB
/
geojson2svg.html
1
2
3
4
<script type="text/javascript">
!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 c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);throw(f=new Error("Cannot find module '"+i+"'")).code="MODULE_NOT_FOUND",f}c=n[i]={exports:{}},e[i][0].call(c.exports,function(r){return o(e[i][1][r]||r)},c,c.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){require=require("./src/index.js").GeoJSON2SVG;module.exports=require},{"./src/index.js":6}],2:[function(require,module,exports){"use strict";function isArray(arr){return"function"==typeof Array.isArray?Array.isArray(arr):"[object Array]"===toStr.call(arr)}function isPlainObject(obj){if(obj&&"[object Object]"===toStr.call(obj)){var key,hasOwnConstructor=hasOwn.call(obj,"constructor"),hasIsPrototypeOf=obj.constructor&&obj.constructor.prototype&&hasOwn.call(obj.constructor.prototype,"isPrototypeOf");if(!obj.constructor||hasOwnConstructor||hasIsPrototypeOf){for(key in obj);return void 0===key||hasOwn.call(obj,key)}}}function setProperty(target,options){defineProperty&&"__proto__"===options.name?defineProperty(target,options.name,{enumerable:!0,configurable:!0,value:options.newValue,writable:!0}):target[options.name]=options.newValue}function getProperty(obj,name){if("__proto__"===name){if(!hasOwn.call(obj,name))return;if(gOPD)return gOPD(obj,name).value}return obj[name]}var hasOwn=Object.prototype.hasOwnProperty,toStr=Object.prototype.toString,defineProperty=Object.defineProperty,gOPD=Object.getOwnPropertyDescriptor;module.exports=function extend(){var options,name,copy,copyIsArray,src,target=arguments[0],i=1,length=arguments.length,deep=!1;for("boolean"==typeof target&&(deep=target,target=arguments[1]||{},i=2),(null==target||"object"!=typeof target&&"function"!=typeof target)&&(target={});i<length;++i)if(null!=(options=arguments[i]))for(name in options)src=getProperty(target,name),target!==(copy=getProperty(options,name))&&(deep&©&&(isPlainObject(copy)||(copyIsArray=isArray(copy)))?(src=copyIsArray?(copyIsArray=!1,src&&isArray(src)?src:[]):src&&isPlainObject(src)?src:{},setProperty(target,{name:name,newValue:extend(deep,src,copy)})):void 0!==copy&&setProperty(target,{name:name,newValue:copy}));return target}},{}],3:[function(require,module,exports){module.exports=function(gj){var bbox;if(gj.hasOwnProperty("type"))return gj=function getCoordinatesDump(gj){var coords;"Point"==gj.type?coords=[gj.coordinates]:"LineString"==gj.type||"MultiPoint"==gj.type?coords=gj.coordinates:"Polygon"==gj.type||"MultiLineString"==gj.type?coords=gj.coordinates.reduce(function(dump,part){return dump.concat(part)},[]):"MultiPolygon"==gj.type?coords=gj.coordinates.reduce(function(dump,poly){return dump.concat(poly.reduce(function(points,part){return points.concat(part)},[]))},[]):"Feature"==gj.type?coords=getCoordinatesDump(gj.geometry):"GeometryCollection"==gj.type?coords=gj.geometries.reduce(function(dump,g){return dump.concat(getCoordinatesDump(g))},[]):"FeatureCollection"==gj.type&&(coords=gj.features.reduce(function(dump,f){return dump.concat(getCoordinatesDump(f))},[]));return coords}(gj),bbox=[Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,Number.NEGATIVE_INFINITY,Number.NEGATIVE_INFINITY],gj.reduce(function(prev,coord){return[Math.min(coord[0],prev[0]),Math.min(coord[1],prev[1]),Math.max(coord[0],prev[2]),Math.max(coord[1],prev[3])]},bbox)}},{}],4:[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 multi,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});return!(!sameType||!sameCrs)&&((multi={}).type="Multi"+gs[0].type,multi.coordinates=[],0!=crs&&(multi.crs=crs),gs.forEach(function(g){multi.coordinates.push(g.coordinates)}),multi)}},void 0!==module&&module.exports?module.exports=multigeojson:window&&(window.multigeojson=multigeojson)},{}],5:[function(require,module,exports){var multi=require("multigeojson");function getCoordString(coords,res,origin,precision,opt){return coords.map(function(coord){return[((coord=opt.coordinateConverter?opt.coordinateConverter(coord):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,opt){var r=opt&&opt.r?opt.r:1,pointAsCircle=!(!opt||!opt.hasOwnProperty("pointAsCircle"))&&opt.pointAsCircle,geom=getCoordString([geom.coordinates],res,origin,opt.precision,opt);return pointAsCircle?[geom]:["M"+geom+" 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,opt)]}function polygon(geom,res,origin,opt){var holes,path="M"+getCoordString(geom.coordinates[0],res,origin,opt.precision,opt);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,opt);return[path+="Z"]}module.exports={Point:point,MultiPoint:function(geom,res,origin,opt){var explode=!(!opt||!opt.hasOwnProperty("explode"))&&opt.explode,geom=multi.explode(geom).map(function(single){return point(single,res,origin,opt)[0]});return explode?geom:[geom.join(" ")]},LineString:lineString,MultiLineString:function(geom,res,origin,opt){var explode=!(!opt||!opt.hasOwnProperty("explode"))&&opt.explode,geom=multi.explode(geom).map(function(single){return lineString(single,res,origin,opt)[0]});return explode?geom:[geom.join(" ")]},Polygon:polygon,MultiPolygon:function(geom,res,origin,opt){var explode=!!opt.hasOwnProperty("explode")&&opt.explode,geom=multi.explode(geom).map(function(single){return polygon(single,res,origin,opt)[0]});return explode?geom:[geom.join(" ").replace(/Z/g,"")+"Z"]}}},{multigeojson:4}],6:[function(require,module,exports){function GeoJSON2SVG(options={}){if(options.mapExtent?this.mapExtentFromGeojson=options.mapExtentFromGeojson:this.mapExtentFromGeojson=!0,options.fitTo&&!/^(width|height)$/i.test(options.fitTo))throw new Error('"fitTo" option should be "width" or "height" ');if(this.options=options,this.viewportSize=options.viewportSize||{width:256,height:256},options.coordinateCoverter&&"function"!=typeof options.coordinateConverter)throw new Error('"coordinateConverter" option should be function');var rightTop,leftBottom;this.coordinateConverter=options.coordinateConverter,options.mapExtent&&this.coordinateConverter?(rightTop=this.coordinateConverter([options.mapExtent.right,options.mapExtent.top]),leftBottom=this.coordinateConverter([options.mapExtent.left,options.mapExtent.bottom]),this.mapExtent={left:leftBottom[0],bottom:leftBottom[1],right:rightTop[0],top:rightTop[1]}):this.mapExtent=options.mapExtent,this.mapExtent&&(this.res=this.calResolution(this.mapExtent,this.viewportSize,this.options.fitTo))}var bbox=require("geojson-bbox"),extend=require("extend"),converter=require("./converter.js");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)}GeoJSON2SVG.prototype.calResolution=function(extent,size,fitTo){var xres=(extent.right-extent.left)/size.width,extent=(extent.top-extent.bottom)/size.height;if(fitTo){if("width"===fitTo.toLowerCase())return xres;if("height"===fitTo.toLowerCase())return extent;throw new Error('"fitTo" option should be "width" or "height" ')}return Math.max(xres,extent)},GeoJSON2SVG.prototype.convert=function(geojson,options){var extent,resetExtent=!1,opt=(!this.res&&this.mapExtentFromGeojson&&(resetExtent=!0,extent=bbox(geojson),this.coordinateConverter&&(extent=function(extent,converter){var leftBottom=converter([extent[0],extent[1]]),converter=converter([extent[2],extent[3]]);return[...leftBottom,...converter]}(extent,this.coordinateConverter)),this.mapExtent={left:extent[0],bottom:extent[1],right:extent[2],top:extent[3]},this.res=this.calResolution(this.mapExtent,this.viewportSize,this.options.fitTo)),extend(!0,{},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])throw new Error("Geojson type not supported.");svgElements=this.convertGeometry(geojson,opt)}return resetExtent&&(this.res=null,this.mapExtent=null),opt.callback&&opt.callback.call(this,svgElements),svgElements},GeoJSON2SVG.prototype.convertFeature=function(feature,options){var arr;if(feature||feature.geometry)return(options=extend(!0,{},this.options,options||{})).attributes&&options.attributes instanceof Array?(arr=options.attributes,options.attributes=arr.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},{})):options.attributes=options.attributes||{},(arr=options.attributes.id||feature.id||(feature.properties&&feature.properties.id?feature.properties.id:null))&&(options.attributes.id=arr),this.convertGeometry(feature.geometry,options)},GeoJSON2SVG.prototype.convertGeometry=function(geom,options){var opt,paths;if(converter[geom.type])return options=(opt=extend(!0,{},this.options,options||{})).output||"svg",paths=converter[geom.type].call(this,geom,this.res,{x:this.mapExtent.left,y:this.mapExtent.top},opt),"svg"==options.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,opt){var key,opt=!(!opt||!opt.hasOwnProperty("pointAsCircle"))&&opt.pointAsCircle,ele="<path";"Point"!=type&&"MultiPoint"!=type||!opt||(ele="<circle");for(key in json)ele+=" "+key+'="'+json[key]+'"';return ele+="/>"}(json,geom.type,opt)}):paths;throw new Error("Geojson type not supported.")},module.exports={GeoJSON2SVG:GeoJSON2SVG}},{"./converter.js":5,extend:2,"geojson-bbox":3}]},{},[1])(1)});
</script>