Skip to content

Commit

Permalink
handle CoverageJSON Domain documents & expose .type
Browse files Browse the repository at this point in the history
  • Loading branch information
letmaik committed Apr 5, 2016
1 parent 264415f commit 6352335
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "covjson-reader",
"version": "0.8.4",
"version": "0.9.0",
"license": "BSD-3-Clause",
"description": "A reader for CoverageJSON files.",
"repository": {
Expand Down
4 changes: 4 additions & 0 deletions src/Coverage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ndarray from 'ndarray'

import {COVERAGE} from './constants.js'
import {shallowcopy, minMax, assert, asTime,
indexOfNearest, indicesOfNearest, PREFIX} from './util.js'
import {isISODateAxis, isLongitudeAxis, getLongitudeWrapper} from './referencing.js'
Expand Down Expand Up @@ -28,6 +30,8 @@ export default class Coverage {
constructor (covjson, options) {
this._covjson = covjson

this.type = COVERAGE

/**
* JSON-LD document
*
Expand Down
3 changes: 3 additions & 0 deletions src/CoverageCollection.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {COVERAGECOLLECTION} from './constants.js'
import {default as Coverage, transformDomain, transformParameter} from './Coverage.js'
import {shallowcopy, asTime, PREFIX} from './util.js'
import {isISODateAxis, isLongitudeAxis, getLongitudeWrapper} from './referencing.js'
Expand All @@ -13,6 +14,8 @@ export default class CoverageCollection {
* @param {Object} covjson The CoverageJSON Collection document.
*/
constructor(covjson) {
this.type = COVERAGECOLLECTION

/**
* JSON-LD document
*
Expand Down
4 changes: 4 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const DOMAIN = 'Domain'
export const COVERAGE = 'Coverage'
export const COVERAGECOLLECTION = COVERAGE + 'Collection'
export const LINKRELPREFIX = 'http://www.iana.org/assignments/relation/'
39 changes: 21 additions & 18 deletions src/reader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Coverage from './Coverage.js'
import {transformDomain} from './Coverage.js'
import CoverageCollection from './CoverageCollection.js'
import {assert} from './util.js'
import {COVERAGE, COVERAGECOLLECTION, DOMAIN, LINKRELPREFIX} from './constants.js'

// NO FILE EXTENSION, to work around JSPM bug in handling package.json's "browser" field
// see https://github.com/jspm/jspm-cli/issues/1062#issuecomment-170342414
Expand All @@ -27,31 +29,30 @@ export function load (url, options) {

/**
* Reads a CoverageJSON document and returns a {@link Promise} that succeeds with
* a {@link Coverage} or {@link CoverageCollection} object.
* a Domain, {@link Coverage}, or {@link CoverageCollection} object.
*
* Note that if the document references external domain or range documents,
* then these are not loaded immediately.
*
*
* @example
* CovJSON.read('http://example.com/coverage.covjson').then(function (cov) {
* // work with Coverage object
* // work with Coverage data object
* }).catch(function (e) {
* // there was an error when loading the coverage
* // there was an error when loading the coverage data
* console.log(e)
* })
* @param {Object|string} input
* Either a URL pointing to a CoverageJSON Coverage or Coverage Collection document
* or a CoverageJSON Coverage or Coverage Collection object.
* A CoverageJSON Domain, Coverage, or Coverage Collection document, as URL or object,
* @param {Object} [options]
* An options object.
* @param {Object} [options.headers]
* Additional HTTP headers to send if input is a URL.
* @param {Object} [options.eagerload]
* Request a stand-alone CoverageJSON document (with domain and ranges embedded) if input is a URL.
* Note that the server may ignore that preference.
* Note that the server may ignore that preference.
* @return {Promise}
* A promise object succeeding with a {@link Coverage} or {@link CoverageCollection} object,
* A promise object succeeding with a Domain, {@link Coverage}, or {@link CoverageCollection} object,
* and failing with an {@link Error} object.
*/
export function read (input, options = {}) {
Expand All @@ -71,18 +72,23 @@ export function read (input, options = {}) {
*/
function transformCovJSON (obj, headers) {
checkValidCovJSON(obj)
if (obj.type !== 'Coverage' && obj.type !== 'CoverageCollection') {
throw new Error('CoverageJSON document must be of Coverage or CoverageCollection type')
if ([COVERAGE, COVERAGECOLLECTION, DOMAIN].indexOf(obj.type) === -1) {
throw new Error('CoverageJSON document must be of Coverage, CoverageCollection, or Domain type')
}

let result
if (obj.type === 'Coverage') {
if (obj.type === DOMAIN) {
transformDomain(obj)
result = obj
} else if (obj.type === COVERAGE) {
result = new Coverage(obj)
} else {
result = new CoverageCollection(obj)
}

addLinkRelations(result, headers)
if (obj.type === COVERAGE || obj.type === COVERAGECOLLECTION) {
addLinkRelations(result, headers)
}

return result
}
Expand All @@ -91,10 +97,7 @@ function transformCovJSON (obj, headers) {
* Scans the supplied HTTP headers for Link relations and adds them
* to the .ld property of the Coverage/CoverageCollection.
*/
function addLinkRelations (cov, headers) {
// for registered rel's
const IANAPrefix = 'http://www.iana.org/assignments/relation/'

function addLinkRelations (cov, headers) {
if (!headers || !headers['link']) {
return
}
Expand All @@ -113,7 +116,7 @@ function addLinkRelations (cov, headers) {
}
let rel = param.substring(relStart+5, param.length-1)
if (!rel.startsWith('http://') && !rel.startsWith('https://')) {
rel = IANAPrefix + rel
rel = LINKRELPREFIX + rel
}
if (ld[rel]) {
if (Array.isArray(ld[rel])) {
Expand All @@ -139,11 +142,11 @@ function addLinkRelations (cov, headers) {
*/
function checkValidCovJSON (obj) {
assert('type' in obj, '"type" missing')
if (obj.type === 'Coverage') {
if (obj.type === COVERAGE) {
assert('parameters' in obj, '"parameters" missing')
assert('domain' in obj, '"domain" missing')
assert('ranges' in obj, '"ranges" missing')
} else if (obj.type === 'CoverageCollection') {
} else if (obj.type === COVERAGECOLLECTION) {
assert(Array.isArray(obj.coverages), '"coverages" must be an array')
}
}
11 changes: 10 additions & 1 deletion test/Coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ import 'core-js/es6/map'
import assert from 'assert'

import {read} from '../lib/reader.js'
import {COVERAGE} from '../lib/constants.js'

import {FIXTURES} from './data.js'

describe('Coverage structure', () => {
it('should have loaded=true and type=Coverage', () => {
return read(FIXTURES.Profile()).then(cov => {
assert.equal(cov.loaded, true)
assert.equal(cov.type, COVERAGE)
})
})
})

describe('Coverage methods', () => {
describe('#subsetByIndex', () => {
it('should not modify the original coverage', () => {
return read(FIXTURES.Profile()).then(cov => {
assert.equal(cov.loaded, true)
return cov.subsetByIndex({z: 0}).then(subset => {
return Promise.all([cov.loadDomain(), cov.loadRange('PSAL')]).then(([domain,range]) => {
assert.strictEqual(domain.axes.size, 4)
Expand Down
13 changes: 13 additions & 0 deletions test/CoverageCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@ import 'core-js/es6/map'
import assert from 'assert'

import {read} from '../lib/reader.js'
import {COVERAGECOLLECTION} from '../lib/constants.js'

import {runServerIfNode} from './node-setup.js'
import {FIXTURES} from './data.js'

describe('CoverageCollection structure', () => {

runServerIfNode()

it('should have correct structure', () => {
return read(FIXTURES.CollectionURL).then(coll => {
assert.equal(coll.type, COVERAGECOLLECTION)
})
})

})

describe('CoverageCollection methods', () => {

runServerIfNode()
Expand Down
20 changes: 20 additions & 0 deletions test/Domain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// IE11 support
import 'core-js/es6/array'
import 'core-js/es6/promise'
import 'core-js/es6/symbol'
import 'core-js/es6/map'

import assert from 'assert'

import {read} from '../lib/reader.js'
import {DOMAIN} from '../lib/constants.js'

import {FIXTURES} from './data.js'

describe('Domain structure', () => {
it('should have correct structure', () => {
return read(FIXTURES.GridRegularDomain()).then(domain => {
assert.equal(domain.type, DOMAIN)
})
})
})
24 changes: 24 additions & 0 deletions test/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,30 @@ export const FIXTURES = {
}
}
}),
GridRegularDomain: () => ({
"type" : "Domain",
"profile" : "Grid",
"axes": {
"x": { "values": [-10,-5,0] },
"y": { "start": 40, "stop": 50, "num": 2 },
"z": { "values": [5] },
"t": { "values": ["2010-01-01T00:12:20Z"] }
},
"rangeAxisOrder": ["t","z","y","x"],
"referencing": [{
"components": ["y","x","z"],
"system": {
"type": "GeodeticCRS",
"id": "http://www.opengis.net/def/crs/EPSG/0/4979"
}
}, {
"components": ["t"],
"system": {
"type": "TemporalRS",
"calendar": "Gregorian"
}
}]
}),
CollectionEmpty: () => ({
"type" : "CoverageCollection",
"coverages": []
Expand Down

0 comments on commit 6352335

Please sign in to comment.