geojson-editor: a browser GeoJSON viewer, editor and RFC 7946 validator I built to learn
A small GeoJSON viewer, editor and validator built with MapLibre GL JS and terra-draw that runs fully in the browser and uploads nothing.
GeoJSON looks simple: it is JSON with coordinates in it. Then a file refuses to load somewhere, and you find out how many small rules sit behind that simplicity. A polygon ring that does not close. Latitude and longitude in the wrong order. A ring wound the wrong way. A crs member that the current specification no longer allows.
To learn those rules properly, I built the tool I would want when a file misbehaves. geojson-editor is a small browser-based GeoJSON viewer, editor and validator, made with MapLibre GL JS and terra-draw. Everything runs in the browser and nothing is uploaded.
It is a learning project. The interesting part is not the map; it is the pure logic underneath it and the decisions about who owns the data.
What it does
Load from anywhere. Pick a file, drag and drop one on the map, give it a URL (the server must allow CORS) or paste text. A bare Geometry or a single Feature is wrapped into a FeatureCollection, so you always work on the same shape of document.
Draw and edit. You can draw points, lines, polygons and rectangles. You can drag vertices, insert midpoints, move whole features, and delete with the Delete key.
A properties panel. Add, rename, edit and delete keys. Values are typed from what you write: 42 becomes a number, true a boolean, "42" a string and [1,2] an array. Key order is preserved, so the file you export does not come back shuffled.
Validation against RFC 7946. Errors cover ring closure, ring length, position shape, swapped lat/lon and bad feature members. Warnings cover winding order, crs, positions with more than three dimensions and odd GeometryCollections. Clicking an issue selects its feature, and a one-click "Fix winding" applies the right-hand rule.
Export. Download or copy the result, with optional coordinate rounding (precision) and compact output.
Autosave. The current document is saved in localStorage. If storage is blocked, the editor still works.
How to use it
There is nothing to install. The live demo is a static site on GitHub Pages, and your files stay in the browser: https://abdeldjalilchougui.github.io/geojson-editor/
To run it locally or host your own copy:
git clone https://github.com/AbdeldjalilChougui/geojson-editor.git
cd geojson-editor
pnpm install
pnpm dev # http://localhost:5173/geojson-editor/, then Paste or Open file (try public/sample.geojson)
pnpm test # vitest, pure logic only
pnpm build # type-check + production build into dist/
The production build uses the Vite base /geojson-editor/ (see vite.config.ts). Change it if you serve the site from another path.
A quick way to try the validator is to paste a polygon whose ring is not closed, or one with latitude first. The issue appears in the list, and clicking it selects the feature on the map.
What I built from scratch
The code is split in two on purpose. Everything in src/lib/ is pure, free of the DOM, and unit tested. src/main.ts is only glue: the MapLibre map, the terra-draw modes and the panels.
src/lib/ pure, DOM-free, unit tested
parse.ts text -> JSON -> FeatureCollection (friendly errors, never throws)
validate.ts structural RFC 7946 validator, issues with JSONPath-like paths
properties.ts value inference + immutable add/rename/set/delete
winding.ts signed area, orientation, rewind (exterior CCW, holes CW)
bbox.ts bounding boxes for fit-to-data
export.ts precision rounding (coordinates only) + serialisation
storage.ts injected key/value store with every access guarded
model.ts document items with stable uids, which geometries are editable
src/main.ts glue: MapLibre map, terra-draw modes, panels
A few of these files hold the real lessons:
parse.tsnever throws. Bad text gives a friendly error instead of an exception that kills the page.validate.tsreports each issue with a JSONPath-like path, which is what makes "click an issue to select the feature" possible.winding.tscomputes the signed area of a ring to find its orientation, then rewinds it so that exterior rings are counter-clockwise and holes are clockwise.export.tsrounds coordinates only. Your property values are never touched by the precision setting.storage.tstakes an injected key/value store and guards every access, which is why a blockedlocalStoragedoes not break the editor.
What I learned
Keep your own document as the source of truth. This was the main design decision. The editor keeps a list of {uid, feature} items. Features that terra-draw can edit (Point, LineString, single-ring Polygon) are mirrored into terra-draw under the same uid. The user's properties stay in the document, so terra-draw's reserved mode property never reaches the export.
If the drawing library owned the data, its internal fields would leak into the output, and anything it could not represent would be lost. With the document on my side, the drawing library is only a view with handles.
Not every geometry can be edited, and that is fine. Geometries terra-draw cannot edit (Multi* types, GeometryCollection, polygons with holes, or anything its validation rejects) are drawn read-only by a plain MapLibre layer. They stay in the document, so their properties can still be edited and they are exported unchanged. Losing a feature silently because the editor could not handle it would be the worst outcome for a tool like this.
RFC 7946 is stricter than most files. Writing the validator meant reading the specification line by line. The split between errors and warnings came out of that: a ring that does not close is wrong, while a clockwise exterior ring breaks the right-hand rule, but the specification tells parsers not to reject it. Swapped lat/lon is the kind of mistake that is easy to make and that a validator can catch for you.
Pure logic is the part worth testing. pnpm test runs vitest on the src/lib/ modules only. Because none of them touch the DOM, the tests need no browser. The map glue stays thin enough that there is little in it to get wrong.
Things to know
- Multi-geometries, GeometryCollections and polygons with holes are read-only on the map.
- There is no undo/redo yet.
- Validation is structural only. It does not detect self-intersections or holes outside the exterior ring.
- Basemap tiles come from tile.openstreetmap.org, which is fine for light personal use under the OSM tile usage policy. Change the style in
src/main.tsfor anything heavier. - The production bundle is about 1.2 MB (MapLibre plus terra-draw) and is not code-split.
- Loading from a URL only works when the remote server allows CORS.
- The sample file holds hand-drawn approximate shapes, not real data.
Why I built it
GeoJSON problems are usually small and annoying: one unclosed ring or one swapped coordinate pair in a file with hundreds of features. To see the problem you need three things together: the shape on a map, the properties, and a list of what breaks the specification. And when the file is not yours to share, you want a tool that keeps it in the browser.
Building that tool was also a direct way to learn RFC 7946 and to practise a clean split between pure logic and UI glue. That is the whole motivation: a small editor that explains why a file is wrong, written in a way that I could test.
Get the code
The source, the tests and the sample file are on GitHub: https://github.com/AbdeldjalilChougui/geojson-editor. The code is MIT licensed. Map data is © OpenStreetMap contributors (ODbL). Issues and pull requests are welcome.
Originally published at abdeldjalilchougui.biz.
Sumber artikel open-source oleh Abdeldjalil Chougui di DEV Community — Baca artikel asli. Topik:
#opensource#frontend#geospatial#typescript

Komentar