Fix for SAP-HANA-datastore when working with views and no SRID found

Description

I’ve been recently using the SAP HANA-datastore in GeoServer, which works great.
Unfortunately there is a problem if you publish a HANA-view as a layer and try to do a spatial GetFeature-request on this layer. The view can be published and displayed as WMS, but if you do a GetFeature-request, you get this exception:

<ows:ExceptionText>java.lang.RuntimeException: java.io.IOException
java.io.IOExceptionSAP DBTech JDBC: [8]: invalid argument: The given SRID (-1) does not match the field's SRID (31466) at function _st_geomfromwkb_() (at pos 134) </ows:ExceptionText>

The SRID from the geometry-column in my view is EPSG:31466.

I have already analyzed the problem and have also found a fix for this problem.
The problem is, that in a spatial GetFeature-request the code tries to get the SRID from the layer by executing this SQL-Statement:

"SELECT SRS_ID FROM PUBLIC.ST_GEOMETRY_COLUMNS WHERE SCHEMA_NAME = ? AND TABLE_NAME = ? AND COLUMN_NAME = ?");

https://github.com/geotools/geotools/blob/b1be2cef1c06058f726bce94ac665ee50b571db6/modules/plugin/jdbc/jdbc-hana/src/main/java/org/geotools/data/hana/HanaDialect.java#L193

But the PUBLIC.ST_GEOMETRY-view in HANA is only filled for tables and not for views. That is why in my request the SRID of the layer can not be found and a „-1“ is returned.
Further have I analyzed, that the received SRID is passed in the „prepareGeometryValue“-function, which is used to create the sql-statement.
https://github.com/geotools/geotools/blob/b1be2cef1c06058f726bce94ac665ee50b571db6/modules/plugin/jdbc/jdbc-hana/src/main/java/org/geotools/data/hana/HanaDialect.java#L732

There is an easy fix for this problem:
If you change the „prepareGeometryValue“-code like this, the SRID will only be passed to the „ST_GeomFromWKB“-function, if it is „>1“:

public void prepareGeometryValue(
Class<? extends Geometry> gClass,
int dimension,
int srid,
Class binding,
StringBuffer sql) {
String pattern = null;
if (srid > -1) {
pattern = "ST_GeomFromWKB( ? ,{0})";
sql.append(MessageFormat.format(pattern, Integer.toString(srid)));
} else {
sql.append("ST_GeomFromWKB( ? )");
}
}

If no SRID is passed, the HANA-function assumes, that the SRID is in the same coordinate system as the passed coordinates.
The fix is working in my locally build JDBC-HANA-Datastore and GeoServer.

Environment

None

Assignee

Stefan Uhrig

Reporter

Paul Biskup

Triage

Components

Fix versions

Affects versions

Priority

Low
Configure