[postgis] About description of usage of C Clients (libpq) in PostGIS Manual

Dave Blasby dblasby at refractions.net
Tue Jul 24 10:05:08 PDT 2001

>  This is my first message in this mailing list.

Welcome aboard.
>  I've tried to use PostGIS in cygwin's PostgreSQL 7.1,2 on WinNT 4.0
> Server SP6.0a for a month. I've been very impressed with this
> extension's performance which can handle large-scale geospatial data.

Excellent - Its good to hear from people who are using it.
>  I'd like to use a function of C Clients (libpq) of PostGIS to create
> some applications, but still no description in PostGIS Manual.

You can use it just like the normal libpq.  You should, first, decide if
you want binary access or text access.  

If you want text access you can easily get geometry in Well Known Text
(the WKT spec is at http://www.opengis.org/techno/specs/99-049.pdf) -
this will give you things like the string 'POINT(100 100)' or the string
'LINESTRING(0 0, 100 100)'.  WKT is a bit of a pain to parse.  You might
want to use the force_2d(geometry) to get results that are in 2D.

select force2d(mygeom),mydesc,mygeo_id from <table> where ...;

If you want binary access, you should check out the examples/readwkb.c
example in the PostGIS distribution or the examples in the postgresql
client (libpq) documentation from the postgresql website.  Basically,
you have to allocate a postgresql binary cursor, then execute a fetch
from it.  Unfortunately all the results are in binary format (and that
could mean endian problems if the client endian is not the same as the

You have to convert the geometry column to Well Known Binary (the WKB
spec is also at http://www.opengis.org/techno/specs/99-049.pdf).  The
WKB format will specifies what endian its in. You do this with the
asbinary(geometry) function.   There are two version of this.

a) asbinary(geometry)  - returns geometry in WKB form with the
postgresql's server's endianess.
b) asbinary(geometry,'XDR') or asbinary(geometry,'NDR') - return
geometry in WKB form with 
	the specified endian (NDR for i386 machines, and XDR for sparc

If you specify the endian to match the client machine, it will make the
WKB easier to parse.  To make it even easier to parse, you could also do
a force_collection(geometry) and the geometry will be returned as a
geometrycollection with points, lines, or polygons inside.  This is what
I do for the mapserver connector - it might not help you...

3D geometries are not specified in WKB, I use the same method as Frank
Warmerdam and add 32768 to the geometry type to indicate that the points
are 3d.  If you use the force2d(geometry) function, you'll never get 3d
points out.

One easy way around having all the other columns in binary format (and
endian problems) is to force postgresql to return the columns in text
format.  Its cheating, but its useful.

select asbinary(force2d(mygeom),'NDR'), text(mydesc), text(mygeo_id)
from <table> where ...;

ps. Whats endian?  Basically the IEEE defined what information would be
in binary data types like integers and floating point numbers. 
Unfortunately, they didnt specify the order of the bytes for these
types.  Different processor companies decided to do it differently. 
Basically, to convert endian you just have to reverse the order of the
bytes.  Ie. to convert a 4-byte integer (bytes 1 2 3 4) in BIG ENDIAN to
LITTLE ENDIAN you just re-order the bytes as (4 3 2 1).

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Small business owners...
Tell us what you think! http://promo2.yahoo.com/sbin/Yahoo!_BusinessNewsletter/survey.cgi

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