import { Feature , FeatureCollection } from 'geojson' ; import { AbstractVectorSource , BoundsRequestMeta , GeoJsonWithMeta } from '../vector_source' ; import { EMPTY_FEATURE_COLLECTION , FIELD_ORIGIN , SOURCE_TYPES } from '../../../../common/constants' ; import { InlineFieldDescriptor , GeojsonFileSourceDescriptor , MapExtent , } from '../../../../common/descriptor_types' ; import { registerSource } from '../source_registry' ; import { IField } from '../../fields/field' ; import { getFeatureCollectionBounds } from '../../util/get_feature_collection_bounds' ; import { Adapters } from '../../../../../../../src/plugins/inspector/common/adapters' ; import { InlineField } from '../../fields/inline_field' ; function getFeatureCollection ( geoJson ) { if ( ! geoJson ) { return EMPTY_FEATURE_COLLECTION ; } if ( geoJson . type === 'FeatureCollection' ) { return geoJson ; } if ( geoJson . type === 'Feature' ) { return { type : 'FeatureCollection' , features : [ geoJson ] , } ; } return EMPTY_FEATURE_COLLECTION ; } export class GeoJsonFileSource extends AbstractVectorSource { static createDescriptor ( descriptor ) { return { type : SOURCE_TYPES . GEOJSON_FILE , __featureCollection : getFeatureCollection ( descriptor . __featureCollection ) , __fields : descriptor . __fields || [ ] , areResultsTrimmed : descriptor . areResultsTrimmed !== undefined ? descriptor . areResultsTrimmed : false , tooltipContent : descriptor . tooltipContent ? descriptor . tooltipContent : null , name : descriptor . name || 'Features' , } ; } constructor ( descriptor , inspectorAdapters ? ) { const normalizedDescriptor = GeoJsonFileSource . createDescriptor ( descriptor ) ; super ( normalizedDescriptor , inspectorAdapters ) ; } _getFields ( ) { const fields = ( this . _descriptor as GeojsonFileSourceDescriptor ) . __fields ; return fields ? fields : [ ] ; } createField ( { fieldName } ) { const fields = this . _getFields ( ) ; const descriptor = fields . find ( ( field ) => { return field . name === fieldName ; } ) ; if ( ! descriptor ) { throw new Error ( `Cannot find corresponding field ${ fieldName } in __fields array ${ JSON . stringify ( this . _getFields ( ) ) } ` ) ; } return new InlineField < GeoJsonFileSource > ( { fieldName : descriptor . name , source : this , origin : FIELD_ORIGIN . SOURCE , dataType : descriptor . type , } ) ; } async getFields ( ) { const fields = this . _getFields ( ) ; return fields . map ( ( field ) => { return new InlineField < GeoJsonFileSource > ( { fieldName : field . name , source : this , origin : FIELD_ORIGIN . SOURCE , dataType : field . type , } ) ; } ) ; } isBoundsAware ( ) { return true ; } async getBoundsForFilters ( boundsFilters , registerCancelCallback ) { const featureCollection = ( this . _descriptor as GeojsonFileSourceDescriptor ) . __featureCollection ; return getFeatureCollectionBounds ( featureCollection , false ) ; } async getGeoJsonWithMeta ( ) { return { data : ( this . _descriptor as GeojsonFileSourceDescriptor ) . __featureCollection , meta : { } , } ; } async getDisplayName ( ) { return ( this . _descriptor as GeojsonFileSourceDescriptor ) . name ; } hasTooltipProperties ( ) { return true ; } getSourceStatus ( ) { return { tooltipContent : ( this . _descriptor as GeojsonFileSourceDescriptor ) . tooltipContent , areResultsTrimmed : ( this . _descriptor as GeojsonFileSourceDescriptor ) . areResultsTrimmed , } ; } } registerSource ( { ConstructorFunction : GeoJsonFileSource , type : SOURCE_TYPES . GEOJSON_FILE , } ) ;