~awuerl/blitzortung-python/master

« back to all changes in this revision

Viewing changes to blitzortung/builder/strike_cluster.py

  • Committer: Andreas Würl
  • Date: 2016-09-14 20:39:51 UTC
  • mto: This revision was merged to the branch mainline in revision 392.
  • Revision ID: git-v1:0577158d36adaee55ba3f8a63918b601e8a4edd4
remove pandas dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf8 -*-
2
 
 
3
 
"""
4
 
 
5
 
   Copyright 2014-2016 Andreas Würl
6
 
 
7
 
   Licensed under the Apache License, Version 2.0 (the "License");
8
 
   you may not use this file except in compliance with the License.
9
 
   You may obtain a copy of the License at
10
 
 
11
 
       http://www.apache.org/licenses/LICENSE-2.0
12
 
 
13
 
   Unless required by applicable law or agreed to in writing, software
14
 
   distributed under the License is distributed on an "AS IS" BASIS,
15
 
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
   See the License for the specific language governing permissions and
17
 
   limitations under the License.
18
 
 
19
 
"""
20
 
 
21
 
from geographiclib.geodesic import Geodesic
22
 
from geographiclib.polygonarea import PolygonArea
23
 
 
24
 
from .. import data
25
 
 
26
 
 
27
 
class StrikeCluster(object):
28
 
    """
29
 
    class for building strike cluster objects
30
 
    """
31
 
 
32
 
    def __init__(self):
33
 
        self.cluster_id = -1
34
 
        self.timestamp = None
35
 
        self.interval_seconds = 0
36
 
        self.shape = None
37
 
        self.strike_count = 0
38
 
 
39
 
    def with_id(self, cluster_id):
40
 
        self.cluster_id = cluster_id
41
 
        return self
42
 
 
43
 
    def with_timestamp(self, timestamp):
44
 
        self.timestamp = timestamp
45
 
        return self
46
 
 
47
 
    def with_interval_seconds(self, interval_seconds):
48
 
        self.interval_seconds = interval_seconds
49
 
        return self
50
 
 
51
 
    def with_shape(self, shape):
52
 
        self.shape = shape
53
 
 
54
 
        return self
55
 
 
56
 
    def with_strike_count(self, strike_count):
57
 
        self.strike_count = strike_count
58
 
        return self
59
 
 
60
 
    def build(self):
61
 
        if self.shape is not None:
62
 
            poly_area = PolygonArea(Geodesic.WGS84)
63
 
            if self.shape.coords:
64
 
                try:
65
 
                    for (x, y) in zip(self.shape.coords.xy[0], self.shape.coords.xy[1]):
66
 
                        poly_area.AddPoint(x, y)
67
 
                    area = round(poly_area.Compute(False, True)[2] / 1e6, 1)
68
 
                except ValueError:
69
 
                    area = None
70
 
            else:
71
 
                area = None
72
 
        else:
73
 
            area = None
74
 
 
75
 
        return data.StrikeCluster(self.cluster_id, self.timestamp, self.interval_seconds, self.shape, self.strike_count,
76
 
                                  area)