source: mauRepo/MolesManager/trunk/cedaMoles/libs/postgisutil.py @ 8539

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

Incomplete - # 22528: Migration of FAtCat Open Search link for HPFeld
 http://team.ceda.ac.uk/trac/ceda/ticket/22528

  • 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 _process_geometries(bboxes, epb, 'ST_Union')
57
58def unifyGeometriesAsBBox(bboxes, epb):
59    union = _process_geometries(bboxes, epb, 'ST_Union')
60    if union is not None:
61        return getBox2D(union, epb)
62    return None
63
64def intersectGeometries(bboxes, epb):
65    return _process_geometries(bboxes, epb, 'ST_Intersection')
66
67def getBox2D(geometry, epb):
68    cmdString = 'SELECT ST_Box2D(st_setSRID(\'%s\', 4326));' % (geometry)
69    bbox = normalizeUnicode(epb.executeNative(cmdString).first()[0])
70    return bbox
71       
72def _process_geometries(bboxes, epb, function):
73    '''
74        Unifies a collection of geometries
75        @param bboxes: the collection of bboxes to unify
76        @param epb: a 'executeNative' enabled EPB
77        @param function: one of the following: ST_Union, ST_Intersection
78        @return: a string representing the union or None if invalid or a POINT   
79    '''
80    if len(bboxes) == 0:
81        return None           
82    union = None
83    for bbox in bboxes:
84        if union == None:
85            if 'st_setSRID' in bbox:
86                unionCmd = 'SELECT ST_AsText(%s);' % (bbox)
87            else:
88                unionCmd = 'SELECT ST_AsText(ST_GeomFromText(\'%s\',4326));' % (bbox)                   
89        else:
90            unionCmd = 'SELECT ST_AsText(%s(ST_GeomFromText(\'%s\', 4326), %s));' % (function, union, bbox)
91        ilogging.debug(unionCmd)               
92        union = normalizeUnicode(epb.executeNative(unionCmd).first()[0])
93        if 'POINT(' in union:
94                union = None
95           
96    return union
97
98def normalizeUnicode(text):
99    if isinstance(text, unicode):
100        return unicodedata.normalize('NFKD', text).encode('ascii','ignore')
101    return text
Note: See TracBrowser for help on using the repository browser.