diff --git a/osmtogeojson/osmtogeojson-tests.ts b/osmtogeojson/osmtogeojson-tests.ts new file mode 100644 index 000000000..3bb83c100 --- /dev/null +++ b/osmtogeojson/osmtogeojson-tests.ts @@ -0,0 +1,94 @@ +/// + +import osmtogeojson from 'osmtogeojson'; +import {OsmJSON, GeoJSON} from 'osmtogeojson'; + +let xml: Document = (new DOMParser()).parseFromString("", 'text/xml'); +let geojson: GeoJSON.FeatureCollection = { + type: "FeatureCollection", + features: [ + { + type: "Feature", + id: "node/1", + properties: { + type: "node", + id: 1, + tags: {}, + relations: [], + meta: {} + }, + geometry: { + type: "Point", + coordinates: [4.321, 1.234] + } + } + ] +}; + +osmtogeojson.toGeojson(xml); +osmtogeojson(xml, { + flatProperties: true, + uninterestingTags: {foo:true} +}); + +let json: OsmJSON.Root = { + elements: [ + { + type: "node", + id: 1, + lat: 1.234, + lon: 4.321, + timestamp: "2013-01-13T22:56:07Z", + version: 7, + changeset: 1234, + user: "johndoe", + uid: 666 + }, + { + type: "relation", + tags: {"type": "multipolygon"}, + id: 1, + members: [ + { + type: "way", + ref: 2, + role: "outer" + }, + { + type: "way", + ref: 3, + role: "outer" + } + ] + }, + { + type: "way", + id: 2, + nodes: [4,5,6,4] + }, + { + type: "node", + id: 4, + lat: 0.0, + lon: 0.0 + }, + { + type: "node", + id: 5, + lat: 0.0, + lon: 1.0 + }, + { + type: "node", + id: 6, + lat: 1.0, + lon: 0.0 + } + ] +}; + +osmtogeojson.toGeojson(json); +osmtogeojson(json, { + flatProperties: true, + uninterestingTags: {foo:true} +}); diff --git a/osmtogeojson/osmtogeojson.d.ts b/osmtogeojson/osmtogeojson.d.ts new file mode 100644 index 000000000..0edee3509 --- /dev/null +++ b/osmtogeojson/osmtogeojson.d.ts @@ -0,0 +1,96 @@ +// Type definitions for osmtogeojson 2.2.5 +// Project: https://github.com/tyrasd/osmtogeojson.git +// Definitions by: Qubo +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "osmtogeojson" { + export interface OsmToGeoJSON { + (data: Document|OsmJSON.Root, options?: Options): GeoJSON.GeoJSONObject; + toGeojson(data: Document|OsmJSON.Root, options?: Options): GeoJSON.GeoJSONObject; + } + + export interface Options { + verbose?: boolean; + /** + * If true, the resulting GeoJSON feature's properties will be a simple key-value list instead of a structured json object (with separate tags and metadata). default: false + */ + flatProperties?: boolean; + /** + * Either a blacklist of tag keys or a callback function. Will be used to decide if a feature is interesting enough for its own GeoJSON feature. + */ + uninterestingTags?: { [tag: string]: boolean; }|Function; //TODO: type function + /** + * Either a json object or callback function that is used to determine if a closed way should be treated as a Polygon or LineString. + */ + polygonFeatures?: any|Function; //TODO: type this + } + + export namespace GeoJSON { + export interface GeoJSONObject { + type: string; + } + + export interface Feature extends GeoJSONObject { + id?: string; + geometry: Geometry; + properties: any; //TODO: type this + } + + export interface FeatureCollection extends GeoJSONObject { + features: Feature[]; + } + + export interface Geometry extends GeoJSONObject { + coordinates: Coordinate|Coordinate[]|Coordinate[][]; + } + + export interface GeometryCollection extends GeoJSONObject { + geometries: Geometry[]; + } + + export type Coordinate2d = [number, number]; + export type Coordinate3d = [number, number, number]; + export type Coordinate = Coordinate2d|Coordinate3d; + } + + export namespace OsmJSON { + export interface Root { + elements: (Node|Way|Relationship)[]; + } + + export interface OsmJSONObject { + type: string; + id: number; + tags?: { [name: string]: string; } + timestamp?: string; + version?: number; + changeset?: number; + user?: string; + uid?: number; + } + + export interface Node extends OsmJSONObject { + lat: number; + lon: number; + } + + export interface Way extends OsmJSONObject { + nodes: number[]; + } + + export interface Relationship extends OsmJSONObject { + members: Member[]; + } + + export interface Member { + type: string; + ref: number; + role: string; + } + } + + var osmtogeojson: OsmToGeoJSON; + + export default osmtogeojson; +} +