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.
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