https://github.com/yagajs/generic-geojson
Enhancement of the original GeoJSON type definition for TypeScript
https://github.com/yagajs/generic-geojson
geojson typescript
Last synced: 6 days ago
JSON representation
Enhancement of the original GeoJSON type definition for TypeScript
- Host: GitHub
- URL: https://github.com/yagajs/generic-geojson
- Owner: yagajs
- License: isc
- Created: 2017-01-26T10:03:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-26T11:43:15.000Z (over 8 years ago)
- Last Synced: 2025-04-22T21:16:07.518Z (7 days ago)
- Topics: geojson, typescript
- Language: TypeScript
- Size: 2.93 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Generic GeoJSON type definition
This is just a type definition and an enhancement of the
[original GeoJSON type definition](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/geojson) for
[TypeScript](http://www.typescriptlang.org/).You can use this definition to handle [generic](https://www.typescriptlang.org/docs/handbook/generics.html)
[properties of features](http://geojson.org/geojson-spec.html#feature-objects) in [GeoJSON](http://geojson.org/).## How to use
Just install this definition with:
```bash
npm install --save @yaga/generic-geojson
```and import into your TypeScript source:
```typescript
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:
```typescript
import OriginalFeature = GeoJSON.Feature;
import GeometryObject = GeoJSON.GeometryObject;
import { GenericGeoJSONFeature } from '@yaga/generic-geojson';let aFeature: OriginalFeature = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [1, 2]
},
properties: {
all: 'should',
be: true
}
};// This is valid!
let aGenericFeatureWithAny: GenericGeoJSONFeature = 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:```typescript
import GeometryObject = GeoJSON.GeometryObject;
import { GenericGeoJSONFeature } from '@yaga/generic-geojson';interface TestInterface {
just: boolean;
specific: string;
properties?: number;
}let aSpecifiedFeature: GenericGeoJSONFeature = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [1, 2]
},
// properties must fulfill the TestInterface!
properties: {
just: true,
specific: '',
properties: 123
}
};```
The same for `FeatureCollection`s:
```typescript
import GeometryObject = GeoJSON.GeometryObject;
import { GenericGeoJSONFeature, GenericGeoJSONFeatureCollection } from '@yaga/generic-geojson';interface TestInterface {
just: boolean;
specific: string;
properties: number;
}let aSpecifiedFeature: GenericGeoJSONFeature = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [1, 2]
},
properties: {
just: true,
specific: '',
properties: 123
}
};// Same meaning for FeatureCollections:
let aSpecificFeatureCollection: GenericGeoJSONFeatureCollection = {
type: 'FeatureCollection',
features: [ aSpecifiedFeature ]
}```