Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an 'AS IS' BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 16 | import { |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 17 | Component, EventEmitter, |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 18 | Input, |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 19 | OnChanges, Output, |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 20 | SimpleChanges |
| 21 | } from '@angular/core'; |
| 22 | import { MapObject } from '../maputils'; |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 23 | import {LogService, MapBounds} from '../../../../gui2-fw-lib/public_api'; |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 24 | import {HttpClient} from '@angular/common/http'; |
| 25 | import * as d3 from 'd3'; |
| 26 | import * as topojson from 'topojson-client'; |
| 27 | |
| 28 | const BUNDLED_URL_PREFIX = 'data/map/'; |
| 29 | |
| 30 | /** |
| 31 | * Model of the transform attribute of a topojson file |
| 32 | */ |
| 33 | interface TopoDataTransform { |
| 34 | scale: number[]; |
| 35 | translate: number[]; |
| 36 | } |
| 37 | |
| 38 | /** |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 39 | * Model of the Feature returned prom topojson library |
| 40 | */ |
| 41 | interface Feature { |
| 42 | geometry: Object; |
| 43 | id: string; |
| 44 | properties: Object; |
| 45 | type: string; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Model of the Features Collection returned by the topojson.features function |
| 50 | */ |
| 51 | interface FeatureCollection { |
| 52 | type: string; |
| 53 | features: Feature[]; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Model of the topojson file |
| 58 | */ |
Sean Condon | 64060ff | 2019-05-30 15:48:11 +0100 | [diff] [blame] | 59 | export interface TopoData { |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 60 | type: string; // Usually "Topology" |
| 61 | objects: Object; // Can be a list of countries or individual countries |
| 62 | arcs: number[][][]; // Coordinates |
| 63 | bbox: number[]; // Bounding box |
| 64 | transform: TopoDataTransform; // scale and translate |
| 65 | } |
| 66 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 67 | @Component({ |
| 68 | selector: '[onos-mapsvg]', |
| 69 | templateUrl: './mapsvg.component.html', |
| 70 | styleUrls: ['./mapsvg.component.css'] |
| 71 | }) |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 72 | export class MapSvgComponent implements OnChanges { |
| 73 | @Input() map: MapObject = <MapObject>{id: 'none'}; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 74 | @Output() mapBounds = new EventEmitter<MapBounds>(); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 75 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 76 | geodata: FeatureCollection; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 77 | pathgen: any; // (Feature) => string; have to leave it general, as there is the bounds method used below |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 78 | // testPath: string; |
| 79 | // testFeature = <Feature>{ |
| 80 | // id: 'test', |
| 81 | // type: 'Feature', |
| 82 | // geometry: { |
| 83 | // coordinates: [ |
| 84 | // [[-15, 60], [45, 60], [45, 45], [-15, 45], [-15, 60]], |
| 85 | // [[-10, 55], [45, 55], [45, 50], [-10, 50], [-10, 55]], |
| 86 | // ], |
| 87 | // type: 'Polygon' |
| 88 | // }, |
| 89 | // properties: { name: 'Test'} |
| 90 | // }; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 91 | |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 92 | constructor( |
| 93 | private log: LogService, |
| 94 | private httpClient: HttpClient, |
| 95 | ) { |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 96 | // Scale everything to 360 degrees wide and 150 high |
| 97 | // See background.component.html for more details |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 98 | this.pathgen = d3.geoPath().projection(d3.geoIdentity().reflectY(true) |
| 99 | // MapSvgComponent.scale() |
| 100 | ); |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 101 | |
| 102 | // this.log.debug('Feature Test',this.testFeature); |
| 103 | // this.testPath = this.pathgen(this.testFeature); |
| 104 | // this.log.debug('Feature Path', this.testPath); |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 105 | this.log.debug('MapSvgComponent constructed'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 108 | static getUrl(id: string): string { |
| 109 | if (id && id[0] === '*') { |
| 110 | return BUNDLED_URL_PREFIX + id.slice(1) + '.topojson'; |
| 111 | } |
| 112 | return id + '.topojson'; |
| 113 | } |
| 114 | |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 115 | /** |
| 116 | * Wrapper for the path generator function |
| 117 | * @param feature The county or state within the map |
| 118 | */ |
| 119 | pathGenerator(feature: Feature): string { |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 120 | return this.pathgen(feature); |
| 121 | } |
| 122 | |
| 123 | ngOnChanges(changes: SimpleChanges): void { |
| 124 | this.log.debug('Change detected', changes); |
| 125 | if (changes['map']) { |
| 126 | const map: MapObject = <MapObject>(changes['map'].currentValue); |
| 127 | if (map.id) { |
| 128 | this.httpClient |
| 129 | .get(MapSvgComponent.getUrl(map.filePath)) |
| 130 | .subscribe((topoData: TopoData) => { |
| 131 | // this.mapPathGenerator = |
| 132 | this.handleTopoJson(map, topoData); |
| 133 | this.log.debug('Path Generated for', map.id, |
| 134 | 'from', MapSvgComponent.getUrl(map.filePath)); |
| 135 | }); |
| 136 | } |
| 137 | } |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Handle the topojson file stream as it arrives back from the server |
| 142 | * |
| 143 | * The topojson library converts the topojson file in to a FeatureCollection |
| 144 | * d3.geo then further converts this in to a Path |
| 145 | * |
| 146 | * @param map The Map chosen in the GUI |
| 147 | * @param topoData The data in the TopoJson file |
| 148 | */ |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 149 | handleTopoJson(map: MapObject, topoData: TopoData): void { |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 150 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 151 | let topoObject = topoData.objects[map.id]; |
| 152 | if (!topoObject) { |
| 153 | topoObject = topoData.objects['states']; |
| 154 | } |
| 155 | this.log.debug('Topo obj', topoObject, 'topodata', topoData); |
| 156 | this.geodata = <FeatureCollection>topojson.feature(topoData, topoObject); |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 157 | const bounds = this.pathgen.bounds(this.geodata); |
| 158 | this.mapBounds.emit(<MapBounds>{ |
| 159 | lngMin: bounds[0][0], |
| 160 | latMin: -bounds[0][1], // Y was inverted in the transform |
| 161 | lngMax: bounds[1][0], |
| 162 | latMax: -bounds[1][1] // Y was inverted in the transform |
| 163 | }); |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 164 | this.log.debug('Map retrieved', topoData, this.geodata); |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 165 | |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 166 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 167 | } |