Last updated

API / geotoolkit / util / UnitFactory / UnitFactory

Class: UnitFactory

util.UnitFactory.UnitFactory

Factory that creates Unit This class acts as a factory and a registry class that can convert string representation of units into plain unit object. Unit objects themselves can perform conversion of values.
This class embeds a default catalog of commonly used units. Units Tutorial in Geotoolkit/CarnacJS/Utils demonstrates how one could extend or replace units by creating and registering new units.
It lists the base units and shows how to convert and add new units to the factory as shown in the example below.
This class also holds the information used by the toolkit to convert device (screen) dimensions to rendering dimensions.
For example if we are rendering a track with width 1" on display or device size, this 1" can be measured through a ruler. But Javascript cannot set size in device coordinates, so we need Pixel per Inch (PPI, each device has different value) and Pixel per css Pixel (PPCP is what can be used in Javascript and is not same as Pixel).
UnitFactory Class has a static dictionary and can recognize all existing Apple devices and we have APIs to let predefined devices set correct values. The toolkit embeds a recognition algorithm that will try to retrieve those values from a dictionary by analysing the user-agent and other global properties of the browser. However, this algorithm will only recognize some of the predefined devices.
One could either extend this list or customize the algorithm or explicitly set those ppi & ppcp values.
The later is recommended for production application where the displayed scales are critical and the supported platforms list is open. The suggested approach is to provide the user with a calibration tool that will let him set the actual ratios manually.

Example

const unit = factory.getUnit('foot');
// unit is named unit.getName(), and display a unit.getQuantityType() in unit.getSymbol()
// convert to another length unit:
unit.convert(1500.00, 'm'); // 1500.00 ft = 457.20 m

// Add new unit for your own use
factory.addUnit({
// information about the new Unit
'name': 'pint',
'quantityType': 'volume', //or create your own
'symbol': 'pt',
'description': 'drink responsibly',
'baseUnitSymbol': 'm3',
// Computation Detail : newunit = (a+b*base)/(c+d*base)
'a': 0, 'b': 0.000473176, 'c': 1, 'd': 0
});

Hierarchy

Table of contents

Constructors
Methods

Contents

Constructors

new UnitFactory()

new UnitFactory()

Overrides

AbstractUnitFactory.constructor

Methods

addDevicePPIList

addDevicePPIList(list): UnitFactory

Adds device PPI list

Parameters

Name Type Description
listPPI[]Accept function returns true if device matched.

Returns

UnitFactory

this


addUnit

addUnit(name, quantityType, symbol, baseUnitSymbol, a, b, c, d, description?): UnitFactory

Adds a unit to factory. please reference the constructor in Unit for more details.

Parameters

Name Type Description
namestring | AbstractUnitunit name or unit inherited from AbstractUnit
quantityTypestring | string[]expected quantity type
symbolstringunit symbol
baseUnitSymbolstringbase unit symbol
anumberfactor
bnumberfactor
cnumberfactor
dnumberfactor
Optional descriptionstringdescription of the unit

Returns

UnitFactory

this


addUnitAlias

addUnitAlias(baseUnitSymbol, isCaseSensitive, alias?): UnitFactory

Adds a unit alias to unit factory

Parameters

Name Type Description
baseUnitSymbolstringbase unit symbol
isCaseSensitivebooleanisCaseSensitive case sensitivity flag
Optional aliasstring | string[]alias unit symbols

Returns

UnitFactory

this


addUnitClass

addUnitClass(unitInfo): UnitFactory

Parameters

Name Type
unitInfoUnitInfo

Returns

UnitFactory

addUnitClass(name, baseUnitSymbol?, unitSymbols?): UnitFactory

Adds a unit class to unit factory

Parameters

Name Type Description
namestringunit name or object
Optional baseUnitSymbolstringbase unit symbol
Optional unitSymbolsstring | string[]array of unit symbols

Returns

UnitFactory

this


clearDevicePPIList

clearDevicePPIList(): UnitFactory

Clears device PPI list

Returns

UnitFactory

this


clearUnitAlias

clearUnitAlias(): UnitFactory

Clears all unit alias from unit factory

Returns

UnitFactory

this


clearUnitClasses

clearUnitClasses(): UnitFactory

Clears all unit classes from unit factory

Returns

UnitFactory

this


clearUnits

clearUnits(): UnitFactory

Clears all units from unit factory except CSS units (pixel, point and pica)

Returns

UnitFactory

this


getCSSPixelPerInch

getCSSPixelPerInch(): number

Returns the number of CSS pixels (browser) per physical inch (device) CSS pixels per physical inch is different from pixels per inch of the device. It considers operating system and browser scaling and the ratio between browser inch and physical device inch

Returns

number


getClassName

getClassName(): string

Returns

string

Inherited from

AbstractUnitFactory.getClassName


getConvertableUnitSymbols

getConvertableUnitSymbols(unit): string[]

Returns all convertible unit symbols

Parameters

Name Type Description
unitstring | AbstractUnitunit to convert from/to

Returns

string[]


getPPI

getPPI(): number

Returns physical pixels per inch of the device

Returns

number


getTimeStamp

getTimeStamp(): number

Returns the time stamp (version) of unit factory. This value will be updated whenever modification made.

Returns

number

Overrides

AbstractUnitFactory.getTimeStamp


getUnit

getUnit(value, quantityType?, nullIfNotExist?): AbstractUnit

Returns an instance of unit based on specified information

Parameters

Name Type Description
valuestring | AbstractUnitrepresent the {string} name, {string} symbol or {AbstractUnit} unit to be created
Optional quantityTypestring[]expected quantity types
Optional nullIfNotExistbooleanreturn null if unit does not exist instead of returning a user-defined unit

Returns

AbstractUnit

Overrides

AbstractUnitFactory.getUnit


getUnitSymbolsByClass

getUnitSymbolsByClass(name): string[]

Return array of unit symbols by class name

Parameters

Name Type Description
namestringclass name

Returns

string[]


removeUnit

removeUnit(unit): UnitFactory

Removes a unit from factory.

Parameters

Name Type Description
unitstring | AbstractUnitunit name or unit inherited from AbstractUnit

Returns

UnitFactory

this


removeUnitAlias

removeUnitAlias(baseUnitSymbol, isCaseSensitive, alias): UnitFactory

Removes a unit alias from unit factory

Parameters

Name Type Description
baseUnitSymbolstringbase unit symbol
isCaseSensitivebooleancase sensitivity flag
aliasstring | string[]alias unit symbols

Returns

UnitFactory

this


removeUnitClass

removeUnitClass(name, unitSymbols?): UnitFactory

Removes a unit class from unit factory

Parameters

Name Type Description
namestring | Omit<UnitInfo, "baseunitsymbol">unit name or object
Optional unitSymbolsstring | string[]array of unit symbols

Returns

UnitFactory

this


setPPI

setPPI(ppi, ppcp): UnitFactory

Sets physical pixels per inch of the device

Parameters

Name Type Description
ppinumberpixel per inch
ppcpnumberpixel per css pixel (sometimes different from window.devicePixelRatio)

Returns

UnitFactory

this


updateDevicePPI

updateDevicePPI(): UnitFactory

Updates CSS pixels per physical inch

Returns

UnitFactory

this


getClassName

Static getClassName(): string

Returns

string

Inherited from

AbstractUnitFactory.getClassName


getDefaultFactory

Static getDefaultFactory(): UnitFactory

Returns instance of unit factory

Returns

UnitFactory

factory

Inherited from

AbstractUnitFactory.getDefaultFactory


getInstance

Static getInstance(): UnitFactory

Returns instance of unit factory

Returns

UnitFactory

factory


registerDefaults

Static registerDefaults(): UnitFactory

Returns instance of unit factory

Returns

UnitFactory

factory


setType

Static setType(type): void

Returns instance of unit factory

Parameters

Name Type Description
typetypeof UnitFactoryfactory ctor

Returns

void

Inherited from

AbstractUnitFactory.setType