import destination from '@turf/destination' ; import envelope from '@turf/envelope' ; import * as turfHelpers from '@turf/helpers' ; import bbox from '@turf/bbox' ; export function envelopeBufferFromPoint ( point , radius ) { var nwPoint = destination ( point , radius , 315 , { 'units' : 'meters' } ) ; var sePoint = destination ( point , radius , 135 , { 'units' : 'meters' } ) ; return envelope ( turfHelpers . featureCollection ( [ nwPoint , sePoint ] ) ) ; } export function bboxFromPolygon ( polygon ) { var bboxCoords = bbox ( polygon ) ; return { "minX" : bboxCoords [ 0 ] , "minY" : bboxCoords [ 1 ] , "maxX" : bboxCoords [ 2 ] , "maxY" : bboxCoords [ 3 ] } ; } function cleanProperties ( og_props ) { var new_props = { } ; for ( var prop of Object . keys ( og_props ) ) { new_props [ prop . toLocaleLowerCase ( ) . replace ( " " , "_" ) ] = og_props [ prop ] ; } return new_props ; } export function reverseLineString ( line ) { var reverseLineFeature = JSON . parse ( JSON . stringify ( line ) ) ; if ( reverseLineFeature . geometry && reverseLineFeature . geometry . coordinates ) { reverseLineFeature . geometry . coordinates . reverse ( ) ; return reverseLineFeature ; } else { var reverseLine = JSON . parse ( JSON . stringify ( line ) ) ; reverseLine . coordinates . reverse ( ) ; return reverseLine ; } } export class CleanedPoints { clean : turfHelpers . Feature < turfHelpers . Point > [ ] = [ ] ; invalid : any [ ] = [ ] ; constructor ( inputData ) { try { var inputFeatures = [ ] ; if ( inputData . type === "FeatureCollection" ) { inputFeatures = inputFeatures . concat ( inputData . features ) ; } else if ( inputData . type === "Feature" ) { inputFeatures . push ( inputData ) ; } else if ( inputData . type === "GeometryCollection" ) { for ( var geometry of inputData . geometies ) { inputFeatures . push ( { type : "Feature" , properties : { } , geometry : geometry } ) ; } } else if ( inputData . type === "Point" ) { inputFeatures . push ( { type : "Feature" , properties : { } , geometry : inputData } ) ; } else { this . invalid . push ( inputData ) ; } for ( var inputFeature of inputFeatures ) { inputFeature . properties = cleanProperties ( inputFeature . properties ) ; if ( inputFeature . geometry . type === "Point" ) { this . clean . push ( inputFeature ) ; } else { this . invalid . push ( inputFeature ) ; } } } catch ( e ) { throw e ; } } } export class CleanedLines { clean : turfHelpers . Feature < turfHelpers . LineString > [ ] = [ ] ; invalid : any [ ] ; constructor ( inputData ) { this . invalid = [ ] ; try { var inputFeatures = [ ] ; if ( inputData . type === "FeatureCollection" ) { inputFeatures = inputFeatures . concat ( inputData . features ) ; } else if ( inputData . type === "Feature" ) { inputFeatures . push ( inputData ) ; } else if ( inputData . type === "GeometryCollection" ) { for ( var geometry of inputData . geometies ) { inputFeatures . push ( { type : "Feature" , properties : { } , geometry : geometry } ) ; } } else if ( inputData . type === "LineString" || inputData . type === "MultiLineString" ) { inputFeatures . push ( { type : "Feature" , properties : { } , geometry : inputData } ) ; } else { this . invalid . push [ inputData ] ; } for ( var inputFeature of inputFeatures ) { inputFeature . properties = cleanProperties ( inputFeature . properties ) ; if ( inputFeature . geometry . type === "LineString" ) { if ( this . validLength ( inputFeature ) ) this . clean . push ( inputFeature ) ; else this . invalid . push ( inputFeature ) ; } else if ( inputFeature . geometry . type === "MultiLineString" ) { if ( inputFeature . geometry . coordinates . length == 1 ) { inputFeature . geometry . coordinates = inputFeature . geometry . coordinates [ 0 ] ; inputFeature . geometry . type = "LineString" ; if ( this . validLength ( inputFeature ) ) this . clean . push ( inputFeature ) ; else this . invalid . push ( inputFeature ) ; } else if ( inputFeature . geometry . coordinates . length > 1 ) { var newFeature = JSON . parse ( JSON . stringify ( inputFeature ) ) ; ; newFeature . geometry . type = "LineString" ; newFeature . geometry . coordinates = [ ] ; for ( var lineStringCoordinates of inputFeature . geometry . coordinates ) { if ( newFeature . geometry . coordinates . length == 0 ) { newFeature . geometry . coordinates = lineStringCoordinates ; } else if ( newFeature . geometry . coordinates [ newFeature . geometry . coordinates . length - 1 ] [ 0 ] === lineStringCoordinates [ 0 ] [ 0 ] && newFeature . geometry . coordinates [ newFeature . geometry . coordinates . length - 1 ] [ 1 ] === lineStringCoordinates [ 0 ] [ 1 ] ) { newFeature . geometry . coordinates . splice ( - 1 ) ; newFeature . geometry . coordinates = newFeature . geometry . coordinates . concat ( lineStringCoordinates ) ; } else { if ( this . validLength ( newFeature ) ) this . clean . push ( newFeature ) ; else this . invalid . push ( newFeature ) ; newFeature = JSON . parse ( JSON . stringify ( inputFeature ) ) ; ; newFeature . geometry . type = "LineString" ; newFeature . geometry . coordinates = lineStringCoordinates ; } } if ( newFeature . geometry . coordinates . length > 0 ) { if ( this . validLength ( newFeature ) ) this . clean . push ( newFeature ) ; else this . invalid . push ( newFeature ) ; } } else { this . invalid . push ( inputFeature ) ; } } else { this . invalid . push ( inputFeature ) ; } } } catch ( e ) { throw e ; } } validLength ( line ) { if ( line . geometry . coordinates . length > 1 ) return true ; else return false ; } }