mark-p-thomas : GeoJSON Hooks

Sections

Introduction

The GeoJSON library can hook into a GeoJSON object & update it remotely. This can be useful if the object is to be associated with other objects & calls. This allows those other uses (such as rendering in a Leaflet display) to benefit in a ‘lazy load’ style of GeoJSON updates. It is also an optimized & safe way to update only the portions of the raw JSON file that have changed, & only when a unit of work has been completed.

The object can be changed in the GeoJSON library without constantly affecting other references. Only when the save method is called will the pointer to the object actually update the original JSON, essentially completing an update transaction.

Procedure

In order to implement hooking, the highest level parent created in the GeoJSON library needs to be instantiated with the raw GeoJSON object that was read from a file or streamed from another source. This object & all child objects will contain a reference to the appropriate portion of the raw JSON, such that updates are optimized to only writing/replacing the relevant portions of the object & all children below.

In the case of newly instantiated object from provided coordinates, there is no raw JSON to update. However, toJson is then called, which serializes the objects into a new JSON object that is returned. This object is also hooked in to the current & all child objects. Returning of the serialized JSON allows a reference to be made from other sources so that updates can be reflected elsewhere.

toJSON only serializes the currently instantiated class into a new JSON object. Calling it alone does NOT update any hooked GeoJSON object or its children.