Skip to content

Commit

Permalink
[SEDONA-351] Support XYZM coordinate (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyy1000 committed Aug 11, 2023
1 parent 9428bca commit 2754997
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,14 @@ public static String getEWKT(Geometry geometry) {
if (srid != 0) {
sridString = "SRID=" + String.valueOf(srid) + ";";
}

return sridString + new WKTWriter(GeomUtils.getDimension(geometry)).write(geometry);
return sridString + new WKTWriter(4).write(geometry);
}

public static String getWKT(Geometry geometry) {
if (geometry == null) {
return null;
}
return new WKTWriter(GeomUtils.getDimension(geometry)).write(geometry);
return new WKTWriter(4).write(geometry);
}

public static byte[] getEWKB(Geometry geometry) {
Expand Down
76 changes: 76 additions & 0 deletions common/src/test/java/org/apache/sedona/common/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,82 @@ private Coordinate[] coordArray3d(double... coordValues) {
return coords;
}

@Test
public void asEWKT() throws Exception{
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4236);
Geometry geometry = geometryFactory.createPoint(new Coordinate(1.0, 2.0));
String actualResult = Functions.asEWKT(geometry);
String expectedResult = "SRID=4236;POINT (1 2)";
Geometry acutal = Constructors.geomFromEWKT(expectedResult);
assertEquals(geometry, acutal);
assertEquals(expectedResult, actualResult);

geometry = geometryFactory.createPoint(new Coordinate(1.0, 2.0, 3.0));
actualResult = Functions.asEWKT(geometry);
expectedResult = "SRID=4236;POINT Z(1 2 3)";
acutal = Constructors.geomFromEWKT(expectedResult);
assertEquals(geometry, acutal);
assertEquals(expectedResult, actualResult);

geometry = geometryFactory.createPoint(new CoordinateXYM(1.0, 2.0, 3.0));
actualResult = Functions.asEWKT(geometry);
expectedResult = "SRID=4236;POINT M(1 2 3)";
acutal = Constructors.geomFromEWKT(expectedResult);
assertEquals(geometry, acutal);
assertEquals(expectedResult, actualResult);

geometry = geometryFactory.createPoint(new CoordinateXYZM(1.0, 2.0, 3.0, 4.0));
actualResult = Functions.asEWKT(geometry);
expectedResult = "SRID=4236;POINT ZM(1 2 3 4)";
acutal = Constructors.geomFromEWKT(expectedResult);
assertEquals(geometry, acutal);
assertEquals(expectedResult, actualResult);
}

@Test
public void asWKT() throws Exception {
Geometry geometry = GEOMETRY_FACTORY.createPoint(new Coordinate(1.0, 2.0));
String actualResult = Functions.asWKT(geometry);
String expectedResult = "POINT (1 2)";
Geometry acutal = Constructors.geomFromEWKT(expectedResult);
assertEquals(geometry, acutal);
assertEquals(expectedResult, actualResult);

geometry = GEOMETRY_FACTORY.createPoint(new Coordinate(1.0, 2.0, 3.0));
actualResult = Functions.asWKT(geometry);
expectedResult = "POINT Z(1 2 3)";
acutal = Constructors.geomFromEWKT(expectedResult);
assertEquals(geometry, acutal);
assertEquals(expectedResult, actualResult);

geometry = GEOMETRY_FACTORY.createPoint(new CoordinateXYM(1.0, 2.0, 3.0));
actualResult = Functions.asWKT(geometry);
expectedResult = "POINT M(1 2 3)";
acutal = Constructors.geomFromEWKT(expectedResult);
assertEquals(geometry, acutal);
assertEquals(expectedResult, actualResult);

geometry = GEOMETRY_FACTORY.createPoint(new CoordinateXYZM(1.0, 2.0, 3.0, 4.0));
actualResult = Functions.asWKT(geometry);
expectedResult = "POINT ZM(1 2 3 4)";
acutal = Constructors.geomFromEWKT(expectedResult);
assertEquals(geometry, acutal);
assertEquals(expectedResult, actualResult);
}

@Test
public void asWKB() throws Exception{
Geometry geometry = GEOMETRY_FACTORY.createPoint(new Coordinate(1.0, 2.0));
byte[] actualResult = Functions.asWKB(geometry);
Geometry expected = Constructors.geomFromWKB(actualResult);
assertEquals(expected, geometry);

geometry = GEOMETRY_FACTORY.createPoint(new Coordinate(1.0, 2.0, 3.0));
actualResult = Functions.asWKB(geometry);
expected = Constructors.geomFromWKB(actualResult);
assertEquals(expected, geometry);
}

@Test
public void splitLineStringByMultipoint() {
LineString lineString = GEOMETRY_FACTORY.createLineString(coordArray(0.0, 0.0, 1.5, 1.5, 2.0, 2.0));
Expand Down
53 changes: 52 additions & 1 deletion docs/api/flink/Function.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ Introduction: Return the Extended Well-Known Binary representation of a geometry
EWKB is an extended version of WKB which includes the SRID of the geometry.
The format originated in PostGIS but is supported by many GIS tools.
If the geometry is lacking SRID a WKB format is produced.
It will ignore the M coordinate if present.

Format: `ST_AsEWKB (A:geometry)`

Expand All @@ -293,6 +294,7 @@ EWKT is an extended version of WKT which includes the SRID of the geometry.
The format originated in PostGIS but is supported by many GIS tools.
If the geometry is lacking SRID a WKT format is produced.
[See ST_SetSRID](#ST_SetSRID)
It will support M coodinate if present since v1.5.0.

Format: `ST_AsEWKT (A:geometry)`

Expand All @@ -309,6 +311,30 @@ Output:
SRID=4326;POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
```

Example:

```sql
SELECT ST_AsEWKT(ST_MakePointM(1.0, 1.0, 1.0))
```

Output:

```
POINT M(1 1 1)
```

Example:

```sql
SELECT ST_AsEWKT(ST_MakePoint(1.0, 1.0, 1.0, 1.0))
```

Output:

```
POINT ZM(1 1 1 1)
```

## ST_AsGeoJSON

Introduction: Return the [GeoJSON](https://geojson.org/) string representation of a geometry
Expand Down Expand Up @@ -379,7 +405,8 @@ Output:

## ST_AsText

Introduction: Return the Well-Known Text string representation of a geometry
Introduction: Return the Well-Known Text string representation of a geometry.
It will support M coodinate if present since v1.5.0.

Format: `ST_AsText (A:geometry)`

Expand All @@ -397,6 +424,30 @@ Output:
POINT (1 1)
```

Example:

```sql
SELECT ST_AsText(ST_MakePointM(1.0, 1.0, 1.0))
```

Output:

```
POINT M(1 1 1)
```

Example:

```sql
SELECT ST_AsText(ST_MakePoint(1.0, 1.0, 1.0, 1.0))
```

Output:

```
POINT ZM(1 1 1 1)
```

## ST_Azimuth

Introduction: Returns Azimuth for two given points in radians null otherwise.
Expand Down
53 changes: 52 additions & 1 deletion docs/api/sql/Function.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ EWKB is an extended version of WKB which includes the SRID of the geometry.
The format originated in PostGIS but is supported by many GIS tools.
If the geometry is lacking SRID a WKB format is produced.
[Se ST_SetSRID](#ST_SetSRID)
It will ignore the M coordinate if present.

Format: `ST_AsEWKB (A:geometry)`

Expand All @@ -288,6 +289,7 @@ EWKT is an extended version of WKT which includes the SRID of the geometry.
The format originated in PostGIS but is supported by many GIS tools.
If the geometry is lacking SRID a WKT format is produced.
[See ST_SetSRID](#ST_SetSRID)
It will support M coodinate if present since v1.5.0.

Format: `ST_AsEWKT (A:geometry)`

Expand All @@ -305,6 +307,30 @@ Output:
SRID=4326;POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
```

Example:

```sql
SELECT ST_AsEWKT(ST_MakePointM(1.0, 1.0, 1.0))
```

Output:

```
POINT M(1 1 1)
```

Example:

```sql
SELECT ST_AsEWKT(ST_MakePoint(1.0, 1.0, 1.0, 1.0))
```

Output:

```
POINT ZM(1 1 1 1)
```

## ST_AsGeoJSON

Introduction: Return the [GeoJSON](https://geojson.org/) string representation of a geometry
Expand Down Expand Up @@ -376,7 +402,8 @@ Output:

## ST_AsText

Introduction: Return the Well-Known Text string representation of a geometry
Introduction: Return the Well-Known Text string representation of a geometry.
It will support M coodinate if present since v1.5.0.

Format: `ST_AsText (A:geometry)`

Expand All @@ -394,6 +421,30 @@ Output:
POINT (1 1)
```

Example:

```sql
SELECT ST_AsText(ST_MakePointM(1.0, 1.0, 1.0))
```

Output:

```
POINT M(1 1 1)
```

Example:

```sql
SELECT ST_AsText(ST_MakePoint(1.0, 1.0, 1.0, 1.0))
```

Output:

```
POINT ZM(1 1 1 1)
```

## ST_Azimuth

Introduction: Returns Azimuth for two given points in radians null otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public static class ST_AsText extends ScalarFunction {
@DataTypeHint("String")
public String eval(@DataTypeHint(value = "RAW", bridgedTo = org.locationtech.jts.geom.Geometry.class) Object o) {
Geometry geom = (Geometry) o;
return org.apache.sedona.common.Functions.asEWKT(geom);
return org.apache.sedona.common.Functions.asWKT(geom);
}
}

Expand Down

0 comments on commit 2754997

Please sign in to comment.