GeoJSON.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**
  2. * (c) 2010-2019 Torstein Honsi
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. /**
  7. * A latitude/longitude object.
  8. *
  9. * @interface Highcharts.MapLatLonObject
  10. *//**
  11. * The latitude.
  12. * @name Highcharts.MapLatLonObject#lat
  13. * @type {number}
  14. *//**
  15. * The longitude.
  16. * @name Highcharts.MapLatLonObject#lon
  17. * @type {number}
  18. */
  19. /**
  20. * Result object of a map transformation.
  21. *
  22. * @interface Highcharts.MapCoordinateObject
  23. *//**
  24. * X coordinate on the map.
  25. * @name Highcharts.MapCoordinateObject#x
  26. * @type {number}
  27. *//**
  28. * Y coordinate on the map.
  29. * @name Highcharts.MapCoordinateObject#y
  30. * @type {number}
  31. */
  32. 'use strict';
  33. import H from '../parts/Globals.js';
  34. import '../parts/Utilities.js';
  35. import '../parts/Options.js';
  36. import '../parts/Chart.js';
  37. var Chart = H.Chart,
  38. extend = H.extend,
  39. format = H.format,
  40. merge = H.merge,
  41. win = H.win,
  42. wrap = H.wrap;
  43. /* *
  44. * Test for point in polygon. Polygon defined as array of [x,y] points.
  45. */
  46. function pointInPolygon(point, polygon) {
  47. var i,
  48. j,
  49. rel1,
  50. rel2,
  51. c = false,
  52. x = point.x,
  53. y = point.y;
  54. for (i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
  55. rel1 = polygon[i][1] > y;
  56. rel2 = polygon[j][1] > y;
  57. if (
  58. rel1 !== rel2 &&
  59. (
  60. x < (polygon[j][0] -
  61. polygon[i][0]) * (y - polygon[i][1]) /
  62. (polygon[j][1] - polygon[i][1]) +
  63. polygon[i][0]
  64. )
  65. ) {
  66. c = !c;
  67. }
  68. }
  69. return c;
  70. }
  71. /**
  72. * Highmaps only. Get point from latitude and longitude using specified
  73. * transform definition.
  74. *
  75. * @requires module:modules/map
  76. *
  77. * @sample maps/series/latlon-transform/
  78. * Use specific transformation for lat/lon
  79. *
  80. * @function Highcharts.Chart#transformFromLatLon
  81. *
  82. * @param {Highcharts.MapLatLonObject} latLon
  83. * A latitude/longitude object.
  84. *
  85. * @param {object} transform
  86. * The transform definition to use as explained in the
  87. * {@link https://www.highcharts.com/docs/maps/latlon|documentation}.
  88. *
  89. * @return {Highcharts.MapCoordinateObject}
  90. * An object with `x` and `y` properties.
  91. */
  92. Chart.prototype.transformFromLatLon = function (latLon, transform) {
  93. if (win.proj4 === undefined) {
  94. H.error(21, false, this);
  95. return {
  96. x: 0,
  97. y: null
  98. };
  99. }
  100. var projected = win.proj4(transform.crs, [latLon.lon, latLon.lat]),
  101. cosAngle = transform.cosAngle ||
  102. (transform.rotation && Math.cos(transform.rotation)),
  103. sinAngle = transform.sinAngle ||
  104. (transform.rotation && Math.sin(transform.rotation)),
  105. rotated = transform.rotation ? [
  106. projected[0] * cosAngle + projected[1] * sinAngle,
  107. -projected[0] * sinAngle + projected[1] * cosAngle
  108. ] : projected;
  109. return {
  110. x: (
  111. (rotated[0] - (transform.xoffset || 0)) * (transform.scale || 1) +
  112. (transform.xpan || 0)
  113. ) * (transform.jsonres || 1) +
  114. (transform.jsonmarginX || 0),
  115. y: (
  116. ((transform.yoffset || 0) - rotated[1]) * (transform.scale || 1) +
  117. (transform.ypan || 0)
  118. ) * (transform.jsonres || 1) -
  119. (transform.jsonmarginY || 0)
  120. };
  121. };
  122. /**
  123. * Highmaps only. Get latLon from point using specified transform definition.
  124. * The method returns an object with the numeric properties `lat` and `lon`.
  125. *
  126. * @requires module:modules/map
  127. *
  128. * @sample maps/series/latlon-transform/
  129. * Use specific transformation for lat/lon
  130. *
  131. * @function Highcharts.Chart#transformToLatLon
  132. *
  133. * @param {Highcharts.Point|Highcharts.MapCoordinateObject} point
  134. * A `Point` instance, or any object containing the properties `x` and
  135. * `y` with numeric values.
  136. *
  137. * @param {object} transform
  138. * The transform definition to use as explained in the
  139. * {@link https://www.highcharts.com/docs/maps/latlon|documentation}.
  140. *
  141. * @return {Highcharts.MapLatLonObject}
  142. * An object with `lat` and `lon` properties.
  143. */
  144. Chart.prototype.transformToLatLon = function (point, transform) {
  145. if (win.proj4 === undefined) {
  146. H.error(21, false, this);
  147. return;
  148. }
  149. var normalized = {
  150. x: (
  151. (
  152. point.x -
  153. (transform.jsonmarginX || 0)
  154. ) / (transform.jsonres || 1) -
  155. (transform.xpan || 0)
  156. ) / (transform.scale || 1) +
  157. (transform.xoffset || 0),
  158. y: (
  159. (
  160. -point.y - (transform.jsonmarginY || 0)
  161. ) / (transform.jsonres || 1) +
  162. (transform.ypan || 0)
  163. ) / (transform.scale || 1) +
  164. (transform.yoffset || 0)
  165. },
  166. cosAngle = transform.cosAngle ||
  167. (transform.rotation && Math.cos(transform.rotation)),
  168. sinAngle = transform.sinAngle ||
  169. (transform.rotation && Math.sin(transform.rotation)),
  170. // Note: Inverted sinAngle to reverse rotation direction
  171. projected = win.proj4(transform.crs, 'WGS84', transform.rotation ? {
  172. x: normalized.x * cosAngle + normalized.y * -sinAngle,
  173. y: normalized.x * sinAngle + normalized.y * cosAngle
  174. } : normalized);
  175. return { lat: projected.y, lon: projected.x };
  176. };
  177. /**
  178. * Highmaps only. Calculate latitude/longitude values for a point. Returns an
  179. * object with the numeric properties `lat` and `lon`.
  180. *
  181. * @requires module:modules/map
  182. *
  183. * @sample maps/demo/latlon-advanced/
  184. * Advanced lat/lon demo
  185. *
  186. * @function Highcharts.Chart#fromPointToLatLon
  187. *
  188. * @param {Highcharts.Point|Highcharts.MapCoordinateObject} point
  189. * A `Point` instance or anything containing `x` and `y` properties with
  190. * numeric values.
  191. *
  192. * @return {Highcharts.MapLatLonObject}
  193. * An object with `lat` and `lon` properties.
  194. */
  195. Chart.prototype.fromPointToLatLon = function (point) {
  196. var transforms = this.mapTransforms,
  197. transform;
  198. if (!transforms) {
  199. H.error(22, false, this);
  200. return;
  201. }
  202. for (transform in transforms) {
  203. if (
  204. transforms.hasOwnProperty(transform) &&
  205. transforms[transform].hitZone &&
  206. pointInPolygon(
  207. { x: point.x, y: -point.y },
  208. transforms[transform].hitZone.coordinates[0]
  209. )
  210. ) {
  211. return this.transformToLatLon(point, transforms[transform]);
  212. }
  213. }
  214. return this.transformToLatLon(
  215. point,
  216. transforms['default'] // eslint-disable-line dot-notation
  217. );
  218. };
  219. /**
  220. * Highmaps only. Get chart coordinates from latitude/longitude. Returns an
  221. * object with x and y values corresponding to the `xAxis` and `yAxis`.
  222. *
  223. * @requires module:modules/map
  224. *
  225. * @sample maps/series/latlon-to-point/
  226. * Find a point from lat/lon
  227. *
  228. * @function Highcharts.Chart#fromLatLonToPoint
  229. *
  230. * @param {Highcharts.MapLatLonObject} latLon
  231. * Coordinates.
  232. *
  233. * @return {Highcharts.MapCoordinateObject}
  234. * X and Y coordinates in terms of chart axis values.
  235. */
  236. Chart.prototype.fromLatLonToPoint = function (latLon) {
  237. var transforms = this.mapTransforms,
  238. transform,
  239. coords;
  240. if (!transforms) {
  241. H.error(22, false, this);
  242. return {
  243. x: 0,
  244. y: null
  245. };
  246. }
  247. for (transform in transforms) {
  248. if (
  249. transforms.hasOwnProperty(transform) &&
  250. transforms[transform].hitZone
  251. ) {
  252. coords = this.transformFromLatLon(latLon, transforms[transform]);
  253. if (pointInPolygon(
  254. { x: coords.x, y: -coords.y },
  255. transforms[transform].hitZone.coordinates[0]
  256. )) {
  257. return coords;
  258. }
  259. }
  260. }
  261. return this.transformFromLatLon(
  262. latLon,
  263. transforms['default'] // eslint-disable-line dot-notation
  264. );
  265. };
  266. /**
  267. * Highmaps only. Restructure a GeoJSON object in preparation to be read
  268. * directly by the
  269. * {@link https://api.highcharts.com/highmaps/plotOptions.series.mapData|series.mapData}
  270. * option. The GeoJSON will be broken down to fit a specific Highcharts type,
  271. * either `map`, `mapline` or `mappoint`. Meta data in GeoJSON's properties
  272. * object will be copied directly over to {@link Point.properties} in Highmaps.
  273. *
  274. * @requires module:modules/map
  275. *
  276. * @sample maps/demo/geojson/
  277. * Simple areas
  278. * @sample maps/demo/geojson-multiple-types/
  279. * Multiple types
  280. *
  281. * @function Highcharts.geojson
  282. *
  283. * @param {object} geojson
  284. * The GeoJSON structure to parse, represented as a JavaScript object
  285. * rather than a JSON string.
  286. *
  287. * @param {string} [hType=map]
  288. * The Highmaps series type to prepare for. Setting "map" will return
  289. * GeoJSON polygons and multipolygons. Setting "mapline" will return
  290. * GeoJSON linestrings and multilinestrings. Setting "mappoint" will
  291. * return GeoJSON points and multipoints.
  292. *
  293. * @return {Array<object>}
  294. * An object ready for the `mapData` option.
  295. */
  296. H.geojson = function (geojson, hType, series) {
  297. var mapData = [],
  298. path = [],
  299. polygonToPath = function (polygon) {
  300. var i,
  301. len = polygon.length;
  302. path.push('M');
  303. for (i = 0; i < len; i++) {
  304. if (i === 1) {
  305. path.push('L');
  306. }
  307. path.push(polygon[i][0], -polygon[i][1]);
  308. }
  309. };
  310. hType = hType || 'map';
  311. geojson.features.forEach(function (feature) {
  312. var geometry = feature.geometry,
  313. type = geometry.type,
  314. coordinates = geometry.coordinates,
  315. properties = feature.properties,
  316. point;
  317. path = [];
  318. if (hType === 'map' || hType === 'mapbubble') {
  319. if (type === 'Polygon') {
  320. coordinates.forEach(polygonToPath);
  321. path.push('Z');
  322. } else if (type === 'MultiPolygon') {
  323. coordinates.forEach(function (items) {
  324. items.forEach(polygonToPath);
  325. });
  326. path.push('Z');
  327. }
  328. if (path.length) {
  329. point = { path: path };
  330. }
  331. } else if (hType === 'mapline') {
  332. if (type === 'LineString') {
  333. polygonToPath(coordinates);
  334. } else if (type === 'MultiLineString') {
  335. coordinates.forEach(polygonToPath);
  336. }
  337. if (path.length) {
  338. point = { path: path };
  339. }
  340. } else if (hType === 'mappoint') {
  341. if (type === 'Point') {
  342. point = {
  343. x: coordinates[0],
  344. y: -coordinates[1]
  345. };
  346. }
  347. }
  348. if (point) {
  349. mapData.push(extend(point, {
  350. name: properties.name || properties.NAME,
  351. /**
  352. * In Highmaps, when data is loaded from GeoJSON, the GeoJSON
  353. * item's properies are copied over here.
  354. *
  355. * @requires module:modules/map
  356. * @name Highcharts.Point#properties
  357. * @type {*}
  358. */
  359. properties: properties
  360. }));
  361. }
  362. });
  363. // Create a credits text that includes map source, to be picked up in
  364. // Chart.addCredits
  365. if (series && geojson.copyrightShort) {
  366. series.chart.mapCredits = format(
  367. series.chart.options.credits.mapText,
  368. { geojson: geojson }
  369. );
  370. series.chart.mapCreditsFull = format(
  371. series.chart.options.credits.mapTextFull,
  372. { geojson: geojson }
  373. );
  374. }
  375. return mapData;
  376. };
  377. // Override addCredits to include map source by default
  378. wrap(Chart.prototype, 'addCredits', function (proceed, credits) {
  379. credits = merge(true, this.options.credits, credits);
  380. // Disable credits link if map credits enabled. This to allow for in-text
  381. // anchors.
  382. if (this.mapCredits) {
  383. credits.href = null;
  384. }
  385. proceed.call(this, credits);
  386. // Add full map credits to hover
  387. if (this.credits && this.mapCreditsFull) {
  388. this.credits.attr({
  389. title: this.mapCreditsFull
  390. });
  391. }
  392. });