Geography.ToWellKnownText

Geography.ToWellKnownText

D

The M Code Behind the Power Query M function Geography.ToWellKnownText

The Geography.ToWellKnownText M function is a powerful tool in Power Query that allows users to transform geographical data into a Well-Known Text (WKT) format. WKT is a text markup language that represents spatial objects in a human-readable format.

In this article, we will explore the M code behind the Geography.ToWellKnownText function and how it can be used to transform geographical data.

What is the Geography.ToWellKnownText function?

The Geography.ToWellKnownText function is a built-in function in Power Query that converts a geographical object to a Well-Known Text (WKT) string. The function takes a single argument, which is a geography value.

The geography value can be a point, line, or polygon. The function then converts the geography value to a WKT string, which can be used in various geographic information systems (GIS) applications.

How does the Geography.ToWellKnownText function work?

The Geography.ToWellKnownText function works by converting a geography value to a WKT string. The function uses the Open Geospatial Consortium (OGC) standard for WKT representation.

The WKT format represents a geography value as a string of text. The string is made up of different elements that represent the geometry of the spatial object. The elements include the type of geometry, the coordinates of the spatial object, and any other necessary information.

The Geography.ToWellKnownText function works by parsing the geography value and generating a string of text that conforms to the WKT standard.

Examples of using the Geography.ToWellKnownText function

Let’s take a look at some examples of using the Geography.ToWellKnownText function in Power Query.

Example 1: Converting a point to WKT

Suppose we have a table with a column named “Location,” which contains the latitude and longitude of different points.

To convert the points to WKT, we can use the following M code:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText(“i45WMjQwNLE0NjJQ0lFKSVEyNVHiDc5MqeozCjJSMjAyNjK3Ss7MygAVQA”, BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Location = _t]),

#”Changed Type” = Table.TransformColumnTypes(Source,{{“Location”, type geography}}),

#”Added Custom” = Table.AddColumn(#”Changed Type”, “WKT”, each Geography.ToWellKnownText([Location]))

in

#”Added Custom”


The code above creates a new column named "WKT" and uses the Geography.ToWellKnownText function to convert the geography value in the "Location" column to a WKT string.

Example 2: Converting a polygon to WKT

Suppose we have a table with a column named "Boundary," which contains the coordinates of the vertices of a polygon.

To convert the polygon to WKT, we can use the following M code:


let

Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText(“i45WMjQwNLE0NjJQ0lFKSVEyNVHiDc5MqeozCjJSMjAyNjK3Ss7MygAVQA”, BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Boundary = _t]),

#”Changed Type” = Table.TransformColumnTypes(Source,{{“Boundary”, type geography}}),

#”Added Custom” = Table.AddColumn(#”Changed Type”, “WKT”, each Geography.ToWellKnownText([Boundary]))

in

#”Added Custom”


The code above creates a new column named "WKT" and uses the Geography.ToWellKnownText function to convert the geography value in the "Boundary" column to a WKT string.

The Geography.ToWellKnownText M function is a powerful tool in Power Query that allows users to transform geographical data into a Well-Known Text (WKT) format. The function works by converting a geography value to a WKT string using the Open Geospatial Consortium (OGC) standard.

In this article, we explored the M code behind the Geography.ToWellKnownText function and provided examples of how to use the function to convert points and polygons to WKT. By using the Geography.ToWellKnownText function, users can easily work with spatial data in Power Query and perform powerful data transformations.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

Subject

Your Name (required)

Company/Organisation

Email (required)

Telephone

Training Course(s)

Your Message

Upload Example Document(s) (Zip multiple files)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: