Solved

INSERT INTO POSTGIS TABLE USING PERL

Posted on 2006-06-17
9
892 Views
Last Modified: 2008-01-09
Hello
I am developing a script in Perl that writes points to a spatial table in Postgis. I use :

my $sth = $dbh->prepare("INSERT INTO gtest (geom) VALUES (GeomFromEWKT('SRID=?;POINT(? ? ?)'));")or
     die "Can't prepareSQL: " . $dbh->errstr();;

         $sth->execute($srd, $x, $y, $z)  or  die "Can't  write SQL: " . $sth->errstr();;
When I run the script I get an "Can't write SQL error" that is found near 'SRID='.
And when I use  select  table I get no result.
When I changed the '?' with numbers everything was ok.
Any ideas on how the correct syntax is?
Thank you
George
0
Comment
Question by:geomvigla
  • 5
  • 4
9 Comments
 
LVL 22

Expert Comment

by:earth man2
Comment Utility
I guess you are missing commas, try

my $sth = $dbh->prepare( "INSERT INTO gtest (geom) VALUES ( GeomFromEWKT( SRID=?, POINT( ?, ?, ? )))") or
     die "Can't prepareSQL: " . $dbh->errstr();;

         $sth->execute($srd, $x, $y, $z)  or  die "Can't  write SQL: " . $sth->errstr();;
0
 
LVL 22

Expert Comment

by:earth man2
Comment Utility
or even ...

my $sth = $dbh->prepare( "INSERT INTO gtest (geom) VALUES ( GeomFromEWKT( ?, POINT( ?, ?, ? )))") or
     die "Can't prepareSQL: " . $dbh->errstr();;
0
 

Author Comment

by:geomvigla
Comment Utility
Right now my code is:

my $sth = $dbh->prepare(" INSERT INTO gtest (geom) VALUES ( geomFromEWKT(SRID=?,POINT( ? ? ? ))) ")or
     die "Can't prepareSQL: " . $dbh->errstr();;

     $sth->execute($srd,$x,$y,$z)    or die "Can't write sql\n". $sth->errstr();;;

The error I get is
Can't write sql ERROR: syntax error at or near "'35.535295'" at character 79  x=139.38657 y=35.535295 z=18.3

There is no change with or without SRID=

When I have the code with commas I get no point function available.
Any ideas??
0
 
LVL 22

Expert Comment

by:earth man2
Comment Utility
When I have the code with commas I get no point function available.

-- do you have the point function available to this account ?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:earth man2
Comment Utility
there are a number of point functions in pg_calalog but I suspect are specifying too many arguments.
 pg_catalog | point | point            | double precision, double precision | postgres | internal | construct_point | convert x, y to point

That is to say point as implented in a standard installation is 2d

try

my $sth = $dbh->prepare( "INSERT INTO gtest (geom) VALUES ( GeomFromEWKT( ?, POINT( ?, ? )))") or
     die "Can't prepareSQL: " . $dbh->errstr();;
0
 

Author Comment

by:geomvigla
Comment Utility
I use postgis standars and this is a geometry field POINT(X Y Z).
I created through AddGeometryColumn function and the dimension is 3.
0
 

Author Comment

by:geomvigla
Comment Utility
The sql command that I use directly is:

INSERT INTO gtest (geom) VALUES (GeomFromEWKT('SRID=290;POINT(35.2 27.55 12.5)'));
0
 
LVL 22

Accepted Solution

by:
earth man2 earned 250 total points
Comment Utility
O
GeomFromEWKT('SRID=290;POINT(35.2 27.55 12.5)'
function has one string argument. So try something like ...

my $sth = $dbh->prepare(" INSERT INTO gtest (geom) VALUES ( geomFromEWKT( ? ) ) ")or
     die "Can't prepareSQL: " . $dbh->errstr();;
   
     $str = 'SRD=' . $srd . ';POINT( ' . $x . ' ' . $y . ' ' . $z . ')';
     $sth->execute($str)    or die "Can't write sql\n". $sth->errstr();;;

0
 

Author Comment

by:geomvigla
Comment Utility
Finally this is it!
Just a tiny change: it is 'SRID=' so
my $sth = $dbh->prepare(" INSERT INTO gtest (geom) VALUES ( geomFromEWKT( ? ) ) ")or
     die "Can't prepareSQL: " . $dbh->errstr();;
   
     $str = 'SRID=' . $srd . ';POINT( ' . $x . ' ' . $y . ' ' . $z . ')';
     $sth->execute($str)    or die "Can't write sql\n". $sth->errstr();;;

Thank you very much!
George
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now