Copilot commented on code in PR #2677:
URL: https://github.com/apache/sedona/pull/2677#discussion_r2870963996
##########
common/src/main/java/org/apache/sedona/common/raster/RasterAccessors.java:
##########
@@ -359,4 +363,94 @@ public static RasterMetadata rasterMetadata(GridCoverage2D
raster) throws Factor
(int) meta[10],
(int) meta[11]);
}
+
+ /**
+ * Returns the CRS of a raster as PROJJSON string.
+ *
+ * @param raster The input raster.
+ * @return The CRS definition as PROJJSON string, or null if no CRS is set.
+ */
+ public static String crs(GridCoverage2D raster) {
+ return crs(raster, "projjson");
+ }
+
+ /**
+ * Returns the CRS of a raster in the specified format.
+ *
+ * @param raster The input raster.
+ * @param format The desired output format: "projjson", "wkt2", "wkt1", or
"proj".
+ * @return The CRS definition string in the requested format, or null if no
CRS is set.
+ */
+ public static String crs(GridCoverage2D raster, String format) {
+ if (format == null || format.trim().isEmpty()) {
+ format = "projjson";
+ }
+ CoordinateReferenceSystem crsDef = raster.getCoordinateReferenceSystem();
+ if (crsDef instanceof DefaultEngineeringCRS) {
+ if (((DefaultEngineeringCRS) crsDef).isWildcard()) {
+ return null;
+ }
+ }
+
+ // Get WKT1 representation from GeoTools (native, no conversion needed)
+ String wkt1;
+ if (crsDef instanceof Formattable) {
+ wkt1 = ((Formattable) crsDef).toWKT(2, false);
+ } else {
+ wkt1 = crsDef.toWKT();
+ }
+
+ String fmt = format.toLowerCase(Locale.ROOT).trim();
+ if ("wkt1".equals(fmt) || "wkt".equals(fmt)) {
+ return wkt1;
+ }
+
+ // For all other formats, convert through proj4sedona.
+ // Prefer EPSG SRID when available: GeoTools WKT1 projection names (e.g.
Mercator_2SP)
+ // may not be recognized by proj4sedona, but EPSG codes always work.
+ try {
+ Proj proj;
+ int srid = srid(raster);
+ if (srid > 0) {
+ try {
+ proj = new Proj("EPSG:" + srid);
+ } catch (Exception e) {
+ // EPSG code not recognized by proj4sedona, fall back to WKT1
+ proj = new Proj(wkt1);
Review Comment:
In the `srid > 0` branch, if `new Proj("EPSG:" + srid)` fails, the fallback
uses `new Proj(wkt1)` without attempting the same projection-name normalization
that the `srid == 0` branch does. This can cause CRS export to fail
unnecessarily when proj4sedona doesn’t recognize the EPSG code and also can’t
parse GeoTools’ WKT1 projection name (e.g., Mercator_2SP). Consider applying
`normalizeWkt1ForProj4sedona(wkt1)` (and retry) in this fallback path as well.
```suggestion
// EPSG code not recognized by proj4sedona, fall back to WKT1.
// proj4sedona may not recognize some GeoTools projection names
(e.g. Mercator_2SP),
// so normalize the projection name and retry if needed.
try {
proj = new Proj(wkt1);
} catch (Exception wktError) {
String normalized =
CrsNormalization.normalizeWkt1ForProj4sedona(wkt1);
if (!normalized.equals(wkt1)) {
proj = new Proj(normalized);
} else {
throw wktError;
}
}
```
##########
docs/api/sql/Raster-Operators/RS_CRS.md:
##########
@@ -0,0 +1,101 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ -->
+
+# RS_CRS
+
+Introduction: Returns the coordinate reference system (CRS) of a raster as a
string in the specified format. If no format is specified, the CRS is returned
in PROJJSON format. Returns null if the raster has no CRS defined.
+
+Format:
+
+```
+RS_CRS (raster: Raster)
+```
+
+```
+RS_CRS (raster: Raster, format: String)
+```
+
+Since: `v1.9.0`
+
+## Supported output formats
+
+| Format | Description |
+| :--- | :--- |
+| `'projjson'` | PROJJSON format (default). Modern, machine-readable JSON
representation. |
+| `'wkt2'` | Well-Known Text 2 (ISO 19162). Modern standard CRS
representation. |
+| `'wkt1'` | Well-Known Text 1 (OGC 01-009). Legacy format, widely supported. |
+| `'proj'` | PROJ string format. Compact, human-readable representation. |
+
+## SQL Examples
+
+Getting CRS in default PROJJSON format:
+
+```sql
+SELECT RS_CRS(raster) FROM raster_table
+```
+
+Output:
+
+```json
+{
+ "$schema": "https://proj.org/schemas/v0.7/projjson.schema.json",
+ "type": "GeographicCRS",
+ "name": "WGS 84",
+ ...
+}
+```
+
+Getting CRS in WKT1 format:
+
+```sql
+SELECT RS_CRS(raster, 'wkt1') FROM raster_table
+```
+
+Output:
+
+```
+GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS
84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]
+```
+
+Getting CRS in PROJ string format:
+
+```sql
+SELECT RS_CRS(raster, 'proj') FROM raster_table
+```
+
+Output:
+
+```
++proj=longlat +datum=WGS84 +no_defs +type=crs
+```
+
+Getting CRS in WKT2 format:
+
+```sql
+SELECT RS_CRS(raster, 'wkt2') FROM raster_table
+```
+
+## Limitations
+
+The `wkt2`, `proj`, and `projjson` output formats are generated by
[proj4sedona](https://github.com/jiayuasu/proj4sedona) from the raster's
internal WKT1 CRS. This conversion may cause the following limitations:
+
+- **Unsupported projection types**: Some projection types (e.g., Krovak,
Hotine Oblique Mercator) cannot be exported to `wkt2`, `proj`, or `projjson`
formats and will throw an error. Use `'wkt1'` format for these.
+
+!!!note
+ Returns null if the raster has no CRS defined (e.g., when RS_SRID returns
0). The `wkt1` format always produces a lossless representation of the
internally stored CRS.
Review Comment:
The note implies `RS_SRID = 0` means “no CRS defined”, but this PR also
documents that `RS_SRID` returns 0 for custom (non-EPSG) CRS values set via
`RS_SetCRS`. That means `RS_CRS` can be non-null even when `RS_SRID` is 0.
Consider rewording this note to distinguish “no CRS defined” from “custom CRS
with no EPSG authority” (e.g., recommend `RS_CRS(...) IS NULL` to test for
missing CRS).
```suggestion
`RS_CRS` returns null only when the raster has no CRS defined. Note that
`RS_SRID` may return `0` either when no CRS is defined or when a custom
(non‑EPSG) CRS has been set via `RS_SetCRS`, so `RS_SRID = 0` does not always
mean “no CRS”. To test for a missing CRS, use `RS_CRS(raster) IS NULL`. The
`wkt1` format always produces a lossless representation of the internally
stored CRS.
```
--
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]