parseGeoJson.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. /**
  41. * Parse and decode geo json
  42. */
  43. import * as zrUtil from 'zrender/lib/core/util.js';
  44. import { GeoJSONLineStringGeometry, GeoJSONPolygonGeometry, GeoJSONRegion } from './Region.js';
  45. function decode(json) {
  46. if (!json.UTF8Encoding) {
  47. return json;
  48. }
  49. var jsonCompressed = json;
  50. var encodeScale = jsonCompressed.UTF8Scale;
  51. if (encodeScale == null) {
  52. encodeScale = 1024;
  53. }
  54. var features = jsonCompressed.features;
  55. zrUtil.each(features, function (feature) {
  56. var geometry = feature.geometry;
  57. var encodeOffsets = geometry.encodeOffsets;
  58. var coordinates = geometry.coordinates; // Geometry may be appeded manually in the script after json loaded.
  59. // In this case this geometry is usually not encoded.
  60. if (!encodeOffsets) {
  61. return;
  62. }
  63. switch (geometry.type) {
  64. case 'LineString':
  65. geometry.coordinates = decodeRing(coordinates, encodeOffsets, encodeScale);
  66. break;
  67. case 'Polygon':
  68. decodeRings(coordinates, encodeOffsets, encodeScale);
  69. break;
  70. case 'MultiLineString':
  71. decodeRings(coordinates, encodeOffsets, encodeScale);
  72. break;
  73. case 'MultiPolygon':
  74. zrUtil.each(coordinates, function (rings, idx) {
  75. return decodeRings(rings, encodeOffsets[idx], encodeScale);
  76. });
  77. }
  78. }); // Has been decoded
  79. jsonCompressed.UTF8Encoding = false;
  80. return jsonCompressed;
  81. }
  82. function decodeRings(rings, encodeOffsets, encodeScale) {
  83. for (var c = 0; c < rings.length; c++) {
  84. rings[c] = decodeRing(rings[c], encodeOffsets[c], encodeScale);
  85. }
  86. }
  87. function decodeRing(coordinate, encodeOffsets, encodeScale) {
  88. var result = [];
  89. var prevX = encodeOffsets[0];
  90. var prevY = encodeOffsets[1];
  91. for (var i = 0; i < coordinate.length; i += 2) {
  92. var x = coordinate.charCodeAt(i) - 64;
  93. var y = coordinate.charCodeAt(i + 1) - 64; // ZigZag decoding
  94. x = x >> 1 ^ -(x & 1);
  95. y = y >> 1 ^ -(y & 1); // Delta deocding
  96. x += prevX;
  97. y += prevY;
  98. prevX = x;
  99. prevY = y; // Dequantize
  100. result.push([x / encodeScale, y / encodeScale]);
  101. }
  102. return result;
  103. }
  104. export default function parseGeoJSON(geoJson, nameProperty) {
  105. geoJson = decode(geoJson);
  106. return zrUtil.map(zrUtil.filter(geoJson.features, function (featureObj) {
  107. // Output of mapshaper may have geometry null
  108. return featureObj.geometry && featureObj.properties && featureObj.geometry.coordinates.length > 0;
  109. }), function (featureObj) {
  110. var properties = featureObj.properties;
  111. var geo = featureObj.geometry;
  112. var geometries = [];
  113. switch (geo.type) {
  114. case 'Polygon':
  115. var coordinates = geo.coordinates; // According to the GeoJSON specification.
  116. // First must be exterior, and the rest are all interior(holes).
  117. geometries.push(new GeoJSONPolygonGeometry(coordinates[0], coordinates.slice(1)));
  118. break;
  119. case 'MultiPolygon':
  120. zrUtil.each(geo.coordinates, function (item) {
  121. if (item[0]) {
  122. geometries.push(new GeoJSONPolygonGeometry(item[0], item.slice(1)));
  123. }
  124. });
  125. break;
  126. case 'LineString':
  127. geometries.push(new GeoJSONLineStringGeometry([geo.coordinates]));
  128. break;
  129. case 'MultiLineString':
  130. geometries.push(new GeoJSONLineStringGeometry(geo.coordinates));
  131. }
  132. var region = new GeoJSONRegion(properties[nameProperty || 'name'], geometries, properties.cp);
  133. region.properties = properties;
  134. return region;
  135. });
  136. }