#### Compute geographical data in TypeScript project!

08 mai 2018 clemdesign

You look for a library able to compute geographical data (distance, ellipsoids, polygones, bounds, track lengths...), the solution is TsGeo...

`tsgeo` is an adaptation of phpgeo by Marcus Jaschen.

This project is developed in order to have a simple solution to do geographical operation for TypeScript projects, language used in recent mobile projects (Angular, Ionic, ...)

This project is available under MIT lisense at https://github.com/clemdesign/typescript-tsgeo.

## Possibilities

TsGeo provides several geometry classes:

• Coordinate
• Line
• Polyline
• Polygon

A Coordinate represents a geographic location, i. e. it contains a latitude and a longitude - together with an so called Ellipsoid.

A Line consists of two coordinates, while polylines and polygons are built from two or more coordinates.

### Distance between 2 coordinates

`TsGeo` offers 2 possibilities to compute distance between 2 coordinates:

``````// Vincenty’s Formula
import {Coordinate} from "tsgeo/Coordinate";
import {Vincenty}   from "tsgeo/Distance/Vincenty";

/* Add the following in a method of TS class */

let coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
let coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit

console.log(coordinate1.getDistance(coordinate2, new Vincenty())); // returns 128130.850 (meters; ≈128 kilometers)``````
``````// Haversine Formula
import {Coordinate} from "tsgeo/Coordinate";
import {Haversine}   from "tsgeo/Distance/Haversine";

/* Add the following in a method of TS class */

let coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
let coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit

console.log(coordinate1.getDistance(coordinate2, new Haversine())); // returns 128384.515 (meters; ≈128 kilometers)``````

### Line length

Operation on ellipsoid line is possible with `tsgeo`. The `Line` class provides a method to calculate its own length.

``````// Vincenty’s Formula
import {Coordinate} from "tsgeo/Coordinate";
import {Vincenty}   from "tsgeo/Distance/Vincenty";

/* Add the following in a method of TS class */

let line = new Line(
new Coordinate(52.5, 13.5),
new Coordinate(52.6, 13.4)
);

console.log(line.getLength(new Vincenty())); // returns 13013.849``````

### Bearing

Bearing is not yet implemented.

### Polyline

A polyline consists of an ordered list of locations (coordinates). The following operations are possible:

• Polyline length: length of all segments point to point.
• Segments operation: Array of `Line` with possibility to do operation on the object.
• Coordinate: List all latitudes and/or longitudes.
``````// >>> Define a polyline
//----------------------
import {Coordinate} from "tsgeo/Coordinate";
import {Polyline}   from "tsgeo/Polyline";

/* Add the following in a method of TS class */

let track = new Polyline();

// >>> Get length of each segments
//--------------------------------
let index = 1;
for (const segment of track.getSegments()) {
console.log('Segment ' + index + ' length: ' +
(segment.getLength(new Vincenty()) / 1000).toString() +
' kilometers');
index++;
}
/* Display:
Segment 1 length: 275.154731 kilometers
Segment 2 length: 143.797789 kilometers
*/

// >>> Get length of each segments, where points are reversed
//-----------------------------------------------------------
let index = 1;
let reverseTrack = track.getReverse();
for (const segment of reverseTrack.getSegments()) {
console.log('Segment ' + index + ' length: ' +
(segment.getLength(new Vincenty()) / 1000).toString() +
' kilometers');
index++;
}
/* Display:
Segment 1 length: 143.797789 kilometers
Segment 2 length: 275.154731 kilometers
*/

// >>> Get polyline length
//------------------------
console.log('Polyline length: ' +
(track.getLength(new Vincenty()) / 1000).toString() +
' kilometers');
/* Display:
Length: 418.95252 kilometers
*/
``````

### Polygon

A polygon consists of an ordered list of locations (coordinates) like Polyline but its start and end points are connected.

``````// >>> Define a polygon
//----------------------
import {Coordinate} from "tsgeo/Coordinate";
import {Polygon}   from "tsgeo/Polygon";

/* Add the following in a method of TS class */

let polygon = new Polygon();

// >>> Get length of each segments
//--------------------------------
let index = 1;
for (const segment of polygon.getSegments()) {
console.log('Segment ' + index + ' length: ' +
(segment.getLength(new Vincenty()) / 1000).toString() +
' kilometers');
index++;
}
/* Display:
Segment 1 length: 275.154731 kilometers
Segment 2 length: 143.797789 kilometers
Segment 3 length: 340.283134 kilometers
*/

// >>> Get length of each segments, where points are reversed
//-----------------------------------------------------------
let index = 1;
let reversePolygon = polygon.getReverse();
for (const segment of reversePolygon.getSegments()) {
console.log('Segment ' + index + ' length: ' +
(segment.getLength(new Vincenty()) / 1000).toString() +
' kilometers');
index++;
}
/* Display:
Segment 1 length: 143.797789 kilometers
Segment 2 length: 275.154731 kilometers
Segment 3 length: 340.283134 kilometers
*/

// >>> Get Area and Perimeter
//------------------------
console.log(Polygon Area = ' + (polygon.getArea() / 1000000) + ' km², <br>' +
'Perimeter = ' + (polygon.getPerimeter(new Vincenty()) / 1000) + ' m);
/* Display:
Polygon Area = 17773.260742317438 km²,
Perimeter = 742.16571 m
*/
``````

### Format and Output

TsGeo allow to format the output in different styles:

• Decimal Degrees
• Decimal Minutes
• DMS
• GeoJSON

Decimal Degrees

``````import {Coordinate}       from "tsgeo/Coordinate";
import {DecimalDegrees}   from "tsgeo/Formatter/Coordinate/DecimalDegrees";

/* Add the following in a method of TS class */

let coordinate = new Coordinate(19.820664, -155.468066);

console.log(coordinate.format(new DecimalDegrees()))         // Display "19.82066 -155.46807"
console.log(coordinate.format(new DecimalDegrees(', ', 3)))  // Display "19.821 -155.468"``````

Decimal Minutes

``````import {Coordinate}       from "tsgeo/Coordinate";
import {DecimalMinutes}   from "tsgeo/Formatter/Coordinate/DecimalMinutes";

/* Add the following in a method of TS class */

let coordinate = new Coordinate(43.62310, -70.20787);
let formatter  = new DecimalMinutes();

console.log(coordinate.format(formatter))         // Display "43° 37.386′ -070° 12.472′"

formatter.setSeparator(', ')
.useCardinalLetters(true)
.setUnits(DecimalMinutes.UNITS_ASCII);

console.log(coordinate.format(formatter))         // Display "43° 37.386' N, 070° 12.472' W"
``````

DMS

``````import {Coordinate}  from "tsgeo/Coordinate";
import {DMS}         from "tsgeo/Formatter/Coordinate/DMS";

/* Add the following in a method of TS class */

let coordinate = new Coordinate(18.911306, -155.678268);
let formatter  = new DMS();

console.log(coordinate.format(formatter))         // Display "18° 54′ 41″ -155° 40′ 42″"

formatter.setSeparator(', ')
.useCardinalLetters(true)
.setUnits(DecimalMinutes.UNITS_ASCII);

console.log(coordinate.format(formatter))         // Display "18° 54' 41" N, 155° 40' 42" W"
``````

GeoJSON

``````import {Coordinate}    from "tsgeo/Coordinate";
import {GeoJSON}       from "tsgeo/Formatter/Coordinate/GeoJSON";

/* Add the following in a method of TS class */

let coordinate = new Coordinate(18.911306, -155.678268);

console.log(coordinate.format(new GeoJSON()))         // Display "{"type":"Point","coordinates":[-155.678268,18.911306]}"``````