Data can be extracted from the database using either SQL or the Shape file loader/dumper. In the section on SQL we will discuss some of the operators available to do comparisons and queries on spatial tables.
The most straightforward means of pulling data out of the database is to use a SQL select query and dump the resulting columns into a parsable text file:
db=# SELECT * FROM ROADS_GEOM;
id | geom | name
1 | LINESTRING(191232 243118,191108 243242) | Jeff Rd
2 | LINESTRING(189141 244158,189265 244817 | Geordie Rd
3 | LINESTRING(192783 228138,192612 229814) | Paul St
4 | LINESTRING(189412 252431,189631 259122) | Graeme Ave
5 | LINESTRING(190131 224148,190871 228134) | Phil Tce
6 | LINESTRING(198231 263418,198213 268322) | Dave Cres
(6 rows)
db=#
However, there will be times when some kind of restriction is necessary to cut down the number of fields returned. In the case of attribute-based restrictions, just use the same SQL syntax as normal with a non-spatial table. In the case of spatial restrictions, the following operators are available/useful:
This operator tells whether the bounding box of one geometry overlaps the bounding box of another.
This operators tests whether two geometries are geometrically identical. For example, if "POLYGON((0 0,1 1,1 0,0 0))" is the same as "POLYGON((0 0,1 0,1 1,0 0))" (it is).
This operator is a little more naive, it only tests whether the bounding boxes of to geometries are the same.
Next, you can use these operators in queries. Note that when specifying geometries and boxes on the SQL command line, you should explicitly cast the string representations to their correct type using the "::" casting operator. So, for example:
SELECT * FROM ROADS_GEOM WHERE GEOM ~= 'LINESTRING(191232 243118,191108 243242)'::geometry;
The above query would return the single record from the "ROADS_GEOM" table in which the geometry was equal to that value.
When using the "&&" operator, you can specify either a BOX3D as the comparison feature or a GEOMETRY. When you specify a GEOMETRY, however, the bounding box will be used for the comparison.
SELECT * FROM ROADS_GEOM WHERE GEOM && 'POLYGON(191232 243117,191232 243119,191234 243117,191232 243117)'::geometry;
The above query will use the bounding box of the polygon for comparison purposes.
The most common spatial query will probably be a "frame-based" query, used by client software, like data browsers and web mappers, to grab a "map frame" worth of data for display. Using a "BOX3D" object for the frame, such a query looks like this:
SELECT GEOM FROM ROADS_GEOM WHERE GEOM && 'BOX3D(191232 243117,191232 243119)'::box3d;
Note the use of the "::box3d" casting opertator at the end of the SQL statement to ensure that the BOX3D string representation is properly cast into a BOX3D object before the query executes.
This section to be written.