Alex Smith
Alex Smith

Reputation: 71

Convert well-known text or well-known binary into an ArcGIS Polygon

I have a geometry object in a database and I would like to render it on the front end using the ArcGIS JavaScript SDK.

var srid = 4326;           // or any other spatial reference
var wkt = 'POLYGON (...)'; // or wkb equivalent
var rings = ''             // <--- Conversion step I would like to make
const polygon = new Polygon({
    rings: rings,
    spatialReference: { wkid: srid },
});

Right now, I pass in well-known binary and well-known text. On the plus side, the code doesn't error out. On the negative side, the polygon doesn't appear on the map. Instead, I just get a blank map.


Edit: I solved it using terraformer-wkt-parser.

import wkt from 'terraformer-wkt-parser';
...
const geom: GeoJSON.Polygon = wkt.parse(geometryString) as GeoJSON.Polygon;
const coordinates = geom.coordinates.flat() as any;
const polygon = new Polygon({
    rings: coordinates,
    spatialReference: { wkid: 4326 },
});

Upvotes: 1

Views: 183

Answers (1)

Thomas
Thomas

Reputation: 10135

Seems like you already found a solution with terraformer-wkt-parser. However, terraformer-wkt-parser is deprecated and if possible, maybe consider using @terraformer/wkt instead:

// import { wktToGeoJSON } from 'https://esm.sh/@terraformer/wkt';
// import { Polygon } from 'https://esm.sh/@arcgis/[email protected]/geometry';

const srid = 4326;
const wkt = 'POLYGON((8.504 47.362, 8.508 47.363, 8.511 47.361, 8.504 47.362))'; 

const geojson = wktToGeoJSON(wkt);

const polygon = new Polygon({
  rings: geojson.coordinates,
  spatialReference: { wkid: srid },
});

I recently came across with the JSTS library which would be an alternative:

// import { 
//     WKTReader,
//     GeoJSONWriter,
// } from 'https://esm.sh/[email protected]/org/locationtech/jts/io';
// import { Polygon } from 'https://esm.sh/@arcgis/[email protected]/geometry';

const srid = 4326;
const wkt = 'POLYGON((8.504 47.362, 8.508 47.363, 8.511 47.361, 8.504 47.362))'; 

const geometry = new WKTReader().read(wkt);

const geojson = new GeoJSONWriter().write(geometry);

const polygon = new Polygon({
  rings: geojson.coordinates,
  spatialReference: { wkid: srid },
});

Addendum: There are other libraries such as wkt, wicket and wellknown. I just compile them here and hope it helps you and others to get an overview of libraries that parse a WKT string...

Let's start with wkt:

// import { parse } from 'https://esm.sh/wkt';
// import { Polygon } from 'https://esm.sh/@arcgis/[email protected]/geometry';

const srid = 4326;
const wkt = 'POLYGON((8.504 47.362, 8.508 47.363, 8.511 47.361, 8.504 47.362))'; 

const geojson = parse(wkt);

const polygon = new Polygon({
  rings: geojson.coordinates,
  spatialReference: { wkid: srid },
});

Here is an example using wicket:

// import Wkt from 'https://esm.sh/wicket';
// import { Polygon } from 'https://esm.sh/@arcgis/[email protected]/geometry';

const srid = 4326;
const wktStr = 'POLYGON((8.504 47.362, 8.508 47.363, 8.511 47.361, 8.504 47.362))'; 

const geometry = new Wkt.Wkt().read(wktStr);

const geojson = geometry.toJson();

const polygon = new Polygon({
  rings: geojson.coordinates,
  spatialReference: { wkid: srid },
});

And finally an example with wellknown:

// import wellknown from 'https://esm.sh/wellknown';
// import { Polygon } from 'https://esm.sh/@arcgis/[email protected]/geometry';

const srid = 4326;
const wkt = 'POLYGON((8.504 47.362, 8.508 47.363, 8.511 47.361, 8.504 47.362))'; 

const geojson = wellknown.parse(wkt);

const polygon = new Polygon({
  rings: geojson.coordinates,
  spatialReference: { wkid: srid },
});

Upvotes: 1

Related Questions