Revibalog

home

Using PostGIS to convert from State Plane Coordinates

24 Jul 2014

PostGIS is a wonderfully practical tool. Let's say you have data in State Place Coordinate System (SRID 3359), and you need to output it as GeoJSON (SRID 4326). Here's how you can use PostGIS spatial functions to do the conversion. The example script uses the Sequel library to interact with the database.

SELECT ST_AsGeoJSON(ST_Transform(ST_SetSRID(ST_MakePoint(x::numeric, y::numeric), 3359), 4326)) AS latlng FROM places

An example script to get you up and running:

require 'pg'
require 'sequel'

DB = Sequel.connect('postgres://localhost/places_development')

DB << "CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;"

DB.create_table? :places do
  String :x
  String :y
end

DB[:places].delete
DB[:places].insert(x: '1470119', y: '502973')

res = DB.dataset.with_sql <<-SQL
  SELECT ST_AsGeoJSON(ST_Transform(ST_SetSRID(ST_MakePoint(x::numeric, y::numeric), 3359), 4326)) AS converted FROM places
SQL

puts res.first[:converted]
#=> {"type":"Point","coordinates":[-80.7721053795691,35.1190132279955]}

Download the script and get setup:

$ curl -o postgis-state-plane.rb https://gist.githubusercontent.com/invisiblefunnel/39875ca1e9f142683ade/raw/84881bf462b5790be382edfd4b85e73fff0f0049/postgis-example.rb
$ gem install pg
$ gem install sequel
$ createdb places_development
$ ruby postgis-state-plane.rb

Swap in your desired conversion function:

SELECT ST_AsEWKT(ST_Transform(ST_SetSRID(ST_MakePoint(x::numeric, y::numeric), 3359), 4326)) AS converted FROM places

Thank you to @michalmigurski for pointing me to this solution.



comments powered by Disqus