source: mauRepo/MolesManager/trunk/src/libs/postgisutil.py @ 8416

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/libs/postgisutil.py@8416
Revision 8416, 4.2 KB checked in by mnagni, 7 years ago (diff)

Incomplete - # 22488: CEDA Observation Collection - Geographical Extent
 http://team.ceda.ac.uk/trac/ceda/ticket/22488

  • Property svn:mime-type set to text/plain
Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 16 Jun 2012
30
31@author: Maurizio Nagni
32'''
33import logging
34import unicodedata
35
36from logging import StreamHandler
37ilogging = logging.getLogger('postgisutil')
38ilogging.addHandler(StreamHandler())
39ilogging.setLevel(logging.INFO)
40
41
42def create_st_setSRID(west, south, east, north, srid = 4326):
43    '''
44        Create a string useful to build bounding box for queries
45        @param west: the west longitude (in decimal degrees) 
46        @param south: the south latitude (in decimal degrees)       
47        @param east: the east longitude (in decimal degrees) 
48        @param north: the north latitude (in decimal degrees)
49        @param srid: an integer value that uniquely identifies the Spatial Referencing System (SRS) within the database       
50        @return: Returns a postgis string describing a BOX3D using EPSG:4326 decimal degree
51    '''
52    return 'st_setSRID(\'BOX3D(%d %d, %d %d)\'::box3d, %d)' \
53        % (west, south, east, north, srid)
54
55def unifyGeometries(bboxes, epb):
56    return _processGeometries(bboxes, epb, 'ST_Union')
57
58def unifyGeometriesAsBBox(bboxes, epb):
59    union = _processGeometries(bboxes, epb, 'ST_Union')
60    return getBox2D(union, epb)
61
62def intersectGeometries(bboxes, epb):
63    return _processGeometries(bboxes, epb, 'ST_Intersection')
64
65def getBox2D(geometry, epb):
66    cmdString = 'SELECT ST_Box2D(st_setSRID(\'%s\', 4326));' % (geometry)
67    bbox = normalizeUnicode(epb.executeNative(cmdString).first()[0])
68    return bbox
69       
70def _processGeometries(bboxes, epb, function):
71    '''
72        Unifies a collection of geometries
73        @param bboxes: the collection of bboxes to unify
74        @param epb: a 'executeNative' enabled EPB
75        @param function: one of the following: ST_Union, ST_Intersection
76        @return: a string representing the union or None if invalid or a POINT   
77    '''
78    if len(bboxes) == 0:
79        return None           
80    union = None
81    for bbox in bboxes:
82        if union == None:
83            if 'st_setSRID' in bbox:
84                unionCmd = 'SELECT ST_AsText(%s);' % (bbox)
85            else:
86                unionCmd = 'SELECT ST_AsText(ST_GeomFromText(\'%s\',4326));' % (bbox)                   
87        else:
88            unionCmd = 'SELECT ST_AsText(%s(ST_GeomFromText(\'%s\', 4326), %s));' % (function, union, bbox)
89        ilogging.debug(unionCmd)               
90        union = normalizeUnicode(epb.executeNative(unionCmd).first()[0])
91        if 'POINT(' in union:
92                union = None
93           
94    return union
95
96def normalizeUnicode(text):
97    if isinstance(text, unicode):
98        return unicodedata.normalize('NFKD', text).encode('ascii','ignore')
99    return text
Note: See TracBrowser for help on using the repository browser.