Search code examples
ruby-on-railspostgresqlgispostgislatitude-longitude

Rails with PostGIS - LineString won't go past -90 degrees


I have a simple PostGIS database table as follows:

    create_table :blocked_search_regions do |t|
      # use PostGIS
      t.line_string :path, geographic: true

      t.string :name

      t.timestamps null: false
    end

    # PostGIS index
    add_index :blocked_search_regions, :path, using: :gist

I am able to create a BlockedSearchRegion by running something like:

linestring = "LINESTRING (48.0 -122.1, 47.74 -90.0, 47.52 -90.0, 47.35 -90.0)"
bsr = BlockedSearchRegion.create!(name: 'new region', path: linestring)

The LINESTRING arguments are just LAT LON, as far as I understand. This seems to mostly work... except when going past -90 degrees. The coordinate -122.1 (or anything lower that -90 degrees) will be automatically converted to -90.0 when the BlockedSearchRegion is saved:

=> #<BlockedSearchRegion:
 path: #<RGeo::Geographic::SphericalLineStringImpl:0x2b004c8535c8 "LINESTRING (48.0 -90.0, 47.74 -90.0, 47.52 -90.0, 47.35 -90.0)">,
 name: "new region",
 created_at: Thu, 03 Jun 2021 16:12:54 UTC +00:00,
 updated_at: Thu, 03 Jun 2021 16:12:54 UTC +00:00>

How do I enter lat/lon beyond 90 degrees?

I have a feeling it has something to do with the geographic: true flag on my table column.


Solution

  • In PostGIS, the order of the coordinates is longitude first, followed by latitude. Your coordinates are swapped and therefore invalid.

    On a side note, what happens to out of bounds coordinates vary greatly, and can range from ignoring the issue, restricting to the proper bounds, going around the globe, going back from the pole etc