[postgis-users] C++ and Postgis

Sufficool, Stanley ssufficool at sbcounty.gov
Tue Nov 24 07:36:12 PST 2009


I use st_asbinary(the_geom) with PDO postgresql driver. I then use PHP
unpack on the bytea to extract the coordinates. It is much faster than
string parsing.



	function DrawShape($label="") {
		//TODO: Check byte order and set unpack accordingly
	    $this->wkbIndex++;     //Skip byteorder

		if ($this->wkbIndex > strlen($this->wkbArray))
			return;

		//get unsigned long for shape type
	    $ShapeType = unpack("l",
substr($this->wkbArray,$this->wkbIndex, 4));
	    $this->wkbIndex += 4;		//Advance byte pointer

	    switch ($ShapeType[1]) {
	        case 1: //WKB_POINT:
	            $this->DrawPoint($label);
				break;
	        case 2: //WKB_LINESTRING:
	            $this->DrawLinestring($label);
				break;
	        case 3: //WKB_POLYGON:
	            $this->DrawPolygon($label);
				break;
	        case 4: //WKB_MULTIPOINT:
	        case 5: //WKB_MULTILINESTRING:
	        case 6: //WKB_MULTIPOLYGON:
	        case 7: //WKB_GEOMETRYCOLLECTION:
				//Recursive multi geometry collection
draw routine
	            $this->DrawMultiAnything($label);
				break;
	        default:
				echo "Invalid shape
type:{$ShapeType[1]}<br />";
	            $this->wkbIndex = strlen($this->wkbArray);
				break;
		}
	}

function DrawPoint($label) {
	    $myPoint = unpack("d2",
substr($this->wkbArray,$this->wkbIndex,16));
--- SNIP ----


Hopefully you get the idea.


> -----Original Message-----
> From: postgis-users-bounces at postgis.refractions.net
> [mailto:postgis-users-bounces at postgis.refractions.net] On 
> Behalf Of Pedro Doria Meunier
> Sent: Monday, November 23, 2009 2:43 PM
> To: PostGIS Users Discussion
> Subject: Re: [postgis-users] C++ and Postgis
> 
> 
> Thank you Paul and John for your ideas. :)
> 
> Actually what I'm trying to do is to decode simple POINT geoms...
> 
> This is what I've done in PHP (using the astext() approach)
> 
>        // extract the geometry
>        $geometry = str_replace("POINT(","", $loc);
>        $geometry = str_replace(")","",$geometry);
>        $coordinate = explode(" ",$geometry);
>        $lon=$coordinate[0];
>        $lat=$coordinate[1];
> 
> Now this was for the web interface... ( the user can wait
> another 100ms 
> or so ... :] )
> As far as a server solution is concerned this *is* a time-consuming 
> procedure... especially so when you've got *thousands* of connections 
> per minute...
> 
> Furthermore, I'm developing the "beast" in Qt4 and - *ahem* -
> had never 
> needed to use libpq so I'm a bit lost here... :)
> 
> Could someone be so kind as to point to a code example (outside using
> astext() method) ? :]
> 
> P.S. @John - found this to be interesting as I could store it in
> QByteArray and go from there... ;) -- the real question here 
> is: how to 
> interpret the byte array?
> 
> BR,
> Pedro.
> 
> 
> On 11/23/2009 09:50 PM, Paul Ramsey wrote:
> > That depends entirely on what you're planning to do with
> them once you
> > get them. You'll probably want to serialize them into a
> known form so
> > you can use them on the client side. ST_AsText() is one,
> very heavy,
> > option. Mapserver, to take an example, uses
> > encode(ST_AsBinary(ST_Force2D(geom)), 'hex'), so what goes over the 
> > wire is hex strings, but those are easy to turn into binary 
> arrays, in
> > WKB format, suitable for reading by a number of C++ libraries (like
> > OGR or GEOG or etc).
> >
> > Best,
> >
> > P.
> >
> > On Mon, Nov 23, 2009 at 1:19 PM, Pedro Doria Meunier
> > <pdoria at netmadeira.com>  wrote:
> >    
> >> Hi All,
> >>
> >> First off sorry for cross-posting. I'm hoping for a bigger
> >> audience... :]
> >>
> >> As for my question:
> >>
> >> How does one deal with Postgis geom fields in C++ ?
> >> Google hasn't offered much help so far.
> >>
> >> I know I can make a "SELECT astext(my_geom) FROM my_table"
> and after
> >> parsing the resulting text. This seems a bit awkward and
> >> time-consuming...
> >>
> >> Is there a better way to deal with it?
> >>
> >> Already thankful for any ideas,
> >> BR,
> >> Pedro.
> >> _______________________________________________
> >> postgis-users mailing list postgis-users at postgis.refractions.net
> >> http://postgis.refractions.net/mailman/listinfo/postgis-users
> >>
> >>      
> > _______________________________________________
> > postgis-users mailing list postgis-users at postgis.refractions.net
> > http://postgis.refractions.net/mailman/listinfo/postgis-users
> >
> >    
> _______________________________________________
> postgis-users mailing list postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
> 



More information about the postgis-users mailing list