All files / lib/helpers normalize-spatial-reference.js

97.91% Statements 47/48
85% Branches 34/40
100% Functions 8/8
100% Lines 45/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 1181x 1x 1x 1x 1x                     18x   17x   17x 1x 1x     1x     16x   16x 12x     4x         16x 8x           8x 1x           7x 3x           4x             16x       8x       1x 1x       6x   6x   1x 1x   1x     5x     5x 5x       4x   2x 2x 1x   1x 1x             2x 1x 1x 1x   1x  
const esriProjCodes = require('@esri/proj-codes')
const Joi = require('joi')
const wktParser = require('wkt-parser')
const wktLookup = new Map()
const schema = Joi.alternatives(
  Joi.string(),
  Joi.number().integer(),
  Joi.object({
    wkid: Joi.number().integer().optional(),
    latestWkid: Joi.number().integer().optional(),
    wkt: Joi.string().optional()
  }).unknown().or('wkid', 'latestWkid', 'wkt').required()
)
 
function normalizeSpatialReference (input) {
  if (!input) return { wkid: 4326, latestWkid: 4326 }
 
  const { error } = schema.validate(input)
 
  if (error) {
    Eif (process.env.NODE_ENV !== 'production') {
      console.log(`WARNING: ${input} is not a valid spatial reference; defaulting to none, error: ${error}`)
    }
    // Todo: throw error
    return { wkid: 4326, latestWkid: 4326 }
  }
 
  const { type, value } = parseSpatialReferenceInput(input)
 
  if (type === 'wkid') {
    return wktLookup.get(value) || esriWktLookup(value) || { wkid: 4326, latestWkid: 4326 }
  }
 
  return convertStringToSpatialReference(value) || { wkid: 4326, latestWkid: 4326 }
}
 
function parseSpatialReferenceInput (spatialReference) {
  // Search input for a wkid
  if (isNumericSpatialReferenceId(spatialReference)) {
    return {
      type: 'wkid',
      value: Number(spatialReference)
    }
  }
 
  if (isPrefixedSpatialReferenceId(spatialReference)) {
    return {
      type: 'wkid',
      value: extractPrefixedSpatialReferenceId(spatialReference)
    }
  }
 
  if (spatialReference.wkid || spatialReference.latestWkid) {
    return {
      type: 'wkid',
      value: spatialReference.wkid || spatialReference.latestWkid
    }
  }
 
  return {
    type: 'wkt',
    value: spatialReference.wkt || spatialReference
  }
}
 
function isNumericSpatialReferenceId (spatialReference) {
  return Number.isInteger(spatialReference) || Number.isInteger(Number(spatialReference))
}
 
function isPrefixedSpatialReferenceId (spatialReference) {
  return /[A-Z]+:/.test(spatialReference)
}
 
function extractPrefixedSpatialReferenceId (prefixedId) {
  const spatialRefId = prefixedId.match(/[A-Z]*:(.*)/)[1]
  return Number(spatialRefId)
}
 
function esriWktLookup (lookupValue) {
  const result = esriProjCodes.lookup(lookupValue)
 
  if (!result) {
    // Todo - throw error
    Eif (process.env.NODE_ENV !== 'production') {
      console.log(`WARNING: An unknown spatial reference was detected: ${lookupValue}; defaulting to none`)
    }
    return
  }
 
  const { wkid, latestWkid } = result
 
  // Add the WKT to the local lookup so we don't need to scan the Esri lookups next time
  wktLookup.set(wkid, { wkid, latestWkid })
  return { latestWkid, wkid }
}
 
function convertStringToSpatialReference (wkt) {
  if (/WGS_1984_Web_Mercator_Auxiliary_Sphere/.test(wkt)) return { wkid: 102100, latestWkid: 3857 }
 
  try {
    const wkid = getWktWkid(wkt)
    return wktLookup.get(wkid) || esriWktLookup(wkid) || { wkt }
  } catch (err) {
    Eif (process.env.NODE_ENV !== 'production') {
      console.log(`WARNING: An un-parseable WKT spatial reference was detected: ${wkt}`)
    }
    // Todo: throw error
  }
}
 
function getWktWkid (wkt) {
  const { AUTHORITY: authority } = wktParser(wkt)
  Iif (!authority) return
  const [, wkid] = Object.entries(authority)[0]
  return wkid
}
module.exports = normalizeSpatialReference