Skip to content

A predictable state container implementation for GeoJSON in Redux

License

Notifications You must be signed in to change notification settings

yagajs/geojson-redux

Repository files navigation

GeoJSON in states with Redux

Build Status Coverage Status FOSSA Status

YAGA's geojson-redux is a predictable state container implementation for GeoJSON in Redux.

How to use

First you have to install this library from npm:

npm install --save @yaga/geojson-redux
# OR
yarn isntall --save @yaga/geojson-redux # for those who prefer yarn...

This module works like a normal Redux module. You should do something like that:

import { createStore, combineReducers } from "redux"
import { geoJSONReducer } from "@yaga/geojson-redux";

const reducer = combineReducers({
    geojson: geoJSONReducer,
    // maybe: anotherReducer
});

const store = createStore(reducer);

If you want to use it in a simple way, just for GeoJSON and not in combination with other Redux reducers:

import { createGeoJSONStore } from "@yaga/geojson-redux";
const store = createGeoJSONStore();

Note: This module is developed in TypeScript. You can specify the Geometry and properties type

import { createGeoJSONStore } from "@yaga/geojson-redux";
import { Point } from "geojson";

interface IMyProperties {
    name: string;
}

const store = createGeoJSONStore<Point, IMyProperties>();

Working with this geojson-redux

After you created your store you can handle your store by actions:

store.dispatch(action);

You can perform the following action:

  • addFeature
  • removeFeature
  • changeGeometry
  • changeProperties

Add feature

const geoJSONFeature = {
    geometry: {
        type: "Point",
        coordinates: [
            // ...
        ],
    },
    properties: {
        // ...
    },
    type: "Feature",
};

const addAction = {
    type: "addFeature",
    feature: geoJSONFeature,
};

store.dispatch(addAction);

Note: every feature will get an autogenerated ID if you not specify one!

Remove feature

const removeAction = {
    type: "removeFeature",
    featureId: "feature-id",
};

store.dispatch(removeAction);

Change geometry of a feature

const geoJSONGeometry = {
    coordinates: [1,2],
    type: "Point",
};
const changeGeometryAction = {
    type: "changeGeometry",
    featureId: "feature-id",
    geometry: geoJSONGeometry,
};

store.dispatch(changeGeometryAction);

Change properties of a feature

const geoJSONProperties = {
    name: "just a test",
    test: "OK",
};
const changePropertiesAction = {
    type: "changeProperties",
    featureId: "feature-id",
    geometry: geoJSONProperties,
};

store.dispatch(changePropertiesAction);

License

This library is released under the ISC License.

FOSSA Status

About

A predictable state container implementation for GeoJSON in Redux

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published