Skip to content

Commit 57bd44d

Browse files
committed
extract gis constants and grids and caches
1 parent 970b592 commit 57bd44d

11 files changed

Lines changed: 217 additions & 169 deletions

File tree

blitzortung/cli/db.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import blitzortung.config
3232
import blitzortung.db.query
33+
import blitzortung.gis.geo
3334
import blitzortung.geom
3435
import blitzortung.util
3536

@@ -54,23 +55,23 @@ def parse_time(date_string, time_string, tz, description, is_end_time=False):
5455

5556

5657
def prepare_grid_if_applicable(options, area):
57-
if options.grid is not None or options.xgrid is not None or options.ygrid is not None:
58+
if blitzortung.gis.geo.grid is not None or options.xgrid is not None or options.ygrid is not None:
5859

5960
grid_x = 1.0
6061
grid_y = 1.0
6162

62-
if options.grid is not None:
63-
grid_x = options.grid
64-
grid_y = options.grid
63+
if blitzortung.gis.geo.grid is not None:
64+
grid_x = blitzortung.gis.geo.grid
65+
grid_y = blitzortung.gis.geo.grid
6566

6667
if options.xgrid is not None:
6768
grid_x = options.xgrid
68-
if options.ygrid is None and options.grid is None:
69+
if options.ygrid is None and blitzortung.gis.geo.grid is None:
6970
grid_y = grid_x
7071

7172
if options.ygrid is not None:
7273
grid_y = options.ygrid
73-
if options.xgrid is None and options.grid is None:
74+
if options.xgrid is None and blitzortung.gis.geo.grid is None:
7475
grid_x = grid_y
7576

7677
if options.area is None:

blitzortung/cli/webservice.py

Lines changed: 54 additions & 157 deletions
Large diffs are not rendered by default.

blitzortung/db/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222

2323
from injector import Module, singleton, inject, provider
2424

25-
try:
26-
from psycopg2cffi import compat
27-
compat.register()
28-
except ImportError:
29-
pass
25+
from . import compat # Register psycopg2cffi compatibility
3026

3127
import psycopg2
3228
import psycopg2.pool

blitzortung/gis/__init__.py

Whitespace-only changes.

blitzortung/gis/constants.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pyproj
2+
3+
import blitzortung.geom
4+
5+
UTM_EU = pyproj.CRS('epsg:32633') # UTM 33 N / WGS84
6+
UTM_NORTH_AMERICA = pyproj.CRS('epsg:32614') # UTM 14 N / WGS84
7+
UTM_CENTRAL_AMERICA = pyproj.CRS('epsg:32614') # UTM 14 N / WGS84
8+
UTM_SOUTH_AMERICA = pyproj.CRS('epsg:32720') # UTM 20 S / WGS84
9+
UTM_OCEANIA = pyproj.CRS('epsg:32755') # UTM 55 S / WGS84
10+
UTM_ASIA = pyproj.CRS('epsg:32650') # UTM 50 N / WGS84
11+
UTM_AFRICA = pyproj.CRS('epsg:32633') # UTM 33 N / WGS84
12+
UTM_NORTH = pyproj.CRS('epsg:32631') # UTM 31 N / WGS84
13+
UTM_SOUTH = pyproj.CRS('epsg:32731') # UTM 31 S / WGS84
14+
15+
grid = {
16+
1: blitzortung.geom.GridFactory(-25, 57, 27, 72, UTM_EU),
17+
2: blitzortung.geom.GridFactory(110, 180, -50, 0, UTM_OCEANIA),
18+
3: blitzortung.geom.GridFactory(-140, -50, 10, 60, UTM_NORTH_AMERICA),
19+
4: blitzortung.geom.GridFactory(85, 150, -10, 60, UTM_ASIA),
20+
5: blitzortung.geom.GridFactory(-100, -30, -50, 20, UTM_SOUTH_AMERICA),
21+
6: blitzortung.geom.GridFactory(-20, 50, -40, 40, UTM_AFRICA),
22+
7: blitzortung.geom.GridFactory(-115, -50, 0, 30, UTM_CENTRAL_AMERICA)
23+
}
24+
25+
global_grid = blitzortung.geom.GridFactory(-180, 180, -90, 90, UTM_EU, 11, 48)

blitzortung/gis/local_grid.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from dataclasses import dataclass
2+
3+
import blitzortung.geom
4+
from .constants import UTM_NORTH, UTM_SOUTH
5+
6+
7+
@dataclass
8+
class LocalGrid:
9+
data_area: int
10+
x: int
11+
y: int
12+
13+
@property
14+
def size(self):
15+
return self.data_area * DATA_AREA_SIZE_FACTOR
16+
17+
@property
18+
def reference_longitude(self):
19+
return (self.x - 1) * self.data_area
20+
21+
@property
22+
def reference_latitude(self):
23+
return (self.y - 1) * self.data_area
24+
25+
@property
26+
def center_latitude(self):
27+
return self.reference_latitude + self.size / 2.0
28+
29+
@property
30+
def longitude_extension(self):
31+
return abs(self.center_latitude) / 15.0
32+
33+
34+
def get_grid_factory(self) -> blitzortung.geom.GridFactory:
35+
return blitzortung.geom.GridFactory(
36+
self.reference_longitude - self.longitude_extension,
37+
self.reference_longitude + self.size + self.longitude_extension,
38+
self.reference_latitude,
39+
self.reference_latitude + self.size,
40+
UTM_NORTH if self.reference_latitude >= 0 else UTM_SOUTH,
41+
LOCAL_GRID_UTM_LONGITUDE,
42+
self.reference_latitude + self.size / 2.0
43+
)
44+
45+
46+
DATA_AREA_SIZE_FACTOR = 3
47+
LOCAL_GRID_UTM_LONGITUDE = 3

blitzortung/service/cache.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from ..cache import ObjectCache
2+
3+
4+
class ServiceCache:
5+
CACHE_CLEANUP_PERIOD = 300 # 5 minutes
6+
CACHE_TTL_SHORT = 20 # seconds
7+
CACHE_TTL_LONG = 60 # seconds
8+
LOCAL_CACHE_SIZE_CURRENT = 100
9+
LOCAL_CACHE_SIZE_HISTORY = 400
10+
11+
def __init__(self):
12+
self.__strikes_grid = ObjectCache(
13+
ttl_seconds=self.CACHE_TTL_SHORT, cleanup_period=self.CACHE_CLEANUP_PERIOD)
14+
self.__strikes_history_grid = ObjectCache(
15+
ttl_seconds=self.CACHE_TTL_LONG, cleanup_period=self.CACHE_CLEANUP_PERIOD)
16+
17+
self.__global_strikes_grid = ObjectCache(
18+
ttl_seconds=self.CACHE_TTL_SHORT, cleanup_period=self.CACHE_CLEANUP_PERIOD)
19+
self.__global_strikes_history_grid = ObjectCache(
20+
ttl_seconds=self.CACHE_TTL_LONG, cleanup_period=self.CACHE_CLEANUP_PERIOD)
21+
22+
self.__local_strikes_grid = ObjectCache(
23+
ttl_seconds=self.CACHE_TTL_SHORT, size=self.LOCAL_CACHE_SIZE_CURRENT,
24+
cleanup_period=self.CACHE_CLEANUP_PERIOD)
25+
self.__local_strikes_history_grid = ObjectCache(
26+
ttl_seconds=self.CACHE_TTL_LONG, size=self.LOCAL_CACHE_SIZE_HISTORY,
27+
cleanup_period=self.CACHE_CLEANUP_PERIOD)
28+
29+
self.histogram = ObjectCache(
30+
ttl_seconds=self.CACHE_TTL_LONG, cleanup_period=self.CACHE_CLEANUP_PERIOD)
31+
32+
def global_strikes(self, minute_offset):
33+
return self.__global_strikes_grid if minute_offset == 0 else self.__global_strikes_history_grid
34+
35+
def local_strikes(self, minute_offset):
36+
return self.__local_strikes_grid if minute_offset == 0 else self.__local_strikes_history_grid
37+
38+
def strikes(self, minute_offset):
39+
return self.__strikes_grid if minute_offset == 0 else self.__strikes_history_grid

blitzortung/service/db.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import psycopg2
2020
import psycopg2.extras
21-
from pytest_twisted import inlineCallbacks
2221
from twisted.internet.defer import Deferred
2322
from twisted.python import log
2423
from txpostgres import reconnection
@@ -67,6 +66,7 @@ class DictConnectionPool(ConnectionPool):
6766
def __init__(self, _ignored, *connargs, **connkw):
6867
super(DictConnectionPool, self).__init__(_ignored, *connargs, **connkw)
6968

69+
7070
def create_connection_pool() -> Deferred:
7171
"""Create and start the database connection pool."""
7272
config = blitzortung.config.config()
@@ -79,5 +79,6 @@ def create_connection_pool() -> Deferred:
7979

8080
return d
8181

82+
8283
def execute(connection, query: SelectQuery):
8384
return connection.runQuery(str(query), query.get_parameters())

blitzortung/util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,15 @@ def force_range(lower_limit, value, upper_limit):
105105
return upper_limit
106106
else:
107107
return value
108+
109+
class TimeConstraint:
110+
111+
def __init__(self, default_minute_length: int, max_minute_length: int):
112+
self.default_minute_length = default_minute_length
113+
self.max_minute_length = max_minute_length
114+
115+
def enforce(self, minute_length, minute_offset):
116+
minute_length = force_range(minute_length, 0, self.max_minute_length)
117+
minute_length = self.default_minute_length if minute_length == 0 else minute_length
118+
minute_offset = force_range(minute_offset, -self.max_minute_length + minute_length, 0)
119+
return minute_length, minute_offset

tests/gis/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)