jaxtrax

jaxtrax

GeoJSON and typescript

How to use typings for geojson objects from turf

This is a very gentle stroll through some of the types that turf has to offer for GeoJSON objects. In order to use turf you will of course need to install it in your application, you can then import it into your component in the usual way, for example

import {FeatureCollection, Properties} from "@turf/helpers";

GeoJSON files look like this

  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [102.0, 0.5]
      },
      "properties": {
        "prop0": "value0"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
        ]
      },
      "properties": {
        "prop0": "value0",
        "prop1": 0.0
      }
    }
  ]
}

This GeoJSON file is an object with two properties 'type', which identifies the type of the object as being a 'FeatureCollection' and 'features' which is an array of Feature type objects.

in turf there is a type named FeatureCollection, it looks like this

export interface FeatureCollection<G = Geometry | GeometryCollection, P = Properties> extends GeoJSONObject {
    type: "FeatureCollection";
    features: Array<Feature<G, P>>;
}

So this interface would be perfect for the GeoJSON file above.

We can also see that this interface has two properties 'type':'FeatureCollection', and 'features':Array>

If we look back to the example GeoJSON above we can see that features can hold two properties 'geometry', and 'properties', so the typing for G = Geometry and P = Properties covers this.

Here is the typing for a Feature, eg an item in the features array

export interface Feature<G = Geometry | GeometryCollection, P = Properties> extends GeoJSONObject {
    type: "Feature";
    geometry: G;
    id?: Id;
    properties: P;
}

You will notice that each of these interfaces extends the GeoJSONObject, let's take a look at that now

export interface GeoJSONObject {
    type: string;
    bbox?: BBox;
}

So this is a very simple interface with just one required property which specifies the type of GeoJSON object. You can see how both the 'Feature' and 'FeatureCollection' interfaces are able to build on this because they each contain a 'type' property.

Let's take a deeper dive into the Feature object, you will recall it is an item in the 'features' array , it has a type of Feature and two mandatory properties, 'geometry', and (confusingly!) 'properties'. Let's take a look at the 'properties' object. It is an object containing many properties, these can be of type number, string or object, eg nested values.

Here is the type Properties from turf

export declare type Properties = {
    [name: string]: any;
} | null;

here is an example of using this type in your codebase

let propertiesArray: Properties[] = [];

In this case we are initialising an empty array which we will push some properties to.

See - the turf interfaces are pretty easy really. :)

 
Share this