Merge pull request #31908 from danvk/geojson-refactor

Refactor GeoJSON to make Geometry = GeometryObject
This commit is contained in:
Daniel Rosenwasser
2019-01-15 14:37:01 -08:00
committed by GitHub
2 changed files with 48 additions and 24 deletions
+30 -3
View File
@@ -1,10 +1,10 @@
import {
BBox,
Feature, FeatureCollection, GeometryCollection, LineString,
MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, GeometryObject
MultiLineString, MultiPoint, MultiPolygon, Point, Polygon, GeoJsonGeometryTypes,
GeoJsonTypes, GeometryObject
} from "geojson";
let featureCollection: FeatureCollection<Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon | GeometryCollection> = {
let featureCollection: FeatureCollection = {
type: "FeatureCollection",
features: [
{
@@ -53,6 +53,17 @@ let featureCollection: FeatureCollection<Point | LineString | Polygon | MultiPoi
]
};
featureCollection.type; // $ExpectType "FeatureCollection"
featureCollection.features[0].type; // $ExpectType "Feature"
featureCollection.features[0].geometry; // $ExpectType Geometry
featureCollection.features[0].geometry.type; // $ExpectType "Point" | "MultiPoint" | "LineString" | "MultiLineString" | "Polygon" | "MultiPolygon" | "GeometryCollection"
declare let geometryTypes: GeoJsonGeometryTypes;
geometryTypes; // $ExpectType "Point" | "MultiPoint" | "LineString" | "MultiLineString" | "Polygon" | "MultiPolygon" | "GeometryCollection"
declare let geojsonTypes: GeoJsonTypes;
geojsonTypes; // $ExpectType "FeatureCollection" | "Feature" | "Point" | "MultiPoint" | "LineString" | "MultiLineString" | "Polygon" | "MultiPolygon" | "GeometryCollection"
const featureWithPolygon: Feature<Polygon> = {
type: "Feature",
bbox: [-180.0, -90.0, 180.0, 90.0],
@@ -65,6 +76,11 @@ const featureWithPolygon: Feature<Polygon> = {
properties: null
};
featureWithPolygon.type; // $ExpectType "Feature"
featureWithPolygon.geometry; // $ExpectType Polygon
featureWithPolygon.geometry.type; // $ExpectType "Polygon"
featureWithPolygon.geometry.coordinates; // $ExpectType number[][][]
const point: Point = {
type: "Point",
coordinates: [100.0, 0.0]
@@ -135,6 +151,8 @@ let feature: Feature<GeometryObject> = {
properties: null
};
feature.properties; // $ExpectType GeoJsonProperties
feature = {
type: "Feature",
geometry: polygon,
@@ -275,12 +293,16 @@ const featureGeometryNull: Feature<null, TestProperty> = {
geometry: null
};
featureGeometryNull.properties.foo; // $ExpectType "bar" | "baz"
const featureNoNull: Feature<Point, TestProperty> = {
type: "Feature",
properties: testProps,
geometry: point
};
featureNoNull.geometry.type; // $ExpectType "Point"
const collectionAllNull: FeatureCollection<null, null> = {
type: "FeatureCollection",
features: [featureAllNull],
@@ -301,6 +323,8 @@ const collectionGeometryMaybeNull: FeatureCollection<Point | null, TestProperty>
features: [featureGeometryNull, featureNoNull],
};
collectionGeometryMaybeNull.features[0].geometry; // $ExpectType Point | null
const collectionNoNull: FeatureCollection<Point, TestProperty> = {
type: "FeatureCollection",
features: [featureNoNull],
@@ -311,6 +335,9 @@ const collectionDefault: FeatureCollection = {
features: []
};
collectionDefault.features[0].geometry; // $ExpectType Geometry
collectionDefault.features[0].properties!.foo; // $ExpectType any
isNull = featureAllNull.geometry;
isPoint = featurePropertyNull.geometry;
isNull = featureAllNull.properties;
+18 -21
View File
@@ -4,6 +4,7 @@
// Arne Schubert <https://github.com/atd-schubert>
// Jeff Jacobson <https://github.com/JeffJacobson>
// Ilia Choly <https://github.com/icholy>
// Dan Vanderkam <https://github.com/danvk>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -16,14 +17,13 @@ export as namespace GeoJSON;
* 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";
export type GeoJsonGeometryTypes = Geometry['type'];
/**
* 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;
export type GeoJsonTypes = GeoJSON['type'];
/**
* Bounding box
@@ -58,6 +58,10 @@ export interface GeoJsonObject {
type: GeoJsonTypes;
/**
* Bounding box of the coordinate range of the object's Geometries, Features, or Feature Collections.
* The value of the bbox member is an array of length 2*n where n is the number of dimensions
* represented in the contained geometries, with all axes of the most southwesterly point
* followed by all axes of the more northeasterly point.
* The axes order of a bbox follows the axes order of geometries.
* https://tools.ietf.org/html/rfc7946#section-5
*/
bbox?: BBox;
@@ -69,24 +73,17 @@ export interface GeoJsonObject {
export type GeoJSON = Geometry | Feature | FeatureCollection;
/**
* A geometry object.
* https://tools.ietf.org/html/rfc7946#section-3
*/
export interface GeometryObject extends GeoJsonObject {
type: GeoJsonGeometryTypes;
}
/**
* Union of geometry objects.
* Geometry object.
* https://tools.ietf.org/html/rfc7946#section-3
*/
export type Geometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon | GeometryCollection;
export type GeometryObject = Geometry;
/**
* Point geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.2
*/
export interface Point extends GeometryObject {
export interface Point extends GeoJsonObject {
type: "Point";
coordinates: Position;
}
@@ -95,7 +92,7 @@ export interface Point extends GeometryObject {
* MultiPoint geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.3
*/
export interface MultiPoint extends GeometryObject {
export interface MultiPoint extends GeoJsonObject {
type: "MultiPoint";
coordinates: Position[];
}
@@ -104,7 +101,7 @@ export interface MultiPoint extends GeometryObject {
* LineString geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.4
*/
export interface LineString extends GeometryObject {
export interface LineString extends GeoJsonObject {
type: "LineString";
coordinates: Position[];
}
@@ -113,7 +110,7 @@ export interface LineString extends GeometryObject {
* MultiLineString geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.5
*/
export interface MultiLineString extends GeometryObject {
export interface MultiLineString extends GeoJsonObject {
type: "MultiLineString";
coordinates: Position[][];
}
@@ -122,7 +119,7 @@ export interface MultiLineString extends GeometryObject {
* Polygon geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.6
*/
export interface Polygon extends GeometryObject {
export interface Polygon extends GeoJsonObject {
type: "Polygon";
coordinates: Position[][];
}
@@ -131,7 +128,7 @@ export interface Polygon extends GeometryObject {
* MultiPolygon geometry object.
* https://tools.ietf.org/html/rfc7946#section-3.1.7
*/
export interface MultiPolygon extends GeometryObject {
export interface MultiPolygon extends GeoJsonObject {
type: "MultiPolygon";
coordinates: Position[][][];
}
@@ -140,7 +137,7 @@ export interface MultiPolygon extends GeometryObject {
* Geometry Collection
* https://tools.ietf.org/html/rfc7946#section-3.1.8
*/
export interface GeometryCollection extends GeometryObject {
export interface GeometryCollection extends GeoJsonObject {
type: "GeometryCollection";
geometries: Geometry[];
}
@@ -151,7 +148,7 @@ 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<G extends GeometryObject | null = Geometry, P = GeoJsonProperties> extends GeoJsonObject {
export interface Feature<G extends Geometry | null = Geometry, P = GeoJsonProperties> extends GeoJsonObject {
type: "Feature";
/**
* The feature's geometry
@@ -172,7 +169,7 @@ export interface Feature<G extends GeometryObject | null = Geometry, P = GeoJson
* A collection of feature objects.
* https://tools.ietf.org/html/rfc7946#section-3.3
*/
export interface FeatureCollection<G extends GeometryObject | null = Geometry, P = GeoJsonProperties> extends GeoJsonObject {
export interface FeatureCollection<G extends Geometry | null = Geometry, P = GeoJsonProperties> extends GeoJsonObject {
type: "FeatureCollection";
features: Array<Feature<G, P>>;
}