Extract X and Y coordinates from a spatial table in Spatialite

A quick note on adding two columns with X and Y values in a vector layer in spatialite. Spatialite stores the information about the geometry in binary format in the ‘Geometry’ field.

You can use the Spatialite function AsText() to convert and extract these values as a Well Known Text (WKT), which is an easy to read format. If you for example have a spatial table ‘Waypoints’ with two columns ‘PKUID’ and ‘Geometry’, you can extract a column with WKT values using:

SELECT AsText(Geometry) FROM Waypoints 

This gives you a column with values that look like ‘MULTIPOINT(X-coordinate, Y-coordinate)‘.

To extract the X and Y values separately, you can use the functions X() and Y(). The following SQL statement creates a new table ‘Waypoints2′ with the column ‘PKUID’ and two columns X and Y that hold the X and Y coordinates respectively.

CREATE TABLE 'Waypoints2' AS

SELECT PKUID, X(Geometry) as X, Y(Geometry) as Y

FROM Waypoints

For more information, see this spatialite tutorial.

About these ads

One thought on “Extract X and Y coordinates from a spatial table in Spatialite

  1. Pingback: From xy table to spatial table in spatialite | Ecostudies

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s