TsGeo - Format and Output - Polygon

TsGeo allow to format Polygon class into GeoJSON output representation.

There are 2 ways to format output:

  • Using FormatterInterface
  • Using Polygon class

Using FormatterInterface

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

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

let polygon = new Polygon();

polygon.addPoint(new Coordinate(10, 20));
polygon.addPoint(new Coordinate(20, 40));
polygon.addPoint(new Coordinate(30, 40));
polygon.addPoint(new Coordinate(30, 20));

let formatter = new GeoJSON();

console.log(formatter.format(polygon)); 

This snippet display the following data:

{"type":"Polygon","coordinates":[[20,10],[40,20],[40,30],[20,30]]}

Using Polygon class

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

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

let polygon = new Polygon();

polygon.addPoint(new Coordinate(10, 20));
polygon.addPoint(new Coordinate(20, 40));
polygon.addPoint(new Coordinate(30, 40));
polygon.addPoint(new Coordinate(30, 20));

console.log(polygon.format(new GeoJSON())); 

This snippet display the following data:

{"type":"Polygon","coordinates":[[20,10],[40,20],[40,30],[20,30]]}

Polyline Retour


Ajouter un commentaire