Skip to content

Commit

Permalink
Separate data (resources) and polygons (features)
Browse files Browse the repository at this point in the history
(closes #7)
  • Loading branch information
bhousel committed Mar 13, 2018
1 parent 491307d commit ad9d827
Show file tree
Hide file tree
Showing 31 changed files with 512 additions and 1,169 deletions.
64 changes: 60 additions & 4 deletions README.md
Expand Up @@ -11,20 +11,76 @@ This project is a list of resources for local users of OpenStreetMap.
mailing lists, and so on. Anything that mappers, especially beginners,
might find interesting or helpful.


### About the index

Each community resource is defined in a `.geojson` file containing information
about the resource along with the bounding geometry where it applies.
The source files for this index are stored in two kinds of files:

#### Features

These are `*.geojson` files found under the `features/` folder.
Each feature file contains a single GeoJSON `Feature` for an area where a
resource is active.

Each feature must have a unique `id` property, for example `usa_full`.

```js
{
"type": "Feature",
"properties": {
"id": "usa_full"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
...
]
}
}
```

#### Resources

These are `*.json` files found under the `resources/` folder.
Each resource file contains a single JSON object with information about
the resource.

Each resource must have a unique `id` property.
The `featureId` property links the resource to a single feature.

```js
{
"id": "OSM-US-Slack",
"featureId": "usa_full",
"type": "slack",
"countryCode": "US",
"name": "OpenStreetMap US Slack",
"description": "Sign up at {url}",
"url": "https://osmus-slack.herokuapp.com/"
}
```


### Contributing

#### Prerequisites

* [Node.js](https://nodejs.org/) version 4 or newer
* [`git`](https://www.atlassian.com/git/tutorials/install-git/) for your platform

#### Installing

* Clone this project, for example:
`git clone git@github.com:osmlab/osm-community-index.git`
* `cd` into the project folder,
* Run `npm install` to install libraries

#### Building

* Just `npm run build`
* These files will be generated:
* `/dist/community.json`
* `/dist/community.min.json`
* `/dist/features.json`
* `/dist/features.min.json`
* `/dist/resources.json`
* `/dist/resources.min.json`

53 changes: 30 additions & 23 deletions build.js
@@ -1,13 +1,15 @@
/* eslint-disable no-console */
const colors = require('colors/safe');
const fs = require('fs');
const glob = require('glob');
const Validator = require('jsonschema').Validator;
const shell = require('shelljs');
const prettyStringify = require('json-stringify-pretty-compact');
const YAML = require('js-yaml');
const colors = require('colors/safe');

const geojsonSchema = require('./schema/geojson.json');
const resourceSchema = require('./schema/schema.json');
const featureSchema = require('./schema/feature.json');
const resourceSchema = require('./schema/resource.json');

var v = new Validator();
v.addSchema(geojsonSchema, 'http://json.schemastore.org/geojson.json');
Expand All @@ -23,47 +25,52 @@ function buildAll() {
shell.rm('-f', ['dist/*', 'i18n/en.yaml']);

var tstrings = {}; // translation strings
var features = generateFeatures();
var resources = generateResources(tstrings);

// Save individual data files
fs.writeFileSync(
'dist/community.json',
JSON.stringify({ communities: resources }, null, 4)
);

fs.writeFileSync(
'dist/community.min.json',
JSON.stringify({ communities: resources })
);

fs.writeFileSync(
'i18n/en.yaml',
YAML.safeDump({ en: tstrings }, { lineWidth: -1 })
);
fs.writeFileSync('dist/features.json', prettyStringify({ features: features }) );
fs.writeFileSync('dist/features.min.json', JSON.stringify({ features: features }) );
fs.writeFileSync('dist/resources.json', prettyStringify({ resources: resources }) );
fs.writeFileSync('dist/resources.min.json', JSON.stringify({ resources: resources }) );
fs.writeFileSync('i18n/en.yaml', YAML.safeDump({ en: tstrings }, { lineWidth: -1 }) );

console.timeEnd(colors.green('data built'));
}


function generateFeatures() {
var features = {};
glob.sync(__dirname + '/features/**/*.geojson').forEach(function(file) {
var feature = JSON.parse(fs.readFileSync(file, 'utf8'));
validateFile(file, feature, featureSchema);

var id = feature.properties.id;
features[id] = feature;
});

return features;
}

function generateResources(tstrings) {
var resources = {};
glob.sync(__dirname + '/sources/**/*.geojson').forEach(function(file) {
glob.sync(__dirname + '/resources/**/*.json').forEach(function(file) {
var resource = JSON.parse(fs.readFileSync(file, 'utf8'));
validateResource(file, resource);
validateFile(file, resource, resourceSchema);

var id = resource.properties.id;
var id = resource.id;
resources[id] = resource;
tstrings[id] = {
name: resource.properties.name,
description: resource.properties.description
name: resource.name,
description: resource.description
};
});

return resources;
}

function validateResource(file, resource) {
var validationErrors = v.validate(resource, resourceSchema).errors;
function validateFile(file, resource, schema) {
var validationErrors = v.validate(resource, schema).errors;
if (validationErrors.length) {
console.error(colors.red('Validation Error:'));
console.error(file + ': ');
Expand Down

0 comments on commit ad9d827

Please sign in to comment.