osm_poi_matchmaker.libs.geo.check_geom()   C
last analyzed

Complexity

Conditions 11

Size

Total Lines 44
Code Lines 28

Duplication

Lines 28
Ratio 63.64 %

Importance

Changes 0
Metric Value
cc 11
eloc 28
nop 3
dl 28
loc 44
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like osm_poi_matchmaker.libs.geo.check_geom() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
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}')
0 ignored issues
show
Bug introduced by
A suspicious escape sequence \d was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
16
17
18
def geom_point(latitude, longitude, projection):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
19
    if latitude is not None and longitude is not None:
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
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 != ''):
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
36 View Code Duplication
        if not isinstance(latitude, float):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
37
            la = PATTERN_COORDINATE.search(latitude.replace(',', '.').strip())
0 ignored issues
show
Coding Style Naming introduced by
Variable name "la" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
38
            try:
39
                if la is not None:
40
                    lat = la.group()
41
                else:
42
                    return None
43
            except (AttributeError, IndexError) as e:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "e" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
52
            lo = PATTERN_COORDINATE.search(longitude.replace(',', '.').strip())
0 ignored issues
show
Coding Style Naming introduced by
Variable name "lo" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
53
            try:
54
                if lo is not None:
55
                    lon = lo.group()
56
                else:
57
                    return None
58
            except (AttributeError, IndexError) as e:
0 ignored issues
show
Coding Style Naming introduced by
Variable name "e" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
72
    if (latitude is not None and latitude != '' and latitude != 0.0) and (
0 ignored issues
show
unused-code introduced by
Unnecessary "else" after "return"
Loading history...
best-practice introduced by
Too many boolean expressions in if statement (6/5)
Loading history...
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