1.0.2 • Published 3 years ago

node-geojson v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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 and FeatureCollection was created using createUsingFile function, then data is written to the same file In case of FeatureCollection created using createFromObject, the filePath is mandatory if you want to call Save 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 interface

const param = {
    geometry: {
        type: 'Point'
    },
    properties: {
        name : "PengooX",
        color : ' red'
        // ... many more
    }
}

Example Usage

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago