jiayuasu opened a new issue, #2709:
URL: https://github.com/apache/sedona/issues/2709
## Expected behavior
`ST_LineMerge` should return a `LineString` unchanged when given a single
`LineString` as input, consistent with PostGIS and GEOS (`GEOSLineMerge`).
A single `LineString` is already "merged" — there is nothing to merge.
### PostGIS / GEOS behavior
```sql
SELECT ST_AsText(ST_LineMerge('LINESTRING(0 0, 1 1)'));
-- Returns: LINESTRING(0 0, 1 1)
```
```python
>>> from shapely import line_merge
>>> from shapely.geometry import LineString
>>> line_merge(LineString([(0, 0), (1, 1)]))
<LINESTRING (0 0, 1 1)>
```
## Actual behavior
Sedona returns `GEOMETRYCOLLECTION EMPTY` for `LineString` input:
```python
>>> df.selectExpr("ST_LineMerge(ST_GeomFromText('LINESTRING(0 0, 1
1)'))").show()
# Returns: GEOMETRYCOLLECTION EMPTY
```
## Root cause
In [`Functions.java` line
1198](https://github.com/apache/sedona/blob/master/common/src/main/java/org/apache/sedona/common/Functions.java#L1198),
`lineMerge` only handles `MultiLineString` and falls through to returning an
empty `GeometryCollection` for all other types — including `LineString`:
```java
public static Geometry lineMerge(Geometry geometry) {
if (geometry instanceof MultiLineString) {
// ... merge logic ...
}
return geometry.getFactory().createGeometryCollection(); // <--
LineString hits this
}
```
## Suggested fix
Add a check for `LineString` before the `MultiLineString` block:
```java
public static Geometry lineMerge(Geometry geometry) {
if (geometry instanceof LineString) {
return geometry; // A single LineString is already merged
}
if (geometry instanceof MultiLineString) {
// ... existing merge logic ...
}
return geometry.getFactory().createGeometryCollection();
}
```
## Environment
- Sedona version: 1.9.0-SNAPSHOT (master)
- Found while implementing geopandas `line_merge()` wrapper (#2230, #2701)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]