Perform CRUD operations on geoJSON data using node
For node, use npm: $ npm install node-geojson
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);
}
createUsingFile
function reads a geoJSON file and returns a promise which resolves to an Object ofFeatureCollection
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 ofFeatureCollection
class.
Instance of FeatureCollection provide following methods to operate on the geojson.
// Add a new feature of interface feature to features Array
FeatureCollection.Add(f)
// Returns features that matches the Query q
FeatureCollection.Find(q?:Query)
// When called without any parameter, returns all features in the collection
FeatureCollection.Find()
/*
Return features matching geometry
specified in the parameter
*/
const geometry = {type:"Point"}
FeatureCollection.FindByGeometry(geometry)
// Return features matching properties
// specified in the parameter
const property = {
name : "pengooX",
Id : 123
}
FeatureCollection.FindByProperty(property)
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.
FeatureCollection.Update(update : Query, query : Query)
// Update all features in the FeatureCollection, when only update is passed
FeatureCollection.Update(update : Query)
// Remove features matching query
FeatureCollection.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 to save the file. If no path is specified andFeatureCollection
was created usingcreateUsingFile
function, then data is overwritten to the same file In case ofFeatureCollection
created usingcreateFromObject
, the filePath is mandatory if you want to callSave
without path it will fail.
//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 return all features in the collection
FeatureCollection.GetAllFeatures()
All functions take parameter of the following form satisfying the
Query
interface
const param = {
geometry: {
type: 'Point'
},
properties: {
name : "PengooX",
color : ' red'
// ... many more
}
}