Class: GD::GIS::CRS::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/gd/gis/crs_normalizer.rb

Overview

Normalizes coordinates from different CRS definitions into a consistent [longitude, latitude] order.

This class handles:

  • Axis order normalization (e.g. EPSG:4326)

  • Web Mercator (EPSG:3857) to WGS84 conversion

  • Flexible input formats

The output is always expressed as:

[longitude, latitude] in degrees

Instance Method Summary collapse

Constructor Details

#initialize(crs) ⇒ Normalizer

Creates a new CRS normalizer.

Parameters:

  • crs (String, Symbol, nil)

    CRS identifier (e.g. “EPSG:4326”, “EPSG:3857”, CRS84)



36
37
38
# File 'lib/gd/gis/crs_normalizer.rb', line 36

def initialize(crs)
  @crs = normalize_name(crs)
end

Instance Method Details

#normalize(*args) ⇒ Array<Float>?

Normalizes coordinates into [longitude, latitude].

Accepted input forms:

normalize(lon, lat)
normalize(lon, lat, z)
normalize([lon, lat])
normalize([lon, lat, z])

Extra dimensions (e.g. Z) are ignored.

Parameters:

  • args (Array<Float, Array<Float>>)

Returns:

  • (Array<Float>, nil)

    normalized [lon, lat] or nil if input is invalid



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gd/gis/crs_normalizer.rb', line 54

def normalize(*args)
  lon, lat = args.flatten
  return nil if lon.nil? || lat.nil?

  lon = lon.to_f
  lat = lat.to_f

  case @crs
  when CRS84, nil
    [lon, lat]

  when EPSG4326
    # axis order lat,lon → lon,lat
    [lat, lon]

  when EPSG3857
    mercator_to_wgs84(lon, lat)

  when "EPSG:22195"
    gk_to_wgs84(lon, lat)

  else
    raise ArgumentError, "Unsupported CRS: #{@crs}"
  end
end