source: TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/__init__.py @ 2194

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/GeoTypes/__init__.py@2194
Revision 2194, 6.3 KB checked in by lawrence, 13 years ago (diff)

Adding various specs and 3rd party code of interest for the CSML
services development.

Line 
1
2################################################################################
3# Copyright (c) QinetiQ Plc 2003
4#
5# Licensed under the LGPL. For full license details see the LICENSE file.
6#
7# 2005, dec 5: corrected typo (Fred, fredericback@gmail.com)
8#
9################################################################################
10
11"""
12Package of classes for working with OpenGIS classes in PostGIS and
13basic postgres 2d geometric types.
14
15These classes were designed for use with the geometric functions
16supported by Postgresql (http://www.postgresql.com) although they
17do not need Postgresql to be present for the classes to be used.
18
19The basic 2d types implemented are those listed in the Postgresql documentation
20at http://www.postgresql.org/docs/7.3/static/datatype-geometric.html with
21the exception of 'line' which, as the documentation is says, is 'not fully
22implemented yet'.
23
24The package exports the following classes:
25
26    (class Point)
27    (class LineSeg)
28    (class Path)
29    (class Polygon)
30    (class Circle)
31    (class box)
32
33In addition the following factory methods are provided to make construction
34of these classes easier:
35
36    (func pointFromValues)
37    (func pointFromSequence)
38    (func lineSegFromPoints)
39    (func lineSegFromSequence)
40    (func pathFromPoints)
41    (func pathFromSequence)
42    (func boxFromPoints)
43    (func boxFromSequence)
44    (func circleFromCentreAndRadius)
45    (func circelFromSequence)
46    (func polygonFromPoints)
47    (func polygonFromSequence)
48
49The OpenGIS classes implemented are documented at
50http://postgis.refractions.net/docs/ch04.html#RefObject
51
52Supported OpenGIS classes are:
53
54    (class OGMultiPolygon)
55    (class OGPoint)
56    (class OGLineString)
57    (class OGLinearRing)
58    (class OGPolygon)
59    (class OGMultiPoint)
60    (class OGMultiLineString)
61    (class OGGeometryCollection)
62
63    * these classes support SRID, (x,y) and (x,y,z) variants but there
64    is currently no support for (x,y,m) or (x,y,z,m) variants.
65   
66Factory methods for OpenGIS classes
67
68    (func OGpointFromValues)
69    (func OGpointFromSequence)
70    (func OGlineStringFromOGPoints)
71    (func OGlineStringFromSequence)
72    (func OGlinearRingFromOGPoints)
73    (func OGlinearRingFromSequence)
74    (func OGpolygonFromOGLines)
75    (func OGpolygonFromSequence)
76    (func OGmultiPointFromOGPoints)
77    (func OGmultiPointFromSequence)
78    (func OGmultiLineStringFromOGLineStrings)
79    (func OGmultiLineStringFromSequence)
80    (func OGmultiPolygonFromOGPolygons)
81    (func OGmultiPolygonFromSequence)
82    (func OGgeometryCollectionFromOGGeometries)
83
84The OpenGIS format parsers are available as:
85
86    (class OGGeoTypeFactory)
87    (class EWKBParser)
88    (class HEXEWKBParer)
89    (class WKTParser)
90    (class WKBParser)
91   
92One final method is provided to help with linking these classes into psycopg
93(http://initd.org/software/initd/psycopg) database wrapper:
94
95    (func initialisePsycopgTypes)
96
97Example usage:
98
99    import psycopg
100    import GeoTypes
101
102    connect_string='dbname=schema_test user=postgres'
103    GeoTypes.initialisePsycopgTypes(psycopg_module=psycopg,
104                                    connect_string=connect_string,
105                                    register_opengis_types=True)
106
107    conn = psycopg.connect(connect_string)
108    curs = conn.cursor()
109
110    curs.execute("CREATE TABLE table_name (p1 point, b box)")
111
112    p = GeoTypes.pointFromValues(5.0,7.0)
113    b = GeoTypes.boxFromPoints(p,GeoTypes.pointFromValues(1.0,1.0))
114
115    curs.execute("INSERT INTO table_name VALUES (%(p)s,%(b)s)", {'p':p, 'b':b})
116
117
118    curs.execute("SELECT p1, b FROM table_name WHERE (p1 ~= %(p)s)", {'p':p})
119
120    ret = curs.fetchall()[0]
121
122    new_point = ret[0]
123    new_box   = ret[1]
124
125    if new_box == b:
126        print "We have the same box!"
127
128
129To use a binary cursor:
130
131    curs.execute("DECLARE zot BINARY CURSOR FOR SELECT p1,b FROM table_name
132                                           FOR READ ONLY")
133    curs.execute("FETCH ALL FROM zot")
134
135    raw = curs.fetchall()
136
137    conn.commit()
138
139Using the parser without hooking in to psycopg.
140
141You can parse the EWKT or EWKB formats manually if you do not want to use
142the psycopg implicit casting machinery.
143
144Example:
145
146    import GeoTypes
147    import psycopg
148   
149    conn = psycopg.connect('dbname=mq_test')
150    curs = conn.cursor()
151   
152    factory = GeoTypes.OGGeoTypeFactory()
153    parser = GeoTypes.WKTParser(factory)
154   
155    curs.execute("select AsEWKT(p1) from test");
156    f = curs.fetchall()
157   
158    parser.parseGeometry(f[0][0])
159    geom = factory.getGeometry()
160
161"""
162
163# Main (default) Postgres classes
164
165from _Point import Point
166from _LineSeg import LineSeg
167from _Path import Path
168from _Polygon import Polygon
169from _Circle import Circle
170from _Box import Box
171
172
173# Useful factory methods
174
175
176from _Point   import pointFromValues,           pointFromSequence
177from _LineSeg import lineSegFromPoints,         lineSegFromSequence
178from _Path    import pathFromPoints,            pathFromSequence
179from _Box     import boxFromPoints,             boxFromSequence
180from _Circle  import circleFromCentreAndRadius, circelFromSequence
181from _Polygon import polygonFromPoints,         polygonFromSequence
182
183
184# Psycopg initialisation support
185
186from _PsycopgInit import initialisePsycopgTypes
187
188# OpenGIS classes
189
190from _OGGeometry import OGGeometry
191from _OGMultiPolygon import OGMultiPolygon
192from _OGPoint import OGPoint
193from _OGLineString import OGLineString
194from _OGLinearRing import OGLinearRing
195from _OGPolygon import OGPolygon
196from _OGMultiPoint import OGMultiPoint
197from _OGMultiLineString import OGMultiLineString
198from _OGGeometryCollection import OGGeometryCollection
199
200# OpenGIS factory methods
201from _OGPoint       import OGpointFromValues,        OGpointFromSequence
202from _OGLineString import  OGlineStringFromOGPoints, OGlineStringFromSequence
203from _OGLinearRing import  OGlinearRingFromOGPoints, OGlinearRingFromSequence
204from _OGPolygon    import  OGpolygonFromOGLines,     OGpolygonFromSequence
205from _OGMultiPoint import  OGmultiPointFromOGPoints, OGmultiPointFromSequence
206from _OGMultiLineString import  OGmultiLineStringFromOGLineStrings, OGmultiLineStringFromSequence
207from _OGMultiPolygon import  OGmultiPolygonFromOGPolygons, OGmultiPolygonFromSequence
208from _OGGeometryCollection import OGgeometryCollectionFromOGGeometries
209
210# Direct access to parsers
211from _OGGeoTypeFactory import OGGeoTypeFactory
212from _EWKBParser import EWKBParser, HEXEWKBParser
213from _WKTParser import WKTParser
214from _WKBParser import WKBParser
Note: See TracBrowser for help on using the repository browser.