-
Notifications
You must be signed in to change notification settings - Fork 304
/
foliummap.py
120 lines (105 loc) · 3.75 KB
/
foliummap.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""Folium map class."""
from typing import Iterable
from numbers import Number
import warnings
import folium
# pylint: enable=locally-disabled, unused-import
from .utility import export
from .entityschema import IpAddress
from .._version import VERSION
__version__ = VERSION
__author__ = "Ian Hellen"
# pylint: disable=too-many-arguments, too-few-public-methods
@export
class FoliumMap:
"""Wrapper class for Folium/Leaflet mapping."""
def __init__(
self,
title: str = "layer1",
zoom_start: int = 7,
tiles=None,
width: str = "100%",
height: str = "100%",
):
"""
Create an instance of the folium map.
Parameters
----------
title : str, optional
Name of the layer (the default is 'layer1')
zoom_start : int, optional
The zoom level of the map (the default is 7)
tiles : [type], optional
Custom set of tiles or tile URL (the default is None)
width : str, optional
Map display width (the default is '100%')
height : str, optional
Map display height (the default is '100%')
Attributes
----------
folium_map : folium.Map
The map object.
"""
self.folium_map = folium.Map(
zoom_start=zoom_start, tiles=tiles, width=width, height=height
)
folium.TileLayer(name=title).add_to(self.folium_map)
def __repr__(self):
"""Return folium map."""
return self.folium_map
def add_ip_cluster(self, ip_entities: Iterable[IpAddress], **kwargs):
"""
Add a collection of IP Entities to the map.
Parameters
----------
ip_entities : Iterable[IpAddress]
a iterable of IpAddress Entities
Other Parameters
----------------
kwargs: icon properties to use for displaying this cluster
"""
for ip_entity in ip_entities:
if not (
isinstance(ip_entity.Location.Latitude, Number)
and isinstance(ip_entity.Location.Longitude, Number)
):
warnings.warn(
"Invalid location information for IP: " + ip_entity.Address,
RuntimeWarning,
)
continue
loc_props = ", ".join(
[
f"{key}={val}"
for key, val in ip_entity.Location.properties.items()
if val
]
)
popup_text = "{loc_props}<br>{IP}".format(
IP=ip_entity.Address, loc_props=loc_props
)
tooltip_text = "{City}, {CountryName}".format(
**ip_entity.Location.properties
)
if ip_entity.AdditionalData:
addl_props = ", ".join(
[
f"{key}={val}"
for key, val in ip_entity.AdditionalData.items()
if val
]
)
popup_text = f"{popup_text}<br>{addl_props}"
tooltip_text = f"{tooltip_text}, {addl_props}"
marker = folium.Marker(
location=[ip_entity.Location.Latitude, ip_entity.Location.Longitude],
popup=popup_text,
tooltip=tooltip_text,
icon=folium.Icon(**kwargs),
)
marker.add_to(self.folium_map)