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

     1  // Copyright 2020 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  // This file was generated from `./pkg/cmd/generate-spatial-ref-sys`.
    12  
    13  package geoprojbase
    14  
    15  import (
    16  	"bytes"
    17  	_ "embed" // required for go:embed
    18  	"sync"
    19  
    20  	"github.com/cockroachdb/cockroachdb-parser/pkg/geo/geopb"
    21  	"github.com/cockroachdb/cockroachdb-parser/pkg/geo/geoprojbase/embeddedproj"
    22  	"github.com/cockroachdb/errors"
    23  )
    24  
    25  //go:embed data/proj.json.gz
    26  var projData []byte
    27  
    28  var once sync.Once
    29  var projectionsInternal map[geopb.SRID]ProjInfo
    30  
    31  // MakeSpheroid is an injectable function which creates a spheroid.
    32  // If you hit the assertion here, you may want to blank import geographic lib, e.g.
    33  // _ "github.com/cockroachdb/cockroachdb-parser/pkg/geo/geographiclib".
    34  var MakeSpheroid = func(radius, flattening float64) (Spheroid, error) {
    35  	return nil, errors.AssertionFailedf("MakeSpheroid not initialised")
    36  }
    37  
    38  // getProjections returns the mapping of SRID to projections.
    39  // Use the `Projection` function to obtain one.
    40  func getProjections() map[geopb.SRID]ProjInfo {
    41  	once.Do(func() {
    42  		d, err := embeddedproj.Decode(bytes.NewReader(projData))
    43  		if err != nil {
    44  			panic(errors.NewAssertionErrorWithWrappedErrf(err, "error decoding embedded projection data"))
    45  		}
    46  
    47  		// Build a temporary map of spheroids so we can look them up by hash.
    48  		spheroids := make(map[int64]Spheroid, len(d.Spheroids))
    49  		for _, s := range d.Spheroids {
    50  			spheroids[s.Hash], err = MakeSpheroid(s.Radius, s.Flattening)
    51  			if err != nil {
    52  				panic(err)
    53  			}
    54  		}
    55  
    56  		projectionsInternal = make(map[geopb.SRID]ProjInfo, len(d.Projections))
    57  		for _, p := range d.Projections {
    58  			srid := geopb.SRID(p.SRID)
    59  			spheroid, ok := spheroids[p.Spheroid]
    60  			if !ok {
    61  				panic(errors.AssertionFailedf("embedded projection data contains invalid spheroid %x", p.Spheroid))
    62  			}
    63  			projectionsInternal[srid] = ProjInfo{
    64  				SRID:      srid,
    65  				AuthName:  "EPSG",
    66  				AuthSRID:  p.AuthSRID,
    67  				SRText:    p.SRText,
    68  				Proj4Text: MakeProj4Text(p.Proj4Text),
    69  				Bounds: Bounds{
    70  					MinX: p.Bounds.MinX,
    71  					MaxX: p.Bounds.MaxX,
    72  					MinY: p.Bounds.MinY,
    73  					MaxY: p.Bounds.MaxY,
    74  				},
    75  				IsLatLng: p.IsLatLng,
    76  				Spheroid: spheroid,
    77  			}
    78  		}
    79  	})
    80  
    81  	return projectionsInternal
    82  }