happydave1 commented on code in PR #1138:
URL: https://github.com/apache/iceberg-go/pull/1138#discussion_r3405959884


##########
table/arrow_utils.go:
##########
@@ -1826,3 +1846,152 @@ func positionDeleteRecordsToDataFilesDV(ctx 
context.Context, rootLocation string
                }
        }
 }
+
+func checkCRSString(rawCrs json.RawMessage) bool {
+       b := bytes.TrimSpace(rawCrs)
+
+       return len(b) > 0 && b[0] == '"'
+}
+
+func checkCRSSJSON(rawCrs json.RawMessage) bool {
+       b := bytes.TrimSpace(rawCrs)
+
+       return len(b) > 0 && b[0] == '{'
+}
+
+func geoArrowCRSToIcebergCRS(meta geoarrow.Metadata) (string, error) {
+       if len(meta.CRS) == 0 {
+               return "srid:0", nil
+       }
+
+       switch {
+       case checkCRSString(meta.CRS):
+               var crs string
+               if len(meta.CRS) > 0 {
+                       if err := json.Unmarshal(meta.CRS, &crs); err != nil {
+                               return "", fmt.Errorf("invalid geoarrow CRS 
metadata: %w", err)
+                       }
+               }
+
+               if strings.EqualFold(crs, "OGC:CRS84") || 
strings.EqualFold(crs, "EPSG:4326") {
+                       return "OGC:CRS84", nil
+               }
+
+               switch meta.CRSType {
+               case geoarrow.CRSTypeSRID:
+                       return "srid:" + crs, nil
+               case geoarrow.CRSTypeWKT22019:
+                       return "", errors.New("CRS type wkt2:2019 not 
supported")
+               default:
+                       if len(crs) <= 32 {
+                               return crs, nil
+                       }
+
+                       return "", errors.New("crs length too long")
+               }
+       case checkCRSSJSON(meta.CRS):
+               var crs map[string]json.RawMessage
+               if len(meta.CRS) > 0 {
+                       if err := json.Unmarshal(meta.CRS, &crs); err != nil {
+                               return "", fmt.Errorf("invalid geoarrow CRS 
metadata: %w", err)
+                       }
+               }
+
+               idRaw, ok := crs["id"]
+               if !ok || len(idRaw) == 0 {
+                       return "", errors.New("unsupported CRS")
+               }
+
+               var id map[string]json.RawMessage
+               if err := json.Unmarshal(idRaw, &id); err != nil {
+                       return "", errors.New("unsupported CRS")
+               }
+
+               var authority string
+               if err := json.Unmarshal(id["authority"], &authority); err != 
nil || authority == "" {
+                       return "", errors.New("unsupported CRS")
+               }
+
+               codeRaw := id["code"]
+               if len(codeRaw) == 0 {
+                       return "", errors.New("unsupported CRS")
+               }
+
+               var code string
+               if err := json.Unmarshal(codeRaw, &code); err != nil {
+                       var codeNum json.Number
+                       if err := json.Unmarshal(codeRaw, &codeNum); err != nil 
|| codeNum.String() == "" {
+                               return "", errors.New("unsupported CRS")
+                       }
+                       code = codeNum.String()
+               }
+               if code == "" {
+                       return "", errors.New("unsupported CRS")
+               }
+
+               authorityCode := authority + ":" + code
+               if strings.EqualFold(authorityCode, "OGC:CRS84") || 
strings.EqualFold(authorityCode, "EPSG:4326") {
+                       return "OGC:CRS84", nil
+               }
+
+               return authorityCode, nil
+       default:
+               return "", errors.New("unsupported CRS: CRS must either be 
omitted, a string, or a JSON object")
+       }
+}
+
+func geoArrowMetadataToIcebergType(meta geoarrow.Metadata) (iceberg.Type, 
error) {
+       crs, err := geoArrowCRSToIcebergCRS(meta)
+       if err != nil {
+               return nil, err
+       }
+
+       switch meta.Edges {
+       case geoarrow.EdgePlanar:
+               return iceberg.GeometryTypeOf(crs)
+       case geoarrow.EdgeVincenty, geoarrow.EdgeKarney, geoarrow.EdgeThomas, 
geoarrow.EdgeAndoyer, geoarrow.EdgeSpherical:
+               return iceberg.GeographyTypeOf(crs, string(meta.Edges))
+       default:
+               return nil, fmt.Errorf("unsupported geoarrow edges %q", 
meta.Edges)
+       }
+}
+
+var authorityCodeCRS = 
regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_-]*:[A-Za-z0-9_.-]+$`)
+
+func icebergCRSToGeoArrowMetadata(crs string) geoarrow.Metadata {
+       lowerCRS := strings.ToLower(crs)
+       if strings.HasPrefix(lowerCRS, "srid:") {
+               id := lowerCRS[len("srid:"):]

Review Comment:
   good catch, will update the tests to include this.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to