(pUGGeo)
| 77 | * // => {"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,1],[0,0]]]} |
| 78 | */ |
| 79 | export function ugGeometry2Geojson(pUGGeo) { |
| 80 | // if (!pUGGeo) { |
| 81 | // throw new Error("No UGC geometry pointer provided"); |
| 82 | // } |
| 83 | if (!pUGGeo) { |
| 84 | return null; |
| 85 | } |
| 86 | const geomType = window.ugcModule._UGCWasm_Geometry_GetType(pUGGeo); |
| 87 | switch (geomType) { |
| 88 | case 1: { |
| 89 | // UGGeoPoint |
| 90 | const point2d = []; |
| 91 | var x = window.ugcModule._UGCWasm_GeoPoint_GetX(pUGGeo); |
| 92 | var y = window.ugcModule._UGCWasm_GeoPoint_GetY(pUGGeo); |
| 93 | point2d.push(x, y) |
| 94 | |
| 95 | // create geojson point |
| 96 | const pointJson = { |
| 97 | type: "Point", |
| 98 | coordinates: point2d |
| 99 | }; |
| 100 | |
| 101 | return pointJson |
| 102 | } |
| 103 | case 3: { |
| 104 | // UGGeoLine |
| 105 | const outlines = []; |
| 106 | // get part count |
| 107 | const partCount = window.ugcModule._UGCWasm_GeoLine_GetPartCount(pUGGeo); |
| 108 | for (let j = 0; j < partCount; j++) { |
| 109 | // get part j point count |
| 110 | var count = window.ugcModule._UGCWasm_GeoLine_GetPartPointCount(pUGGeo, j); |
| 111 | // 一个double是8个字节,而一个point2D是两个double,所以需要申请 点个数 * 2 * 8 |
| 112 | const pBuffer = window.ugcModule._malloc(count * 2 * 8); |
| 113 | |
| 114 | // get part j points |
| 115 | window.ugcModule._UGCWasm_GeoLine_GetPart2(pUGGeo, pBuffer, j); |
| 116 | |
| 117 | // Float64Array to line part coordinates |
| 118 | const view = new Float64Array(window.ugcModule.HEAPF64.buffer, pBuffer, count * 2); |
| 119 | const coords = []; |
| 120 | for (let i = 0; i < count * 2; i = i + 2) { |
| 121 | coords.push([view[i], view[i + 1]]); |
| 122 | } |
| 123 | window.ugcModule._free(pBuffer); |
| 124 | |
| 125 | outlines.push(coords); |
| 126 | } |
| 127 | |
| 128 | // create geojson linestring |
| 129 | const linestringJson = { |
| 130 | type: "LineString", |
| 131 | coordinates: outlines[0] |
| 132 | }; |
| 133 | |
| 134 | return linestringJson |
| 135 | } |
| 136 | case 5: { |
no test coverage detected