import { GISTypeDetails , Subtype } from "./interfaces" ; import { GIS_SUBTYPE_NAME } from "./constants" ; export const getGISTypeDetails = ( modifier ) : GISTypeDetails => { const allZeroesHopefully = modifier >> 24 ; if ( allZeroesHopefully !== 0 ) { throw new Error ( "Unsupported PostGIS modifier" ) ; } const srid = ( ( modifier & 0x0fffff00 ) - ( modifier & 0x10000000 ) ) >> 8 ; const subtype = ( modifier & 0x000000fc ) >> 2 ; const hasZ = ( modifier & 0x00000002 ) >> 1 === 1 ; const hasM = ( modifier & 0x00000001 ) === 1 ; if ( subtype !== 0 && subtype !== 1 && subtype !== 2 && subtype !== 3 && subtype !== 4 && subtype !== 5 && subtype !== 6 && subtype !== 7 ) { throw new Error ( `Unsupported PostGIS modifier, expected 0-7, received ${ subtype } (${ modifier })` ) ; } return { subtype , hasZ , hasM , srid , } ; } ; export const getGISTypeModifier = ( subtype , hasZ , hasM , srid ) : number => { return ( ( ( srid & 0x001fffff ) << 8 ) + ( ( subtype & 0x0000003f ) << 2 ) + ( hasZ ? 0x00000002 : 0 ) + ( hasM ? 0x00000001 : 0 ) ) ; } ; export const getGISTypeName = ( subtype , hasZ , hasM ) : string => { return `${ GIS_SUBTYPE_NAME [ subtype ] }${ hasZ ? "Z" : "" }${ hasM ? "M" : "" }` ; } ;