Class: Proj::Crs

Inherits:
PjObject show all
Defined in:
lib/proj/crs.rb

Overview

Represents a coordinate reference system.

Instance Method Summary collapse

Methods inherited from PjObject

#accuracy, #auth, #auth_code, #auth_name, #context, #definition, #description, finalize, #has_inverse?, #id, #info, #name, #proj_type, #to_json, #to_proj_string, #to_ptr, #to_wkt

Constructor Details

#initialize(value, context = nil) ⇒ Crs

To create a coordinate system, you can use CRS codes, well-known text (WKT) strings or old-style Proj4 strings (which are deprecated).

Notice when using the old-style Proj4 string, the addition of the “+type=crs” value.

Examples:

crs1 = Proj::Crs.new('EPSG:4326')
crs2 = Proj::Crs.new('urn:ogc:def:crs:EPSG::4326')
crs3 = Proj::Crs.new('+proj=longlat +datum=WGS84 +no_defs +type=crs')
crs4 = Proj::Crs.new(<<~EOS)
         GEOGCRS["WGS 84",
         DATUM["World Geodetic System 1984",
               ELLIPSOID["WGS 84",6378137,298.257223563,
                         LENGTHUNIT["metre",1]]],
         PRIMEM["Greenwich",0,
                ANGLEUNIT["degree",0.0174532925199433]],
         CS[ellipsoidal,2],
         AXIS["geodetic latitude (Lat)",north,
              ORDER[1],
              ANGLEUNIT["degree",0.0174532925199433]],
         AXIS["geodetic longitude (Lon)",east,
              ORDER[2],
              ANGLEUNIT["degree",0.0174532925199433]],
         USAGE[
             SCOPE["unknown"],
                 AREA["World"],
             BBOX[-90,-180,90,180]],
         ID["EPSG",4326]]
       EOS

Parameters:

  • value (String)

    . See above

  • context (Context) (defaults to: nil)

    . An optional Context that the Crs will use for calculations.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/proj/crs.rb', line 39

def initialize(value, context=nil)
  pointer = Api.proj_create(context || Context.current, value)

  if pointer.null?
    Error.check
  end

  super(pointer, context)

  if Api.method_defined?(:proj_is_crs) && !Api.proj_is_crs(pointer)
    raise(Error, "Invalid crs definition. Proj created an instance of: #{self.proj_type}.")
  end
end

Instance Method Details

#areaArea

Return the area of use of an object.

Returns:



143
144
145
# File 'lib/proj/crs.rb', line 143

def area
  @area ||= Area.for_object(self)
end

#axis_countInteger

Returns the number of axis of the coordinate system.

Returns:

  • (Integer)


91
92
93
94
95
96
97
# File 'lib/proj/crs.rb', line 91

def axis_count
  result = Api.proj_cs_get_axis_count(self.context, self.coordinate_system)
  if result == -1
    Error.check
  end
  result
end

#axis_infoArray<Hash>

Returns information on an axis.

Returns:

  • (Array<Hash>)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/proj/crs.rb', line 102

def axis_info
  self.axis_count.times.map do |index|
    p_name = FFI::MemoryPointer.new(:pointer)
    p_abbreviation = FFI::MemoryPointer.new(:pointer)
    p_direction = FFI::MemoryPointer.new(:pointer)
    p_unit_conv_factor = FFI::MemoryPointer.new(:double)
    p_unit_name = FFI::MemoryPointer.new(:pointer)
    p_unit_auth_name = FFI::MemoryPointer.new(:pointer)
    p_unit_code = FFI::MemoryPointer.new(:pointer)

    result = Api.proj_cs_get_axis_info(self.context, self.coordinate_system, index,
                                       p_name, p_abbreviation, p_direction, p_unit_conv_factor, p_unit_name, p_unit_auth_name, p_unit_code)

    unless result
      Error.check
    end

    {:name => p_name.read_pointer.read_string,
     :abbreviation => p_abbreviation.read_pointer.read_string_to_null,
     :direction => p_direction.read_pointer.read_string_to_null,
     :unit_conv_factor => p_unit_conv_factor.read_double,
     :unit_name => p_unit_name.read_pointer.read_string_to_null,
     :unit_auth_name => p_unit_auth_name.read_pointer.read_string_to_null,
     :unit_code => p_unit_code.read_pointer.read_string_to_null}
  end
end

#coordinate_systemCrs

Returns the coordinate system of a SingleCRS.

Returns:



84
85
86
# File 'lib/proj/crs.rb', line 84

def coordinate_system
  PjObject.new(Api.proj_crs_get_coordinate_system(self.context, self))
end

#crs_type:PJ_COORDINATE_SYSTEM_TYPE

Returns the type of the coordinate system.

Returns:

  • (:PJ_COORDINATE_SYSTEM_TYPE)


132
133
134
135
136
137
138
# File 'lib/proj/crs.rb', line 132

def crs_type
  result = Api.proj_cs_get_type(self.context, self.coordinate_system)
  if result == :PJ_CS_TYPE_UNKNOWN
    Error.check
  end
  result
end

#datumCrs

Returns the datum of a SingleCRS.

Returns:



70
71
72
# File 'lib/proj/crs.rb', line 70

def datum
  PjObject.new(Api.proj_crs_get_datum(self.context, self))
end

#ellipsoidPjObject

Get the ellipsoid from a CRS or a GeodeticReferenceFrame.

Returns:



150
151
152
# File 'lib/proj/crs.rb', line 150

def ellipsoid
  PjObject.new(Api.proj_get_ellipsoid(self.context, self))
end

#geodetic_crsCrs

Get the geodeticCRS / geographicCRS from a CRS.

Returns:



56
57
58
# File 'lib/proj/crs.rb', line 56

def geodetic_crs
  PjObject.new(Api.proj_crs_get_geodetic_crs(self.context, self))
end

#horizontal_datumCrs

Get the horizontal datum from a CRS.

Returns:



77
78
79
# File 'lib/proj/crs.rb', line 77

def horizontal_datum
  PjObject.new(Api.proj_crs_get_horizontal_datum(self.context, self))
end

#inspectString

A nicely printed out description

Returns:

  • (String)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/proj/crs.rb', line 176

def inspect
  result = StringIO.new
  result.set_encoding('UTF-8')
  result << <<~EOS
              <#{self.class.name}>: #{self.auth(0)}
              #{self.description}
              Axis Info [#{self.crs_type}]:
            EOS

  self.axis_info.each do |axis_info|
    result << "- #{axis_info[:abbreviation]}[#{axis_info[:direction]}]: #{axis_info[:name]} (#{axis_info[:unit_name]})" << "\n"
  end

  result << <<~EOS
              Area of Use:
              - name: #{self.area.name}
              - bounds: (#{self.area.west_lon_degree}, #{self.area.south_lat_degree}, #{self.area.east_lon_degree}, #{self.area.north_lat_degree})
              Coordinate operation:
              - name: ?
              - method: ?
              Datum: #{self.datum.name}
              - Ellipsoid: #{self.ellipsoid.name}
              - Prime Meridian: #{self.prime_meridian.name}
            EOS

  result.string
end

#operationPjObject

Return the Conversion of a DerivedCRS (such as a ProjectedCRS), or the Transformation from the baseCRS to the hubCRS of a BoundCRS.

Returns:



158
159
160
161
162
163
164
# File 'lib/proj/crs.rb', line 158

def operation
  pointer = Api.proj_crs_get_coordoperation(self.context, self)
  if pointer.null?
    Error.check
  end
  PjObject.new(pointer)
end

#prime_meridianPjObject

Get the prime meridian of a CRS or a GeodeticReferenceFrame.

Returns:



169
170
171
# File 'lib/proj/crs.rb', line 169

def prime_meridian
  PjObject.new(Api.proj_get_prime_meridian(self.context, self))
end

#sub_crs(index) ⇒ Crs

Get a CRS component from a CompoundCRS.

Returns:



63
64
65
# File 'lib/proj/crs.rb', line 63

def sub_crs(index)
  PjObject.new(Api.proj_crs_get_sub_crs(self.context, self, index))
end