Function Repository Resource:

ToWellKnownText

Source Notebook

Convert graphics primitives to well-known text (WKT)

Contributed by: Anton Antonov

ResourceFunction["ToWellKnownText"][arg]

Converts graphics primitives in arg into Well-known text strings.

Details

ResourceFunction["ToWellKnownText"] converts graphics primities Point, Line, Polygon into Well-Known Text (WKT) strings.
Point[{x_,y_, ___}] is converted into a string that starts with "POINT".
Point[{p1,p2,p3,..}] is converted into a string that starts with "MULTIPOINT".
Line[{p1, p2, …}] is converted to a string that starts with "LINE".
Line[{{p11, p12, …}, {p21, p22, …}, …}] is converted to a string that starts with "MULTILINESTRING".
Polygon[{p1,p2,}] is converted to a string that starts with "POLYGON".
Polygon[{{p11, p12, …}, {p21, p22, …}, …}] is converted to a string that starts with "MULTIPOLYGON".
Lists of graphics primitives are converted to WKT strings that start with "GEOMETRYCOLLECTION".

Examples

Basic Examples (2) 

Here a graphics specification of three points is converted into a WKT string:

In[1]:=
ResourceFunction["ToWellKnownText"][Point[{{1, 1}, {1, 2}, {3, 4}}]]
Out[1]=

Here a multi-element graphics specification is converted into a WKT string:

In[2]:=
ResourceFunction[
 "ToWellKnownText"][{Point[{3, 3}], Line[{{1, 1}, {0, 2}, {2, 3}}], Polygon[{{0, 0}, {1, 0}, {0, 1}}]}]
Out[2]=

Scope (7) 

Over a single point:

In[3]:=
ResourceFunction["ToWellKnownText"][Point[{1, 2, 5}]]
Out[3]=

Over multiple points:

In[4]:=
ResourceFunction["ToWellKnownText"][
 Point[{{1, 2, 5}, {1, 4, 2}, {3, 3, 3}}]]
Out[4]=

Over a line:

In[5]:=
ResourceFunction["ToWellKnownText"][
 Line[{{1, 2, 5}, {1, 4, 2}, {3, 3, 3}}]]
Out[5]=

Over multiple lines:

In[6]:=
ResourceFunction["ToWellKnownText"][
 Line[{{{1, 2, 5}, {1, 4, 2}, {3, 3, 3}}, 10 {{1, 2, 5}, {1, 4, 2}, {3, 3, 3}}}]]
Out[6]=

Over a polygon:

In[7]:=
ResourceFunction["ToWellKnownText"][
 Polygon[{{0, 0}, {0, 1}, {1, 1}, {1, 0}}]]
Out[7]=

Over multiple polygons:

In[8]:=
ResourceFunction["ToWellKnownText"][
 Polygon[{{{0, 0}, {0, 1}, {1, 1}, {1, 0}}, 2*{{0.1, 0.1}, {0.1, 1}, {1, 1}, {1, 0.1}}}]]
Out[8]=

Over a collection of polygons and lines:

In[9]:=
ResourceFunction[
 "ToWellKnownText"][{Polygon[{{0, 0}, {0, 1}, {1, 1}, {1, 0}}], Polygon[2*{{0, 0}, {0, 1}, {1, 1}, {1, 0}}], Line[{{{1, 2, 5}, {1, 4, 2}, {3, 3, 3}}, 10 {{1, 2, 5}, {1, 4, 2}, {3, 3, 3}}}]}]
Out[9]=

Properties and Relations (2) 

Here is multi-element graphics:

In[10]:=
Graphics[{Point[{3, 3}], Line[{{1, 1}, {0, 2}, {2, 3}}], Polygon[{{0, 0}, {1, 0}, {0, 1}}]}, Frame -> True]
Out[10]=

Here is the corresponding WKT string:

In[11]:=
ResourceFunction[
 "ToWellKnownText"][{Point[{3, 3}], Line[{{1, 1}, {0, 2}, {2, 3}}], Polygon[{{0, 0}, {1, 0}, {0, 1}}]}]
Out[11]=

Multi-polygon specification:

In[12]:=
Graphics[{FaceForm[Opacity[0.5]], Polygon[{{{0, 0}, {0, 1}, {1, 1}, {1, 0}}, 2*{{0.1, 0.1}, {0.1, 1}, {1, 1}, {1, 0.1}}}]}, Frame -> True]
Out[12]=

Here is the corresponding WKT string:

In[13]:=
ResourceFunction["ToWellKnownText"][
 Polygon[{{{0, 0}, {0, 1}, {1, 1}, {1, 0}}, 2*{{0.1, 0.1}, {0.1, 1}, {1, 1}, {1, 0.1}}}]]
Out[13]=

Possible Issues (2) 

ToWellKnownText does not check the dimensions of the given graphics specs.


Well-known Text (WKT) specifications allow for representation of polygons with holes. For example:

POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))

The function ToWellKnownText does not make WKT strings for polygons-with-holes.

Publisher

Anton Antonov

Version History

  • 1.0.0 – 12 September 2022

Related Resources

Author Notes

Well-known Text (WKT) is used a lot in Geo-computations. A closely related format is Well-known Binary (WKB). WKT and WKB are often converted to GeoJSON.

License Information