GeoJSON.js 16 KB

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