source: TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/README @ 2194

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/GeoTypes-0.6.0/README@2194
Revision 2194, 5.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
2GeoTypes
3
4A package of classes for working with geomtric types in postgres.
5
6The package contains two collections of classes:
7
8  *  The first were designed for use with the geometric functions
9     supported by Postgresql (http://www.postgresql.com) although
10     they do not need Postgresql to be present for them to be used.
11
12     The types implemented are those listed in the Postgresql
13     documentation at http://www.postgresql.org/docs/7.3/static/datatype-geometric.html
14     with the exception of 'line' which, as the documentation is says,
15     is 'not fully implemented yet'.
16
17  *  The second were designed for use with the PostGIS/OpenGIS
18     extensions to postgres. All these classes begin with "OG" so
19     as to avoid name clashed with the standard postgres types.
20
21The package exports the following classes:
22
23   Standard Postgres classes:
24
25    (class Point)
26    (class LineSeg)
27    (class Path)
28    (class Polygon)
29    (class Circle)
30    (class box)
31
32    In addition the following factory methods are provided to make construction
33    of these classes easier:
34
35    (func pointFromValues)
36    (func pointFromSequence)
37    (func lineSegFromPoints)
38    (func lineSegFromSequence)
39    (func pathFromPoints)
40    (func pathFromSequence)
41    (func boxFromPoints)
42    (func boxFromSequence)
43    (func circleFromCentreAndRadius)
44    (func circelFromSequence)
45    (func polygonFromPoints)
46    (func polygonFromSequence)
47
48   OpenGIS classes:
49
50    (class OGMultiPolygon)
51    (class OGPoint)
52    (class OGLineString)
53    (class OGLinearRing)
54    (class OGPolygon)
55    (class OGMultiPoint)
56    (class OGMultiLineString)
57    (class OGGeometryCollection)
58
59    In addition the following factory methods are provided to make construction
60    of these classes easier:
61
62    (func OGpointFromValues)
63    (func OGpointFromSequence)
64    (func OGlineStringFromOGPoints)
65    (func OGlineStringFromSequence)
66    (func OGlinearRingFromOGPoints)
67    (func OGlinearRingFromSequence)
68    (func OGpolygonFromOGLines)
69    (func OGpolygonFromSequence)
70    (func OGmultiPointFromOGPoints)
71    (func OGmultiPointFromSequence)
72    (func OGmultiLineStringFromOGLineStrings)
73    (func OGmultiLineStringFromSequence)
74    (func OGmultiPolygonFromOGPolygons)
75    (func OGmultiPolygonFromSequence)
76    (func OGgeometryCollectionFromOGGeometries)
77
78
79One final method is provided to help with linking these classes into the
80psycopg (http://initd.org/software/initd/psycopg) database wrapper:
81
82    (func initialisePsycopgTypes)
83
84
85Example usage (Standard Postgres Types):
86
87
88    import psycopg
89    import GeoTypes
90
91    GeoTypes.initialisePsycopgTypes(psycopg_module=psycopg)
92
93    conn = psycopg.connect('dbname=schema_test user=postgres')
94    curs = conn.cursor()
95
96    curs.execute("CREATE TABLE table_name (p1 point, b box)")
97
98    p = GeoTypes.pointFromValues(5.0,7.0)
99    b = GeoTypes.boxFromPoints(p,GeoTypes.pointFromValues(1.0,1.0))
100
101    curs.execute("INSERT INTO table_name VALUES (%(p)s,%(b)s)", {'p':p, 'b':b})
102
103
104    curs.execute("SELECT p1, b FROM table_name WHERE (p1 ~= %(p)s)", {'p':p})
105
106    ret = curs.fetchall()[0]
107
108    new_point = ret[0]
109    new_box   = ret[1]
110
111    if new_box == b:
112        print "We have the same box!"
113
114
115
116Example usage (OpenGIS Types):
117
118 * Note * before you can use any of the OpenGIS types with a database you
119          must first prepare the database. Follow the instructions at:
120          http://postgis.refractions.net/docs/x83.html. The simplified
121          operations are:
122              createlang plpgsql [yourdatabase]
123              psql -d [yourdatabase] -f postgis.sql
124              psql -d [yourdatabase] -f spatial_ref_sys.sql
125
126
127    import psycopg
128    import GeoTypes
129
130    GeoTypes.initialisePsycopgTypes(psycopg_module=psycopg, connect_string='dbname=schema_test user=postgres',
131                                    register_opengis_types=1)
132
133    conn = psycopg.connect('dbname=schema_test user=postgres')
134    curs = conn.cursor()
135
136
137    curs.execute("CREATE TABLE table_name (dumy int)")
138    curs.execute("SELECT AddGeometryColumn('schema_test', 'table_name', 'p1', 128, 'POINT', 2 )")
139    curs.execute("SELECT AddGeometryColumn('schema_test', 'table_name', 'p2', 128, 'POLYGON', 2 )")
140
141
142    point   = GeoTypes.OGpointFromValues(5.0,7.0)
143    polygon = GeoTypes.OGpolygonFromOGLines(
144                GeoTypes.OGlinearRingFromOGPoints(
145                    GeoTypes.OGpointFromValues(3,4),
146                    GeoTypes.OGpointFromValues(4,4),
147                    GeoTypes.OGpointFromValues(4,3),
148                    GeoTypes.OGpointFromValues(3,4),
149                    ),
150                GeoTypes.OGlinearRingFromOGPoints(
151                    GeoTypes.OGpointFromValues(3,4),
152                    GeoTypes.OGpointFromValues(4,4),
153                    GeoTypes.OGpointFromValues(4,3),
154                    GeoTypes.OGpointFromValues(3,4),
155                    )
156                )
157
158    curs.execute("INSERT INTO table_name VALUES (1,%(point)s,%(polygon)s)", {'point':point, 'polygon':polygon})
159
160
161    curs.execute("SELECT p1, p2 FROM table_name WHERE (p1 ~= %(point)s)", {'point':point})
162
163    ret = curs.fetchall()[0]
164
165    new_point   = ret[0]
166    new_polygon = ret[1]
167
168    if new_polygon == polygon:
169        print "We have the same polygon!"
170    else:
171        print "Something bad has happend."
172        print "new_polygon = ", new_polygon
173        print "old_polygon = ", polygon
Note: See TracBrowser for help on using the repository browser.