Skip to content

Instantly share code, notes, and snippets.

@dmfenton
Last active August 25, 2017 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmfenton/54036634ecc752a9a049413b18b0da53 to your computer and use it in GitHub Desktop.
Save dmfenton/54036634ecc752a9a049413b18b0da53 to your computer and use it in GitHub Desktop.
Koop Aggregator
const Controller = {
dataset (req, res) {
switch (req.method) {
case get:
return this.model.getDataset(req.params.id, handleResponse)
case: put:
return this.model.putDataset(req.params.id, handleResponse)
}
},
datasetSchema (req, res) {
switch (req.method) {
case get:
return this.model.getDatasetSchema(req.params, handleResponse)
case: put:
return this.model.putDatasetSchema(req.params, req.body, handleResponse)
}
}
}
function handleResponse (error, json) {
if (error) return res.status(error.code || 500).json({error: error.message})
else res.status(200).json(json)
}
function getData(req, callback) {
this.cache.get(req.params.id, (e, schema) => {
if (e) return callback(e)
// create geojson stub w/metadata
const queries = buildQueries(schema, req.query)
const features = []
// suggest async.each or async.queue
// fetch url
// translate features
// add to features array
callback(e, geojson)
})
}
function buildQueries (schema, query) {
return schema.schemas.maps(schema => {
const base = schema.url
const newQuery = translateQuery(schema.fields, query)
return `${base}?${newQuery}`
})
}
function tranlsateQuery(fields, query)
const provider = {}
provider.routes = [
{
path: '/nad/:id',
methods: ['get', 'put', 'delete'],
handler: 'dataset'
},
{
path: '/nad/:id/:schema',
methods: ['get', 'put', 'delete'],
handler: 'datasetSchema'
}
]
const esri = {
features: [
{
attributes: {},
geometry: {}
}
]
}
const geojson = {}
geojson.features = esri.features.map(f => {
return {
type: 'Feature',
geometry: f.geometry,
properties: f.attributes
}
})
Model = {
getDatasetSchema(params, callback) {
this.cache.get(params.id, (e, data) => {
callback(e, data)
})
},
putDatasetSchema(params, schema, callback) {
this.cache.get(params.id, (e, data) => {
if (e) return callback(e)
data.schemas[params.schema] = schema
this.cache.insert(params.id, data, e => {
callback(e, data)
})
}
}
getData(req, callback) {
/*
{
type: 'FeatureCollection' // Static
features: Array, // GeoJSON features
statistics: Object, // pass statistics to an outStatistics request to or else they will be calculated from geojson features passed in
metadata: {
name: String, // The name of the layer
description: String, // The description of the layer
extent: Array, // valid extent array e.g. [[180,90],[-180,-90]]
displayField: String, // The display field to be used by a client
geometryType: String // REQUIRED if no features are returned with this object Point || MultiPoint || LineString || MultiLineString || Polygon || MultiPolygon
idField: String, // unique identifier field,
maxRecordCount: Number, // the maximum number of features a provider can return at once
timeInfo: Object // describes the time extent and capabilities of the layer,
fields: [
{ // Subkeys are optional
name: String,
type: String, // 'Date' || 'Double' || 'Integer' || 'String'
alias: String, // how should clients display this field name,
}
]
},
filtersApplied: {
geometry: Boolean, // true if a geometric filter has already been applied to the data'
where: Boolean // true if a sql-like where filter has already been applied to the data
}
count: Number // pass count if the number of features in a query has been pre-calculated
}
*/
data.metadata = {
fields: [
{
name: 'address',
type: 'string'
}
]
}
data.filtersApplied = {
geometry: true,
where: true
}
callback(null, data)
}
}
#!/bin/bash
curl -XPUT -H 'content-type: application/json' koop.com/nad/addresses @fullDataset.json
curl -XPUT -H 'content-type: application/json' koop.com/nad/addresses/florida @oneSchema.json
GET /:provider/:id
DELETE /:provider/:id
PUT /:provider/:id Content-Type: application/json
{
name:
description:
schemas: {
georgia: {
},
texas: {
}
}
}
GET /:provider/:id/:schema
DELTE /:provider/:id/:schema
PUT /:provider/:id/:schema Content-Type: application/json
{
url: 'https://foobar.com/FeatureServer/0',
map: []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment