The GeoJSON.js is designed to build and validate GeoJSON feature collections.
To get started with the GeoJSON.js, install geojsonjs
package to your project.
npm i geojsonjs
yarn add geojsonjs
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);
Name | Returns | |
---|---|---|
parse |
FeatureCollection |
More info |
getGeometries |
Geometry[] |
More info |
getFeatures |
Feature[] |
More info |
getFeatureCollection |
FeatureCollection |
More info |
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);
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);
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);
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);
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 |
Supports all geometry types.
Example:
import { validate } from 'geojsonjs';
const result = validate(geom);
// TypeScript
import { validate, ValidationResult } from 'geojsonjs';
const result: ValidationResult = validate(geom);
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]);
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);
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);
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);
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);
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
);
- Geometry types
- Geometry item types
- Point
- MultiPoint
- LineString
- MultiLineString
- Polygon
- MultiPolygon
- Validation result
- Validation errors
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [11, 22]
}
}
]
}
[
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [11, 22]
}
}
]
},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [33, 44]
}
}
]
}
]
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [11, 22]
}
}
[
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [11, 22]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [33, 44]
}
}
]
{
"type": "Point",
"coordinates": [11, 22]
}
[
{
"type": "Point",
"coordinates": [11, 22]
},
{
"type": "Point",
"coordinates": [33, 44]
}
]
{
"type": "GeometryCollection",
"geometries": [
{ "type": "Point", "coordinates": [11, 22] },
{
"type": "LineString",
"coordinates": [
[11, 22],
[22, 33]
]
}
]
}
[
{
"type": "GeometryCollection",
"geometries": [{ "type": "Point", "coordinates": [11, 22] }]
},
{
"type": "GeometryCollection",
"geometries": [
{
"type": "LineString",
"coordinates": [
[11, 22],
[22, 33]
]
}
]
}
]
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
Valid example:
{
"valid": true
}
Invalid example:
{
"valid": false,
"error": "INVALID_TYPE",
"data": {
"type": "Pointt"
}
}
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
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.
This project is licensed under the MIT License.