geojson-to-gml-3
Version:
a package to translate geojson to GML 3.2.1
302 lines (301 loc) • 7.85 kB
JavaScript
const I = /[A-Za-z_:]/, U = (
/**/
/[A-Za-z_:0-9_.-]/
), v = new RegExp(
`^${I.source}${U.source}*$`
), E = (t) => v.test(t), $ = (t) => {
if (E(t)) return t;
throw new Error(`invalid XML name: '${t}'`);
}, G = (t, n) => `${t}:${n}`;
class k {
constructor(n, e) {
this.name = n, this.uri = e;
}
qualify(n) {
return G(this.name, n);
}
attr() {
return `xmlns:${this.name}="${this.uri}"`;
}
}
const x = (t, n = (e) => {
throw new Error(`unable to escape ${typeof t}: '${String(t)}'`);
}) => {
switch (typeof t) {
case "string":
return R(t);
case "number":
case "bigint":
return t.toString();
case "boolean":
return t ? "true" : "false";
default:
return n(t);
}
}, q = (t) => x(t), D = {
"&": "&",
"<": "<",
">": ">",
"'": "'",
'"': """
}, R = (t) => t.replace(
/[<>&'"]/g,
(n) => D[n] ?? ""
), z = (t, n) => `${t}="${n}"`;
function u(t) {
return Object.entries(t).map(([n, e]) => e == null ? null : z($(n), q(e))).filter((n) => n !== null);
}
const Z = "", X = (...t) => t.join(Z), B = (t) => (n, ...e) => l(t, n, ...e), l = (t, n, ...e) => (r) => {
let i = n.sort().join(" ");
i && (i = " " + i);
const o = X(
...e.map((s) => typeof s == "function" ? s(r) : s)
);
return o.length ? `<${t}${i}>${o}</${t}>` : `<${t}${i}/>`;
};
class y {
// and this could be omitted
constructor(n) {
this.uriToName = {}, this.nameToUri = {}, this.nameToCache = {}, n && this.bulkUpdate(n);
}
set(n, e) {
const r = new k(n, e);
return this.uriToName[r.uri] = r.name, this.nameToUri[r.name] = r.uri, this.nameToCache[r.name] = r, r;
}
get(n) {
let e = this.getName(n);
return e ? this.nameToCache[e] ?? null : null;
}
getName(n) {
return this.uriToName[n] || null;
}
clone() {
let n = new y();
return Object.assign(n.uriToName, this.uriToName), Object.assign(n.nameToUri, this.nameToUri), n;
}
getOrInsert(n, e) {
return this.get(e) ?? this.set(n, e);
}
/**
* update the namespace in-place.
* @param nameToUriMap a map of names to URIs, e.g. `{ "wfs": "http://www.opengis.net/wfs/2.0"}`
* @returns a new NsRegistry instance with the given namespaces added
* @throws if a name is already registered with a different URI
* @throws if a key isn't a valid XML name
*/
bulkUpdate(n) {
for (let [e, r] of Object.entries(n)) {
let i = $(e), o = q(r);
switch (this.nameToUri[i]) {
case o:
continue;
case void 0:
this.set(i, o);
continue;
default:
throw new Error(`namespace ${i} already registered`);
}
}
return this;
}
extend(n) {
return this.clone().bulkUpdate(n);
}
xmlnsAttrs() {
return Object.values(this.nameToCache).map((n) => n.attr()).sort();
}
}
const g = (t) => {
switch (t) {
case c.LON_LAT:
return ([n, e, ...r]) => [n, e, ...r];
case c.LAT_LON:
return ([n, e, ...r]) => [e, n, ...r];
default:
throw new Error("invalid order: " + t);
}
}, a = (t) => (n, e = {}) => (r) => {
r = r ?? new y();
const { ns: i = "gml" } = e, o = r.getOrInsert(i, _);
return t(o, n, e)(r);
}, L = (t) => (n, e, r) => t(n, e.coordinates, r), w = (t, n, e = {}) => {
let {
order: r = c.LON_LAT,
srsName: i = null,
srsDimension: o = null,
gmlId: s = null
} = e;
return l(
t.qualify("Point"),
u({ srsName: i, srsDimension: o, [t.qualify("id")]: s }),
l(
t.qualify("pos"),
u({ srsDimension: o }),
g(r)(n).join(" ")
)
);
}, M = (t, n, e = {}) => {
switch (n.type) {
case "Point":
return w(t, n.coordinates, e);
case "LineString":
return p(t, n.coordinates, e);
case "Polygon":
return N(t, n.coordinates, e);
case "MultiPoint":
return b(t, n, e);
case "MultiLineString":
return j(t, n, e);
case "MultiPolygon":
return P(t, n, e);
case "GeometryCollection":
return O(t, n, e);
default:
throw new Error(`unknown geometry type: ${n == null ? void 0 : n.type}`);
}
}, O = /* @__PURE__ */ m(
"MultiGeometry",
"geometryMembers",
"geometries",
M
), p = (t, n, e = {}) => {
let {
srsName: r = null,
srsDimension: i = null,
gmlId: o = null,
order: s = c.LON_LAT
} = e;
return l(
t.qualify("LineString"),
u({ srsName: r, [t.qualify("id")]: o }),
l(
t.qualify("posList"),
u({ srsDimension: i }),
F(n, s)
)
);
}, F = (t, n = c.LON_LAT) => {
let e = "";
const r = g(n);
for (let i of t)
for (let o of r(i))
e += `${o} `;
return e.trim();
}, T = (t, n, e = {}) => {
let {
srsName: r = null,
srsDimension: i = null,
gmlId: o = null,
order: s = c.LON_LAT
} = e;
const f = g(s);
return l(
t.qualify("LinearRing"),
u({ srsName: r, [t.qualify("id")]: o }),
l(
t.qualify("posList"),
u({ srsDimension: i }),
n.map((h) => f(h).join(" ")).join(" ")
)
);
}, N = (t, n, e = {}) => {
let { srsName: r = null, gmlId: i = null, ...o } = e, [s, ...f] = n;
const h = B(t.qualify("interior"));
return l(
t.qualify("Polygon"),
u({ srsName: r, [t.qualify("id")]: i }),
l(t.qualify("exterior"), [], T(t, s, o)),
...f.map((d) => h([], T(t, d, o)))
);
}, b = /* @__PURE__ */ m(
"MultiPoint",
"pointMembers",
// see file://./../../../spec/gml-3/geometryAggregates.xsd#pointMembers
"coordinates",
w
), j = /* @__PURE__ */ m(
"MultiCurve",
// see file://./../../../spec/gml-3/geometryAggregates.xsd#MultiCurveType
"curveMembers",
// see file://./../../../spec/gml-3/geometryBasic0d1d.xsd#CurveArrayPropertyType
"coordinates",
p
), P = /* @__PURE__ */ m(
"MultiSurface",
// see file://./../../../spec/gml-3/geometryAggregates.xsd#MultiSurface
"surfaceMembers",
"coordinates",
N
);
function m(t, n, e, r) {
return (i, o, s = {}) => {
let { srsName: f = null, gmlId: h = null, gmlIds: d = [], ...C } = s;
return l(
i.qualify(t),
u({ srsName: f, [i.qualify("id")]: h }),
l(
i.qualify(n),
[],
...o[e].map(
(A, S) => r(i, A, {
gmlId: d[S] ?? null,
...C
})
)
)
);
};
}
const _ = "http://www.opengis.net/gml/3.2", H = (t, n) => (e) => {
e = e ?? new y();
const { ns: r = "gml", srsDimension: i, srsName: o } = n, s = e.getOrInsert(r, _);
return l(
s.qualify("Envelope"),
u({
srsDimension: i,
srsName: o
}),
l(
s.qualify("lowerCorner"),
[],
t.slice(0, t.length / 2).join(" ")
// safe since `BBox` is composed entirely of numbers
),
l(
s.qualify("upperCorner"),
[],
t.slice(t.length / 2).join(" ")
)
)(e);
};
var c = /* @__PURE__ */ ((t) => (t.LON_LAT = "lon,lat", t.LAT_LON = "lat,lon", t))(c || {});
/*!! use-example file://./../tests/point.example.ts */
const J = /* @__PURE__ */ a(
L(w)
);
/*!! use-example file://./../tests/lineString.example.ts */
const K = /* @__PURE__ */ a(L(p));
/*!! use-example file://./../tests/polygon.example.ts */
const Q = /* @__PURE__ */ a(L(N));
/*!! use-example file://./../tests/multiPoint.example.ts */
const V = /* @__PURE__ */ a(b);
/*!! use-example file://./../tests/multiLineString.example.ts */
const W = /* @__PURE__ */ a(j);
/*!! use-example file://./../tests/multiPolygon.example.ts */
const Y = /* @__PURE__ */ a(P), tt = /* @__PURE__ */ a(M);
/*!! use-example file://./../tests/geometryCollection.example.ts */
const nt = /* @__PURE__ */ a(O);
export {
c as CoordinateOrder,
_ as GML,
H as bbox,
tt as default,
tt as geometry,
nt as geometryCollection,
K as lineString,
W as multiLineString,
V as multiPoint,
Y as multiPolygon,
J as point,
Q as polygon
};