- Categories:
TRY_ TO_ GEOMETRY¶
Parses an input and returns a value of type GEOMETRY.
This function is identical to TO_GEOMETRY except that it returns NULL when TO_GEOMETRY would return an error.
- See also:
Syntax¶
Use one of the following:
Arguments¶
Required:
varchar_expressionThe argument must be a string expression that represents a valid geometric object in one of the following formats:
- WKT (well-known text).
- WKB (well-known binary) in hexadecimal format (without a leading
0x). - EWKT (extended well-known text).
- EWKB (extended well-known binary) in hexadecimal format (without a leading
0x). - GeoJSON.
binary_expressionThe argument must be a binary expression in WKB or EWKB format.
variant_expressionThe argument must be an OBJECT in GeoJSON format.
Optional:
sridThe integer value of the SRID to use.
allow_invalidIf TRUE, specifies that the function returns a GEOGRAPHY or GEOMETRY object, even when the input shape isn’t valid and can’t be repaired. For more information, see Specifying how invalid geospatial shapes are handled.
Returns¶
The function returns a value of type GEOMETRY or NULL when TO_GEOMETRY would return an error.
Usage notes¶
- Returns NULL if the input can’t be parsed as the appropriate supported format (WKT, WKB, EWKT, EWKB, GeoJSON).
- For GeoJSON, WKT, and WKB input, if the
sridargument is not specified, the resulting GEOMETRY object has the SRID set to 0.
Examples¶
This shows a simple use of the TRY_TO_GEOMETRY function with VARCHAR data:
Create a temporary table and insert rows with GEOMETRY values:
This table has columns with data types that the TO_GEOMETRY function accepts as inputs in the following formats:
- VARCHAR (WKT/WKB and hex/EWKT/EWKB/GeoJSON)
- BINARY (WKB/EWKB)
- VARIANT (GeoJSON object)
- GEOGRAPHY
Optional srid and allow_invalid values can follow any of these formats. The St_Aswkb function
generates valid WKB BINARY values.
The following example tries to convert VARCHAR values in the invalid_wkt_col column to GEOMETRY values,
but the shapes aren’t valid: