(feature, sourceCRS = L.CRS.EPSG4326, targetCRS)
| 31 | * ``` |
| 32 | */ |
| 33 | export var transform = function (feature, sourceCRS = L.CRS.EPSG4326, targetCRS) { |
| 34 | let selfFeatures = null; |
| 35 | let selfCallback = null; |
| 36 | //将数据统一为 geojson 格式处理: |
| 37 | if (["FeatureCollection", "Feature", "Geometry"].indexOf(feature.type) === -1) { |
| 38 | if (feature.toGeoJSON) { |
| 39 | feature = feature.toGeoJSON(); |
| 40 | } else if (feature instanceof L.LatLngBounds) { |
| 41 | feature = L.rectangle(feature).toGeoJSON(); |
| 42 | } else if (feature instanceof L.Bounds) { |
| 43 | feature = L.rectangle([[feature.getTopLeft().x, feature.getTopLeft().y], |
| 44 | [feature.getBottomRight().x, feature.getBottomRight().y]]).toGeoJSON(); |
| 45 | } else { |
| 46 | throw new Error("This tool only supports data conversion in geojson format or Vector Layers of Leaflet.") |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | //geojson 几种数据类型及处理形式 |
| 51 | const parseCoords = { |
| 52 | "point": function (array) { |
| 53 | return selfCallback(array); |
| 54 | }, |
| 55 | |
| 56 | "multipoint": function (array) { |
| 57 | return parseCoords["linestring"].apply(this, [array]) |
| 58 | }, |
| 59 | |
| 60 | "linestring": function (array) { |
| 61 | let points = []; |
| 62 | let p = null; |
| 63 | for (let i = 0, len = array.length; i < len; ++i) { |
| 64 | try { |
| 65 | p = parseCoords["point"].apply(this, [array[i]]); |
| 66 | } catch (err) { |
| 67 | throw err; |
| 68 | } |
| 69 | points.push(p); |
| 70 | } |
| 71 | return points; |
| 72 | }, |
| 73 | |
| 74 | "multilinestring": function (array) { |
| 75 | return parseCoords["polygon"].apply(this, [array]); |
| 76 | }, |
| 77 | |
| 78 | "polygon": function (array) { |
| 79 | let rings = []; |
| 80 | let l; |
| 81 | for (let i = 0, len = array.length; i < len; ++i) { |
| 82 | try { |
| 83 | l = parseCoords["linestring"].apply(this, [array[i]]); |
| 84 | } catch (err) { |
| 85 | throw err; |
| 86 | } |
| 87 | rings.push(l); |
| 88 | } |
| 89 | return rings; |
| 90 | }, |
no test coverage detected