feat: osmtogeojson

This commit is contained in:
tkqubo
2016-01-08 11:30:57 +09:00
parent 81b951f1ac
commit 939bf16689
2 changed files with 190 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
/// <reference path="osmtogeojson.d.ts" />
import osmtogeojson from 'osmtogeojson';
import {OsmJSON, GeoJSON} from 'osmtogeojson';
let xml: Document = (new DOMParser()).parseFromString("<osm><node id='1' lat='1.234' lon='4.321' /></osm>", '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}
});
+96
View File
@@ -0,0 +1,96 @@
// Type definitions for osmtogeojson 2.2.5
// Project: https://github.com/tyrasd/osmtogeojson.git
// Definitions by: Qubo <https://github.com/tkqubo>
// 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;
}