@placemarkio/tokml
Version:
Convert GeoJSON to KML
3 lines (2 loc) • 6.67 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).toKML={})}(this,(function(t){"use strict";const e=function(t,e,n){const r={type:String(t)};return null!=n||"string"!=typeof e&&!Array.isArray(e)?Object.assign(r,e):n=e,Array.isArray(n)?r.children=n:null!=n&&(r.value=String(n)),r},n=/["&'<>`]/g,r=/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,o=/[\x01-\t\v\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g,i=/[|\\{}()[\]^$+*?.]/g,u=new WeakMap;function c(t,e){return t=t.replace(e.subset?function(t){let e=u.get(t);e||(e=function(t){const e=[];let n=-1;for(;++n<t.length;)e.push(t[n].replace(i,"\\$&"));return new RegExp("(?:"+e.join("|")+")","g")}(t),u.set(t,e));return e}(e.subset):n,c),e.subset||e.escapeOnly?t:t.replace(r,(function(t,n,r){return e.format(1024*(t.charCodeAt(0)-55296)+t.charCodeAt(1)-56320+65536,r.charCodeAt(n+2),e)})).replace(o,c);function c(t,n,r){return e.format(t.charCodeAt(0),r.charCodeAt(n+1),e)}}function s(t){return"&#x"+t.toString(16).toUpperCase()+";"}const a=/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;function l(t,e,n){const r=function(t){return String(t||"").replace(a,"")}(t);return n?r.replace(n,o):o(r);function o(t){return function(t,e){return c(t,Object.assign({format:s},e))}(t,{subset:e})}}const f=["\t","\n"," ",'"',"&","'","/","<","=",">"];function p(t){return l(t,f)}function y(t,e){const n=String(t);if("string"!=typeof e)throw new TypeError("Expected character");let r=0,o=n.indexOf(e);for(;-1!==o;)r++,o=n.indexOf(e,o+e.length);return r}function d(t,e){const n=String(t);let r=e.options.quote||'"';if(e.options.quoteSmart){const t='"'===r?"'":'"';y(n,r)>y(n,t)&&(r=t)}return r+l(n,["<","&",r])+r}const g={}.hasOwnProperty;const m=["&","<"];function h(t){return l(t.value,m)}const x=/\?>/g,b=[">"];const w=/]]>/g,M=[">"];const v={}.hasOwnProperty,S={root:E,element:function(t,e){const n=p(t.name),r=E(t,e),o=t.attributes||{},i=!r&&e.options.closeEmptyElements,u=[];let c;for(c in o)if(g.call(o,c)){const t=o[c];null!=t&&u.push(p(c)+"="+d(t,e))}return"<"+n+(0===u.length?"":" "+u.join(" "))+(i?(e.options.tightClose?"":" ")+"/":"")+">"+r+(i?"":"</"+n+">")},text:h,comment:function(t){return"\x3c!--"+l(t.value,["-"])+"--\x3e"},doctype:function(t,e){const n=p(t.name),r=t.public,o=t.system;let i="<!DOCTYPE";return""!==n&&(i+=" "+n),r?i+=" PUBLIC "+d(r,e):o&&(i+=" SYSTEM"),o&&(i+=" "+d(o,e)),i+">"},instruction:function(t){const e=p(t.name)||"x",n=l(t.value,b,x);return"<?"+e+(n?" "+n:"")+"?>"},cdata:function(t){return"<![CDATA["+l(t.value,M,w)+"]]>"},raw:function(t,e){return e.options.allowDangerousXml?t.value:h(t)}};function A(t,e){const n=t&&t.type;if(!n)throw new Error("Expected node, not `"+t+"`");if(!v.call(S,n))throw new Error("Cannot compile unknown node `"+n+"`");return(0,S[n])(t,e)}function E(t,e){const n=t&&t.children||[];let r=-1;const o=[];for(;++r<n.length;)o[r]=A(n[r],e);return o.join("")}function F(t,e){const n={options:{}};if("string"==typeof n.options.quote&&'"'!==n.options.quote&&"'"!==n.options.quote)throw new Error("Invalid quote `"+n.options.quote+"`, expected `'` or `\"`");return A(Array.isArray(t)?{type:"root",children:t}:t,n)}const C=function(t,e,...n){let r,o=-1;if(null==t)r={type:"root",children:[]},n.unshift(e);else{if("string"!=typeof t)throw new TypeError("Expected element name, got `"+t+"`");if(r={type:"element",name:t,attributes:{},children:[]},function(t){if(null==t||"object"!=typeof t||Array.isArray(t))return!1;return!0}(e)){let t;for(t in e)void 0===e[t]||null===e[t]||"number"==typeof e[t]&&Number.isNaN(e[t])||(r.attributes[t]=String(e[t]))}else n.unshift(e)}for(;++o<n.length;)D(r.children,n[o]);return r};function D(t,e){let n=-1;if(null==e);else if("string"==typeof e||"number"==typeof e)t.push({type:"text",value:String(e)});else if(Array.isArray(e))for(;++n<e.length;)D(t,e[n]);else{if("object"!=typeof e||!("type"in e))throw new TypeError("Expected node, nodes, string, got `"+e+"`");"root"===e.type?D(t,e.children):t.push(e)}}const P=e("text","\n"),j=e("text"," ");function O(t){switch(t.type){case"Feature":return k(t);case"folder":return function(t){const e=["string","number"].includes(typeof t.meta.id)?{id:String(t.meta.id)}:{};return[P,C("Folder",e,[P,...T(t.meta),P,j,...t.children.flatMap((t=>O(t)))])]}(t)}}const L=["address","description","name","open","visibility","phoneNumber"];function T(t){return L.filter((e=>void 0!==t[e])).map((n=>C(n,[e("text",String(t[n]))])))}function k(t){const{id:e}=t,n=["string","number"].includes(typeof e)?{id:e}:{};return[P,C("Placemark",n,[P,...I(t.properties),P,j,...t.geometry?[K(t.geometry)]:[]])]}function q(t){return`${t[0]},${t[1]}`}function B(t){return C("coordinates",[e("text",t.map(q).join("\n"))])}function N(t){switch(typeof t){case"string":return t;case"boolean":case"number":return String(t);case"object":try{return JSON.stringify(t)}catch(t){return""}}return""}function G(t){return t&&"object"==typeof t&&"@type"in t&&"html"===t["@type"]&&"value"in t&&"string"==typeof t.value?e("cdata",t.value):N(t)}function I(t){if(!t)return[];const{name:n,description:r,visibility:o,...i}=t;return[n&&C("name",[e("text",N(n))]),r&&C("description",[e("text",G(r))]),void 0!==o&&C("visibility",[e("text",o?"1":"0")]),C("ExtendedData",Object.entries(i).flatMap((([t,n])=>[P,j,C("Data",{name:t},[C("value",[e("text","string"==typeof n?n:JSON.stringify(n))])])])))].filter(Boolean)}const $=t=>C("LinearRing",[B(t)]);function K(t){switch(t.type){case"Point":return C("Point",[(n=t.coordinates,C("coordinates",[e("text",q(n))]))]);case"MultiPoint":return function(t){return C("MultiGeometry",t.coordinates.flatMap((t=>[P,K({type:"Point",coordinates:t})])))}(t);case"LineString":return C("LineString",[B(t.coordinates)]);case"MultiLineString":return function(t){return C("MultiGeometry",t.coordinates.flatMap((t=>[P,K({type:"LineString",coordinates:t})])))}(t);case"Polygon":return function(t){const[e,...n]=t.coordinates;return C("Polygon",[P,C("outerBoundaryIs",[P,j,$(e)]),...n.flatMap((t=>[P,C("innerBoundaryIs",[P,j,$(t)])]))])}(t);case"MultiPolygon":return function(t){return C("MultiGeometry",t.coordinates.flatMap((t=>[P,K({type:"Polygon",coordinates:t})])))}(t);case"GeometryCollection":return C("MultiGeometry",t.geometries.flatMap((t=>[P,K(t)])))}var n}t.foldersToKML=function(t){return F(e("root",[C("kml",{xmlns:"http://www.opengis.net/kml/2.2"},C("Document",t.children.flatMap((t=>O(t)))))]))},t.toKML=function(t){return F(e("root",[C("kml",{xmlns:"http://www.opengis.net/kml/2.2"},C("Document",t.features.flatMap((t=>k(t)))))]))}}));
//# sourceMappingURL=tokml.umd.js.map