source: mauRepo/HPFos/trunk/src/libs/postgisutil.py @ 8354

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

Implemented the bbox query URL (at least for the CEDA_ObservationCollection)

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