1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
|
|
try: |
4
|
|
|
import logging |
5
|
|
|
import sys |
6
|
|
|
import re |
7
|
|
|
from geoalchemy2 import WKTElement |
8
|
|
|
from osm_poi_matchmaker.utils import config |
9
|
|
|
except ImportError as err: |
10
|
|
|
logging.error('Error %s import module: %s', __name__, err) |
11
|
|
|
logging.exception('Exception occurred') |
12
|
|
|
|
13
|
|
|
sys.exit(128) |
14
|
|
|
|
15
|
|
|
PATTERN_COORDINATE = re.compile('[\d]{1,3}.[\d]{2,5}') |
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def geom_point(latitude, longitude, projection): |
|
|
|
|
19
|
|
|
if latitude is not None and longitude is not None: |
|
|
|
|
20
|
|
|
return WKTElement('POINT({} {})'.format(latitude, longitude), srid=projection) |
21
|
|
|
else: |
22
|
|
|
return None |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def check_geom(latitude, longitude, proj=config.get_geo_default_projection()): |
26
|
|
|
""" |
27
|
|
|
Basic check of latitude and longitude geom point |
28
|
|
|
Are both coordinates are exist and extract only the right format |
29
|
|
|
|
30
|
|
|
:param latitude: Coordinate latitude part of geom |
31
|
|
|
:param longitude: Coordinate longitude part of geom |
32
|
|
|
:param proj: Projection of geom |
33
|
|
|
:return: Validated coordinates or None on error |
34
|
|
|
""" |
35
|
|
|
if (latitude is not None and latitude != '') and (longitude is not None and longitude != ''): |
|
|
|
|
36
|
|
View Code Duplication |
if not isinstance(latitude, float): |
|
|
|
|
37
|
|
|
la = PATTERN_COORDINATE.search(latitude.replace(',', '.').strip()) |
|
|
|
|
38
|
|
|
try: |
39
|
|
|
if la is not None: |
40
|
|
|
lat = la.group() |
41
|
|
|
else: |
42
|
|
|
return None |
43
|
|
|
except (AttributeError, IndexError) as e: |
|
|
|
|
44
|
|
|
logging.error('%s;%s', latitude, longitude) |
45
|
|
|
logging.error(e) |
46
|
|
|
logging.exception('Exception occurred') |
47
|
|
|
|
48
|
|
|
return None |
49
|
|
|
else: |
50
|
|
|
lat = latitude |
51
|
|
View Code Duplication |
if not isinstance(longitude, float): |
|
|
|
|
52
|
|
|
lo = PATTERN_COORDINATE.search(longitude.replace(',', '.').strip()) |
|
|
|
|
53
|
|
|
try: |
54
|
|
|
if lo is not None: |
55
|
|
|
lon = lo.group() |
56
|
|
|
else: |
57
|
|
|
return None |
58
|
|
|
except (AttributeError, IndexError) as e: |
|
|
|
|
59
|
|
|
logging.error('%s;%s', latitude, longitude) |
60
|
|
|
logging.error(e) |
61
|
|
|
logging.exception('Exception occurred') |
62
|
|
|
|
63
|
|
|
return None |
64
|
|
|
else: |
65
|
|
|
lon = longitude |
66
|
|
|
return geom_point(lat, lon, proj) |
67
|
|
|
else: |
68
|
|
|
return None |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def check_hu_boundary(latitude, longitude): |
|
|
|
|
72
|
|
|
if (latitude is not None and latitude != '' and latitude != 0.0) and ( |
|
|
|
|
73
|
|
|
longitude is not None and longitude != '' and longitude != 0.0): |
74
|
|
|
# This is a workaround because original datasource may contains swapped lat / lon parameters |
75
|
|
|
if float(latitude) < 44: |
76
|
|
|
logging.warning( |
77
|
|
|
'Latitude-longitude replacement. Originally was: latitude: %s, longitude: %s.', |
78
|
|
|
latitude, longitude) |
79
|
|
|
longitude, latitude = latitude, longitude |
80
|
|
|
# Another workaround to insert missing decimal point |
81
|
|
|
if float(longitude) > 200: |
82
|
|
|
longitude = '{}.{}'.format(longitude[:2], longitude[3:]) |
83
|
|
|
if longitude.count('.') > 1: |
84
|
|
|
lon_tmp = longitude.split('.') |
85
|
|
|
longitude = '.'.join(lon_tmp[0:1]) |
86
|
|
|
if float(latitude) > 200: |
87
|
|
|
latitude = '{}.{}'.format(latitude[:2], latitude[3:]) |
88
|
|
|
if latitude.count('.') > 1: |
89
|
|
|
lat_tmp = latitude.split('.') |
90
|
|
|
latitude = '.'.join(lat_tmp[0:1]) |
91
|
|
|
return latitude, longitude |
92
|
|
|
else: |
93
|
|
|
return None, None |
94
|
|
|
|