aboutsummaryrefslogtreecommitdiff
path: root/lib/toOSM.js
blob: eb12c61d6c80b409aca9d5b0fe24d511b14f3945 (plain) (blame)
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
const { capitalCase } = require('capital-case')

const buildingUnitType = require('./buildingUnitType.js')
const roadSuffixMap = require('./roadSuffixMap.js')
const suburbMap = require('./suburbMap.js')

// likely these are not proper names, so we will ignore them
const emptyNames = [
  'UNNAMED',
  'NOT NAMED'
]

/**
 * Transforms a GeoJSON Feature from the Vicmap address schema into OSM schema
 *
 * @param {Array} sourceFeature Feature in Vicmap address schema
 * @param {Object} [options]
 * @param {string} [options.tracing=false]
 * @param {string} [options.includeUnitPrefix=false] include addr:unit:prefix to indicate unit type (UNIT, SHOP, SUITE, FLAT, etc.)
 * @param {string} [options.includeDerivableProperties=false] include properties addr:suburb, addr:postcode, addr:state which can be derived from existing boundaries on each address object
 * @returns {Object} Feature in OSM schema
 */
module.exports = (sourceFeature, options) => {

  const outputFeature = Object.assign({}, sourceFeature)
  const sourceProperties = sourceFeature.properties
  const outputProperties = {}

  if (options && options.tracing) {
    outputProperties['_pfi'] = sourceProperties.PFI
  }

  // Building unit
  // bld_unit_*
  const bld_unit_1 = [
      sourceProperties.BLG_UNIT_PREFIX_1,
      sourceProperties.BLG_UNIT_ID_1 || null, // 0 is used for an empty value in the source data, so convert 0 to null
      sourceProperties.BLG_UNIT_SUFFIX_1,
    ].join("") || null;

  const bld_unit_2 = [
      sourceProperties.BLG_UNIT_PREFIX_2,
      sourceProperties.BLG_UNIT_ID_2 || null, // 0 is used for an empty value in the source data, so convert 0 to null
      sourceProperties.BLG_UNIT_SUFFIX_2,
    ].join("") || null;

  // if both 1 and 2 defined, then use a range 1-2 otherwise just select the one which was defined
  let bld_unit = null
  if (sourceProperties.HSA_FLAG === 'Y') {
    bld_unit = sourceProperties.HSA_UNIT_ID
  } else {
    if (bld_unit_1 && bld_unit_2) {
      bld_unit = `${bld_unit_1}-${bld_unit_2}`
    } else if (bld_unit_1) {
      bld_unit = bld_unit_1
    } else if (bld_unit_2) {
      bld_unit = bld_unit_2
    }
  }

  if (bld_unit) {
    if (options && options.includeUnitPrefix) {
      // building unit type (Unit, Shop, Suite...)
      if (sourceProperties.BLG_UNIT_TYPE) {
        if (sourceProperties.BLG_UNIT_TYPE in buildingUnitType) {
          outputProperties['addr:unit:prefix'] = capitalCase(buildingUnitType[sourceProperties.BLG_UNIT_TYPE])
        } else {
          if (options && options.debug) {
            console.log(`Building Unity Type ${sourceProperties.BLG_UNIT_TYPE} not recognised for ${sourceFeature}`)
          }
        }
      }
    }

    outputProperties['addr:unit'] = bld_unit
  }

  // house number
  // house_*
  const house_1 = [
    sourceProperties.HOUSE_PREFIX_1,
    sourceProperties.HOUSE_NUMBER_1 || null, // 0 is used for an empty value in the source data, so convert 0 to null
    sourceProperties.HOUSE_SUFFIX_1,
  ].join("");

  const house_2 = [
    sourceProperties.HOUSE_PREFIX_2,
    sourceProperties.HOUSE_NUMBER_2 || null, // 0 is used for an empty value in the source data, so convert 0 to null
    sourceProperties.HOUSE_SUFFIX_2,
  ].join("");

  let housenumber = null
  if (house_1 && house_2) {
    housenumber = `${house_1}-${house_2}`
  } else if (house_1) {
    housenumber = house_1
  } else if (house_2) {
    housenumber = house_2
  }

  if (housenumber) {
    outputProperties['addr:housenumber'] = housenumber
  }

  // display numbers used predominately in the City of Melbourne CBD by large properties.
  // Primarily to simplify an assigned number range.
  // so should map the assigned address or the signposted address?

  // every record has at least ROAD_NAME populated
  if (sourceProperties.ROAD_NAME && !emptyNames.includes(sourceProperties.ROAD_NAME)) {
    outputProperties['addr:street'] = capitalCase([
      sourceProperties.ROAD_NAME,
      sourceProperties.ROAD_TYPE,
      sourceProperties.ROAD_SUFFIX in roadSuffixMap ? roadSuffixMap[sourceProperties.ROAD_SUFFIX] : sourceProperties.ROAD_SUFFIX
    ].join(' '))
  }

  if (options && options.includeDerivableProperties) {
    // every record has LOCALITY populated, however some values should be empty
    if (sourceProperties.LOCALITY_NAME && !emptyNames.includes(sourceProperties.LOCALITY_NAME)) {
      const suburb = capitalCase(sourceProperties.LOCALITY_NAME)

      // some special cases are defined in suburbMap
      outputProperties['addr:suburb'] = suburb in suburbMap ? suburbMap[suburb] : suburb
    }

    // every record has STATE populated
    if (sourceProperties.STATE) {
      outputProperties['addr:state'] = sourceProperties.STATE
    }

    // some records have no POSTCODE populated
    if (sourceProperties.POSTCODE) {
      outputProperties['addr:postcode'] = sourceProperties.POSTCODE
    }
  }

  outputFeature.properties = outputProperties
  return outputFeature
}