Skip to content

TRK_DistanceWithin takes a track column and a geometry column and returns a float column. The geometry type can be linestring or polygon. The float column contains the distance along the track that intersects the linestring or polygon. The result is returned in the units specified by output_unit. When output_unit is None, the result is in the units of the input track's spatial reference if it's projected; otherwise, the result is in meters. If the track crosses the geometry multiple times, the function returns the total distance traveled within the geometry.

If the input track and geometry both have a spatial reference defined but do not have the same spatial reference, the geometry will be transformed to the spatial reference of the track. If only one of the input columns has a spatial reference defined, the track and geometry are both assumed to have that spatial reference.

If the track and geometry have a projected spatial reference or have no spatial reference set, planar distance calculations will be used. Otherwise, geodesic distance calculations will be used. For more information see Coordinate systems and transformations.

Tracks are linestrings that represent the change in an entity's location over time. Each vertex in the linestring has a timestamp (stored as the M-value) and the vertices are ordered sequentially.

For more information on using tracks in GeoAnalytics Engine, see the core concept topic on tracks.

FunctionSyntax
Pythondistance_within(track, geometry, output_unit=None)
SQLTRK_DistanceWithin(track, geometry, output_unit)
ScaladistanceWithin(track, geometry, outputUnit)

For more details, go to the GeoAnalytics Engine API reference for distance_within.

Examples

PythonPythonSQLScala
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

from geoanalytics.sql import functions as ST
from geoanalytics.tracks import functions as TRK
from pyspark.sql import functions as F

data = [(1, "LINESTRING M (-117.27 34.05 1633455010, -117.22 33.91 1633456062, -116.96 33.64 1633457132)",
            "POLYGON((-117.22 33.91, -117.22 33.75, -117.05 33.91, -117.22 33.91))"),
        (2, "LINESTRING M (-116.89 33.96 1633575895, -116.71 34.01 1633576982, -116.66 34.08 1633577061)",
            "POLYGON((-116.80 34.05, -116.62 34.05, -116.72 33.98, -116.80 33.98, -116.80 34.05))"),
        (3, "LINESTRING M (-116.24 33.88 1633575234, -116.33 34.02 1633576336)",
            "POLYGON((-116.38 34.05, -116.20 34.05, -116.20 33.85, -116.38 33.85,-116.38 34.05))"),]

df = spark.createDataFrame(data, ["id", "trk_wkt", "poly_wkt"]) \
          .withColumn("track", ST.line_from_text("trk_wkt", 4326)) \
          .withColumn("polygon", ST.poly_from_text("poly_wkt", 4326))

df.select("id", F.round(TRK.distance_within("track", "polygon", "kilometers"), 3).alias("distance_within")).show()
Result
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
+---+---------------+
| id|distance_within|
+---+---------------+
|  1|         11.941|
|  2|         13.927|
|  3|         17.617|
+---+---------------+

Version table

ReleaseNotes

1.5.0

Python, SQL, and Scala functions introduced

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.