forked from ronreiter/anyway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
99 lines (82 loc) · 2.48 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from __future__ import print_function
import os
from csv import DictReader
from flask import Flask
import pyproj
import threading
import sys
import re
def init_flask(name):
"""
initializes a Flask instance with default values
:param name: the name of the instance
"""
app = Flask(name)
app.config.from_object('config')
return app
class ProgressSpinner(object):
def __init__(self):
self.counter = 0
self.chars = ['|', '/', '-', '\\']
def show(self):
"""
prints a rotating spinner
"""
current_char = self.counter % len(self.chars)
sys.stderr.write("\r%s" % self.chars[current_char])
self.counter += 1
class CsvReader(object):
"""
loads and handles csv files
"""
_digit_pattern = re.compile('^-?\d*(\.\d+)?$')
def __init__(self, filename):
self._file = open(filename)
self._lock = threading.RLock()
self._closed = False
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
with self._lock:
if not self._closed:
self._file.close()
def name(self):
"""
the filename of the csv file
:return:
"""
return self._file.name
def close(self):
with self._lock:
if not self._closed:
self._file.close()
def _convert(self, value):
"""
converts an str value to a typed one
"""
if value == '':
return None
# the isdigit function doesn't match negative numbers
if CsvReader._digit_pattern.match(value):
return int(float(value))
return value
def __iter__(self):
for line in DictReader(self._file):
converted = dict([(key.upper(), self._convert(val)) for key, val in line.iteritems()])
yield converted
class ItmToWGS84(object):
def __init__(self):
# initializing WGS84 (epsg: 4326) and Israeli TM Grid (epsg: 2039) projections.
# for more info: http://spatialreference.org/ref/epsg/<epsg_num>/
self.wgs84 = pyproj.Proj(init='epsg:4326')
self.itm = pyproj.Proj(init='epsg:2039')
def convert(self, x, y):
"""
converts ITM to WGS84 coordinates
:type x: float
:type y: float
:rtype: tuple
:return: (long,lat)
"""
longitude, latitude = pyproj.transform(self.itm, self.wgs84, x, y)
return longitude, latitude