Skip to content

Commit

Permalink
refactor: enable other data meta models
Browse files Browse the repository at this point in the history
  • Loading branch information
redmer committed Sep 28, 2023
1 parent 286ba08 commit 1fae51f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { Bye } from "./cli-error.js";
import { GeoPackageParser } from "./geopackage.js";
import { EXTENSION_MIMETYPES, mimetypeForExtension } from "./mimetypes.js";
import { ModelRegistry } from "./models/models.js";
import { FX, GEO, RDFNS, XSD, XYZ } from "./prefixes.js";

const pipeline = promisify(streampipeline);
Expand Down Expand Up @@ -53,7 +54,7 @@ async function cli() {
.option("model", {
desc: "Data meta model",
})
.choices("model", ["facade-x"])
.choices("model", ModelRegistry.knownModels())
.parse();

if (!existsSync(argv.input)) Bye(`File '${argv.input}' not found`);
Expand All @@ -77,9 +78,11 @@ async function cli() {
: mimetypeForExtension(path.extname(argv.output)) ??
mimetypeForExtension("nq");
const wantsGzip: boolean = argv.output?.endsWith(".gz");
const model: string = argv.model ?? ModelRegistry.knownModels()[0];

const parser = new GeoPackageParser(argv.input, {
boundingBox: boundingBox,
model,
boundingBox,
allowedLayers: argv.onlyLayers,
baseIRI: argv.baseIri ?? pathToFileURL(argv.input).href,
includeBinaryValues: argv.includeBinaryValues,
Expand Down
15 changes: 13 additions & 2 deletions src/geopackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { BoundingBox, GeoPackage, GeoPackageAPI } from "@ngageoint/geopackage";
import type * as RDF from "@rdfjs/types";
import { Readable } from "node:stream";
import { DataFactory } from "rdf-data-factory";
import { quadsFromGeoPackage } from "./rdf-geopackage.js";
import { quadsFromGeoPackage } from "./models/facade-x/rdf-geopackage.js";
import { ModelRegistry, QuadsGeneratorFunc } from "./models/models.js";

// Register known quad generating modules here.
// I don't know how to make this a true plugin (but that's not really necessary either)
const WellKnownModels = { "facade-x": quadsFromGeoPackage };
for (const [modelName, func] of Object.entries(WellKnownModels))
ModelRegistry.add(modelName, func);

export interface GeoPackageOptions {
/** Pass a data factory or rdf-data-factory is used */
Expand All @@ -16,6 +23,8 @@ export interface GeoPackageOptions {
boundingBox?: BoundingBox;
/** Generate quads where the object/value is a binary (Base-64 encoded). */
includeBinaryValues?: boolean;
/** Data meta model by which triples are generated */
model: string;
}

export class GeoPackageParser extends Readable implements RDF.Stream {
Expand All @@ -24,6 +33,7 @@ export class GeoPackageParser extends Readable implements RDF.Stream {
iterQuad: Generator<RDF.Quad>;
gpkg: GeoPackage;
shouldRead: boolean;
generator: QuadsGeneratorFunc;

/**
* Read a GeoPackage and output a stream of RDF.Quads
Expand All @@ -35,14 +45,15 @@ export class GeoPackageParser extends Readable implements RDF.Stream {

this.filepath = filepath;
this.options = { dataFactory: new DataFactory(), ...options };
this.generator = ModelRegistry.get(this.options.model);
this.shouldRead = false;
}

_construct(callback: (error?: Error) => void): void {
GeoPackageAPI.open(this.filepath)
.then((gpkg) => {
this.gpkg = gpkg;
this.iterQuad = quadsFromGeoPackage(this.gpkg, this.options);
this.iterQuad = this.generator(this.gpkg, this.options);
callback();
})
.catch(callback);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DBValue } from "@ngageoint/geopackage/dist/lib/db/dbAdapter.js";
import { enumerate } from "./py-enumerate.js";
import { enumerate } from "../../py-enumerate.js";
import {
QuadsFromTableOptions,
getRowNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type * as RDF from "@rdfjs/types";
import type { Feature, Geometry } from "geojson";
import stringify from "json-stable-stringify";
import { DataFactory } from "rdf-data-factory";
import { GEO, RDFNS } from "./prefixes.js";
import { enumerate } from "./py-enumerate.js";
import { GEO, RDFNS } from "../../prefixes.js";
import { enumerate } from "../../py-enumerate.js";
import {
QuadsFromTableOptions,
getRowNode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GeoPackage } from "@ngageoint/geopackage";
import type * as RDF from "@rdfjs/types";
import { GeoPackageOptions } from "./geopackage.js";
import { GeoPackageOptions } from "../../geopackage.js";
import { quadsFromAttributeTable } from "./rdf-attribute-table.js";
import { quadsFromFeatureTable } from "./rdf-feature-table.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { DBValue } from "@ngageoint/geopackage/dist/lib/db/dbAdapter.js";
import type * as RDF from "@rdfjs/types";
import { DataFactory } from "rdf-data-factory";
import { toRdf } from "rdf-literal";
import { FX, RDFNS, XSD, XYZ } from "./prefixes.js";
import { FX, RDFNS, XSD, XYZ } from "../../prefixes.js";

export interface QuadsFromTableOptions {
/** Name of the originating table */
Expand Down
42 changes: 42 additions & 0 deletions src/models/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { GeoPackage } from "@ngageoint/geopackage";
import type * as RDF from "@rdfjs/types";
import type { GeoPackageOptions } from "../geopackage.js";

/**
* The type signature of a quads generating function
*
* @param geopackage The GeoPackage instance
* @param options Options that may guide the generation of the quads.
*/
export type QuadsGeneratorFunc = (
geopackage: GeoPackage,
options: GeoPackageOptions,
) => Generator<RDF.Quad>;

/** Singleton registry of Quad generating models */
export class ModelRegistry {
private static MODEL_REGISTRY: Record<string, QuadsGeneratorFunc> = {};
/**
* Register a quads generator
*
* @param modelName Name to register the model by
* @param mainFunc Function that returns a quads generator
*/
static add(modelName: string, mainFunc: QuadsGeneratorFunc) {
this.MODEL_REGISTRY[modelName] = mainFunc;
}

/**
* Get a registered quads generator
*
* @param modelName Name of registered generator
*/
static get(modelName: string): QuadsGeneratorFunc {
return this.MODEL_REGISTRY[modelName];
}

/** Return a list of known models */
static knownModels(): string[] {
return Object.keys(this.MODEL_REGISTRY);
}
}

0 comments on commit 1fae51f

Please # to comment.