Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @yaga/generic-geojson with all npm packages installed. Try it out:

var genericGeojson = require("@yaga/generic-geojson")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@yaga/generic-geojson v1.0.0

Enhancement of the original GeoJSON type definition for TypeScript

Generic GeoJSON type definition

This is just a type definition and an enhancement of the original GeoJSON type definition for TypeScript.

You can use this definition to handle generic properties of features in GeoJSON.

How to use

Just install this definition with:

npm install --save @yaga/generic-geojson

and import into your TypeScript source:

import { GenericGeoJSONFeature, GenericGeoJSONFeatureCollection } from '@yaga/generic-geojson';

Background

The specification of GeoJSON allows every data type for the properties property. In lots of use-cases you want to specify the properties to a special (generic) one. With this definition you are able to do so.

Example

Every GeoJSON is compatible to the generic one with the any type:


import OriginalFeature = GeoJSON.Feature;
import GeometryObject = GeoJSON.GeometryObject;
import { GenericGeoJSONFeature } from '@yaga/generic-geojson';

let aFeature: OriginalFeature<GeometryObject> = {
    type: 'Feature',
    geometry: {
        type: 'Point',
        coordinates: [1, 2]
    },
    properties: {
        all: 'should',
        be: true
    }
};

// This is valid!
let aGenericFeatureWithAny: GenericGeoJSONFeature<GeometryObject, any> = aFeature;

The first generic have the same meaning like the one from the original GeoJSON definition. Additionally you are able to specify the properties more precise with the second generic:


import GeometryObject = GeoJSON.GeometryObject;
import { GenericGeoJSONFeature } from '@yaga/generic-geojson';

interface TestInterface {
    just: boolean;
    specific: string;
    properties?: number;
}

let aSpecifiedFeature: GenericGeoJSONFeature<GeometryObject, TestInterface> = {
    type: 'Feature',
    geometry: {
        type: 'Point',
        coordinates: [1, 2]
    },
    // properties must fulfill the TestInterface!
    properties: {
       just: true,
       specific: '',
       properties: 123
    }
};

The same for FeatureCollections:


import GeometryObject = GeoJSON.GeometryObject;
import { GenericGeoJSONFeature, GenericGeoJSONFeatureCollection } from '@yaga/generic-geojson';

interface TestInterface {
    just: boolean;
    specific: string;
    properties: number;
}

let aSpecifiedFeature: GenericGeoJSONFeature<GeometryObject, TestInterface> = {
    type: 'Feature',
    geometry: {
        type: 'Point',
        coordinates: [1, 2]
    },
    properties: {
       just: true,
       specific: '',
       properties: 123
    }
};

// Same meaning for FeatureCollections:
let aSpecificFeatureCollection: GenericGeoJSONFeatureCollection<GeometryObject, TestInterface> = {
    type: 'FeatureCollection',
    features: [ aSpecifiedFeature ]
}

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free