parseGeoJson.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var zrUtil = require("zrender/lib/core/util");
  2. var Region = require("./Region");
  3. /**
  4. * Parse and decode geo json
  5. * @module echarts/coord/geo/parseGeoJson
  6. */
  7. function decode(json) {
  8. if (!json.UTF8Encoding) {
  9. return json;
  10. }
  11. var encodeScale = json.UTF8Scale;
  12. if (encodeScale == null) {
  13. encodeScale = 1024;
  14. }
  15. var features = json.features;
  16. for (var f = 0; f < features.length; f++) {
  17. var feature = features[f];
  18. var geometry = feature.geometry;
  19. var coordinates = geometry.coordinates;
  20. var encodeOffsets = geometry.encodeOffsets;
  21. for (var c = 0; c < coordinates.length; c++) {
  22. var coordinate = coordinates[c];
  23. if (geometry.type === 'Polygon') {
  24. coordinates[c] = decodePolygon(coordinate, encodeOffsets[c], encodeScale);
  25. } else if (geometry.type === 'MultiPolygon') {
  26. for (var c2 = 0; c2 < coordinate.length; c2++) {
  27. var polygon = coordinate[c2];
  28. coordinate[c2] = decodePolygon(polygon, encodeOffsets[c][c2], encodeScale);
  29. }
  30. }
  31. }
  32. } // Has been decoded
  33. json.UTF8Encoding = false;
  34. return json;
  35. }
  36. function decodePolygon(coordinate, encodeOffsets, encodeScale) {
  37. var result = [];
  38. var prevX = encodeOffsets[0];
  39. var prevY = encodeOffsets[1];
  40. for (var i = 0; i < coordinate.length; i += 2) {
  41. var x = coordinate.charCodeAt(i) - 64;
  42. var y = coordinate.charCodeAt(i + 1) - 64; // ZigZag decoding
  43. x = x >> 1 ^ -(x & 1);
  44. y = y >> 1 ^ -(y & 1); // Delta deocding
  45. x += prevX;
  46. y += prevY;
  47. prevX = x;
  48. prevY = y; // Dequantize
  49. result.push([x / encodeScale, y / encodeScale]);
  50. }
  51. return result;
  52. }
  53. /**
  54. * @alias module:echarts/coord/geo/parseGeoJson
  55. * @param {Object} geoJson
  56. * @return {module:zrender/container/Group}
  57. */
  58. function _default(geoJson) {
  59. decode(geoJson);
  60. return zrUtil.map(zrUtil.filter(geoJson.features, function (featureObj) {
  61. // Output of mapshaper may have geometry null
  62. return featureObj.geometry && featureObj.properties && featureObj.geometry.coordinates.length > 0;
  63. }), function (featureObj) {
  64. var properties = featureObj.properties;
  65. var geo = featureObj.geometry;
  66. var coordinates = geo.coordinates;
  67. var geometries = [];
  68. if (geo.type === 'Polygon') {
  69. geometries.push({
  70. type: 'polygon',
  71. // According to the GeoJSON specification.
  72. // First must be exterior, and the rest are all interior(holes).
  73. exterior: coordinates[0],
  74. interiors: coordinates.slice(1)
  75. });
  76. }
  77. if (geo.type === 'MultiPolygon') {
  78. zrUtil.each(coordinates, function (item) {
  79. if (item[0]) {
  80. geometries.push({
  81. type: 'polygon',
  82. exterior: item[0],
  83. interiors: item.slice(1)
  84. });
  85. }
  86. });
  87. }
  88. var region = new Region(properties.name, geometries, properties.cp);
  89. region.properties = properties;
  90. return region;
  91. });
  92. }
  93. module.exports = _default;