1

I am building a system which lets users upload shapefiles. It then converts those shapefiles to PostGIS using shp2pgsql. This command requires the SRS ID in form of an EPSG code.

So I need a ruby gem that can read the shapefile's *.prj file (which contains the projection/spatial reference system encoded as WKT) and return a corresponding SRS ID.

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
Sareuon
  • 637
  • 1
  • 7
  • 9

2 Answers2

2

FYI, http://prj2epsg.org/ lets you lookup PRJ files and get the SRID / EPSG code.

Update: Site is down. See this answer: https://gis.stackexchange.com/questions/372381/is-there-an-alternative-to-prj2epsg-org

James McKinney
  • 2,831
  • 1
  • 18
  • 8
2

I'm not sure how Ruby bindings work to GDAL, but OSR (part of GDAL) can extract either the projection WKT (text) or the SRID (integer).

See this gis.SE answer for a solution with Python/GDAL/OSR.

Update: It turns out the Ruby bindings work nicely as expected. To get you going, try this code:

require 'gdal/osr'

prj_fname = 'myfile.prj'
prj = File.open( prj_fname )

# Import the WKT from the PRJ file
srs = Gdal::Osr::SpatialReference.new()
srs.import_from_wkt( prj.read )

# Various exports
puts srs.export_to_wkt

srs.auto_identify_epsg
puts srs.get_authority_name(nil)
puts srs.get_authority_code(nil)

If you need some other aspect of the projection, explore the available public methods:

srs.public_methods.sort
Community
  • 1
  • 1
Mike T
  • 41,085
  • 18
  • 152
  • 203
  • To use gdal with ruby we need to intall library called "libgdal-ruby". Here script that I work with osr spacial reference. sudo apt-get install libgdal-ruby. With irb we need to require 'gdal/gdal' and 'gdal/ogr'. – Sareuon Oct 17 '11 at 02:14
  • Yup, the GDAL/OSR bindings look good, but the documentation is sparse. You can normally translate examples from Python over to Ruby without too much hassle (just subtle differences in the method names). – Mike T Oct 17 '11 at 07:09
  • How do you install this gem extension (the osr part)? I successfully installed the 'gdal' gem, I have all the firmware and so on installed in my mac. But not able to access the `osr` bit – boulder_ruby Aug 06 '13 at 14:40