Portable SQL Functions for Geographies

Description

These functions are only available from Alpha Anywhere when Portable SQL is used. They are intended to insulate you from the differences between the native SQL geography implementations in the supported databases.

Discussion

All of the examples assume that you will be substituting the correct SpatialReferenceID value for the database currently in use for the argument :SRID.

GeogAsBinary 

GeogAsBinary as C(Object as Geography)

Return the object description in the Well Known Binary (WKB) format.

select  GeogAsBinary(g.Location) from GeogTest g

GeogAsText 

GeogAsText as C(Object as Geography)

Return the object description in the Well Known Text (WKT) format.

select GeogAsText(g.Location) from GeogTest g

GeogCreateFromBinary 

GeogCreateFromBinary as Geography(Binary as B [, SpatialReferenceID as N])

Create a geography object from Well Known Binary (WKB) format.

dim MyBlob as B = base64decode("AQEAAAAAAAAAAAAkwAAAAAAAAChA")
args.add("WKB", MyBlob)
    
Connection.Execute("select GeogAsText(GeogCreateFromBinary(:WKB)) from GeogTest g")

GeogCreateFromText 

GeogCreateFromText as Geography(Text as C [, SpatialReferenceID as N])

Create a geography object from Well Known Text (WKT) format.

select first 1 GeogAsText(GeogCreateFromText('POINT(-10 27)')) from GeogTest g

GeogCreateLine 

GeogCreateLine as Geography (Longitude as N, Latitude as N ... [,SpatialReferenceID as N])

Constructs a geographic line from two or more longitude/latitude pairs.

select GeogCreateLine(1,2,3,4,5,6, :SRID) from GeogTest g

GeogCreateLocation 

GeogCreateLocation as Geography (Longitude as N, Latitude as N [,SpatialReferenceID as N])

Constructs a geographic location from a longitude/latitude pair.

select GeogCreateLocation(1,2, :SRID) from GeogTest g

GeogCreatePolygon 

GeogCreatePolygon as Geography (Longitude as N, Latitude as N ... [,SpatialReferenceID as N])

Constructs a geographic polygon from three or more longitude/latitude pairs.

select GeogCreatePolygon(-70, 42, -70, 32, -60, 32, -60, 42, -70, 42, :SRID) from GeogTest g
The first and last point of the polygon must be the same, and the points must be arranged in counter-clockwise order.

GeogDistanceBetween 

GeogDistanceBetween as N (Object as Geography, Object as Geography)

Returns the distance between two objects in the default unit, typically meters.

select GeogDistanceBetween(g.Location, g.Location2) from GeogTest g

GeogLatitude 

GeogLatitude as N (Location as Geography)

Returns the latitude value for a geographic location.

select GeogLatitude(g.Location) from GeogTest g
This will be the value of X or Y depending on the database.

GeogLocationIntersectsLine 

GeogLocationIntersectsLine as L (Location as Geography, Line as Geography, Tolerance as N)

Returns true if the location intersects the line or is within the tolerance distance from it.

select g.Name from GeogTest g where GeogLocationIntersectsLine(g.Location, g.Line)

GeogLocationIsWithinPolygon 

GeogLocationIsWithinPolygon as L (Location as Geography, Polygon as Geography, Tolerance as N)

Returns true if the location is contained within the polygon or within the tolerance distance from it.

select g.Name from GeogTest g where GeogLocationIsWithinPolygon(g.Location, g.Polygon)

GeogLocationIsWithinRadius 

GeogLocationIsWithinRadius as L (Location as Geography, Point as Geography, Radius as N, Tolerance as N)

Returns true if the location is within the radius defined around the point or within the tolerance defined.

select g.Name from GeogTest g where GeogLocationIsWithinRadius(g.Location, g.TargetLocation, 50)
Each database handles tolerance somewhat differently (as it affects indexing performance).

GeogLongitude 

GeogLongitude as N (Location as Geography)

Returns the longitude value for a geographic location.

select GeogLongitude(g.Location) from GeogTest g
This will be the value of X or Y depending on the database.

GeogSRID 

GeogSRID as N (Object as Geography)

Returns the Spatial Reference Identifier (SRID) assigned to the object.

select GeogSRID(g.Location) from GeogTest g

GeogType 

GeogType as C (Object as Geography)

Returns LOCATION, LINE, or POLYGON if the geography object is one of them. Values returned for other types are not guaranteed to be consistent across databases.

select GeogType(g.Location) from GeogTest g

Important Note:

At least one database requires a table name alias in SQL statements that refer to geography objects. All of our geography samples use an alias for the table referenced in your FROM clause, and you will want to as well. For example, in the Portable SQL below g is an alias for table GeogTest:

select GeogAsText(g.Location) from GeogTest g

More discussion and examples can be found on Common Geography Database Tasks.

See Also