3

First of all, apologies if this is really simple, but I just can't seem to figure it out. I am using RGeo to convert between UTM and lat/long, like this;

 srs_database = RGeo::CoordSys::SRSDatabase::ActiveRecordTable.new

 # create the coordinate factory for the relevant UTM zone
 utm_factory = RGeo::Cartesian.factory(:srid => srid,
                                       :srs_database => srs_database)
 utm_location = utm_factory.point(easting, northing)

 # create the standard WGS84 lat/long coordinate factory
 wgs84_proj4 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
 wgs84_factory = RGeo::Geographic.spherical_factory(proj4: wgs84_proj4, :srid => 4326)

 # perform the UTM -> lat/long cast
 RGeo::Feature.cast(utm_location, :factory => wgs84_factory, :project => true)

As you can see I am using RGeo::CoordSys::SRSDatabase::ActiveRecordTable.

I have just upgraded to RGeo 0.5.2 and I note that this class has been deprecated.

Fair enough, but I am now not sure what the alternative methodology is... I have scouted around and can't seem to find the right documentation.

Also, my original method always seemed a bit complex to me - is there a simpler way to accomplish UTM -> lat/long conversion with RGeo?

Thanks in advance!

Ben

benjimix
  • 609
  • 7
  • 18

2 Answers2

3

Ok, actually I managed to work this out pretty quickly. This is what worked for me:

 if hemisphere == 'S'
   srid = 32700 + number.to_i
   utm_proj4 = "+proj=utm +zone=#{zone} +south +datum=WGS84 +units=m +no_defs"
 else
   srid = 32600 + number.to_i
   utm_proj4 = "+proj=utm +zone=#{zone} +datum=WGS84 +units=m +no_defs"
 end

 # create both the UTM and lat / long factories (we will project between them)
 utm_factory = RGeo::Cartesian.simple_factory(srid: srid, proj4: utm_proj4)
 wgs84_factory = RGeo::Geographic.spherical_factory(srid: 4326, proj4: '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')

 # create the UTM location
 utm_location = utm_factory.point(easting, northing)

 # perform the UTM -> lat/long cast
 RGeo::Feature.cast(utm_location, :factory => wgs84_factory, :project => true)

I am creating my own factories based upon Proj4 strings that I pulled from the spatial_ref_sys table.

I am not sure if this is 'right' though and there may be a better way to do it.

But I hope that this helps someone! :)

Community
  • 1
  • 1
benjimix
  • 609
  • 7
  • 18
0

Another approach to this:

EPSG_4326 = RGeo::CoordSys::Proj4.new("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
EPSG_3857 = RGeo::CoordSys::Proj4.new("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")

RGeo::CoordSys::Proj4.transform(EPSG_3857, geom, EPSG_4326, RGeo::Geographic.spherical_factory)

Yields a new RGeo::Geographic::SphericalPointImpl with the appropriate coordinates.

Michael Pell
  • 1,416
  • 1
  • 14
  • 17