diff --git a/types/mapnik/index.d.ts b/types/mapnik/index.d.ts new file mode 100644 index 0000000000..33b455f0dd --- /dev/null +++ b/types/mapnik/index.d.ts @@ -0,0 +1,49 @@ +// Type definitions for mapnik 3.x +// Project: http://mapnik.org +// Definitions by: Loli +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +export const settings: any; +export function register_default_fonts(): void; +export function register_default_input_plugins(): void; +export function register_datasource(path: string): void; +export class VectorTile { + constructor(z: number, x: number, y: number) + addDataSync(vectorTile: any): void; +} +export class Datasource { + constructor(datasource: any) + featureset(): Featureset; +} + +export class Featureset { + constructor() + next(): FeaturesetNext; +} +export class FeaturesetNext { + constructor() + toJSON(): string; +} + +export class Image { + constructor(x: number, y: number) + encode(type: string, callback?: (err: Error, buffer: Buffer) => void): void; + getData(): Buffer; +} + +export interface Image { + // constructor(x: number, y: number) + new(x: number, y: number): () => void; + encode(type: string, callback?: (err: Error, buffer: Buffer) => void): void; + getData(): Buffer; + save(fp: string): () => void; + open(fp: string): () => void; +} + +export class Map { + constructor(x: number, y: number) + load(xml: string, callback?: (err: Error, map: Map) => void): void; + zoomAll(): void; + render(images: Image | VectorTile , callback?: (err: Error, map: Image) => void): void; +} diff --git a/types/mapnik/mapnik-tests.ts b/types/mapnik/mapnik-tests.ts new file mode 100644 index 0000000000..23e726f111 --- /dev/null +++ b/types/mapnik/mapnik-tests.ts @@ -0,0 +1,40 @@ +import * as mapnik from "mapnik"; +import * as fs from "fs"; +import * as path from "path"; + +mapnik.register_default_fonts(); +mapnik.register_default_input_plugins(); + +const map: mapnik.Map = new mapnik.Map(256, 256); +map.load('./test/stylesheet.xml', function xx(err: Error, map: mapnik.Map) { + if (err) throw err; + map.zoomAll(); + const im: mapnik.Image = new mapnik.Image(256, 256); + map.render(im, function xxx(err: Error, im: mapnik.Image) { + if (err) throw err; + im.encode('png', function xxxx(err: Error, buffer: Buffer) { + if (err) throw err; + fs.writeFile('map.png', buffer, function xxxxx(err: Error) { + if (err) throw err; + console.log('saved map image to map.png'); + }); + }); + }); +}); + +// new mapnik.Image.open("xxx").save("xx"); + +mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins, 'shape.input')); +const ds: mapnik.Datasource = new mapnik.Datasource({type: 'shape', file: 'test/data/world_merc.shp'}); +const featureset: mapnik.Featureset = ds.featureset(); +const geojson: any = { + type: "FeatureCollection", + features: [ + ] +}; +let feat: mapnik.FeaturesetNext = featureset.next(); +while (feat) { + geojson.features.push(JSON.parse(feat.toJSON())); + feat = featureset.next(); +} +fs.writeFileSync("output.geojson", JSON.stringify(geojson, null, 2)); diff --git a/types/mapnik/tsconfig.json b/types/mapnik/tsconfig.json new file mode 100644 index 0000000000..81f111a37e --- /dev/null +++ b/types/mapnik/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mapnik-tests.ts" + ] +} \ No newline at end of file diff --git a/types/mapnik/tslint.json b/types/mapnik/tslint.json new file mode 100644 index 0000000000..2750cc0197 --- /dev/null +++ b/types/mapnik/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } \ No newline at end of file