BigQuery Geospatial Functions - ST_IsClosed and ST_IsRing
Kannappan Sirchabesan
Data Engineer
Michael Entin
Software Engineer
Try Google Cloud
Start building on Google Cloud with $300 in free credits and 20+ always free products.
Free trialGeospatial data analytics lets you use location data (latitude and longitude) to get business insights. It's used for a wide variety of applications in industry, such as package delivery logistics services, ride-sharing services, autonomous control of vehicles, real estate analytics, and weather mapping.
BigQuery, Google Cloud’s large-scale data warehouse, provides support for analyzing large amounts of geospatial data. This blog post discusses two geography functions we've recently added in order to expand the capabilities of geospatial analysis in BigQuery: ST_IsClosed
and ST_IsRing
.
BigQuery geospatial functions
In BigQuery, you can use the GEOGRAPHY
data type to represent geospatial objects like points, lines, and polygons on the Earth’s surface. In BigQuery, geographies are based on the Google S2 Library, which uses Hilbert space-filling curves to perform spatial indexing to make the queries run efficiently. BigQuery comes with a set of geography functions that let you process spatial data using standard ANSI-compliant SQL. (If you're new to using BigQuery geospatial analytics, start with Get started with geospatial analytics, a tutorial that uses BigQuery to analyze and visualize the popular NYC Bikes Trip dataset.)
The new ST_IsClosed
and ST_IsRing
functions are boolean accessor functions that help determine whether a geographical object (a point, a line, a polygon, or a collection of these objects) is closed or is a ring. Both of these functions accept a GEOGRAPHY
column as input and return a boolean value.
The following diagram provides a visual summary of the types of geometric objects.
For more information about these geometric objects, see Well-known text representation of geometry in Wikipedia.
Is the object closed? (ST_IsClosed)
The ST_IsClosed
function examines a GEOGRAPHY
object and determines whether each of the elements of the object has an empty boundary. The boundary for each element is defined formally in the ST_Boundary
function. The following rules are used to determine whether a GEOGRAPHY
object is closed:
A point is always closed.
A linestring is closed if the start point and end point of the linestring are the same.
A polygon is closed only if it's a full polygon.
A collection is closed if every element in the collection is closed.
An empty GEOGRAPHY object is not closed.
Is the object a ring? (ST_IsRing)
The other new BigQuery geography function is ST_IsRing
. This function determines whether a GEOGRAPHY
object is a linestring and whether the linestring is both closed and simple. A linestring is considered closed as defined by the ST_IsClosed
function. The linestring is considered simple if it doesn't pass through the same point twice, with one exception: if the start point and end point are the same, the linestring forms a ring. In that case, the linestring is considered simple.
Seeing the new functions in action
The following query shows you what theST_IsClosed
and ST_IsRing
function return for a variety of geometric objects. The query creates a series of ad-hoc geography objects and uses the UNION ALL
statement to create a set of inputs. The query then calls the ST_IsClosed
and ST_IsRing
functions to determine whether each of the inputs are closed or are rings. You can run this query in the BigQuery SQL workspace page in the Google Cloud console.The console shows the following results. You can see in the is_closed
and is_ring
columns what each function returns for the various input geography objects.
The new functions with real-world geography objects
In this section, we show queries using linestring objects that represent line segments that connect some of the cities in Europe. We show the various geography objects on maps and then discuss the results that you get when you call ST_IsClosed
and ST_IsRing
for these geography objects.
You can run the queries by using the BigQuery Geo Viz tool. The maps are the output of the tool. In the tool you can click the Show results button to see the values that the functions return for the query.
Start point and end point are the same, no intersection
In the first example, the query creates a linestring object that has three segments. The segments are defined by using four sets of coordinates: the longitude and latitude for London, Paris, Amsterdam, and then London again, as shown in the following map created by the Geo Viz tool:
The query looks like the following:
In the example table that's created by the query, the columns with the function values show the following:
ST_IsClosed
returnstrue
. The start point and end point of the linestring are the same.ST_IsRing
returnstrue
. The geography is closed, and it's also simple because there are no self-intersections.
Start point and end point are different, no intersection
Another scenario is when the start and end points are different. For example, imagine two segments that connect London to Paris and then Paris to Amsterdam, as in this map:
The following query represents this set of coordinates:
This time, the ST_IsClosed
and ST_IsRing
functions return the following values:
ST_IsClosed
returnsfalse
. The start point and end point of the linestring are different.ST_IsRing
returnsfalse
. The linestring is not closed. It's simple because there are no self-intersections, butST_IsRing
returnstrue
only when the geometry is both closed and simple.
Start point and end point are the same, with intersection
The third example is a query that creates a more complex geography. In the linestring, the start point and end point are the same. However, unlike the earlier example, the line segments of the linestring intersect. A map of the segments shows connections that go from London to Zürich, then to Paris, then to Amsterdam, and finally back to London:
In the following query, the linestring object has five sets of coordinates that define the four segments:
In the query, ST_IsClosed
and ST_IsRing
return the following values:
ST_IsClosed
returnstrue
. The start point and end point are the same, and the linestring is closed despite the self-intersection.ST_IsRing
returnsfalse
. The linestring is closed, but it's not simple because of the intersection.
Start point and end point are different, with intersection
In the last example, the query creates a linestring that has three segments that connect four points: London, Zürich, Paris, and Amsterdam. On a map, the segments look like the following:
The query is as follows:
The new functions return the following values:
ST_IsClosed
returnsfalse
. The start point and end point are not the same.ST_IsRing
returnsfalse
. The linestring is not closed and it's not simple.
Try it yourself
Now that you've got an idea of what you can do with the new ST_IsClosed
and ST_IsRing
functions, you can explore more on your own. For details about the individual functions, read the ST_IsClosed
and ST_IsRing
entries in the BigQuery documentation. To learn more about the rest of the geography functions available in BigQuery Geospatial, take a look at the BigQuery geography functions page.
Thanks to Chad Jennings, Eric Engle and Jing Jing Long for their valuable support to add more functions to BigQuery Geospatial. Thank you Mike Pope for helping review this article.