Geography Reference
In-Depth Information
So how do we create a geometry using WKT? Try the following SQL:
SELECT ST_GeomFromText('LINESTRING(1 1,2 2,3 3,4 4)')
This creates a four-segment linestring that starts at 1,1, ends at 4,4, and passes through 2,2
and 3,3. The following is another example of creating geometry with SQL:
SELECT ST_GeomFromText('POINT(5 6)')
This creates a point at 5, 6 in the current coordinate system.
You can include an optional second parameter that specifies the SRID of the geometry. So if
we wanted to create our point in OSGB36 (SRID 27700) coordinates, we'd use the following:
SELECT ST_GeomFromText('POINT(5 6)',27700)
If you try this using the SQL editor in pgAdmin, you'll find that the database doesn't complain
about the coordinates not being valid for the coordinate system being used. That's because
the second parameter only sets the field in the geometry blob to say what the geometry's
coordinate system is; it does not set anything in the metadata tables or anywhere else in the
system to mark that SRID.
Please also note that if you are trying to insert these geometries into a table you have
created, the triggers and constraints on those tables will have been set by the table creation
functions to allow only certain geometry types and SRIDs to be inserted. This can disrupt
many new GIS developers. If the second parameter is not specified, the database will set it
to the table default. It's all too easy to insert an invalid coordinate if you don't specify the
SRID.
If you do specify the SRID, but it doesn't match the SRID of the table you are inserting the
geometry into, then the insert will be rejected and not committed to the table. It is very
important to be sure when creating new geometries by hand that you have the correct SRID
for your data, and that it matches the constraints of any tables you may have created.
Any of the geometry types we discussed in Chapter 1 can be used in the GeomFromText
function, but beware as sometimes things are not quite as simple as you may expect,
especially when it comes to MULT geometries.
The following is a simple point type.
SELECT ST_GeomFromText('POINT(5 6)')
Search WWH ::




Custom Search