Project

proj4rb

0.02
A long-lived project that still receives updates
Ruby bindings for the Proj coordinate transformation library
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

>= 0
 Project Readme

Proj4rb¶ ↑

This gem provides Ruby bindings for the Proj Library (proj.org). The Proj Library supports converting coordinates between a number of different coordinate systems and projections. Note the Proj library used to be known as Proj4.

The bindings support Proj version 4 through the current version (9.3.1). The Proj library and API were completelely written during this time. To support all these versions, the gem dynamically loads code based on the installed Proj version.

Documentation¶ ↑

Reference documentation is available at rubydoc.info/github/cfis/proj4rb.

Examples can be found in this README file as well as in the Examples file. In addition, the test suite has examples of calling almost every API so when in doubt take a look at them!

Installation¶ ↑

First install the gem:

gem install proj4rb

Next install the Proj Library. This of course varies per system, but you want to install the latest version Proj possible. Once installed, you’ll need to make sure that libproj is installed on your operating system’s load path.

Usage¶ ↑

To get started first require the gem:

require 'proj'

If you are using the old Proj4 namespace, then you can do this:

require 'proj4'

CRS¶ ↑

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

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

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

If you are using Proj 5 or newer, then you should create a transformation using epsg strings. If you are using Proj 4, you need to use the deprecated Projection class (see documentation).

Transformation¶ ↑

After you have created two coordinate systems, you can then create a transformation. For example, if you want to convert coordinates from the ‘3-degree Gauss-Kruger zone 3` coordinate system to `WGS84` (one version of lat-long) first create a transformation:

crs_gk  = Proj::Crs.new('EPSG:31467')
crs_wgs84 = Proj::Crs.new('EPSG:4326')
transform = Proj::Transformation.new(crs_gk, crs_wgs84)

Alternatively, or if you are using Proj 5 or later, you can create a transformation without first creating Crs instances. Instead, pass the EPSG information directly to the transformation:

transform = Proj::Transformation.new('EPSG:31467', 'EPSG:4326')

Once you’ve created the transformation, you can tranform coordinates using either the forward or inverse methods. The forward transformation looks like this:

from = Proj::Coordinate.new(x: 5428192.0, y: 3458305.0, z: -5.1790915237)
to = transform.forward(from)
assert_in_delta(48.98963932450735, to.x, 0.01)
assert_in_delta(8.429263044355544, to.y, 0.01)
assert_in_delta(-5.1790915237, to.z, 0.01)
assert_in_delta(0, to.t, 0.01)

While the inverse transformation looks like this:

from = Proj::Coordinate.new(lam: 48.9906726079, phi: 8.4302123334)
to = transform.inverse(from)
assert_in_delta(5428306.389495558, to.x, 0.01)
assert_in_delta(3458375.3367194114, to.y, 0.01)
assert_in_delta(0, to.z, 0.01)
assert_in_delta(0, to.t, 0.01)

Coordinate Operations¶ ↑

Transformations are a type of Coordinate Operation. PROJ divides coordinate operations into three groups:

  • Conversions

  • Projections

  • Transformations

Conversions are coordinate operations that do not exert a change in reference frame. The Ruby bindings support these via the Conversion class. See proj.org/operations/conversions/index.html for more information.

Projections are cartographic mappings of a sphere onto a plane. Technically projections are conversions (according to ISO standards), but PROJ distinguishes them from conversions. The Ruby bindings support these via the Projection module which has methods to create many common projections. A list can be found at proj.org/operations/projections/index.html.

Transformations are coordinate operations that do cause a change in reference frames. The Ruby bindings support these via the Transformation class.

For more information see proj.org/operations/index.html

Operation Factory¶ ↑

The ‘OperationFactoryContext` class can be used to build coordinate operations between two CRSes. This is done by first creating a factory and setting appropiate filters. These include spatial filters, accuracy filters, grid availability filters, etc. Once filters are set, then the factory can be queried for a list of possible conversions. For examples, please see the operation_factory_context_test.rb file.

Coordinate¶ ↑

Notice the examples above transform Coordinate objects. A Coordinate consists of up to four double values to represent three directions plus time. In general you will need to fill in at least the first two values:

from = Proj::Coordinate.new(x: 5428192.0, y: 3458305.0, z: -5.1790915237)
from = Proj::Coordinate.new(lam: 48.9906726079, phi: 8.4302123334)

Lam is longitude and phi is latitude.

Axis Order¶ ↑

By default, tranformations accept coordinates expressed in the units and axis order of the source CRS and return transformed coordinates in the units and axis order of the target CRS.

For most geographic CRSes, the units will be in degrees. For geographic CRSes defined by the EPSG authority, the order of coordinates is latitude and then longitude.

For projected CRSes, the units will vary (metre, us-foot, etc.). For projected CRSes defined by the EPSG authority, and with EAST / NORTH directions, the order might may be east and then north or north and then east.

If you prefer to work with a uniform axis order, regardless of the axis orders mandated by the source and target CRSes, then call the Context#normalize_for_visualization method:

normalized = transform.normalize_for_visualization

The normalized transformation will return output coordinates in longitude, latitude order for geographic CRSes and easting, northing for most projected CRSes.

For more information see proj.org/faq.html#why-is-the-axis-ordering-in-proj-not-consistent.

Context¶ ↑

Contexts are used to support multi-threaded programs. The bindings expose this object via Context.current and store it using thread local storage. Use the context object to access error codes, set proj4 compatability settings, set the logging level and to install custom logging code.

Both Crs and Transformation objects take a context object in their constructors. If none is passed, they default to using Context.current

Network Access¶ ↑

Proj supports downloading grid files on demand if network access is enabled (it is disabled by default). To enable network use the method ‘Context#network_enabled=`. To specify the url endpoint use `Context#url=`. Advanced users can replace Proj’s networking code, which uses libcurl, with their own implementation. To do this see the ‘NetworkApi` class.

Downloaded grids are cached in a sqlite file named cache.db. To specify the location, size and other characteristics of the cache file refer to the ‘GridCache` class which is accessible via `Context#cache`. By default the cache size is 300MB. Caching is on by default but can be disabled via `GridCache#enabled=`.

For more information see the proj networking documentation.

Error handling¶ ↑

When an error occurs, a ‘Proj::Error` instance will be thrown with the underlying message provided from the Proj library.

Finding Proj Library (PROJ_LIB_PATH)¶ ↑

proj4rb will search in a number of well-known locations for the libproj shared library. You can override this by specifying the full path to the library using the ‘PROJ_LIB_PATH` environmental variable.

Finding Proj Files (PROJ_DATA)¶ ↑

Starting with version 6, Proj stores its information (datums, ellipsoids, prime meridians, coordinate systems, units, etc) in a sqlite file called proj.db. If Proj cannot find its database an exception will be raised. In this case, you can set the environmental variable ‘PROJ_DATA` to point to the folder that contains the proj.db file. Note PROJ_LIB must be set before Ruby is launched. Ruby itself cannot set this variable and have it work correctly (at least not on windows).

For more information see proj.org/resource_files.html

Class Hierarchy¶ ↑

The proj4rb class hierarchy is based on Proj’s class hiearchy which, in turn, is derived from docs.opengeospatial.org/as/18-005r5/18-005r5.html. It is:

PjObject
  CoordinateOperationMixin
    Conversion
    Transformation
  CoordinateMetadata
  CoordinateSystem
  Crs
  Datum
  DatumEnsemble
  Ellipsoid
  PrimeMerdian

The PjObject class defines several methods to create new objects:

  • PjObject.create

  • PjObject.create_from_database

  • PjObject.create_from_name

  • PjObject.create_from_wkt

The methods will return instances of the correct subclass.

Tests¶ ↑

Proj4rb ships with a full test suite designed to work using Proj 6 and higher. If you are using an earlier version of Proj, then expect many test failures.

License¶ ↑

Proj4rb is released under the MIT license.

Authors¶ ↑

The proj4rb Ruby bindings were started by Guilhem Vellut with most of the code written by Jochen Topf. Charlie Savage ported the code to Windows and added the Windows build infrastructure. Later, he rewrote the code to support Proj version 5, 6, 7, 8 and 9 and ported it to use FFI.