流分析地理空间函数的简介Introduction to Stream Analytics geospatial functions

Azure 流分析中的地理空间函数支持对流式处理的地理空间数据进行实时分析。Geospatial functions in Azure Stream Analytics enable real-time analytics on streaming geospatial data. 只需几行代码,即可为复杂的方案开发出生产级解决方案。With just a few lines of code, you can develop a production grade solution for complex scenarios.

可从地理空间函数中受益的示例方案包括:Examples of scenarios that can benefit from geospatial functions include:

  • 拼车Ride-sharing
  • 车队管理Fleet management
  • 资产跟踪Asset tracking
  • 地域隔离Geo-fencing
  • 蜂窝站点之间的电话跟踪Phone tracking across cell sites

流分析查询语言具有七种内置地理空间函数:CreateLineString、CreatePoint、CreatePolygon、ST_DISTANCE、ST_OVERLAPS、ST_INTERSECTS 和 ST_WITHIN。Stream Analytics Query Language has seven built-in geospatial functions: CreateLineString, CreatePoint, CreatePolygon, ST_DISTANCE, ST_OVERLAPS, ST_INTERSECTS, and ST_WITHIN.

CreateLineStringCreateLineString

CreateLineString 函数接受点并返回 GeoJSON LineString,这可在地图上被标绘成一个线条。The CreateLineString function accepts points and returns a GeoJSON LineString, which can be plotted as a line on a map. 必须至少具有两个点才能创建 LineString。You must have at least two points to create a LineString. LineString 点将按顺序相连。The LineString points will be connected in order.

下面的查询使用 CreateLineString 创建使用三个点的 LineString。The following query uses CreateLineString to create a LineString using three points. 第一个点创建自流式处理输入数据,而另外两个点则是手动创建的。The first point is created from streaming input data, while the other two are created manually.

SELECT  
     CreateLineString(CreatePoint(input.latitude, input.longitude), CreatePoint(10.0, 10.0), CreatePoint(10.5, 10.5))  
FROM input  

输入示例Input example

latitudelatitude longitudelongitude
3.03.0 -10.2-10.2
-87.33-87.33 20.232120.2321

输出示例Output example

{"type" :"LineString", "coordinates" : [ [-10.2, 3.0], [10.0, 10.0], [10.5, 10.5] ]}{"type" : "LineString", "coordinates" : [ [-10.2, 3.0], [10.0, 10.0], [10.5, 10.5] ]}

{"type" :"LineString", "coordinates" : [ [20.2321, -87.33], [10.0, 10.0], [10.5, 10.5] ]}{"type" : "LineString", "coordinates" : [ [20.2321, -87.33], [10.0, 10.0], [10.5, 10.5] ]}

若要了解详细信息,请访问 CreateLineString 引用。To learn more, visit the CreateLineString reference.

CreatePointCreatePoint

CreatePoint 函数接受纬度和经度,并返回一个 GeoJSON 点,这可在地图上被标绘出来。The CreatePoint function accepts a latitude and longitude and returns a GeoJSON point, which can be plotted on a map. 维度和精度必须为 float 数据类型。Your latitudes and longitudes must be a float datatype.

下面的示例查询使用 CreatePoint 创建使用流式处理输入数据中的纬度和经度的点。The following example query uses CreatePoint to create a point using latitudes and longitudes from streaming input data.

SELECT  
     CreatePoint(input.latitude, input.longitude)  
FROM input 

输入示例Input example

latitudelatitude longitudelongitude
3.03.0 -10.2-10.2
-87.33-87.33 20.232120.2321

输出示例Output example

{"type" :"Point", "coordinates" : [-10.2, 3.0]}{"type" : "Point", "coordinates" : [-10.2, 3.0]}

{"type" :"Point", "coordinates" : [20.2321, -87.33]}{"type" : "Point", "coordinates" : [20.2321, -87.33]}

若要了解详细信息,请访问 CreatePoint 引用。To learn more, visit the CreatePoint reference.

CreatePolygonCreatePolygon

CreatePolygon 函数接受点并返回一条 GeoJSON 多边形记录。The CreatePolygon function accepts points and returns a GeoJSON polygon record. 这些点的顺序必须遵循向右侧的环方向,否则为逆时针方向。The order of points must follow right-hand ring orientation, or counter-clockwise. 假设以声明的顺序从一个点走到另一个点。Imagine walking from one point to another in the order they were declared. 在整个时间段内,此多边形的中心将偏向左侧。The center of the polygon would be to your left the entire time.

下面的示例查询使用 CreatePolygon 通过三个点来创建多边形。The following example query uses CreatePolygon to create a polygon from three points. 前两个点是手动创建的,最后一个点是通过输入数据创建的。The first two points are created manually, and the last point is created from input data.

SELECT  
     CreatePolygon(CreatePoint(input.latitude, input.longitude), CreatePoint(10.0, 10.0), CreatePoint(10.5, 10.5), CreatePoint(input.latitude, input.longitude))  
FROM input  

输入示例Input example

latitudelatitude longitudelongitude
3.03.0 -10.2-10.2
-87.33-87.33 20.232120.2321

输出示例Output example

{"type" :"Polygon", "coordinates" : [[ [-10.2, 3.0], [10.0, 10.0], [10.5, 10.5], [-10.2, 3.0] ]]}{"type" : "Polygon", "coordinates" : [[ [-10.2, 3.0], [10.0, 10.0], [10.5, 10.5], [-10.2, 3.0] ]]}

{"type" :"Polygon", "coordinates" : [[ [20.2321, -87.33], [10.0, 10.0], [10.5, 10.5], [20.2321, -87.33] ]]}{"type" : "Polygon", "coordinates" : [[ [20.2321, -87.33], [10.0, 10.0], [10.5, 10.5], [20.2321, -87.33] ]]}

若要了解详细信息,请访问 CreatePolygon 引用。To learn more, visit the CreatePolygon reference.

ST_DISTANCEST_DISTANCE

ST_DISTANCE 函数返回两个点之间的距离(以米为单位)。The ST_DISTANCE function returns the distance between two points in meters.

下面的查询使用 ST_DISTANCE 在加油站距离汽车不足 10 千米时生成事件。The following query uses ST_DISTANCE to generate an event when a gas station is less than 10 km from the car.

SELECT Cars.Location, Station.Location 
FROM Cars c  
JOIN Station s ON ST_DISTANCE(c.Location, s.Location) < 10 * 1000

若要了解详细信息,请访问 ST_DISTANCE 引用。To learn more, visit the ST_DISTANCE reference.

ST_OVERLAPSST_OVERLAPS

ST_OVERLAPS 函数比较两个多边形。The ST_OVERLAPS function compares two polygons. 如果多边形重叠,函数将返回 1。If the polygons overlap, the function returns a 1. 如果多边形不重叠,函数将返回 0。The function returns 0 if the polygons don't overlap.

下面的查询使用 ST_OVERLAPS 在某建筑位于可能发生洪灾的区域内时生成事件。The following query uses ST_OVERLAPS to generate an event when a building is within a possible flooding zone.

SELECT Building.Polygon, Building.Polygon 
FROM Building b 
JOIN Flooding f ON ST_OVERLAPS(b.Polygon, b.Polygon) 

下面的示例查询在风暴即将袭击汽车时生成事件。The following example query generates an event when a storm is heading towards a car.

SELECT Cars.Location, Storm.Course
FROM Cars c, Storm s
JOIN Storm s ON ST_OVERLAPS(c.Location, s.Course)

若要了解详细信息,请访问 ST_OVERLAPS 引用。To learn more, visit the ST_OVERLAPS reference.

ST_INTERSECTSST_INTERSECTS

ST_INTERSECTS 函数比较两个 LineString。The ST_INTERSECTS function compares two LineString. 如果 LineString 相交,函数将返回 1。If the LineString intersect, then the function returns 1. 如果 LineString 不相交,函数将返回 0。The function returns 0 if the LineString don't intersect.

下面的示例查询使用 ST_INTERSECTS 来确定柏油路是否与泥路相交。The following example query uses ST_INTERSECTS to determine if a paved road intersects a dirt road.

SELECT  
     ST_INTERSECTS(input.pavedRoad, input.dirtRoad)  
FROM input  

输入示例Input example

datacenterAreadatacenterArea stormAreastormArea
{"type":"LineString", "coordinates": [ [-10.0, 0.0], [0.0, 0.0], [10.0, 0.0] ]}{"type":"LineString", "coordinates": [ [-10.0, 0.0], [0.0, 0.0], [10.0, 0.0] ]} {"type":"LineString", "coordinates": [ [0.0, 10.0], [0.0, 0.0], [0.0, -10.0] ]}{"type":"LineString", "coordinates": [ [0.0, 10.0], [0.0, 0.0], [0.0, -10.0] ]}
{"type":"LineString", "coordinates": [ [-10.0, 0.0], [0.0, 0.0], [10.0, 0.0] ]}{"type":"LineString", "coordinates": [ [-10.0, 0.0], [0.0, 0.0], [10.0, 0.0] ]} {"type":"LineString", "coordinates": [ [-10.0, 10.0], [0.0, 10.0], [10.0, 10.0] ]}{"type":"LineString", "coordinates": [ [-10.0, 10.0], [0.0, 10.0], [10.0, 10.0] ]}

输出示例Output example

11

00

若要了解详细信息,请访问 ST_INTERSECTS 引用。To learn more, visit the ST_INTERSECTS reference.

ST_WITHINST_WITHIN

ST_WITHIN 函数确定一个点或多边形位于另一个多边形内。The ST_WITHIN function determines whether a point or polygon is within a polygon. 如果多边形包含点或多边形,函数将返回 1。If the polygon contains the point or polygon, the function will return 1. 如果点或多边形位于所声明的多边形之外,函数将返回 0。The function will return 0 if the point or polygon isn't located within the declared polygon.

下面的示例查询使用 ST_WITHIN 来确定交付目标点是否位于给定的仓库多边形内。The following example query uses ST_WITHIN to determine whether the delivery destination point is within the given warehouse polygon.

SELECT  
     ST_WITHIN(input.deliveryDestination, input.warehouse)  
FROM input 

输入示例Input example

deliveryDestinationdeliveryDestination warehousewarehouse
{"type":"Point", "coordinates": [76.6, 10.1]}{"type":"Point", "coordinates": [76.6, 10.1]} {"type":"Polygon", "coordinates": [ [0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0], [0.0, 0.0] ]}{"type":"Polygon", "coordinates": [ [0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 10.0], [0.0, 0.0] ]}
{"type":"Point", "coordinates": [15.0, 15.0]}{"type":"Point", "coordinates": [15.0, 15.0]} {"type":"Polygon", "coordinates": [ [10.0, 10.0], [20.0, 10.0], [20.0, 20.0], [10.0, 20.0], [10.0, 10.0] ]}{"type":"Polygon", "coordinates": [ [10.0, 10.0], [20.0, 10.0], [20.0, 20.0], [10.0, 20.0], [10.0, 10.0] ]}

输出示例Output example

00

11

若要了解详细信息,请访问 ST_WITHIN 引用。To learn more, visit the ST_WITHIN reference.

后续步骤Next steps