[postgis] text -> geometry ?

Dave Blasby dblasby at refractions.net
Fri Aug 24 14:38:27 PDT 2001


The reason is because there is no routine that converts 'text' to
'geometry'.

In postgresql, if you type something inside single quotes, it is turned
into a normal c-style string. 

mapserv3=# select 'dave';
 ?column? 
----------
 dave
(1 row)


Here the result is 5 bytes of memory - the characters 'd', 'a', 'v',
'e', <null>.

But, if you use the TEXT type, you actually get a postgresql variable
length datastructure.

mapserv3=# select 'dave'::text;
 ?column? 
----------
 dave
(1 row)

Here the result is 8 bytes long. The first 4 are interpreted as an
integer and give the full length of the structure (its value is 8).  The
next 4 bytes are actually 'd', 'a', 'v', 'e'.

To make a long story short, both the type and structure of 'dave' and
'dave'::text are quite different.  

I've added another function to the CVS version that will automagicly
convert TEXT to GEOMETRY.

mapserv3=# select 'POINT(0 0)'::text::geometry;
      ?column?      
--------------------
 SRID=-1;POINT(0 0)
(1 row)

Unfortunately, you'll have to upgrade to either the CVS version or to
0.6 (to be released when paul comes back from holiday).  0.6 is quite
different from 0.5, so you might not want to upgrade until all the
documentation is finished.

If paul were here, I'd release a 0.5.2 for you.  Unfortunately, he's
not.  Here's how to patch 0.5;

To add this functionality to 0.5, you'll have to:

1. add this to the postgis_inout.c file: [thats
DirectFunctionCall<number one>]
//takes a text argument and parses it to make a geometry
PG_FUNCTION_INFO_V1(geometry_text);
Datum geometry_text(PG_FUNCTION_ARGS)
{
	char		*input = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));

	if (*((int *) input) == 4)
	{
		//empty string
		PG_RETURN_NULL();
	}
	input = &input[4];
	PG_RETURN_POINTER ( 
		DatumGetPointer(
DirectFunctionCall1(geometry_in,PointerGetDatum(input)))
			);

}

2. add this to postgis.sql.in:
CREATE FUNCTION geometry(text)
   RETURNS GEOMETRY
   AS '@MODULE_FILENAME@','geometry_text'
	     LANGUAGE 'c' WITH (iscachable,isstrict);

3. recompile and install.  You'll get a warning that 'geometry_text'
hasnt been previously prototyped - ignore it.

4. if you look in the postgis.sql file, you'll see that the
'@MODULE_FILENAME@' in step 2 has been replaced with an actual location
of your .so.  Copy and paste the CREATE FUNCTION statement into psql.

5. test it by typing "select 'POINT(0 0)'::text::geometry;" into psql

dave

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get VeriSign's FREE GUIDE: "Securing Your Web Site for Business." Learn about using SSL for serious online security. Click Here!
http://us.click.yahoo.com/KYe3qC/I56CAA/yigFAA/PhFolB/TM
---------------------------------------------------------------------~->

To unsubscribe from this group, send an email to:
postgis-unsubscribe at yahoogroups.com

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 





More information about the postgis-users mailing list