parseGeoJson.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. var Region = require("./Region");
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. /**
  40. * Parse and decode geo json
  41. * @module echarts/coord/geo/parseGeoJson
  42. */
  43. function decode(json) {
  44. if (!json.UTF8Encoding) {
  45. return json;
  46. }
  47. var encodeScale = json.UTF8Scale;
  48. if (encodeScale == null) {
  49. encodeScale = 1024;
  50. }
  51. var features = json.features;
  52. for (var f = 0; f < features.length; f++) {
  53. var feature = features[f];
  54. var geometry = feature.geometry;
  55. var coordinates = geometry.coordinates;
  56. var encodeOffsets = geometry.encodeOffsets;
  57. for (var c = 0; c < coordinates.length; c++) {
  58. var coordinate = coordinates[c];
  59. if (geometry.type === 'Polygon') {
  60. coordinates[c] = decodePolygon(coordinate, encodeOffsets[c], encodeScale);
  61. } else if (geometry.type === 'MultiPolygon') {
  62. for (var c2 = 0; c2 < coordinate.length; c2++) {
  63. var polygon = coordinate[c2];
  64. coordinate[c2] = decodePolygon(polygon, encodeOffsets[c][c2], encodeScale);
  65. }
  66. }
  67. }
  68. } // Has been decoded
  69. json.UTF8Encoding = false;
  70. return json;
  71. }
  72. function decodePolygon(coordinate, encodeOffsets, encodeScale) {
  73. var result = [];
  74. var prevX = encodeOffsets[0];
  75. var prevY = encodeOffsets[1];
  76. for (var i = 0; i < coordinate.length; i += 2) {
  77. var x = coordinate.charCodeAt(i) - 64;
  78. var y = coordinate.charCodeAt(i + 1) - 64; // ZigZag decoding
  79. x = x >> 1 ^ -(x & 1);
  80. y = y >> 1 ^ -(y & 1); // Delta deocding
  81. x += prevX;
  82. y += prevY;
  83. prevX = x;
  84. prevY = y; // Dequantize
  85. result.push([x / encodeScale, y / encodeScale]);
  86. }
  87. return result;
  88. }
  89. /**
  90. * @alias module:echarts/coord/geo/parseGeoJson
  91. * @param {Object} geoJson
  92. * @param {string} nameProperty
  93. * @return {module:zrender/container/Group}
  94. */
  95. function _default(geoJson, nameProperty) {
  96. decode(geoJson);
  97. return zrUtil.map(zrUtil.filter(geoJson.features, function (featureObj) {
  98. // Output of mapshaper may have geometry null
  99. return featureObj.geometry && featureObj.properties && featureObj.geometry.coordinates.length > 0;
  100. }), function (featureObj) {
  101. var properties = featureObj.properties;
  102. var geo = featureObj.geometry;
  103. var coordinates = geo.coordinates;
  104. var geometries = [];
  105. if (geo.type === 'Polygon') {
  106. geometries.push({
  107. type: 'polygon',
  108. // According to the GeoJSON specification.
  109. // First must be exterior, and the rest are all interior(holes).
  110. exterior: coordinates[0],
  111. interiors: coordinates.slice(1)
  112. });
  113. }
  114. if (geo.type === 'MultiPolygon') {
  115. zrUtil.each(coordinates, function (item) {
  116. if (item[0]) {
  117. geometries.push({
  118. type: 'polygon',
  119. exterior: item[0],
  120. interiors: item.slice(1)
  121. });
  122. }
  123. });
  124. }
  125. var region = new Region(properties[nameProperty || 'name'], geometries, properties.cp);
  126. region.properties = properties;
  127. return region;
  128. });
  129. }
  130. module.exports = _default;