@mailwoman/spatial 4.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ /**
2
+ * @copyright Sister Software.
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Provides functions for parsing and converting extended Well-Known text and binary data.
7
+ */
8
+
9
+ import type { GeometryCollection, GeometryLiteral } from "@mailwoman/spatial"
10
+ import wkx from "wkx"
11
+
12
+ /**
13
+ * Given an extended Well-Known Binary (EWKB) buffer, parse it into a GeoJSON geometry object.
14
+ *
15
+ * @category Geo
16
+ */
17
+ export function wellKnownGeometryToGeoJSON<T = GeometryLiteral>(extendedWellKnownBinary: Buffer): T
18
+ /**
19
+ * Given an extended Well-Known-Text, parse it into a GeoJSON geometry object.
20
+ *
21
+ * @category Geo
22
+ */
23
+ export function wellKnownGeometryToGeoJSON<T = GeometryLiteral>(wellKnownText: string): T
24
+ /**
25
+ * Given an extended Well-Known-Text, parse it into a GeoJSON geometry object.
26
+ *
27
+ * @category Geo
28
+ */
29
+ export function wellKnownGeometryToGeoJSON<T = GeometryLiteral>(input: Buffer | string): T
30
+ export function wellKnownGeometryToGeoJSON<T = GeometryLiteral>(input: Buffer | string) {
31
+ return wkx.Geometry.parse(input).toGeoJSON() as T
32
+ }
33
+
34
+ /**
35
+ * Given a GeoJSON geometry object, convert it to an well-known binary (EWKB) buffer.
36
+ *
37
+ * @category Geo
38
+ * @returns A hex-encoded EWKB string or WKB buffer.
39
+ */
40
+ export function geometryToWKB(geometry: GeometryLiteral | GeometryCollection) {
41
+ return wkx.Geometry.parseGeoJSON(geometry).toWkb()
42
+ }
43
+
44
+ /**
45
+ * Given a GeoJSON geometry object, convert it to an extended well-known binary (EWKB) buffer.
46
+ *
47
+ * @category Geo
48
+ * @returns A buffer representing the EWKB.
49
+ */
50
+ export function geometryToEWKB(geometry: GeometryLiteral | GeometryCollection) {
51
+ return wkx.Geometry.parseGeoJSON(geometry).toEwkb()
52
+ }
53
+
54
+ /**
55
+ * Given a GeoJSON geometry object, convert it to an extended well-known binary (EWKB) buffer.
56
+ *
57
+ * @category Geo
58
+ * @returns A hex-encoded string representing the EWKB.
59
+ */
60
+ export function geometryToEWKH(geometry: GeometryLiteral | GeometryCollection) {
61
+ return geometryToEWKB(geometry).toString("hex")
62
+ }
63
+
64
+ /**
65
+ * Given a GeoJSON geometry object, convert it to an extended well-known text (EWKT) string.
66
+ *
67
+ * @category Geo
68
+ */
69
+ export function geometryToWKT(geometry: GeometryLiteral | GeometryCollection): string {
70
+ return wkx.Geometry.parseGeoJSON(geometry).toWkt()
71
+ }
72
+
73
+ /**
74
+ * Given a GeoJSON geometry object, convert it to a SQL geometry string.
75
+ *
76
+ * This is useful for composing SQL queries that require geometry literals.
77
+ *
78
+ * @category Geo
79
+ */
80
+ export function geometryToSQL<T extends GeometryLiteral | GeometryCollection>(geometry: T | null | undefined) {
81
+ return () => {
82
+ if (!geometry) return `NULL`
83
+
84
+ return /* sql */ `GeomFromEWKB('${geometryToEWKH(geometry)}')`
85
+ }
86
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "@sister.software/tsconfig",
3
+ "compilerOptions": {
4
+ "allowSyntheticDefaultImports": true,
5
+ "emitDecoratorMetadata": true,
6
+ "experimentalDecorators": true,
7
+ "emitDeclarationOnly": false,
8
+ "allowImportingTsExtensions": false
9
+ },
10
+ "exclude": ["./out/**/*"],
11
+ "references": [
12
+ // ---
13
+ { "path": "../core" }
14
+ ]
15
+ }
package/typedoc.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": ["../typedoc.base.jsonc"],
3
+ "entryPoints": ["index.ts"]
4
+ }