geoHelpers.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. export function getPoint(event) {
  2. // Get the vertices
  3. let a = this.geometry.vertices[event.face.a];
  4. let b = this.geometry.vertices[event.face.b];
  5. let c = this.geometry.vertices[event.face.c];
  6. // Averge them together
  7. let point = {
  8. x: (a.x + b.x + c.x) / 3,
  9. y: (a.y + b.y + c.y) / 3,
  10. z: (a.z + b.z + c.z) / 3
  11. };
  12. return point;
  13. }
  14. export function getEventCenter(event, radius) {
  15. radius = radius || 200;
  16. var point = getPoint.call(this, event);
  17. var latRads = Math.acos(point.y / radius);
  18. var lngRads = Math.atan2(point.z, point.x);
  19. var lat = (Math.PI / 2 - latRads) * (180 / Math.PI);
  20. var lng = (Math.PI - lngRads) * (180 / Math.PI);
  21. return [lat, lng - 180];
  22. }
  23. export function convertToXYZ(point, radius) {
  24. radius = radius || 200;
  25. var latRads = ( 90 - point[0]) * Math.PI / 180;
  26. var lngRads = (180 - point[1]) * Math.PI / 180;
  27. var x = radius * Math.sin(latRads) * Math.cos(lngRads);
  28. var y = radius * Math.cos(latRads);
  29. var z = radius * Math.sin(latRads) * Math.sin(lngRads);
  30. return {x: x, y: y, z: z};
  31. }
  32. export var geodecoder = function (features) {
  33. let store = {};
  34. for (let i = 0; i < features.length; i++) {
  35. store[features[i].id] = features[i];
  36. }
  37. return {
  38. find: function (id) {
  39. return store[id];
  40. },
  41. search: function (lat, lng) {
  42. let match = false;
  43. let country, coords;
  44. for (let i = 0; i < features.length; i++) {
  45. country = features[i];
  46. if(country.geometry.type === 'Polygon') {
  47. match = pointInPolygon(country.geometry.coordinates[0], [lng, lat]);
  48. if (match) {
  49. return {
  50. code: features[i].id,
  51. name: features[i].properties.name,
  52. chName: features[i].chName //改
  53. };
  54. }
  55. } else if (country.geometry.type === 'MultiPolygon') {
  56. coords = country.geometry.coordinates;
  57. for (let j = 0; j < coords.length; j++) {
  58. match = pointInPolygon(coords[j][0], [lng, lat]);
  59. if (match) {
  60. return {
  61. code: features[i].id,
  62. name: features[i].properties.name,
  63. chName: features[i].chName //改
  64. };
  65. }
  66. }
  67. }
  68. }
  69. return null;
  70. }
  71. };
  72. };
  73. // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
  74. var pointInPolygon = function(poly, point) {
  75. let x = point[0];
  76. let y = point[1];
  77. let inside = false, xi, xj, yi, yj, xk;
  78. for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
  79. xi = poly[i][0];
  80. yi = poly[i][1];
  81. xj = poly[j][0];
  82. yj = poly[j][1];
  83. xk = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
  84. if (xk) {
  85. inside = !inside;
  86. }
  87. }
  88. return inside;
  89. };