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

Guilhem Vellut guilhem.vellut at gmail.com
Wed Feb 27 04:08:43 EST 2008


hello,
To change the default SRID, you can also do it without modifying the
source file by putting somewhere :
GeoRuby::SimpleFeatures::DEFAULT_SRID = 4326. You will get a warning
though.
About the reason why the default SRID (actually the -1 SRID
originally) is not output in the ewkb and ewkt, it is possible that
there may not be any valid reason. I'll check if it is ok for postgis
and mysql to do withouth the check when the SRID is -1 and I will
apply your patch if this is the case.
guilhem

On Tue, Feb 26, 2008 at 10:47 AM, Tilmann Singer <tils at tils.net> wrote:
> Hi,
>
>  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
>  end
>
>  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?
>
>  and
>
>  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?
>
>
>
>  Til
>
>
>
>  Change to geometry.rb:
>  @@ -1,13 +1,20 @@
>   module GeoRuby#:nodoc:
>    module SimpleFeatures
>  -    #arbitrary default SRID
>  -    DEFAULT_SRID=-1
>  -
>  +    # Default to WGS 84
>  +    DEFAULT_SRID=4326
>  @@ -56,7 +40,9 @@
>          if @with_m and allow_m
>            type = type | M_MASK
>          end
>  -        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")
>          else
>  @@ -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
>            ewkt="SRID=#{@srid};"
>
>
>
>
>
>  --
>  Sent from Plazes Berlin, Berlin
>  https://plazes.com/whereis/til?                   http://tils.net
>  Tilmann Singer                                +49-176 20 19 51 69
>  _______________________________________________
>  Georuby-devel mailing list
>  Georuby-devel at rubyforge.org
>  http://rubyforge.org/mailman/listinfo/georuby-devel
>


More information about the Georuby-devel mailing list