All files / lib server-info-route-handler.js

92.45% Statements 49/53
85.07% Branches 57/67
100% Functions 11/11
93.87% Lines 46/49

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 1649x 9x             9x 9x       9x 9x         8x   8x             8x 8x 8x   8x           8x   8x               4x                       8x 5x       5x 5x 3x     2x 2x     2x   2x 2x 2x 2x 2x 2x 2x     2x                             7x       4x                       11x   11x 11x                               2x   2x 2x                   8x 7x   7x   5x   5x 5x           9x  
const _ = require('lodash')
const { calculateBounds } = require('@terraformer/spatial')
const {
  getCollectionCrs,
  getGeometryTypeFromGeojson,
  normalizeExtent,
  normalizeSpatialReference,
  normalizeInputData
} = require('./helpers')
const { serverMetadata: serverMetadataDefaults } = require('./defaults')
const {
  CURRENT_VERSION,
  FULL_VERSION
} = require('./constants')
const debug = process.env.KOOP_LOG_LEVEL === 'debug' || process.env.LOG_LEVEL === 'debug'
 
function serverMetadata (json, req = {}) {
  const {
    query = {}
  } = req
 
  const { extent, metadata = {}, ...rest } = json
  const {
    maxRecordCount,
    hasStaticData,
    copyrightText,
    description: providerLayerDescription,
    serviceDescription: providerServiceDescription
  } = { ...metadata, ...rest }
  const spatialReference = getSpatialReference(json, query)
  const { layers, tables, relationships } = normalizeInputData(json)
  // TODO reproject default extents when non WGS84 CRS is found or passed
  const fullExtent = getServiceExtent({ extent, metadata, layers, spatialReference })
 
  const {
    currentVersion = CURRENT_VERSION,
    fullVersion = FULL_VERSION,
    serviceDescription
  } = _.get(req, 'app.locals.config.featureServer', {})
 
  return _.defaults({
    currentVersion,
    fullVersion,
    spatialReference,
    fullExtent,
    initialExtent: fullExtent,
    layers: layers.map(layerInfo),
    tables: tables.map((json, idx) => {
      return tableInfo(json, layers.length + idx)
    }),
    relationships: relationships.map(relationshipInfo),
    supportsRelationshipsResource: relationships && relationships.length > 0,
    serviceDescription: serviceDescription || providerServiceDescription || providerLayerDescription,
    copyrightText: copyrightText,
    maxRecordCount: maxRecordCount || _.get(layers, '[0].metadata.maxRecordCount'),
    hasStaticData: typeof hasStaticData === 'boolean' ? hasStaticData : false
  }, serverMetadataDefaults)
}
 
function getServiceExtent ({ extent, metadata = {}, layers, spatialReference = { wkid: 4326, latestWkid: 4326 } }) {
  if (extent || metadata.extent) return normalizeExtent(extent || metadata.extent, spatialReference)
  return calculateServiceExtentFromLayers(layers, spatialReference)
}
 
function calculateServiceExtentFromLayers (layers, spatialReference) {
  try {
    if (layers.length === 0) {
      return
    }
 
    const layerBounds = layers.filter(layer => {
      return _.has(layer, 'features[0]')
    }).map(calculateBounds)
 
    Iif (layerBounds.length === 0) return
 
    const { xmins, xmaxs, ymins, ymaxs } = layerBounds.reduce((accumulator, bounds) => {
      const [xmin, ymin, xmax, ymax] = bounds
      accumulator.xmins.push(xmin)
      accumulator.xmaxs.push(xmax)
      accumulator.ymins.push(ymin)
      accumulator.ymaxs.push(ymax)
      return accumulator
    }, { xmins: [], xmaxs: [], ymins: [], ymaxs: [] })
 
    return {
      xmin: Math.min(...xmins),
      xmax: Math.max(...xmaxs),
      ymin: Math.min(...ymins),
      ymax: Math.max(...ymaxs),
      spatialReference
    }
  } catch (error) {
    if (debug) {
      console.log(`Could not calculate extent from data: ${error.message}`)
    }
  }
}
 
function layerInfo (json = {}, defaultId) {
  return formatInfo(json, defaultId, 'layer')
}
 
function tableInfo (json = {}, defaultId) {
  return formatInfo(json, defaultId, 'table')
}
 
function formatInfo (json = {}, defaultId, type) {
  const {
    metadata: {
      id,
      name,
      minScale = 0,
      maxScale = 0,
      defaultVisibility
    } = {}
  } = json
 
  const defaultName = type === 'layer' ? `Layer_${id || defaultId}` : `Table_${id || defaultId}`
  return {
    id: id || defaultId,
    name: name || defaultName,
    parentLayerId: -1,
    defaultVisibility: defaultVisibility !== false,
    subLayerIds: null,
    minScale,
    maxScale,
    geometryType: type === 'layer' ? getGeometryTypeFromGeojson(json) : undefined
  }
}
 
function relationshipInfo (json = {}, relationshipIndex) {
  const {
    id,
    name
  } = json
 
  const defaultName = `Relationship_${id || relationshipIndex}`
  return {
    id: id || relationshipIndex,
    name: name || defaultName
  }
}
 
function getSpatialReference (collection, {
  inputCrs,
  sourceSR
}) {
  if (!inputCrs && !sourceSR && _.isEmpty(collection)) return
  const spatialReference = inputCrs || sourceSR || getCollectionCrs(collection)
 
  if (!spatialReference) return
 
  const { latestWkid, wkid, wkt } = normalizeSpatialReference(spatialReference)
 
  Eif (wkid) {
    return { wkid, latestWkid }
  }
 
  return { wkt }
}
 
module.exports = serverMetadata