Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
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.
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);
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 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 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.
Convert an array of objects with geometry to a GeoJSON feature collection.
npm install geojson
var GeoJSON = require('geojson');
The library has one method, parse
, which takes an array of objects with geometry data as the first parameter, and an object consisting of settings for the second parameter.
Take the example data below:
var data = [
{
name: 'Location A',
category: 'Store',
street: 'Market',
lat: 39.984,
lng: -75.343
},
{
name: 'Location B',
category: 'House',
street: 'Broad',
lat: 39.284,
lng: -75.833
},
{
name: 'Location C',
category: 'Office',
street: 'South'
lat: 39.123,
lng: -74.534
}
];
Convert it to GeoJSON:
GeoJSON.parse(data, {Point: ['lat', 'lng']});
{ "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"
}
}
]
}
Convert the example data to GeoJSON, and only include the name
attribute in properties
for each feature.
GeoJSON.parse(data, {Point: ['lat', 'lng'], 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"
}
}
]
}
The parse
method can handle data with different geometry types. Consider the following sample data:
var data2 = [
{
x: 0.5,
y: 102.0,
prop0: 'value0'
},
{
line: [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]],
prop0: 'value0',
prop1: 0.0
},
{
polygon: [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
],
prop0: 'value0',
prop1: {"this": "that"}
}
];
For each geometry type, specify which attribute contains the geometric data
GeoJSON.parse(data2, {'Point': ['x', 'y'], 'LineString': 'line', 'Polygon': 'polygon'});
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102,0.5]
},
"properties": {
"prop0": "value0"
}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [[102, 0], [103, 1], [104, 0],[105, 1]]
},
"properties": {
"prop0": "value0",
"prop1": 0
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]
},
"properties": {
"prop0": "value0",
"prop1": {
"this": "that"
}
}
}
]
}
You can also specify default settings if you will be parsing mutliple datasets with similiar attributes.
var data1 = [{
name: 'Location A',
street: 'Market',
x: 34,
y: -75
}];
var data2 = [{
name: 'Location B',
date: '11/23/2012',
x: 54,
y: -98
}];
GeoJSON.defaults = {Point: ['x', 'y'], include: ['name']};
GeoJSON.parse(data1);
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-75, 34]
},
"properties": {
"name": "Location A"
}
}
]
}
GeoJSON.parse(data2);
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-98, 54]
},
"properties": {
"name": "Location B"
}
}
]
}
Depending on which makes more sense for the data that is being parsed, either specify an array of attributes to include or exclude in properties
for each feature. If neither include
nor exclude
is set, all the attributes (besides the attributes containing the geometry data) will be added to feature properties
.
include
- Array of attributes to include in properties
for each feature. All other fields will be ignored.exclude
- Array of attributes that shouldn't be included in feature properties
. All other attributes will be added (besides geometry attributes)The geometry parameters specify which attribute(s) contain(s) the geographic/geometric data. A geometry parameter must be specified for each type of geometry object that is present in the data that is being parsed. For example, if the data contains both points and polygons, specify both the Point
and Polygon
parameters. Note that geometry parameters must be in proper case. See the GeoJSON spec for details on each geometry type. The structure of the geometry parameter is:
ParameterName: 'attributeName'
Except for Point
, which can be specified with a field name or an array of field names, i.e:
data = [{
name: 'location',
x: 34,
y: 85
}];
GeoJSON.parse(data, {Point: ['lat', 'lng']});
data = [{
name: 'location',
coords: [85, 34]
}];
GeoJSON.parse(data, {Point: 'coords'});
The valid geometry types are Point
, MultiPoint
, LineString
, MultiLineString
, Polygon
, and MultiPolygon
.
FAQs
Turn your geo data into GeoJSON
We found that geojson demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.