UNPKG

6.32 kBTypeScriptView Raw
1// Note: as of the RFC 7946 version of GeoJSON, Coordinate Reference Systems
2// are no longer supported. (See https://tools.ietf.org/html/rfc7946#appendix-B)}
3
4export as namespace GeoJSON;
5
6/**
7 * The valid values for the "type" property of GeoJSON geometry objects.
8 * https://tools.ietf.org/html/rfc7946#section-1.4
9 */
10export type GeoJsonGeometryTypes = Geometry["type"];
11
12/**
13 * The value values for the "type" property of GeoJSON Objects.
14 * https://tools.ietf.org/html/rfc7946#section-1.4
15 */
16export type GeoJsonTypes = GeoJSON["type"];
17
18/**
19 * Bounding box
20 * https://tools.ietf.org/html/rfc7946#section-5
21 */
22export type BBox = [number, number, number, number] | [number, number, number, number, number, number];
23
24/**
25 * A Position is an array of coordinates.
26 * https://tools.ietf.org/html/rfc7946#section-3.1.1
27 * Array should contain between two and three elements.
28 * The previous GeoJSON specification allowed more elements (e.g., which could be used to represent M values),
29 * but the current specification only allows X, Y, and (optionally) Z to be defined.
30 *
31 * Note: the type will not be narrowed down to `[number, number] | [number, number, number]` due to
32 * marginal benefits and the large impact of breaking change.
33 *
34 * See previous discussions on the type narrowing:
35 * - {@link https://github.com/DefinitelyTyped/DefinitelyTyped/pull/21590|Nov 2017}
36 * - {@link https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/67773|Dec 2023}
37 * - {@link https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/71441| Dec 2024}
38 *
39 * One can use a
40 * {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates|user-defined type guard that returns a type predicate}
41 * to determine if a position is a 2D or 3D position.
42 *
43 * @example
44 * import type { Position } from 'geojson';
45 *
46 * type StrictPosition = [x: number, y: number] | [x: number, y: number, z: number]
47 *
48 * function isStrictPosition(position: Position): position is StrictPosition {
49 * return position.length === 2 || position.length === 3
50 * };
51 *
52 * let position: Position = [-116.91, 45.54];
53 *
54 * let x: number;
55 * let y: number;
56 * let z: number | undefined;
57 *
58 * if (isStrictPosition(position)) {
59 * // `tsc` would throw an error if we tried to destructure a fourth parameter
60 * [x, y, z] = position;
61 * } else {
62 * throw new TypeError("Position is not a 2D or 3D point");
63 * }
64 */
65export type Position = number[];
66
67/**
68 * The base GeoJSON object.
69 * https://tools.ietf.org/html/rfc7946#section-3
70 * The GeoJSON specification also allows foreign members
71 * (https://tools.ietf.org/html/rfc7946#section-6.1)
72 * Developers should use "&" type in TypeScript or extend the interface
73 * to add these foreign members.
74 */
75export interface GeoJsonObject {
76 // Don't include foreign members directly into this type def.
77 // in order to preserve type safety.
78 // [key: string]: any;
79 /**
80 * Specifies the type of GeoJSON object.
81 */
82 type: GeoJsonTypes;
83 /**
84 * Bounding box of the coordinate range of the object's Geometries, Features, or Feature Collections.
85 * The value of the bbox member is an array of length 2*n where n is the number of dimensions
86 * represented in the contained geometries, with all axes of the most southwesterly point
87 * followed by all axes of the more northeasterly point.
88 * The axes order of a bbox follows the axes order of geometries.
89 * https://tools.ietf.org/html/rfc7946#section-5
90 */
91 bbox?: BBox | undefined;
92}
93
94/**
95 * Union of GeoJSON objects.
96 */
97export type GeoJSON<G extends Geometry | null = Geometry, P = GeoJsonProperties> =
98 | G
99 | Feature<G, P>
100 | FeatureCollection<G, P>;
101
102/**
103 * Geometry object.
104 * https://tools.ietf.org/html/rfc7946#section-3
105 */
106export type Geometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon | GeometryCollection;
107export type GeometryObject = Geometry;
108
109/**
110 * Point geometry object.
111 * https://tools.ietf.org/html/rfc7946#section-3.1.2
112 */
113export interface Point extends GeoJsonObject {
114 type: "Point";
115 coordinates: Position;
116}
117
118/**
119 * MultiPoint geometry object.
120 * https://tools.ietf.org/html/rfc7946#section-3.1.3
121 */
122export interface MultiPoint extends GeoJsonObject {
123 type: "MultiPoint";
124 coordinates: Position[];
125}
126
127/**
128 * LineString geometry object.
129 * https://tools.ietf.org/html/rfc7946#section-3.1.4
130 */
131export interface LineString extends GeoJsonObject {
132 type: "LineString";
133 coordinates: Position[];
134}
135
136/**
137 * MultiLineString geometry object.
138 * https://tools.ietf.org/html/rfc7946#section-3.1.5
139 */
140export interface MultiLineString extends GeoJsonObject {
141 type: "MultiLineString";
142 coordinates: Position[][];
143}
144
145/**
146 * Polygon geometry object.
147 * https://tools.ietf.org/html/rfc7946#section-3.1.6
148 */
149export interface Polygon extends GeoJsonObject {
150 type: "Polygon";
151 coordinates: Position[][];
152}
153
154/**
155 * MultiPolygon geometry object.
156 * https://tools.ietf.org/html/rfc7946#section-3.1.7
157 */
158export interface MultiPolygon extends GeoJsonObject {
159 type: "MultiPolygon";
160 coordinates: Position[][][];
161}
162
163/**
164 * Geometry Collection
165 * https://tools.ietf.org/html/rfc7946#section-3.1.8
166 */
167export interface GeometryCollection<G extends Geometry = Geometry> extends GeoJsonObject {
168 type: "GeometryCollection";
169 geometries: G[];
170}
171
172export type GeoJsonProperties = { [name: string]: any } | null;
173
174/**
175 * A feature object which contains a geometry and associated properties.
176 * https://tools.ietf.org/html/rfc7946#section-3.2
177 */
178export interface Feature<G extends Geometry | null = Geometry, P = GeoJsonProperties> extends GeoJsonObject {
179 type: "Feature";
180 /**
181 * The feature's geometry
182 */
183 geometry: G;
184 /**
185 * A value that uniquely identifies this feature in a
186 * https://tools.ietf.org/html/rfc7946#section-3.2.
187 */
188 id?: string | number | undefined;
189 /**
190 * Properties associated with this feature.
191 */
192 properties: P;
193}
194
195/**
196 * A collection of feature objects.
197 * https://tools.ietf.org/html/rfc7946#section-3.3
198 */
199export interface FeatureCollection<G extends Geometry | null = Geometry, P = GeoJsonProperties> extends GeoJsonObject {
200 type: "FeatureCollection";
201 features: Array<Feature<G, P>>;
202}