Skip to content

Instantly share code, notes, and snippets.

@codediodeio
Last active November 9, 2021 12:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codediodeio/5cb4b2be7aae85dc06fb83aba1ada82f to your computer and use it in GitHub Desktop.
Save codediodeio/5cb4b2be7aae85dc06fb83aba1ada82f to your computer and use it in GitHub Desktop.
A Typescript interface for GeoJSON objects based on rfc7946
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?) {}
}
@MohamedRed
Copy link

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

@trevor-coleman
Copy link

You can use a union type, which will allow either a number[] or a number[][].

{
  // ...
  coordinates: number[] | number [][];
}

@OmarOmeiri
Copy link

OmarOmeiri commented Jun 10, 2021

You can use a union type, which will allow either a number[] or a number[][].

{
  // ...
  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