jiayuasu commented on code in PR #2677:
URL: https://github.com/apache/sedona/pull/2677#discussion_r2870988268


##########
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:
   fixed



##########
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:
   fixed



-- 
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]

Reply via email to