0

I am using a PostgreSQL database with PostGIS geometry columns.

I would like to configure the Result classes so that geometry columns are inflated using the ST_AsEWKT function and deflated using the ST_GeomFromEWKT function.

Is there a way to do this so that the "find" method works as normal, and so that the "update" and "create" methods also work as normal. I'd rather not have to write specialized queries for each table, if I can avoid it.

I can use a hack for inflating the column, e.g.

__PACKAGE__->inflate_column( 'geo', {
  inflate => sub {
    my ($raw_value, $result) = @_;
    my $col = $result->result_source->resultset->get_column("geo")->func("ST_AsEWKT");
  },
});

but I am unsure how to implement deflation.

Thanks in advance.

2

1 Answer 1

0

I've got a working solution using DBIx::Class::InflateColumn. It's not ideal, since it makes a separate query to the database for each geometry column. (Ideally, there should be a way to tell DBIC to just use the appropriate functions for queries to this field, if that can even be done without changing DBIC.)

An answer is below.

__PACKAGE__->load_components("InflateColumn");

__PACKAGE__->inflate_column( 'geo', {

  inflate => sub {
    my ($value, $result) = @_;

    my $dbh = $result->result_source->storage->dbh;
    my $sth = $dbh->prepare( q{SELECT ST_AsEWKT(?)} );
    $sth->execute( $value );
    my @row = $sth->fetchrow;

    return $row[0];
  },

  deflate => sub {
    my ($value, $result) = @_;

    my $dbh = $result->result_source->storage->dbh;
    my $sth = $dbh->prepare( q{SELECT ST_GeomFromEWKT(?)} );
    $sth->execute( $value );
    my @row = $sth->fetchrow;

    return $row[0];
  },

});
1
  • Actually, I use a filter instead of inflate/deflate columns. Since the values are scalars instead of references, the inflation/deflation does not work.
    – Rob
    Commented Feb 4, 2013 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.