Last active
November 9, 2021 12:54
-
-
Save codediodeio/5cb4b2be7aae85dc06fb83aba1ada82f to your computer and use it in GitHub Desktop.
A Typescript interface for GeoJSON objects based on rfc7946
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface IGeometry { | |
type: string; | |
coordinates: number[]; | |
} | |
export interface IGeoJson { | |
type: string; | |
geometry: IGeometry; | |
bbox?: number[]; | |
properties?: any; | |
} | |
export class GeoJson implements IGeoJson { | |
constructor(public type, public geometry, properties?, bbox?) {} | |
} |
You can use a union type, which will allow either a number[]
or a number[][]
.
{
// ...
coordinates: number[] | number [][];
}
You can use a union type, which will allow either a
number[]
or anumber[][]
.{ // ... coordinates: number[] | number [][]; }
Yes, but in case the feature type is MultiPolygon
then coordinates must be:
{
// ...
coordinates: [number, number][] | [number, number][][] | [number, number][][][];
}
and since the coordinates are always a two element tuple it is better to describe as [number, number]
instead of number[]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for your sharing your work.
I have a question in case we want to create a GeoJson of type 'LineString', the 'coordinates' property in the 'IGeometry' interface needs to be of type 'number[][]'.
So when we create a new GeoJson object using the constructor and provide the type 'LineString', Typescript will raise an error.
Do you know how to solve this problem ? Thanks