Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Add Misc Geometry Property functions #35698

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 205 additions & 2 deletions docs/reference/sql/functions/geo.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ include-tagged::{sql-specs}/geo/docs.csv-spec[aswkt]
--------------------------------------------------


[[sql-functions-geo-st-as-wkt]]
===== `ST_AsWKT`
[[sql-functions-geo-st-wkt-to-sql]]
===== `ST_WKTToSQL`

.Synopsis:
[source, sql]
Expand All @@ -54,4 +54,207 @@ Returns the geometry from WKT representation. The return type is geometry.
["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[aswkt]
--------------------------------------------------

==== Geometry Properties

[[sql-functions-geo-dimension]]
===== `ST_Dimension`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_Dimension(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: small integer

.Description:

Returns the dimension of the `geometry` in terms of width and length.
For example, a point has dimension 0, a line - 1 and a polygon 2. The
return type is small integer.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[dimension]
--------------------------------------------------

[[sql-functions-geo-geometrytype]]
===== `ST_GeometryType`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_GeometryType(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: string

.Description:

Returns the type of the `geometry` such as "Point", "Polygon", "MultiPolygon", etc.
The return type is string.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[geometrytype]
--------------------------------------------------

[[sql-functions-geo-x]]
===== `ST_X`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_X(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: double

.Description:

Returns the longitude of the first point in the geometry.
The return type is double.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[x]
--------------------------------------------------

[[sql-functions-geo-y]]
===== `ST_Y`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_Y(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: double

.Description:

Returns the the latitude of the first point in the geometry.
The return type is double.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[y]
--------------------------------------------------

[[sql-functions-geo-xmin]]
===== `ST_XMin`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_XMin(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: double

.Description:

Returns the minimum longitude of all points in the geometry.
The return type is double.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[xmin]
--------------------------------------------------

[[sql-functions-geo-ymin]]
===== `ST_YMin`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_YMin(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: double

.Description:

Returns the minimum latitude of all points in the geometry.
The return type is double.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[ymin]
--------------------------------------------------

[[sql-functions-geo-xmax]]
===== `ST_XMax`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_XMax(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: double

.Description:

Returns the maximum longitude of all points in the geometry.
The return type is double.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[xmax]
--------------------------------------------------

[[sql-functions-geo-ymax]]
===== `ST_YMax`

.Synopsis:
[source, sql]
--------------------------------------------------
ST_YMax(geometry<1>)
--------------------------------------------------

*Input*:

<1> geometry

*Output*: double

.Description:

Returns the maximum latitude of all points in the geometry.
The return type is double.

["source","sql",subs="attributes,macros"]
--------------------------------------------------
include-tagged::{sql-specs}/geo/docs.csv-spec[ymax]
--------------------------------------------------
1 change: 1 addition & 0 deletions docs/reference/sql/functions/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ include::math.asciidoc[]
include::string.asciidoc[]
include::type-conversion.asciidoc[]
include::geo.asciidoc[]
include::geo.asciidoc[]
include::conditional.asciidoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ public int numDimensions() {
return Double.isNaN(center.z) ? 2 : 3;
}

@Override
public int inherentDimensions() {
return 2;
}

@Override
public double firstX() {
return center.x;
}

@Override
public double firstY() {
return center.y;
}

@Override
public int hashCode() {
return Objects.hash(center, radius, unit.ordinal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public int numDimensions() {
return Double.isNaN(topLeft.z) ? 2 : 3;
}

@Override
public int inherentDimensions() {
return 2;
}

@Override
public int hashCode() {
return Objects.hash(topLeft, bottomRight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@ public int numDimensions() {
return shapes.get(0).numDimensions();
}

@Override
public int inherentDimensions() {
if (shapes == null || shapes.isEmpty()) {
throw new IllegalStateException("unable to get number of dimensions, " +
"GeometryCollection has not yet been initialized");
}
return shapes.stream().mapToInt(ShapeBuilder::inherentDimensions).max().orElse(0);
}

@Override
public double firstX() {
if (shapes == null || shapes.isEmpty()) {
throw new IllegalStateException("unable to get the first point, " +
"GeometryCollection has not yet been initialized");
}
return shapes.get(0).firstX();
}

@Override
public double firstY() {
if (shapes == null || shapes.isEmpty()) {
throw new IllegalStateException("unable to get the first point, " +
"GeometryCollection has not yet been initialized");
}
return shapes.get(0).firstY();
}

@Override
public Shape buildS4J() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public int numDimensions() {
return Double.isNaN(coordinates.get(0).z) ? 2 : 3;
}

@Override
public int inherentDimensions() {
return 1;
}

@Override
public JtsGeometry buildS4J() {
Coordinate[] coordinates = this.coordinates.toArray(new Coordinate[this.coordinates.size()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ public int numDimensions() {
return lines.get(0).numDimensions();
}

@Override
public int inherentDimensions() {
return 1;
}

public double firstX() {
if (lines == null || lines.isEmpty()) {
throw new IllegalStateException("unable to get the first coordinate");
}
return lines.get(0).firstX();
}

public double firstY() {
if (lines == null || lines.isEmpty()) {
throw new IllegalStateException("unable to get the first coordinate");
}
return lines.get(0).firstY();
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,9 @@ public int numDimensions() {
}
return Double.isNaN(coordinates.get(0).z) ? 2 : 3;
}

@Override
public int inherentDimensions() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ public int numDimensions() {
return polygons.get(0).numDimensions();
}

@Override
public int inherentDimensions() {
return 2;
}

@Override
public double firstX() {
if (polygons == null || polygons.isEmpty()) {
throw new IllegalStateException("unable to get the first point, " +
"Polygons have not yet been initialized");
}
return polygons.get(0).firstX();
}

@Override
public double firstY() {
if (polygons == null || polygons.isEmpty()) {
throw new IllegalStateException("unable to get the first point, " +
"Polygons have not yet been initialized");
}
return polygons.get(0).firstY();
}

@Override
public Shape buildS4J() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,10 @@ public GeoShapeType type() {
public int numDimensions() {
return Double.isNaN(coordinates.get(0).z) ? 2 : 3;
}

@Override
public int inherentDimensions() {
return 0;
}

}
Loading