github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/geo/geoprojbase/embeddedproj/embedded_proj.go (about)

     1  // Copyright 2021 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  // Package embeddedproj defines the format used to embed static projection data
    12  // in the Cockroach binary. Is is used to load the data as well as to generate
    13  // the data file (from cmd/generate-spatial-ref-sys).
    14  package embeddedproj
    15  
    16  import (
    17  	"compress/gzip"
    18  	"encoding/json"
    19  	"io"
    20  )
    21  
    22  // Spheroid stores the metadata for a spheroid. Each spheroid is referenced by
    23  // its unique hash.
    24  type Spheroid struct {
    25  	Hash       int64
    26  	Radius     float64
    27  	Flattening float64
    28  }
    29  
    30  // Projection stores the metadata for a projection; it mirrors the fields in
    31  // geoprojbase.ProjInfo but with modifications that allow serialization and
    32  // deserialization.
    33  type Projection struct {
    34  	SRID      int
    35  	AuthName  string
    36  	AuthSRID  int
    37  	SRText    string
    38  	Proj4Text string
    39  	Bounds    Bounds
    40  	IsLatLng  bool
    41  	// The hash of the spheroid represented by the SRID.
    42  	Spheroid int64
    43  }
    44  
    45  // Bounds stores the bounds of a projection.
    46  type Bounds struct {
    47  	MinX float64
    48  	MaxX float64
    49  	MinY float64
    50  	MaxY float64
    51  }
    52  
    53  // Data stores all the spheroid and projection data.
    54  type Data struct {
    55  	Spheroids   []Spheroid
    56  	Projections []Projection
    57  }
    58  
    59  // Encode writes serializes Data as a gzip-compressed json.
    60  func Encode(d Data, w io.Writer) error {
    61  	data, err := json.MarshalIndent(d, "", " ")
    62  	if err != nil {
    63  		return err
    64  	}
    65  	zw, err := gzip.NewWriterLevel(w, gzip.BestCompression)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if _, err := zw.Write(data); err != nil {
    70  		return err
    71  	}
    72  	return zw.Close()
    73  }
    74  
    75  // Decode deserializes Data from a gzip-compressed json generated by Encode().
    76  func Decode(r io.Reader) (Data, error) {
    77  	zr, err := gzip.NewReader(r)
    78  	if err != nil {
    79  		return Data{}, err
    80  	}
    81  	var result Data
    82  	if err := json.NewDecoder(zr).Decode(&result); err != nil {
    83  		return Data{}, err
    84  	}
    85  	return result, nil
    86  }