node-geojson v1.0.2
node-geojson
perform CRUD operations on geoJSON data using node
Installation
For node, use npm: $ npm install node-geojson
Getting Started
const geoJSON = require('node-geojson');
async function main(){
const geodata = await geoJSON.createUsingFile("path/to/geoJSON/file");
// Optional filePath to manipulate data in memory for front-end applications
// without filesystem
const inMemorygeodata = geoJSON.createFromObject(jsonObject);
}
Documentation
Instantiate
createUsingFile function reads a geoJSON file and returns a promise which resolves to an Object of FeatureCollection class.
createFromObject function takes a valid geoJSON Object along with an optional filePath where we may want to save the file, and returns an Object of FeatureCollection class.
FeatureCollection : class
Methods
Instance of FeatureCollection provide following methods to operate on the geojson.
Find
// Returns features that matches the Query q
FeatureCollection.Find(q?:Query)
// When called without any parameter, returns all features in the collection
FeatureCollection.Find()
FindByGeometry
// Return features having geometry
// specified in the parameter
const geometry = {type:"Point"}
FeatureCollection.FindByGeometry(geometry)
FindByProperty
// Return features having properties
// specified in the parameter
const property = {
name : "pengooX",
Id : 123
}
FeatureCollection.FindByProperty(property)
Update
const update = {
properties : {
color : red,
category : 'Quarantine zone'
}};
const query = {
properties : {
locality : "XYZ"
}
}
// Update features matching query
// by adding properties from update Object to each feature.
geodata.Update(update : Query, query : Query)
// Update all features in the FeatureCollection, when only update is passed
geodata.Update(update : Query)
Remove
// Remove features matching query
geodata.Remove(query : Query)
Methods automatically donot write the data to disk. To write data to disk call
Save
method, optionally passing the path where you want save the file. If no path is specified andFeatureCollection
was created usingcreateUsingFile
function, then data is written to the same file In case ofFeatureCollection
created usingcreateFromObject
, the filePath is mandatory if you want to callSave
else Save will fail
Save
//Save the file to the specified path
FeatureCollection.Save("path/where/i/want/to/save")
// for createUsingFile
// overwrite to same path from which it was created
FeatureCollection.Save()
GetAllFeatures
GetAllFeatures return all features in the collection
FeatureCollection.GetAllFeatures()
Interface
All functions take parameter of the following form satisfying the
Query
interfaceconst param = { geometry: { type: 'Point' }, properties: { name : "PengooX", color : ' red' // ... many more } }