What is geojson?
The geojson npm package provides utilities for working with GeoJSON data, which is a format for encoding a variety of geographic data structures. It allows you to create, manipulate, and validate GeoJSON objects easily.
What are geojson's main functionalities?
Creating GeoJSON Objects
This feature allows you to create GeoJSON objects from an array of data points. The example demonstrates how to convert an array of locations with latitude and longitude into a GeoJSON object.
const geojson = require('geojson');
const data = [
{ name: 'Location 1', lat: 40.7128, lng: -74.0060 },
{ name: 'Location 2', lat: 34.0522, lng: -118.2437 }
];
const geojsonData = geojson.parse(data, { Point: ['lat', 'lng'] });
console.log(JSON.stringify(geojsonData, null, 2));
Validating GeoJSON Objects
This feature allows you to validate GeoJSON objects to ensure they conform to the GeoJSON specification. The example shows how to validate a GeoJSON object representing a point feature.
const geojson = require('geojson');
const geojsonData = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-74.0060, 40.7128]
},
properties: {
name: 'Location 1'
}
}
]
};
const isValid = geojson.isValid(geojsonData);
console.log(isValid);
Converting GeoJSON to Other Formats
This feature allows you to convert GeoJSON objects to other formats such as KML. The example demonstrates how to convert a GeoJSON object to KML format.
const geojson = require('geojson');
const geojsonData = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [-74.0060, 40.7128]
},
properties: {
name: 'Location 1'
}
}
]
};
const kml = geojson.toKML(geojsonData);
console.log(kml);
Other packages similar to geojson
terraformer
Terraformer is a JavaScript library for working with GeoJSON, including parsing, converting, and manipulating GeoJSON data. It offers more advanced spatial operations compared to geojson, such as buffering and union operations.
geojson-utils
GeoJSON Utils is a utility library for working with GeoJSON data. It provides functions for calculating distances, bounding boxes, and other spatial operations. It is more focused on spatial calculations compared to geojson.
turf
Turf is a powerful geospatial analysis library for JavaScript. It provides a wide range of spatial operations, including measurements, transformations, and data classification. Turf is more comprehensive and feature-rich compared to geojson.
node-geojson
Convert an array of objects with coordinates/geometry to a GeoJSON feature collection. Only works with point data at the moment.
Installation
npm install geojson
Getting Started
var GeoJSON = require('geojson');
Example Usage
Sample Data
var data = [
{
name: 'Location A',
category: 'Store',
lat: 39.984,
lng: -75.343
},
{
name: 'Location B',
category: 'House',
lat: 39.284,
lng: -75.833
},
{
name: 'Location C',
category: 'Office',
lat: 39.123,
lng: -74.534
}
];
Specify only coordinate parameters
GeoJSON.parse(data, { point: ['lng', 'lat'] });
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]},
"properties": {
"name": "Location A",
"category": "Store"
}
},
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [-75.833, 39.284]},
"properties": {
"name": "Location B",
"category": "House"
}
},
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]},
"properties": {
"name": "Location C",
"category": "Office"
}
}
]
}
Specify coordinate parameters and include
GeoJSON.parse(data, { point: ['lng', 'lat'], include: ['name']});
{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]},
"properties": {
"name": "Location A"
}
},
...
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]},
"properties": {
"name": "Location C"
}
}
]
}
Parameters
point
- array of field names that contain x and y coordinates. Example: point: [lat, lng]
include
- List of fields to include as properties. All other fields will be ignoredexclude
- List of fields to that shouldn't be included in properties. All other fields will be added (besides geometry fields)