[Georuby-devel] Approaches for default SRID different than -1, with postgis?

Tilmann Singer tils at tils.net
Tue Feb 26 04:47:02 EST 2008


I would like to define my geometry columns with a specific SRID in
postgis instead of the default -1, so that I can use database specific
functions such as distance_sphere on them without having to set it
with setsrid(geom, 4326) in each query.

The schema creation with the srid option works fine. It sets the srid
value in the geometry_columns table of postgis and creates the
constraint enforce_srid_location" CHECK (srid("location") = 4326) on
the table:

create_table :foo
  t.column "location", :point, :srid => 4326

However, I would also like to create geometry objects from ruby and be
able to use them for database operations. Ideally somehow like this:

point = GeoRuby::SimpleFeatures::Point.new
point.set_x_y(42.0, 13.0)
Foo.find(:all, :select => ["distance_sphere(location, ?) as dist", point])

Since the interpolation of geometry columns is not implemented in the
spatial adapter (and I understand ActiveRecord is not so easy to
extend in this regard), I end up trying this:

Foo.find(:all, :select => ["distance_sphere(location, geometry('#{point.as_ewkt}'))"])

which is not as pretty as it could but acceptable, except that I now
get this error:

ActiveRecord::StatementInvalid: PGError: ERROR: LWGEOM_distance_sphere
Operation on two GEOMETRIES with differenc SRIDs

because the srid of point is not set. Doing a

point.srid = 4326

before the DB call should make this work. In my application, every
geometry value uses the same SRID, so I thought it would make sense to
change the DEFAULT_SRID constant which is defined in
geo_ruby/simple_features/geometry.rb with -1.

Question 1.) Is it recommendable to change the DEFAULT_SRID of
geo_ruby, or are there better approaches?


Question 2.) The as_ewkt and as_ewkb methods only include the SRID of
a geometry if it is _different_ from the DEFAULT_SRID. Why is that?
With my approach so far, I had to patch these methods (see below) to
always include the SRID. Again, is there a better way?


Change to geometry.rb:
@@ -1,13 +1,20 @@
 module GeoRuby#:nodoc:
   module SimpleFeatures
-    #arbitrary default SRID
+    # Default to WGS 84
+    DEFAULT_SRID=4326
@@ -56,7 +40,9 @@
         if @with_m and allow_m
           type = type | M_MASK
-        if @srid != DEFAULT_SRID and allow_srid
+        # Always include SRID of a feature, not only when it is different than the default
+        if allow_srid
           type = type | SRID_MASK
           ewkb << [type, at srid].pack("VV")
@@ -84,7 +70,9 @@

       #Outputs the geometry as an EWKT string.
       def as_ewkt(allow_srid=true,allow_z=true,allow_m=true)
-        if @srid!=DEFAULT_SRID and allow_srid #the default SRID is not output like in PostGIS
+        # Always include SRID of a feature, not only when it is different than the default
+        if allow_srid

Sent from Plazes Berlin, Berlin
https://plazes.com/whereis/til?                   http://tils.net
Tilmann Singer                                +49-176 20 19 51 69

More information about the Georuby-devel mailing list