Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- SVG -- Map Service |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 19 | */ |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 20 | |
| 21 | /* |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 22 | The Map Service provides a simple API for loading geographical maps into |
| 23 | an SVG layer. For example, as a background to the Topology View. |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 24 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 25 | e.g. var promise = MapService.loadMapInto(svgLayer, '*continental-us'); |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 26 | |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 27 | The Map Service makes use of the GeoDataService to load the required data |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 28 | from the server and to create the appropriate geographical projection. |
| 29 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 30 | A promise is returned to the caller, which is resolved with the |
| 31 | map projection once created. |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 32 | */ |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 33 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 34 | (function () { |
| 35 | 'use strict'; |
| 36 | |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 37 | // injected references |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 38 | var $log, $q, fs, gds; |
| 39 | |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 40 | // NOTE: This method assumes the datafile has exactly the map data |
| 41 | // that you want to load; for example id="*continental_us" |
| 42 | // mapping to ~/data/map/continental_us.topojson contains |
| 43 | // exactly the paths for the continental US. |
| 44 | |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 45 | function loadMapInto(mapLayer, mapPath, id, opts) { |
| 46 | var promise = gds.fetchTopoData(mapPath), |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 47 | deferredProjection = $q.defer(); |
| 48 | |
| 49 | if (!promise) { |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 50 | $log.warn('Failed to load map: ' + mapPath); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
| 54 | promise.then(function () { |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 55 | |
| 56 | // NOTE: This finds the topo object within the topojson file |
| 57 | var topoObjects = promise.topodata.objects; |
| 58 | |
| 59 | if (topoObjects.hasOwnProperty(id)) { |
| 60 | opts.objectTag = id; |
| 61 | } |
| 62 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 63 | var gen = gds.createPathGenerator(promise.topodata, opts); |
| 64 | |
| 65 | deferredProjection.resolve(gen.settings.projection); |
| 66 | |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 67 | mapLayer.selectAll('path') |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 68 | .data(gen.geodata.features) |
| 69 | .enter() |
| 70 | .append('path') |
| 71 | .attr('d', gen.pathgen); |
Simon Hunt | facad99 | 2016-02-25 09:58:33 -0800 | [diff] [blame] | 72 | |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 73 | reshade(opts.shading); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 74 | }); |
| 75 | return deferredProjection.promise; |
| 76 | } |
| 77 | |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 78 | // --- |
| 79 | |
| 80 | // NOTE: This method uses the countries.topojson data file, and then |
| 81 | // filters the results based on the supplied options. |
| 82 | // Usage: |
| 83 | // promise = loadMapRegionInto(svgGroup, { |
| 84 | // countryFilter: function (country) { |
| 85 | // return country.properties.continent === 'South America'; |
| 86 | // } |
| 87 | // }); |
| 88 | |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 89 | function loadMapRegionInto(mapLayer, opts) { |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 90 | var promise = gds.fetchTopoData("*countries"), |
| 91 | deferredProjection = $q.defer(); |
| 92 | |
| 93 | if (!promise) { |
| 94 | $log.warn('Failed to load countries TopoJSON data'); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | promise.then(function () { |
| 99 | var width = 1000, |
| 100 | height = 1000, |
| 101 | proj = d3.geo.mercator().translate([width/2, height/2]), |
| 102 | pathGen = d3.geo.path().projection(proj), |
| 103 | data = promise.topodata, |
| 104 | features = topojson.feature(data, data.objects.countries).features, |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 105 | country = features.filter(opts.countryFilter), |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 106 | countryFeature = { |
| 107 | type: 'FeatureCollection', |
| 108 | features: country |
| 109 | }, |
| 110 | path = d3.geo.path().projection(proj); |
| 111 | |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 112 | gds.rescaleProjection(proj, 0.95, 1000, path, countryFeature, opts.adjustScale); |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 113 | |
| 114 | deferredProjection.resolve(proj); |
| 115 | |
| 116 | mapLayer.selectAll('path.country') |
| 117 | .data([countryFeature]) |
| 118 | .enter() |
| 119 | .append('path').classed('country', true) |
| 120 | .attr('d', pathGen); |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 121 | |
| 122 | reshade(opts.shading); |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 123 | }); |
| 124 | return deferredProjection.promise; |
| 125 | } |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 126 | |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 127 | function reshade(sh) { |
| 128 | var p = sh && sh.palette, |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 129 | paths, stroke, fill, bg, |
| 130 | svg = d3.select('#ov-topo').select('svg'); |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 131 | if (sh) { |
| 132 | stroke = p.outline; |
| 133 | fill = sh.flip ? p.sea : p.land; |
| 134 | bg = sh.flip ? p.land : p.sea; |
| 135 | |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 136 | paths = d3.select('#topo-map').selectAll('path'); |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 137 | svg.style('background-color', bg); |
| 138 | paths.attr({ |
| 139 | stroke: stroke, |
| 140 | fill: fill |
| 141 | }); |
Thomas Vachuska | 26be4f3 | 2016-03-31 01:10:27 -0700 | [diff] [blame] | 142 | } else { |
| 143 | svg.style('background-color', null); |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 147 | angular.module('onosSvg') |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 148 | .factory('MapService', ['$log', '$q', 'FnService', 'GeoDataService', |
| 149 | function (_$log_, _$q_, _fs_, _gds_) { |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 150 | $log = _$log_; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 151 | $q = _$q_; |
Simon Hunt | f044d8a | 2015-01-07 17:53:04 -0800 | [diff] [blame] | 152 | fs = _fs_; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 153 | gds = _gds_; |
Simon Hunt | 404e54c | 2015-01-09 11:58:49 -0800 | [diff] [blame] | 154 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 155 | return { |
Simon Hunt | 2362b07 | 2015-06-11 20:08:22 -0700 | [diff] [blame] | 156 | loadMapRegionInto: loadMapRegionInto, |
Simon Hunt | a34fcb5 | 2016-02-25 16:27:32 -0800 | [diff] [blame] | 157 | loadMapInto: loadMapInto, |
| 158 | reshade: reshade |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 159 | }; |
| 160 | }]); |
| 161 | |
| 162 | }()); |