terraformer.d.ts
14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* Terraformer module
* @example
* import * as Terraformer from "terraformer";
*/
// Note: Terraformer module exports namespace so it can be augmented by
// terraformer-wkt-parser and potentially others
export as namespace Terraformer;
// Export Terraformer for AMD require
export = Terraformer;
declare namespace Terraformer {
export interface Envelope {
x: number;
y: number;
w: number;
h: number;
}
export type BBox = number[]; // [number, number, number, number]
export type Coordinate = GeoJSON.Position;
export type Coordinates = GeoJSON.Position[];
/**
* Terraformer Primitives are JavaScript objects that map directly to their GeoJSON couterparts.
* Converting a GeoJSON object into a Terraformer Primitive will allow you use convenience methods like
* point.within(polygon).
*
* Every Primitive inherits from the Terraformer.Primitive base class, thus all other Primitives share the
* Terraformer.Primitive methods.
*
* There is a Primitive for every type of GeoJSON object, plus a Circle Primitive which represents a circle as a
* polygon.
*/
export class Primitive<T extends GeoJSON.GeoJsonObject> implements GeoJSON.GeoJsonObject {
public type: string;
/**
* You create a new Terraformer.Primitive object by passing it a valid GeoJSON Object. This will return a
* Terraformer.Primitive with the same type as your GeoJSON object.
* @param geojson GeoJSON Primitive
*/
constructor(geojson: T);
/**
* Converts this GeoJSON object’s coordinates to the web mercator spatial reference.
*/
public toMercator(): this;
/**
* Converts this GeoJSON object’s coordinates to geographic coordinates.
*/
public toGeographic(): this;
/**
* Returns an object with x, y, w and h suitable for passing to most indexes.
*/
public envelope(): Envelope;
// /**
// * Returns the GeoJSON Bounding Box for this primitive.
// */
// bbox(): number[]; // Terraformer docs have this function, but conflicts with GeoJSON typescript definitions for
// // optional bbox property.
/**
* Returns the convex hull of this primitive as a Polygon. Will return null if the convex hull cannot be calculated
* or a valid Polygon cannot be created.
*/
public convexHull(): GeoJSON.Polygon | null;
/**
* Returns true if the passed GeoJSON Geometry object is completely contained inside this primitive.
* @param geometry The geometry that potentially is inside of this one.
* @returns Returns true if the passed GeoJSON Geometry object is completely contained inside this primitive.
*/
public contains(geometry: GeoJSON.GeometryObject): boolean;
/**
* Returns true if the passed GeoJSON Geometry object is completely within this primitive.
* @param geometry GeoJSON geometry
*/
public within(geometry: GeoJSON.GeometryObject): boolean;
/**
* Returns true if the passed GeoJSON Geometry intersects this primitive.
* @param geometry GeoJSON geometry
*/
public intersects(geometry: GeoJSON.GeometryObject): boolean;
}
export class Point extends Primitive<GeoJSON.Point> implements GeoJSON.Point {
public type: "Point";
public coordinates: GeoJSON.Position;
constructor(p: GeoJSON.Point);
constructor(x: number, y: number);
constructor(xy: GeoJSON.Position);
}
export class MultiPoint extends Primitive<GeoJSON.MultiPoint> implements GeoJSON.MultiPoint {
public type: "MultiPoint";
public coordinates: GeoJSON.Position[];
public forEach: (point: Point, index: number, coordinates: Coordinates) => null;
constructor(geojsonMP: GeoJSON.MultiPoint);
constructor(coordinates: Coordinates);
public get(index: number): Point;
public addPoint(coordinate: Coordinate): this;
public insertPoint(coordinate: Coordinate, index: number): this;
public removePoint(index: number): this;
public removePoint(coordinate: Coordinate): this;
}
export class LineString extends Primitive<GeoJSON.LineString> implements GeoJSON.LineString {
public type: "LineString";
public coordinates: GeoJSON.Position[];
constructor(geoJson: GeoJSON.LineString);
constructor(coordinates: Coordinates);
public addVertex(coordinate: Coordinate): this;
public insertVertex(coordinate: Coordinate, index: number): this;
public removeVertex(index: number): this;
}
export class MultiLineString extends Primitive<GeoJSON.MultiLineString> implements GeoJSON.MultiLineString {
public type: "MultiLineString";
public coordinates: Coordinate[][];
public forEach: (linestring: LineString, index: number, coordinates: Coordinates) => null;
constructor(geoJson: GeoJSON.MultiLineString);
constructor(coordinates: Coordinates[]);
public get(index: number): LineString
}
export class Polygon extends Primitive<GeoJSON.Polygon> implements GeoJSON.Polygon {
public type: "Polygon";
public coordinates: Coordinate[][];
constructor(geoJson: GeoJSON.Polygon);
constructor(coordinates: Coordinates[])
public addVertex(coordinate: Coordinate): this;
public insertVertex(coordinate: Coordinate, index: number): this;
public removeVertex(index: number): this;
public close(): this;
public hasHoles(): boolean;
public holes(): Polygon[]
}
export class MultiPolygon extends Primitive<GeoJSON.MultiPolygon> implements GeoJSON.MultiPolygon {
public type: "MultiPolygon";
public coordinates: Coordinates[][];
public forEach: (polygon: Polygon, index: number, coordinates: Coordinates) => null;
constructor(geoJson: GeoJSON.Polygon);
constructor(geoJson: GeoJSON.MultiPolygon);
// // Valid, according to http://terraformer.io/core/#constructor-6
// constructor(geoJson:{
// type: "MultiPolygon",
// coordinates: number[][][]
// });
constructor(coordinates: Coordinates[]);
constructor(coordinates: Coordinates[][]);
public get(index: number): Polygon
}
export class Feature<T extends GeoJSON.GeometryObject> implements GeoJSON.Feature<T> {
public type: "Feature";
public geometry: T;
public properties: any;
constructor(geometry: T);
constructor(geoJson: GeoJSON.Feature<T>);
}
export class FeatureCollection<T extends GeoJSON.GeometryObject> implements GeoJSON.FeatureCollection<T> {
public type: "FeatureCollection";
public features: Array<GeoJSON.Feature<T>>;
public forEach: (feature: Feature<T>, index: number, coordinates: Coordinates) => null;
constructor(geoJson: GeoJSON.FeatureCollection<T>);
constructor(features: Array<GeoJSON.Feature<T>>);
public get(index: number): Feature<T>
}
export class GeometryCollection implements GeoJSON.GeometryCollection {
public type: "GeometryCollection";
public geometries: GeoJSON.GeometryObject[];
public forEach: (geometry: GeoJSON.GeometryObject, index: number, coordinates: Coordinates) => null;
constructor(geoJson: GeoJSON.GeometryCollection);
constructor(geoJson: GeoJSON.FeatureCollection<GeoJSON.GeometryObject>);
constructor(features: GeoJSON.GeometryObject[]);
public get(index: number): Primitive<GeoJSON.GeometryObject>
}
/**
* The GeoJSON spec does not provide a way to visualize circles.
* Terraformer.Circle is actual a GeoJSON Feature object that contains a Polygon representing a circle with a certain number of sides.
* @example
* circle = new Terraformer.Circle([-122.27, 45.65], 500, 64);
*
* circle.contains(point);
*/
export class Circle extends Primitive<GeoJSON.Feature<GeoJSON.Polygon>> implements GeoJSON.Feature<GeoJSON.Polygon> {
public type: "Feature";
public geometry: GeoJSON.Polygon;
public properties: any;
public steps: (steps?: number) => number;
/**
* Returns the radius circle. If the radius parameter is passed the circle will be recalculated witht he new radius before returning.
*/
public radius: (radius?: number) => number;
/**
* Returns the center of the circle. If the center parameter is passed the circle will be recalculated with the new center before returning.
*/
public center: (center?: Coordinate) => Coordinates;
/**
* Terraformer.Circle is created with a center, radius, and steps.
* @param [center=null] Required A GeoJSON Coordinate in [x,y] format.
* @param [radius=250] The radius of the circle in meters.
* @param [steps=32] How many steps will be used to create the polygon that represents the circle.
*/
constructor(center: Coordinate, radius?: number, steps?: number);
/**
* Recalculates the circle
*/
public recalculate(): this;
/**
* Returns the number of steps to produce the polygon representing the circle. If the steps parameter is passed the circle will be recalculated witht he new step count before returning.
*/
}
/**
* Terraformer also has numerous helper methods for working with GeoJSON and geographic data.
* These tools work with a mix of lower level GeoJSON constructs like Coordinates,
* Coordinate Arrays and GeoJSON objects and Terraformer Primitives
*/
export class Tools {
// Spatial Reference Conversions
/**
* Converts this GeoJSON object’s coordinates to the web mercator spatial reference. This is an in-place modification of the passed object.
* @param geojson GeoJSON object
*/
public static toMercator(geojson: GeoJSON.GeoJsonObject): GeoJSON.GeoJsonObject;
/**
* Converts this GeoJSON object’s coordinates to geographic coordinates. This is an in-place modification of the passed object.
* @param geojson GeoJSON object
*/
public static toGeographic(geojson: GeoJSON.GeoJsonObject): GeoJSON.GeoJsonObject;
/**
* Runs the passed function against every Coordinate in the geojson object. Your function will be passed a Coordinate and will be expected to return a Coordinate.
* @param geojson GeoJSON object
* @param converter Function to convert one coordinate to a different coordinate.
*/
public static applyConverter(geojson: GeoJSON.GeoJsonObject, converter: (coordinate: Coordinate) => Coordinate): GeoJSON.GeoJsonObject;
/**
* Converts the passed Coordinate to web mercator spatial reference.
* @param coordinate Coordinate
*/
public static positionToMercator(coordinate: Coordinate): Coordinate;
/**
* Converts the passed Coordinate to geographic coordinates.
* @param coordinate Coordinate to convert
*/
public static positionToGeographic(coordinate: Coordinate): Coordinate;
// Calculations
/**
* Returns a GeoJSON bounding box for the passed geoJSON.
* @param geojson
*/
public static calculateBounds(geojson: GeoJSON.GeoJsonObject): BBox;
/**
* Returns an object with x, y, w, h. Suitable for passing to most indexes.
* @param geojson
*/
public static calculateEnvelope(geojson: GeoJSON.GeoJsonObject): Envelope;
/**
* Returns an array of coordinates representing the convex hull the the passed geoJSON.
* @param geojson
*/
public static convexHull(geojson: Coordinates): Coordinates;
// Comparisons
/**
* Accepts an array of coordinates and a coordinate and returns true if the point falls within the coordinate array.
* @param coordinates Array of coordinates
* @param coordinate Coordinate that will be searched for in the array.
*/
public static coordinatesContainPoint(coordinates: Coordinates[], coordinate: Coordinate): Boolean;
/**
* Accepts the geometry of a polygon and a coordinate and returns true if the point falls within the polygon.
* @param polygon
* @param coordinate
*/
public static polygonContainsPoint(polygon: GeoJSON.Polygon, coordinate: Coordinate): Boolean;
/**
* Accepts the geometry of a polygon and a coordinate and returns true if the point falls within the polygon.
* @param polygon
* @param coordinate
*/
public static polygonContainsPoint(polygon: GeoJSON.Position[][], coordinate: Coordinate): Boolean;
/**
* Accepts two arrays of coordinates and returns true if they cross each other at any point.
* @param c1
* @param c2
* @example
* var pt = [0,0];
* var pt2 = [-111.873779, 40.647303];
*
* var polygon = {
* "type": "Polygon",
* "coordinates": [[
* [-112.074279, 40.52215],
* [-112.074279, 40.853293],
* [-111.610107, 40.853293],
* [-111.610107, 40.52215],
* [-112.074279, 40.52215]
* ]]
* };
*
* var polygonGeometry = polygon.coordinates;
*
* Terraformer.Tools.polygonContainsPoint(polygonGeometry, pt);
* // returns false
* Terraformer.Tools.polygonContainsPoint(polygonGeometry, pt2);
* // returns true
*/
public static arrayIntersectsArray(c1: Coordinates[], c2: Coordinates[]): Boolean;
/**
* Accepts two individual coordinate pairs and returns true if the passed coordinate pairs are equal to each other.
* @param c1
* @param c2
*/
public static coordinatesEqual(c1: Coordinate, c2: Coordinate): Boolean;
}
}