1 | ''' |
---|
2 | BSD Licence |
---|
3 | Copyright (c) 2012, Science & Technology Facilities Council (STFC) |
---|
4 | All rights reserved. |
---|
5 | |
---|
6 | Redistribution and use in source and binary forms, with or without modification, |
---|
7 | are 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 | |
---|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
---|
20 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
21 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
---|
22 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
---|
23 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
25 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | |
---|
29 | Created on 16 Jun 2012 |
---|
30 | |
---|
31 | @author: Maurizio Nagni |
---|
32 | ''' |
---|
33 | import logging |
---|
34 | import unicodedata |
---|
35 | |
---|
36 | from logging import StreamHandler |
---|
37 | ilogging = logging.getLogger('postgisutil') |
---|
38 | ilogging.addHandler(StreamHandler()) |
---|
39 | ilogging.setLevel(logging.INFO) |
---|
40 | |
---|
41 | |
---|
42 | def 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 | |
---|
55 | def unifyGeometries(bboxes, epb): |
---|
56 | return _processGeometries(bboxes, epb, 'ST_Union') |
---|
57 | |
---|
58 | def unifyGeometriesAsBBox(bboxes, epb): |
---|
59 | union = _processGeometries(bboxes, epb, 'ST_Union') |
---|
60 | if union is not None: |
---|
61 | return getBox2D(union, epb) |
---|
62 | return None |
---|
63 | |
---|
64 | def intersectGeometries(bboxes, epb): |
---|
65 | return _processGeometries(bboxes, epb, 'ST_Intersection') |
---|
66 | |
---|
67 | def 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 | |
---|
72 | def _processGeometries(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 | |
---|
98 | def normalizeUnicode(text): |
---|
99 | if isinstance(text, unicode): |
---|
100 | return unicodedata.normalize('NFKD', text).encode('ascii','ignore') |
---|
101 | return text |
---|