GeoJSON.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Chart from '../Core/Chart/Chart.js';
  12. import H from '../Core/Globals.js';
  13. var win = H.win;
  14. import '../Core/Options.js';
  15. import U from '../Core/Utilities.js';
  16. var error = U.error, extend = U.extend, format = U.format, merge = U.merge, wrap = U.wrap;
  17. /**
  18. * Represents the loose structure of a geographic JSON file.
  19. *
  20. * @interface Highcharts.GeoJSON
  21. */ /**
  22. * Full copyright note of the geographic data.
  23. * @name Highcharts.GeoJSON#copyright
  24. * @type {string|undefined}
  25. */ /**
  26. * Short copyright note of the geographic data suitable for watermarks.
  27. * @name Highcharts.GeoJSON#copyrightShort
  28. * @type {string|undefined}
  29. */ /**
  30. * Additional meta information based on the coordinate reference system.
  31. * @name Highcharts.GeoJSON#crs
  32. * @type {Highcharts.Dictionary<any>|undefined}
  33. */ /**
  34. * Data sets of geographic features.
  35. * @name Highcharts.GeoJSON#features
  36. * @type {Array<Highcharts.GeoJSONFeature>}
  37. */ /**
  38. * Map projections and transformations to be used when calculating between
  39. * lat/lon and chart values. Required for lat/lon support on maps. Allows
  40. * resizing, rotating, and moving portions of a map within its projected
  41. * coordinate system while still retaining lat/lon support. If using lat/lon
  42. * on a portion of the map that does not match a `hitZone`, the definition with
  43. * the key `default` is used.
  44. * @name Highcharts.GeoJSON#hc-transform
  45. * @type {Highcharts.Dictionary<Highcharts.GeoJSONTranslation>|undefined}
  46. */ /**
  47. * Title of the geographic data.
  48. * @name Highcharts.GeoJSON#title
  49. * @type {string|undefined}
  50. */ /**
  51. * Type of the geographic data. Type of an optimized map collection is
  52. * `FeatureCollection`.
  53. * @name Highcharts.GeoJSON#type
  54. * @type {string|undefined}
  55. */ /**
  56. * Version of the geographic data.
  57. * @name Highcharts.GeoJSON#version
  58. * @type {string|undefined}
  59. */
  60. /**
  61. * Data set of a geographic feature.
  62. * @interface Highcharts.GeoJSONFeature
  63. * @extends Highcharts.Dictionary<*>
  64. */ /**
  65. * Data type of the geographic feature.
  66. * @name Highcharts.GeoJSONFeature#type
  67. * @type {string}
  68. */
  69. /**
  70. * Describes the map projection and transformations applied to a portion of
  71. * a map.
  72. * @interface Highcharts.GeoJSONTranslation
  73. */ /**
  74. * The coordinate reference system used to generate this portion of the map.
  75. * @name Highcharts.GeoJSONTranslation#crs
  76. * @type {string}
  77. */ /**
  78. * Define the portion of the map that this defintion applies to. Defined as a
  79. * GeoJSON polygon feature object, with `type` and `coordinates` properties.
  80. * @name Highcharts.GeoJSONTranslation#hitZone
  81. * @type {Highcharts.Dictionary<*>|undefined}
  82. */ /**
  83. * Property for internal use for maps generated by Highsoft.
  84. * @name Highcharts.GeoJSONTranslation#jsonmarginX
  85. * @type {number|undefined}
  86. */ /**
  87. * Property for internal use for maps generated by Highsoft.
  88. * @name Highcharts.GeoJSONTranslation#jsonmarginY
  89. * @type {number|undefined}
  90. */ /**
  91. * Property for internal use for maps generated by Highsoft.
  92. * @name Highcharts.GeoJSONTranslation#jsonres
  93. * @type {number|undefined}
  94. */ /**
  95. * Specifies clockwise rotation of the coordinates after the projection, but
  96. * before scaling and panning. Defined in radians, relative to the coordinate
  97. * system origin.
  98. * @name Highcharts.GeoJSONTranslation#rotation
  99. * @type {number|undefined}
  100. */ /**
  101. * The scaling factor applied to the projected coordinates.
  102. * @name Highcharts.GeoJSONTranslation#scale
  103. * @type {number|undefined}
  104. */ /**
  105. * Property for internal use for maps generated by Highsoft.
  106. * @name Highcharts.GeoJSONTranslation#xoffset
  107. * @type {number|undefined}
  108. */ /**
  109. * X offset of projected coordinates after scaling.
  110. * @name Highcharts.GeoJSONTranslation#xpan
  111. * @type {number|undefined}
  112. */ /**
  113. * Property for internal use for maps generated by Highsoft.
  114. * @name Highcharts.GeoJSONTranslation#yoffset
  115. * @type {number|undefined}
  116. */ /**
  117. * Y offset of projected coordinates after scaling.
  118. * @name Highcharts.GeoJSONTranslation#ypan
  119. * @type {number|undefined}
  120. */
  121. /**
  122. * Result object of a map transformation.
  123. *
  124. * @interface Highcharts.MapCoordinateObject
  125. */ /**
  126. * X coordinate on the map.
  127. * @name Highcharts.MapCoordinateObject#x
  128. * @type {number}
  129. */ /**
  130. * Y coordinate on the map.
  131. * @name Highcharts.MapCoordinateObject#y
  132. * @type {number|null}
  133. */
  134. /**
  135. * A latitude/longitude object.
  136. *
  137. * @interface Highcharts.MapLatLonObject
  138. */ /**
  139. * The latitude.
  140. * @name Highcharts.MapLatLonObject#lat
  141. * @type {number}
  142. */ /**
  143. * The longitude.
  144. * @name Highcharts.MapLatLonObject#lon
  145. * @type {number}
  146. */
  147. ''; // detach doclets above
  148. /* eslint-disable no-invalid-this, valid-jsdoc */
  149. /**
  150. * Test for point in polygon. Polygon defined as array of [x,y] points.
  151. * @private
  152. */
  153. function pointInPolygon(point, polygon) {
  154. var i, j, rel1, rel2, c = false, x = point.x, y = point.y;
  155. for (i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
  156. rel1 = polygon[i][1] > y;
  157. rel2 = polygon[j][1] > y;
  158. if (rel1 !== rel2 &&
  159. (x < (polygon[j][0] -
  160. polygon[i][0]) * (y - polygon[i][1]) /
  161. (polygon[j][1] - polygon[i][1]) +
  162. polygon[i][0])) {
  163. c = !c;
  164. }
  165. }
  166. return c;
  167. }
  168. /**
  169. * Highmaps only. Get point from latitude and longitude using specified
  170. * transform definition.
  171. *
  172. * @requires modules/map
  173. *
  174. * @sample maps/series/latlon-transform/
  175. * Use specific transformation for lat/lon
  176. *
  177. * @function Highcharts.Chart#transformFromLatLon
  178. *
  179. * @param {Highcharts.MapLatLonObject} latLon
  180. * A latitude/longitude object.
  181. *
  182. * @param {*} transform
  183. * The transform definition to use as explained in the
  184. * {@link https://www.highcharts.com/docs/maps/latlon|documentation}.
  185. *
  186. * @return {Highcharts.MapCoordinateObject}
  187. * An object with `x` and `y` properties.
  188. */
  189. Chart.prototype.transformFromLatLon = function (latLon, transform) {
  190. /**
  191. * Allows to manually load the proj4 library from Highcharts options
  192. * instead of the `window`.
  193. * In case of loading the library from a `script` tag,
  194. * this option is not needed, it will be loaded from there by default.
  195. *
  196. * @type {function}
  197. * @product highmaps
  198. * @apioption chart.proj4
  199. */
  200. var _a;
  201. var proj4 = (((_a = this.userOptions.chart) === null || _a === void 0 ? void 0 : _a.proj4) || win.proj4);
  202. if (!proj4) {
  203. error(21, false, this);
  204. return {
  205. x: 0,
  206. y: null
  207. };
  208. }
  209. var projected = proj4(transform.crs, [latLon.lon, latLon.lat]), cosAngle = transform.cosAngle ||
  210. (transform.rotation && Math.cos(transform.rotation)), sinAngle = transform.sinAngle ||
  211. (transform.rotation && Math.sin(transform.rotation)), rotated = transform.rotation ? [
  212. projected[0] * cosAngle + projected[1] * sinAngle,
  213. -projected[0] * sinAngle + projected[1] * cosAngle
  214. ] : projected;
  215. return {
  216. x: ((rotated[0] - (transform.xoffset || 0)) * (transform.scale || 1) +
  217. (transform.xpan || 0)) * (transform.jsonres || 1) +
  218. (transform.jsonmarginX || 0),
  219. y: (((transform.yoffset || 0) - rotated[1]) * (transform.scale || 1) +
  220. (transform.ypan || 0)) * (transform.jsonres || 1) -
  221. (transform.jsonmarginY || 0)
  222. };
  223. };
  224. /**
  225. * Highmaps only. Get latLon from point using specified transform definition.
  226. * The method returns an object with the numeric properties `lat` and `lon`.
  227. *
  228. * @requires modules/map
  229. *
  230. * @sample maps/series/latlon-transform/
  231. * Use specific transformation for lat/lon
  232. *
  233. * @function Highcharts.Chart#transformToLatLon
  234. *
  235. * @param {Highcharts.Point|Highcharts.MapCoordinateObject} point
  236. * A `Point` instance, or any object containing the properties `x` and
  237. * `y` with numeric values.
  238. *
  239. * @param {*} transform
  240. * The transform definition to use as explained in the
  241. * {@link https://www.highcharts.com/docs/maps/latlon|documentation}.
  242. *
  243. * @return {Highcharts.MapLatLonObject|undefined}
  244. * An object with `lat` and `lon` properties.
  245. */
  246. Chart.prototype.transformToLatLon = function (point, transform) {
  247. if (typeof win.proj4 === 'undefined') {
  248. error(21, false, this);
  249. return;
  250. }
  251. var normalized = {
  252. x: ((point.x -
  253. (transform.jsonmarginX || 0)) / (transform.jsonres || 1) -
  254. (transform.xpan || 0)) / (transform.scale || 1) +
  255. (transform.xoffset || 0),
  256. y: ((-point.y - (transform.jsonmarginY || 0)) / (transform.jsonres || 1) +
  257. (transform.ypan || 0)) / (transform.scale || 1) +
  258. (transform.yoffset || 0)
  259. }, cosAngle = transform.cosAngle ||
  260. (transform.rotation && Math.cos(transform.rotation)), sinAngle = transform.sinAngle ||
  261. (transform.rotation && Math.sin(transform.rotation)),
  262. // Note: Inverted sinAngle to reverse rotation direction
  263. projected = win.proj4(transform.crs, 'WGS84', transform.rotation ? {
  264. x: normalized.x * cosAngle + normalized.y * -sinAngle,
  265. y: normalized.x * sinAngle + normalized.y * cosAngle
  266. } : normalized);
  267. return { lat: projected.y, lon: projected.x };
  268. };
  269. /**
  270. * Highmaps only. Calculate latitude/longitude values for a point. Returns an
  271. * object with the numeric properties `lat` and `lon`.
  272. *
  273. * @requires modules/map
  274. *
  275. * @sample maps/demo/latlon-advanced/
  276. * Advanced lat/lon demo
  277. *
  278. * @function Highcharts.Chart#fromPointToLatLon
  279. *
  280. * @param {Highcharts.Point|Highcharts.MapCoordinateObject} point
  281. * A `Point` instance or anything containing `x` and `y` properties with
  282. * numeric values.
  283. *
  284. * @return {Highcharts.MapLatLonObject|undefined}
  285. * An object with `lat` and `lon` properties.
  286. */
  287. Chart.prototype.fromPointToLatLon = function (point) {
  288. var transforms = this.mapTransforms, transform;
  289. if (!transforms) {
  290. error(22, false, this);
  291. return;
  292. }
  293. for (transform in transforms) {
  294. if (Object.hasOwnProperty.call(transforms, transform) &&
  295. transforms[transform].hitZone &&
  296. pointInPolygon({ x: point.x, y: -point.y }, transforms[transform].hitZone.coordinates[0])) {
  297. return this.transformToLatLon(point, transforms[transform]);
  298. }
  299. }
  300. return this.transformToLatLon(point, transforms['default'] // eslint-disable-line dot-notation
  301. );
  302. };
  303. /**
  304. * Highmaps only. Get chart coordinates from latitude/longitude. Returns an
  305. * object with x and y values corresponding to the `xAxis` and `yAxis`.
  306. *
  307. * @requires modules/map
  308. *
  309. * @sample maps/series/latlon-to-point/
  310. * Find a point from lat/lon
  311. *
  312. * @function Highcharts.Chart#fromLatLonToPoint
  313. *
  314. * @param {Highcharts.MapLatLonObject} latLon
  315. * Coordinates.
  316. *
  317. * @return {Highcharts.MapCoordinateObject}
  318. * X and Y coordinates in terms of chart axis values.
  319. */
  320. Chart.prototype.fromLatLonToPoint = function (latLon) {
  321. var transforms = this.mapTransforms, transform, coords;
  322. if (!transforms) {
  323. error(22, false, this);
  324. return {
  325. x: 0,
  326. y: null
  327. };
  328. }
  329. for (transform in transforms) {
  330. if (Object.hasOwnProperty.call(transforms, transform) &&
  331. transforms[transform].hitZone) {
  332. coords = this.transformFromLatLon(latLon, transforms[transform]);
  333. if (pointInPolygon({ x: coords.x, y: -coords.y }, transforms[transform].hitZone.coordinates[0])) {
  334. return coords;
  335. }
  336. }
  337. }
  338. return this.transformFromLatLon(latLon, transforms['default'] // eslint-disable-line dot-notation
  339. );
  340. };
  341. /**
  342. * Highmaps only. Restructure a GeoJSON object in preparation to be read
  343. * directly by the
  344. * {@link https://api.highcharts.com/highmaps/plotOptions.series.mapData|series.mapData}
  345. * option. The GeoJSON will be broken down to fit a specific Highcharts type,
  346. * either `map`, `mapline` or `mappoint`. Meta data in GeoJSON's properties
  347. * object will be copied directly over to {@link Point.properties} in Highmaps.
  348. *
  349. * @requires modules/map
  350. *
  351. * @sample maps/demo/geojson/
  352. * Simple areas
  353. * @sample maps/demo/geojson-multiple-types/
  354. * Multiple types
  355. *
  356. * @function Highcharts.geojson
  357. *
  358. * @param {Highcharts.GeoJSON} geojson
  359. * The GeoJSON structure to parse, represented as a JavaScript object
  360. * rather than a JSON string.
  361. *
  362. * @param {string} [hType=map]
  363. * The Highmaps series type to prepare for. Setting "map" will return
  364. * GeoJSON polygons and multipolygons. Setting "mapline" will return
  365. * GeoJSON linestrings and multilinestrings. Setting "mappoint" will
  366. * return GeoJSON points and multipoints.
  367. *
  368. * @return {Array<*>}
  369. * An object ready for the `mapData` option.
  370. */
  371. H.geojson = function (geojson, hType, series) {
  372. var mapData = [], path = [], polygonToPath = function (polygon) {
  373. polygon.forEach(function (point, i) {
  374. if (i === 0) {
  375. path.push(['M', point[0], -point[1]]);
  376. }
  377. else {
  378. path.push(['L', point[0], -point[1]]);
  379. }
  380. });
  381. };
  382. hType = hType || 'map';
  383. geojson.features.forEach(function (feature) {
  384. var geometry = feature.geometry, type = geometry.type, coordinates = geometry.coordinates, properties = feature.properties, point;
  385. path = [];
  386. if (hType === 'map' || hType === 'mapbubble') {
  387. if (type === 'Polygon') {
  388. coordinates.forEach(polygonToPath);
  389. path.push(['Z']);
  390. }
  391. else if (type === 'MultiPolygon') {
  392. coordinates.forEach(function (items) {
  393. items.forEach(polygonToPath);
  394. });
  395. path.push(['Z']);
  396. }
  397. if (path.length) {
  398. point = { path: path };
  399. }
  400. }
  401. else if (hType === 'mapline') {
  402. if (type === 'LineString') {
  403. polygonToPath(coordinates);
  404. }
  405. else if (type === 'MultiLineString') {
  406. coordinates.forEach(polygonToPath);
  407. }
  408. if (path.length) {
  409. point = { path: path };
  410. }
  411. }
  412. else if (hType === 'mappoint') {
  413. if (type === 'Point') {
  414. point = {
  415. x: coordinates[0],
  416. y: -coordinates[1]
  417. };
  418. }
  419. }
  420. if (point) {
  421. mapData.push(extend(point, {
  422. name: properties.name || properties.NAME,
  423. /**
  424. * In Highmaps, when data is loaded from GeoJSON, the GeoJSON
  425. * item's properies are copied over here.
  426. *
  427. * @requires modules/map
  428. * @name Highcharts.Point#properties
  429. * @type {*}
  430. */
  431. properties: properties
  432. }));
  433. }
  434. });
  435. // Create a credits text that includes map source, to be picked up in
  436. // Chart.addCredits
  437. if (series && geojson.copyrightShort) {
  438. series.chart.mapCredits = format(series.chart.options.credits.mapText, { geojson: geojson });
  439. series.chart.mapCreditsFull = format(series.chart.options.credits.mapTextFull, { geojson: geojson });
  440. }
  441. return mapData;
  442. };
  443. // Override addCredits to include map source by default
  444. wrap(Chart.prototype, 'addCredits', function (proceed, credits) {
  445. credits = merge(true, this.options.credits, credits);
  446. // Disable credits link if map credits enabled. This to allow for in-text
  447. // anchors.
  448. if (this.mapCredits) {
  449. credits.href = null;
  450. }
  451. proceed.call(this, credits);
  452. // Add full map credits to hover
  453. if (this.credits && this.mapCreditsFull) {
  454. this.credits.attr({
  455. title: this.mapCreditsFull
  456. });
  457. }
  458. });