feat: add mapnik d.ts

This commit is contained in:
ipv4sec
2017-11-24 15:38:09 +08:00
parent 6f16650b3d
commit c9fbcc9776
4 changed files with 113 additions and 0 deletions

49
types/mapnik/index.d.ts vendored Normal file
View File

@@ -0,0 +1,49 @@
// Type definitions for mapnik 3.x
// Project: http://mapnik.org
// Definitions by: Loli <https://github.com/ipv4sec>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
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;
}

View File

@@ -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));

View File

@@ -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"
]
}

1
types/mapnik/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }