diff --git a/types/geojson/geojson-tests.ts b/types/geojson/geojson-tests.ts index 4adb7f88f2..ce8c62418e 100644 --- a/types/geojson/geojson-tests.ts +++ b/types/geojson/geojson-tests.ts @@ -1,4 +1,10 @@ -let featureCollection: GeoJSON.FeatureCollection = { +import { + BBox, + Feature, FeatureCollection, GeometryCollection, LineString, + MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, GeometryObject +} from "geojson"; + +let featureCollection: FeatureCollection = { type: "FeatureCollection", features: [ { @@ -34,7 +40,7 @@ let featureCollection: GeoJSON.FeatureCollection = { geometry: { type: "Polygon", coordinates: [ - [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] + [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]] ] }, properties: { @@ -44,17 +50,10 @@ let featureCollection: GeoJSON.FeatureCollection = { } } } - ], - crs: { - type: "link", - properties: { - href: "http://example.com/crs/42", - type: "proj4" - } - } + ] }; -let featureWithPolygon: GeoJSON.Feature = { +const featureWithPolygon: Feature = { type: "Feature", bbox: [-180.0, -90.0, 180.0, 90.0], geometry: { @@ -66,57 +65,57 @@ let featureWithPolygon: GeoJSON.Feature = { properties: null }; -let point: GeoJSON.Point = { +const point: Point = { type: "Point", coordinates: [100.0, 0.0] }; // This type is commonly used in the turf package -let pointCoordinates: number[] = point.coordinates; +const pointCoordinates: number[] = point.coordinates; -let lineString: GeoJSON.LineString = { +const lineString: LineString = { type: "LineString", - coordinates: [ [100.0, 0.0], [101.0, 1.0] ] + coordinates: [[100.0, 0.0], [101.0, 1.0]] }; -let polygon: GeoJSON.Polygon = { +const polygon: Polygon = { type: "Polygon", coordinates: [ - [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] + [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]] ] }; -let polygonWithHole: GeoJSON.Polygon = { +const polygonWithHole: Polygon = { type: "Polygon", coordinates: [ - [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ], - [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ] + [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], + [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]] ] }; -let multiPoint: GeoJSON.MultiPoint = { +const multiPoint: MultiPoint = { type: "MultiPoint", - coordinates: [ [100.0, 0.0], [101.0, 1.0] ] + coordinates: [[100.0, 0.0], [101.0, 1.0]] }; -let multiLineString: GeoJSON.MultiLineString = { +const multiLineString: MultiLineString = { type: "MultiLineString", coordinates: [ - [ [100.0, 0.0], [101.0, 1.0] ], - [ [102.0, 2.0], [103.0, 3.0] ] + [[100.0, 0.0], [101.0, 1.0]], + [[102.0, 2.0], [103.0, 3.0]] ] }; -let multiPolygon: GeoJSON.MultiPolygon = { +const multiPolygon: MultiPolygon = { type: "MultiPolygon", coordinates: [ [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], - [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] + [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] ] }; -let geometryCollection: GeoJSON.GeometryCollection = { +const geometryCollection: GeometryCollection = { type: "GeometryCollection", geometries: [ { @@ -125,16 +124,17 @@ let geometryCollection: GeoJSON.GeometryCollection = { }, { type: "LineString", - coordinates: [ [101.0, 0.0], [102.0, 1.0] ] + coordinates: [[101.0, 0.0], [102.0, 1.0]] } ] }; -let feature: GeoJSON.Feature = { +let feature: Feature = { type: "Feature", geometry: lineString, properties: null }; + feature = { type: "Feature", geometry: polygon, @@ -172,44 +172,37 @@ featureCollection = { { type: "Feature", geometry: lineString, - properties: {test: "OK"} + properties: { test: "OK" } }, { type: "Feature", geometry: polygon, - properties: {test: "OK"} + properties: { test: "OK" } }, { type: "Feature", geometry: polygonWithHole, - properties: {test: "OK"} + properties: { test: "OK" } }, { type: "Feature", geometry: multiPoint, - properties: {test: "OK"} + properties: { test: "OK" } }, { type: "Feature", geometry: multiLineString, - properties: {test: "OK"} + properties: { test: "OK" } }, { type: "Feature", geometry: multiPolygon, - properties: {test: "OK"} + properties: { test: "OK" } }, { type: "Feature", geometry: geometryCollection, - properties: {test: "OK"} + properties: { test: "OK" } } - ], - crs: { - type: "link", - properties: { - href: "http://example.com/crs/42", - type: "proj4" - } - } + ] }; // Allow access to custom properties -const pt: GeoJSON.Feature = { +const pt: Feature = { type: "Feature", properties: { foo: "bar", @@ -221,9 +214,14 @@ const pt: GeoJSON.Feature = { coordinates: [0, 0] } }; -pt.properties.foo; -pt.properties.hello; -pt.properties[1]; + +if (pt.properties) { + if (pt.properties.foo == null || pt.properties.hello == null || pt.properties[1] == null) { + throw TypeError("Properties should not be null or undefined."); + } +} else { + throw TypeError("Feature should have a 'properties' property."); +} // Optional generic for properties @@ -232,19 +230,21 @@ interface TestProperty { hello: string; } -const typedPropertiesFeature: GeoJSON.Feature = { +const testProps: TestProperty = { + foo: "bar", + hello: "world" +}; + +const typedPropertiesFeature: Feature = { type: "Feature", - properties: { - foo: "bar", - hello: "world", - }, + properties: testProps, geometry: { type: "Point", coordinates: [0, 0] } }; -const typedPropertiesFeatureCollection: GeoJSON.FeatureCollection = { +const typedPropertiesFeatureCollection: FeatureCollection = { type: "FeatureCollection", features: [typedPropertiesFeature] }; diff --git a/types/geojson/index.d.ts b/types/geojson/index.d.ts index b916179fac..e6783255eb 100644 --- a/types/geojson/index.d.ts +++ b/types/geojson/index.d.ts @@ -1,123 +1,165 @@ -// Type definitions for GeoJSON Format Specification Revision 1.0 -// Project: http://geojson.org/ +// Type definitions for geojson 7946.0 +// Project: https://geojson.org/ // Definitions by: Jacob Bruun // Arne Schubert +// Jeff Jacobson // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 +// Note: as of the RFC 7946 version of GeoJSON, Coordinate Reference Systems +// are no longer supported. (See https://tools.ietf.org/html/rfc7946#appendix-B)} + export as namespace GeoJSON; -/*** - * http://geojson.org/geojson-spec.html#geojson-objects +/** + * The valid values for the "type" property of GeoJSON geometry objects. + * https://tools.ietf.org/html/rfc7946#section-1.4 + */ +export type GeoJsonGeometryTypes = "Point" | "LineString" | "MultiPoint" | "Polygon" | "MultiLineString" | + "MultiPolygon" | "GeometryCollection"; + +/** + * The value values for the "type" property of GeoJSON Objects. + * https://tools.ietf.org/html/rfc7946#section-1.4 + */ +export type GeoJsonTypes = "FeatureCollection" | "Feature" | GeoJsonGeometryTypes; + +/** + * Bounding box + * https://tools.ietf.org/html/rfc7946#section-5 + */ +export type BBox = [number, number, number, number] | [number, number, number, number, number, number]; + +/** + * A Position is an array of coordinates. + * https://tools.ietf.org/html/rfc7946#section-3.1.1 + * Array should contain between two and three elements. + * The previous GeoJSON specification allowed more elements (e.g., which could be used to represent M values), + * but the current specification only allows X, Y, and (optionally) Z to be defined. + */ +export type Position = number[]; // [number, number] | [number, number, number]; + +/** + * The base GeoJSON object. + * https://tools.ietf.org/html/rfc7946#section-3 + * The GeoJSON specification also allows foreign members + * (https://tools.ietf.org/html/rfc7946#section-6.1) + * Developers should use "&" type in TypeScript or extend the interface + * to add these foreign members. */ export interface GeoJsonObject { - type: string; - bbox?: number[]; - crs?: CoordinateReferenceSystem; + // Don't include foreign members directly into this type def. + // in order to preserve type safety. + // [key: string]: any; + /** + * Specifies the type of GeoJSON object. + */ + type: GeoJsonTypes; + /** + * Bounding box of the coordinate range of the object's Geometries, Features, or Feature Collections. + * https://tools.ietf.org/html/rfc7946#section-5 + */ + bbox?: BBox; } -/*** - * http://geojson.org/geojson-spec.html#positions - */ -export type Position = number[]; - -/*** - * http://geojson.org/geojson-spec.html#geometry-objects - */ -export interface DirectGeometryObject extends GeoJsonObject { - coordinates: Position[][][] | Position[][] | Position[] | Position; -} /** - * GeometryObject supports geometry collection as well + * A geometry object. + * https://tools.ietf.org/html/rfc7946#section-3 */ -export type GeometryObject = DirectGeometryObject | GeometryCollection; +export interface GeometryObject extends GeoJsonObject { + type: GeoJsonGeometryTypes; +} -/*** - * http://geojson.org/geojson-spec.html#point +/** + * Point geometry object. + * https://tools.ietf.org/html/rfc7946#section-3.1.2 */ -export interface Point extends DirectGeometryObject { +export interface Point extends GeometryObject { type: "Point"; coordinates: Position; } -/*** - * http://geojson.org/geojson-spec.html#multipoint +/** + * MultiPoint geometry object. + * https://tools.ietf.org/html/rfc7946#section-3.1.3 */ -export interface MultiPoint extends DirectGeometryObject { +export interface MultiPoint extends GeometryObject { type: "MultiPoint"; coordinates: Position[]; } -/*** - * http://geojson.org/geojson-spec.html#linestring +/** + * LineString geometry object. + * https://tools.ietf.org/html/rfc7946#section-3.1.4 */ -export interface LineString extends DirectGeometryObject { +export interface LineString extends GeometryObject { type: "LineString"; coordinates: Position[]; } -/*** - * http://geojson.org/geojson-spec.html#multilinestring +/** + * MultiLineString geometry object. + * https://tools.ietf.org/html/rfc7946#section-3.1.5 */ -export interface MultiLineString extends DirectGeometryObject { +export interface MultiLineString extends GeometryObject { type: "MultiLineString"; coordinates: Position[][]; } -/*** - * http://geojson.org/geojson-spec.html#polygon +/** + * Polygon geometry object. + * https://tools.ietf.org/html/rfc7946#section-3.1.6 */ -export interface Polygon extends DirectGeometryObject { +export interface Polygon extends GeometryObject { type: "Polygon"; coordinates: Position[][]; } -/*** - * http://geojson.org/geojson-spec.html#multipolygon +/** + * MultiPolygon geometry object. + * https://tools.ietf.org/html/rfc7946#section-3.1.7 */ -export interface MultiPolygon extends DirectGeometryObject { +export interface MultiPolygon extends GeometryObject { type: "MultiPolygon"; coordinates: Position[][][]; } -/*** - * http://geojson.org/geojson-spec.html#geometry-collection +/** + * Geometry Collection + * https://tools.ietf.org/html/rfc7946#section-3.1.8 */ -export interface GeometryCollection extends GeoJsonObject { +export interface GeometryCollection extends GeometryObject { type: "GeometryCollection"; - geometries: GeometryObject[]; + geometries: Array; } -/*** +export type GeoJsonProperties = { [name: string]: any; } | null; + +/** + * A feature object which contains a geometry and associated properties. * https://tools.ietf.org/html/rfc7946#section-3.2 */ -export interface Feature extends GeoJsonObject { +export interface Feature extends GeoJsonObject { type: "Feature"; - geometry: G; - properties: P; + /** + * The feature's geometry + */ + geometry: G | null; + /** + * A value that uniquely identifies this feature in a + * https://tools.ietf.org/html/rfc7946#section-3.2. + */ id?: string | number; + /** + * Properties associated with this feature. + */ + properties: P | null; } -/*** - * http://geojson.org/geojson-spec.html#feature-collection-objects +/** + * A collection of feature objects. + * https://tools.ietf.org/html/rfc7946#section-3.3 */ -export interface FeatureCollection extends GeoJsonObject { - type: "FeatureCollection"; +export interface FeatureCollection extends GeoJsonObject { features: Array>; } - -/*** - * http://geojson.org/geojson-spec.html#coordinate-reference-system-objects - */ -export interface CoordinateReferenceSystem { - type: string; - properties: any; -} - -export interface NamedCoordinateReferenceSystem extends CoordinateReferenceSystem { - properties: { name: string }; -} - -export interface LinkedCoordinateReferenceSystem extends CoordinateReferenceSystem { - properties: { href: string; type: string }; -}