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
. When output
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.
Planar distance calculations are used if the input tracks have a projected spatial reference or no spatial reference. Chordal distance calculations are used if the input tracks have a geographic spatial reference. If the track and geometry columns are in different spatial references, the function automatically transforms the geometry into the spatial reference of the track. 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.
Function | Syntax |
---|---|
Python | distance |
SQL | TRK |
Scala | distance |
For more details, go to the GeoAnalytics Engine API reference for distance_within.
Examples
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()
+---+---------------+
| id|distance_within|
+---+---------------+
| 1| 11.941|
| 2| 13.927|
| 3| 17.617|
+---+---------------+
Version table
Release | Notes |
---|---|
1.5.0 | Python, SQL, and Scala functions introduced |