@fboes/geojson
v1.4.1
Published
This dual-purpose library creates GeoJSON according to RFC 7946 in NodeJS as well as directly in your browser.
Downloads
512
Readme
GeoJSON
This dual-purpose library creates GeoJSON according to RFC 7946 in NodeJS as well as directly in your browser.
It contains all of the basic object types of GeoJSON geometries:
GeoJson.Point
andGeoJson.MultiPoint
GeoJson.LineString
andGeoJson.MultiLineString
GeoJson.Polygon
andGeoJson.MultiPolygon
GeoJson.GeometryCollection
…and organizational objects:
GeoJson.Feature
GeoJson.FeatureCollection
Why do we need classes to build JSON objects?
- It makes some of the data types of GeoJSON more accessible, as some types like coordinates are just ordered arrays. Now you have named parameters.
- Working with typed objects allows to see how the objects can be combined.
- Adding properties to
Feature
is improved.
Installation
Either download the dist/index.js
to a sensible location in your web project, or do a NPM installation:
npm install @fboes/geojson --save
Instead of a local installation you may also load the library from https://unpkg.com/. Beware: This makes https://unpkg.com/ a dependency of your project and may pose data protection issues.
<script type="module" src="https://unpkg.com/@fboes/geojson@latest/dist/index.js"></script>
Everything required for the functionality of this library is contained in dist/index.js
.
Usage
Loading the library prior to use:
// 1. NodeJS - NPM installation
import GeoJson from "geojson";
// 2. Local installation and/or browser usage
import GeoJson from "dist/index.js";
Now you are set to build your GeoJSON:
const featureCollection = new GeoJson.FeatureCollection([
new GeoJson.Feature(new GeoJson.Point(1.53946, 51.04571), {
title: "Sailing boat",
"marker-symbol": "harbor",
}),
new GeoJson.Feature(new GeoJson.Point(6.5664576, 58.109285), {
title: "Lighthouse",
"marker-symbol": "lighthouse",
}),
]);
console.log(JSON.stringify(featureCollection));
which yields GeoJSON:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [1.53946, 51.04571] },
"properties": { "title": "Sailing boat", "marker-symbol": "harbor" }
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [6.5664576, 58.109285] },
"properties": { "title": "Lighthouse", "marker-symbol": "lighthouse" }
}
]
}
Further readings
simplestyle-spec
: A simple specification for styling GeoJSON data.- Maki Icons: Maki is an icon set made for map designers. Maki includes icons for common points of interest like parks, museums, and places of worship.
- Mapbox GeoJSON integration: This example adds GeoJSON data from an external file and uses it in a layer on the map.
Status
Legal stuff
Author: Frank Boës
Copyright & license: See LICENSE.txt