|
1 | 1 | import axios from 'axios'
|
2 | 2 | import { NextFunction, Request, Response } from 'express'
|
3 |
| -import * as get from 'lodash/get' |
| 3 | +import * as proj4 from 'proj4' |
| 4 | + |
| 5 | +proj4.defs([ |
| 6 | + ['WGS84', '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'], |
| 7 | + [ |
| 8 | + 'EPSG:27700', |
| 9 | + '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs', |
| 10 | + ], |
| 11 | +]) |
| 12 | + |
| 13 | +const projections = { |
| 14 | + standard: new proj4.Proj('WGS84'), |
| 15 | + ordnanceSurvey: new proj4.Proj('EPSG:27700'), |
| 16 | +} |
4 | 17 |
|
5 | 18 | export async function search(
|
6 | 19 | request: Request,
|
7 | 20 | response: Response,
|
8 | 21 | next: NextFunction
|
9 | 22 | ) {
|
10 |
| - const url = `https://api.ideal-postcodes.co.uk/v1/postcodes/${ |
11 |
| - request.params.postcode |
12 |
| - }?api_key=${process.env.IDEAL_POSTCODES_KEY}` |
| 23 | + // const postcode = await formatPostcode(request.params.postcode) |
13 | 24 |
|
14 | 25 | try {
|
| 26 | + const { data: pdata } = await axios.get( |
| 27 | + `https://api.postcodes.io/postcodes/${request.params.postcode}` |
| 28 | + ) |
| 29 | + |
| 30 | + const postcode = pdata.result.postcode |
| 31 | + const localAuthority = pdata.result.admin_district.toLowerCase() |
| 32 | + |
| 33 | + if (localAuthority !== 'southwark') { |
| 34 | + return response.json({ |
| 35 | + localAuthority, |
| 36 | + results: [], |
| 37 | + }) |
| 38 | + } |
| 39 | + |
| 40 | + const url = `https://llpg.planx.uk/LLPG_ALL?limit=100&POSTALLY_ADDRESSABLE=eq.Y&POSTCODE=eq.${postcode}` |
| 41 | + |
15 | 42 | const { data } = await axios.get(url)
|
16 | 43 |
|
17 | 44 | response.json({
|
18 |
| - localAuthority: get(data, 'result[0].district', 'unknown') |
19 |
| - .toLowerCase() |
20 |
| - .replace(' ', ''), |
21 |
| - results: data.result.map(result => { |
22 |
| - return { |
23 |
| - id: result.udprn.toString(), |
24 |
| - name: [result.line_1, result.line_2].filter(Boolean).join(', '), |
25 |
| - uprn: result.udprn.toString(), |
26 |
| - updrn: result.udprn.toString(), |
27 |
| - x: result.eastings, |
28 |
| - y: result.northings, |
29 |
| - lat: result.latitude, |
30 |
| - lng: result.longitude, |
31 |
| - rawData: result, |
32 |
| - } |
33 |
| - }), |
| 45 | + localAuthority, |
| 46 | + results: data |
| 47 | + .map(result => { |
| 48 | + const { x: lng, y: lat } = proj4.transform( |
| 49 | + projections.ordnanceSurvey, |
| 50 | + projections.standard, |
| 51 | + [Number(result.X), Number(result.Y)] |
| 52 | + ) |
| 53 | + |
| 54 | + return { |
| 55 | + lat, |
| 56 | + lng, |
| 57 | + id: result.UPRN.toString(), |
| 58 | + name: [ |
| 59 | + result.ORGANISATION, |
| 60 | + result.SAO, |
| 61 | + [result.PAO, result.STREET].filter(Boolean).join(' '), |
| 62 | + ] |
| 63 | + .filter(Boolean) |
| 64 | + .join(', '), |
| 65 | + uprn: result.UPRN.toString(), |
| 66 | + updrn: result.UPRN.toString(), |
| 67 | + x: Number(result.X), |
| 68 | + y: Number(result.Y), |
| 69 | + rawData: result, |
| 70 | + } |
| 71 | + }) |
| 72 | + .sort((a, b) => { |
| 73 | + const aa = [a.rawData.PAO, a.rawData.STREET].filter(Boolean).join(' ') |
| 74 | + const bb = [b.rawData.PAO, b.rawData.STREET].filter(Boolean).join(' ') |
| 75 | + // return bb < aa |
| 76 | + return aa < bb ? -1 : aa > bb ? 1 : 0 |
| 77 | + }), |
34 | 78 | })
|
35 | 79 | } catch (e) {
|
36 | 80 | next(e)
|
|
0 commit comments