/geojsonjs

Build and validate GeoJSON

Primary LanguageTypeScriptMIT LicenseMIT

Build and validate GeoJSON with Node.js

License GitHub issues GitHub stars

Table of Contents

About the Project

The GeoJSON.js is designed to build and validate GeoJSON feature collections.

Getting Started

To get started with the GeoJSON.js, install geojsonjs package to your project.

npm i geojsonjs
yarn add geojsonjs

Usage

const geom = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      geometry: {
        type: 'Point',
        coordinates: [11, 22],
      },
    },
  ],
};

import { getFeatureCollection, validate } from 'geojsonjs';
const featureCollection = getFeatureCollection(geom);
const result = validate(featureCollection);

// TypeScript
import {
  getFeatureCollection,
  validate,
  FeatureCollection,
  ValidationResult,
} from 'geojsonjs';
const featureCollection: FeatureCollection = getFeatureCollection(geom);
const result: ValidationResult = validate(featureCollection);

Documentation

Global functions

Name Returns
parse FeatureCollection More info
getGeometries Geometry[] More info
getFeatures Feature[] More info
getFeatureCollection FeatureCollection More info

parse

Accepts array or object of type, coordinates and properties (optional).

Also you can parse GeometryCollection (object or array of objects). In that case instead of coordinates and properties it uses geometries.

Returns feature collection

Example:

import { parse } from 'geojsonjs';

const point1 = { type: 'Point', coordinates: [11, 22] };
const point2 = {
  type: 'Point',
  coordinates: [11, 22],
  properties: { prop1: 'prop1' },
};
const geometryCollection = {
  type: 'GeometryCollection',
  geometries: [{ type: 'Point', coordinates: [11, 22] }],
};

const featureCollection = parse(point1);
const featureCollection2 = parse([point1, point2]);
const featureCollection3 = parse(geometryCollection);

// TypeScript
import { parse, FeatureCollection } from 'geojsonjs';

const featureCollection: FeatureCollection = parse(point1);
const featureCollection2: FeatureCollection = parse([point1, point2]);
const featureCollection3: FeatureCollection = parse(geometryCollection);

getGeometries

Finds and returns all geometries from data.

Supports all geometry types.

Returns array of geometries

Example:

import { getGeometries } from 'geojson';
const geometries = getGeometries(geom);

// TypeScript
import { getGeometries, Geometry } from 'geojson';
const geometries: Geometry[] = getGeometries(geom);

getFeatures

Finds and returns all features from data.

Supports all geometry types.

Returns array of features

Example:

import { getFeatures } from 'geojson';
const features = getFeatures(geom);

// TypeScript
import { getFeatures, Feature } from 'geojson';
const features: Feature[] = getFeatures(geom);

getFeatureCollection

Finds and returns feature collection from data.

Supports all geometry types.

Returns feature collection

Example:

import { getFeatureCollection } from 'geojson';
const featureCollection = getFeatureCollection(geom);

// TypeScript
import { getFeatureCollection, FeatureCollection } from 'geojson';
const featureCollection: FeatureCollection = getFeatureCollection(geom);

Validation

Each validation returns Validation Result response

Name Params
validate geom: AllTypes More info
validateCoordinates type: string, coordinates: CoordinatesTypes More info
validateGeometry geometry: Geometry More info
validateFeature feature: Feature More info
validateFeatures features: Feature[] More info
validateFeatureCollection collection: FeatureCollection More info
validateGeometryTypes types: string | string [], geom: AllTypes More info

validate

Supports all geometry types.

Example:

import { validate } from 'geojsonjs';
const result = validate(geom);

// TypeScript
import { validate, ValidationResult } from 'geojsonjs';
const result: ValidationResult = validate(geom);

validateCoordinates

Accepts type and coordinates.

Example:

import { validateCoordinates } from 'geojsonjs';
const result = validateCoordinates('Point', [11, 12]);

// TypeScript
import { validateCoordinates, ValidationResult } from 'geojsonjs';
const result: ValidationResult = validateCoordinates('Point', [11, 12]);

validateGeometry

Accepts geometry

Example:

const geometry = { type: 'Point', coordinates: [11, 22] };

import { validateGeometry } from 'geojsonjs';
const result = validateGeometry(geometry);

// TypeScript
import { validateGeometry, ValidationResult } from 'geojsonjs';
const result: ValidationResult = validateGeometry(geometry);

validateFeature

Accepts feature

Example:

const feature = {
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [11, 22],
  },
};

import { validateFeature } from 'geojsonjs';
const result = validateFeature(feature);

// TypeScript
import { validateFeature, ValidationResult } from 'geojsonjs';
const result: ValidationResult = validateFeature(feature);

validateFeatures

Accepts features array

Example:

const features = [
  {
    type: 'Feature',
    geometry: {
      type: 'Point',
      coordinates: [11, 22],
    },
  },
];

import { validateFeatures } from 'geojsonjs';
const result = validateFeatures(features);

// TypeScript
import { validateFeatures, ValidationResult } from 'geojsonjs';
const result: ValidationResult = validateFeatures(features);

validateFeatureCollection

Accepts feature collection

Example:

const featureCollection = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      geometry: {
        type: 'Point',
        coordinates: [11, 22],
      },
    },
  ],
};

import { validateFeatureCollection } from 'geojsonjs';
const result = validateFeatureCollection(featureCollection);

// TypeScript
import { validateFeatureCollection, ValidationResult } from 'geojsonjs';
const result: ValidationResult = validateFeatureCollection(featureCollection);

validateGeometryTypes

Example: Accepts geometry type OR array of geometry types and feature collection

const featureCollection = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      geometry: {
        type: 'Point',
        coordinates: [11, 22],
      },
    },
  ],
};
import { GeometryType, validateGeometryTypes } from 'geojsonjs';
const result = validateGeometryTypes([GeometryType.POINT], featureCollection);

// TypeScript
import {
  GeometryType,
  validateGeometryTypes,
  ValidationResult,
} from 'geojsonjs';
const result: ValidationResult = validateGeometryTypes(
  GeometryType.POINT,
  featureCollection
);

Types

Feature Collection

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [11, 22]
      }
    }
  ]
}

Feature Collections (array)

[
  {
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "geometry": {
          "type": "Point",
          "coordinates": [11, 22]
        }
      }
    ]
  },
  {
    "type": "FeatureCollection",
    "features": [
      {
        "type": "Feature",
        "geometry": {
          "type": "Point",
          "coordinates": [33, 44]
        }
      }
    ]
  }
]

Feature

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [11, 22]
  }
}

Features (array)

[
  {
    "type": "Feature",
    "geometry": {
      "type": "Point",
      "coordinates": [11, 22]
    }
  },
  {
    "type": "Feature",
    "geometry": {
      "type": "Point",
      "coordinates": [33, 44]
    }
  }
]

Geometry

{
  "type": "Point",
  "coordinates": [11, 22]
}

Geometries (array)

[
  {
    "type": "Point",
    "coordinates": [11, 22]
  },
  {
    "type": "Point",
    "coordinates": [33, 44]
  }
]

Geometry Collection

{
  "type": "GeometryCollection",
  "geometries": [
    { "type": "Point", "coordinates": [11, 22] },
    {
      "type": "LineString",
      "coordinates": [
        [11, 22],
        [22, 33]
      ]
    }
  ]
}

Geometry Collections (array)

[
  {
    "type": "GeometryCollection",
    "geometries": [{ "type": "Point", "coordinates": [11, 22] }]
  },
  {
    "type": "GeometryCollection",
    "geometries": [
      {
        "type": "LineString",
        "coordinates": [
          [11, 22],
          [22, 33]
        ]
      }
    ]
  }
]

Geometry types

import { GeometryType } from 'geojsonjs';

GeometryType.POINT; // Point
GeometryType.MULTI_POINT; // MultiPoint
GeometryType.LINE_STRING; // LineString
GeometryType.MULTI_LINE_STRING; // MultiLineString
GeometryType.POLYGON; // Polygon
GeometryType.MULTI_POLYGON; // MultiPolygon

Validation Result˝

Valid example:

{
  "valid": true
}

Invalid example:

{
  "valid": false,
  "error": "INVALID_TYPE",
  "data": {
    "type": "Pointt"
  }
}

Validation Errors

import { ValidationError } from 'geojsonjs';

ValidationError.EMTPY; // EMTPY
ValidationError.EMPTY_FEATURES; // EMPTY_FEATURES
ValidationError.EMPTY_COORDINATES; // INVALID_COORDINATES
ValidationError.EMPTY_TYPE; // EMPTY_TYPE
ValidationError.INVALID_TYPE; // INVALID_TYPE
ValidationError.INVALID_FEATURES; // INVALID_FEATURES
ValidationError.INVALID_COORDINATES; // INVALID_COORDINATES
ValidationError.INVALID_PROPERTIES; // INVALID_PROPERTIES

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request. For more information, see the contribution guidelines.

License

This project is licensed under the MIT License.