import * as L from "leaflet" ; import { GeodesicOptions } from "./geodesic-core" ; import { GeodesicGeometry , Statistics } from "./geodesic-geom" ; import { latlngExpressiontoLatLng , latlngExpressionArraytoLatLngArray } from "../src/types-helper" ; /**
 * Draw geodesic lines based on L.Polyline
 */ export class GeodesicLine extends L . Polyline { /** these should be good for most use-cases */ defaultOptions : GeodesicOptions = { wrap : true , steps : 3 } ; /** does the actual geometry calculations */ readonly geom : GeodesicGeometry ; /** use this if you need some detailled info about the current geometry */ statistics : Statistics = { } as any ; /** stores all positions that are used to create the geodesic line */ points : L . LatLng [ ] [ ] = [ ] ; constructor ( latlngs ? , options ? ) { super ( [ ] , options ) ; L . Util . setOptions ( this , { ... this . defaultOptions , ... options } ) ; this . geom = new GeodesicGeometry ( this . options ) ; if ( latlngs !== undefined ) { this . setLatLngs ( latlngs ) ; } } /** calculates the geodesics and update the polyline-object accordingly */ private updateGeometry ( ) { let geodesic = [ ] ; geodesic = this . geom . multiLineString ( this . points ) ; this . statistics = this . geom . updateStatistics ( this . points , geodesic ) ; if ( ( this . options as GeodesicOptions ) . wrap ) { const split = this . geom . splitMultiLineString ( geodesic ) ; super . setLatLngs ( split ) ; } else { super . setLatLngs ( this . geom . wrapMultiLineString ( geodesic ) ) ; } } param latlngs setLatLngs ( latlngs ) { this . points = latlngExpressionArraytoLatLngArray ( latlngs ) ; this . updateGeometry ( ) ; return this ; } param latlng param latlngs addLatLng ( latlng , latlngs ? ) { const point = latlngExpressiontoLatLng ( latlng ) ; if ( this . points . length === 0 ) { this . points . push ( [ point ] ) ; } else { if ( latlngs === undefined ) { this . points [ this . points . length - 1 ] . push ( point ) ; } else { latlngs . push ( point ) ; } } this . updateGeometry ( ) ; return this ; } param input fromGeoJson ( input ) { let latlngs = [ ] ; let features = [ ] ; if ( input . type === "FeatureCollection" ) { features = input . features ; } else if ( input . type === "Feature" ) { features = [ input ] ; } else if ( [ "MultiPoint" , "LineString" , "MultiLineString" , "Polygon" , "MultiPolygon" ] . includes ( input . type ) ) { features = [ { type : "Feature" , geometry : input , properties : { } } ] ; } else { console . log ( `[Leaflet.Geodesic] fromGeoJson() - Type "${ input . type }" not supported.` ) ; } features . forEach ( ( feature ) => { switch ( feature . geometry . type ) { case "MultiPoint" : case "LineString" : latlngs = [ ... latlngs , ... [ L . GeoJSON . coordsToLatLngs ( feature . geometry . coordinates , 0 ) ] ] ; break ; case "MultiLineString" : case "Polygon" : latlngs = [ ... latlngs , ... L . GeoJSON . coordsToLatLngs ( feature . geometry . coordinates , 1 ) ] ; break ; case "MultiPolygon" : feature . geometry . coordinates . forEach ( ( item ) => { latlngs = [ ... latlngs , ... L . GeoJSON . coordsToLatLngs ( item , 1 ) ] ; } ) ; break ; default : console . log ( `[Leaflet.Geodesic] fromGeoJson() - Type "${ feature . geometry . type }" not supported.` ) ; } } ) ; if ( latlngs . length ) { this . setLatLngs ( latlngs ) ; } return this ; } param start param dest return distance ( start , dest ) { return this . geom . distance ( latlngExpressiontoLatLng ( start ) , latlngExpressiontoLatLng ( dest ) ) ; } }