@@ -54,25 +54,13 @@ def from_geocoord(cls, geocoord: GeoCoord, zoom: int) -> WebMercatorPixelCoord:
5454 256 * (1 << zoom ) * (geocoord .lng + 180 ) / 360 ,
5555 256
5656 * (1 << zoom )
57- * (
58- 0.5
59- - math .log (math .tan (math .pi / 4 + geocoord .lat * math .pi / 180 / 2 ))
60- / (2 * math .pi )
61- ),
57+ * (0.5 - math .log (math .tan (math .pi / 4 + geocoord .lat * math .pi / 180 / 2 )) / (2 * math .pi )),
6258 zoom ,
6359 )
6460
6561 def to_geocoord (self ) -> GeoCoord :
6662 lng = (360 * self .pixel_x ) / (1 << self .zoom_level * 256 ) - 180
67- lat = (
68- math .asin (
69- math .tanh (
70- math .pi * (1 - 2 * self .pixel_y / (1 << self .zoom_level * 256 ))
71- )
72- )
73- * 180
74- / math .pi
75- )
63+ lat = math .asin (math .tanh (math .pi * (1 - 2 * self .pixel_y / (1 << self .zoom_level * 256 )))) * 180 / math .pi
7664 return GeoCoord (lat , lng )
7765
7866 def belongs_tile (self ) -> WebMercatorTilePixel :
@@ -103,12 +91,8 @@ def __post_init__(self):
10391
10492 def covered_tiles (self ) -> Tuple [WebMercatorTilePixel , WebMercatorTilePixel ]:
10593 return (
106- WebMercatorPixelCoord (
107- self .pixel_x_west , self .pixel_y_north , self .zoom
108- ).belongs_tile (),
109- WebMercatorPixelCoord (
110- self .pixel_x_east , self .pixel_y_south , self .zoom
111- ).belongs_tile (),
94+ WebMercatorPixelCoord (self .pixel_x_west , self .pixel_y_north , self .zoom ).belongs_tile (),
95+ WebMercatorPixelCoord (self .pixel_x_east , self .pixel_y_south , self .zoom ).belongs_tile (),
11296 )
11397
11498 def geocoord2pixel (self , geocoord : GeoCoord ) -> Tuple [int , int ]:
@@ -122,17 +106,10 @@ def geocoords2pixel_ndarray(self, geocoords: np.ndarray) -> np.ndarray:
122106 return (
123107 np .array (
124108 [
125- 256 * (1 << self .zoom ) * (geocoords [..., 1 ] + 180 ) / 360
126- - self .pixel_x_west ,
109+ 256 * (1 << self .zoom ) * (geocoords [..., 1 ] + 180 ) / 360 - self .pixel_x_west ,
127110 256
128111 * (1 << self .zoom )
129- * (
130- 0.5
131- - np .log (
132- np .tan (np .pi / 4 + geocoords [..., 0 ] * np .pi / 180 / 2 )
133- )
134- / (2 * np .pi )
135- )
112+ * (0.5 - np .log (np .tan (np .pi / 4 + geocoords [..., 0 ] * np .pi / 180 / 2 )) / (2 * np .pi ))
136113 - self .pixel_y_north ,
137114 ]
138115 )
@@ -175,11 +152,7 @@ class RasterTileServer:
175152 def _get_image_content (url ):
176153 return cv2 .imdecode (
177154 np .asarray (
178- bytearray (
179- requests .get (
180- url , headers = {"user-agent" : f"hato-bot/{ VERSION } " }
181- ).content
182- ),
155+ bytearray (requests .get (url , headers = {"user-agent" : f"hato-bot/{ VERSION } " }).content ),
183156 dtype = np .uint8 ,
184157 ),
185158 - 1 ,
@@ -191,22 +164,15 @@ def request(self, bbox: WebMercatorPixelBBox) -> np.ndarray:
191164 request_urls = []
192165 for x in range (tl_tilepx .tile .tile_x , rb_tilepx .tile .tile_x + 1 ):
193166 for y in range (tl_tilepx .tile .tile_y , rb_tilepx .tile .tile_y + 1 ):
194- request_urls .append (
195- string .Template (self .url ).safe_substitute (
196- {"x" : x , "y" : y , "z" : bbox .zoom }
197- )
198- )
167+ request_urls .append (string .Template (self .url ).safe_substitute ({"x" : x , "y" : y , "z" : bbox .zoom }))
199168 with Pool (16 ) as p :
200169 imgs = list (p .imap (self ._get_image_content , request_urls ))
201170
202171 tile_width_cnt = rb_tilepx .tile .tile_x + 1 - tl_tilepx .tile .tile_x
203172 tile_height_cnt = rb_tilepx .tile .tile_y + 1 - tl_tilepx .tile .tile_y
204173
205174 concated = np .concatenate (
206- [
207- np .concatenate (imgs [i : i + tile_height_cnt ], axis = 0 )
208- for i in range (0 , len (imgs ), tile_height_cnt )
209- ],
175+ [np .concatenate (imgs [i : i + tile_height_cnt ], axis = 0 ) for i in range (0 , len (imgs ), tile_height_cnt )],
210176 axis = 1 ,
211177 )
212178
@@ -220,7 +186,6 @@ def request(self, bbox: WebMercatorPixelBBox) -> np.ndarray:
220186
221187@dataclass
222188class Layer (metaclass = ABCMeta ):
223-
224189 @abstractmethod
225190 def get_image (self , bbox : WebMercatorPixelBBox ) -> np .ndarray :
226191 pass
@@ -266,12 +231,7 @@ def get_image(self, bbox: WebMercatorPixelBBox) -> np.ndarray:
266231
267232 px_coords = bbox .geocoords2pixel_ndarray (coords )
268233
269- symbols = {
270- "thunder" : np .array (
271- [[1 , - 3 ], [- 0 , - 0.5 ], [2 , - 0.5 ], [- 1 , 3 ], [0 , 0.5 ], [- 2 , 0.5 ]]
272- )
273- / 6
274- }
234+ symbols = {"thunder" : np .array ([[1 , - 3 ], [- 0 , - 0.5 ], [2 , - 0.5 ], [- 1 , 3 ], [0 , 0.5 ], [- 2 , 0.5 ]]) / 6 }
275235 if self .symbol == "circle" :
276236 for c in px_coords :
277237 print (c )
@@ -316,8 +276,7 @@ def get_image(self, bbox: WebMercatorPixelBBox) -> np.ndarray:
316276 elif self .symbol in symbols .keys ():
317277 symbol_coords = symbols [self .symbol ]
318278 px_coords = (
319- np .repeat (px_coords [:, np .newaxis , :], symbol_coords .shape [0 ], axis = 1 )
320- + symbol_coords * self .size
279+ np .repeat (px_coords [:, np .newaxis , :], symbol_coords .shape [0 ], axis = 1 ) + symbol_coords * self .size
321280 )
322281 px_coords = px_coords .astype (np .int32 )
323282 for c in px_coords :
@@ -364,14 +323,10 @@ def get_image(self, bbox: WebMercatorPixelBBox) -> np.ndarray:
364323 layer_img = np .array (RasterTileServer (url ).request (bbox ), dtype = np .float32 )
365324
366325 if self .brightness != 1.0 or self .chroma != 1.0 :
367- img_hsv = np .array (
368- cv2 .cvtColor (layer_img , cv2 .COLOR_BGR2HSV ), dtype = np .float32
369- )
326+ img_hsv = np .array (cv2 .cvtColor (layer_img , cv2 .COLOR_BGR2HSV ), dtype = np .float32 )
370327 img_hsv [..., 1 ] = img_hsv [..., 1 ] * self .brightness
371328 img_hsv [..., 2 ] = img_hsv [..., 2 ] * self .chroma
372- layer_img = np .array (
373- cv2 .cvtColor (img_hsv , cv2 .COLOR_HSV2BGR ), dtype = np .float32
374- )
329+ layer_img = np .array (cv2 .cvtColor (img_hsv , cv2 .COLOR_HSV2BGR ), dtype = np .float32 )
375330
376331 if self .opacity != 1.0 :
377332 layer_img [layer_img [..., 3 ] != 0 , 3 ] = int (round (self .opacity * 256 ))
@@ -409,9 +364,7 @@ class HatoMap:
409364 layers : Optional [List [Layer ]] = None
410365 title : Optional [str ] = None
411366
412- def update_layout (
413- self , mapbox : Optional [MapBox ] = None , layers : Optional [List [Layer ]] = None
414- ) -> None :
367+ def update_layout (self , mapbox : Optional [MapBox ] = None , layers : Optional [List [Layer ]] = None ) -> None :
415368 if mapbox is not None :
416369 self .mapbox = mapbox
417370 if layers is not None :
@@ -475,9 +428,9 @@ def get_image(self, height: int, width: int) -> np.ndarray:
475428 if body_img .shape == none_body_img_shape :
476429 body_img = layer_img [..., :3 ]
477430 else :
478- body_img = body_img [..., :3 ] * (
479- 1 - layer_img [..., 3 :] / 255
480- ) + layer_img [..., : 3 ] * ( layer_img [..., 3 :] / 255 )
431+ body_img = body_img [..., :3 ] * (1 - layer_img [..., 3 :] / 255 ) + layer_img [..., : 3 ] * (
432+ layer_img [..., 3 :] / 255
433+ )
481434
482435 img = np .zeros ((height , width , 3 ), np .uint8 )
483436 img .fill (255 )
@@ -501,9 +454,7 @@ def get_circle(lat: float, lng: float, radius: float) -> np.ndarray:
501454 earth_e_sq = (2 * earth_f - 1 ) / earth_f ** 2
502455 c = 1 - (earth_e_sq * math .sin (lat ) ** 2 )
503456 meter_1deg_lat = math .pi * earth_radius * (1 - earth_e_sq ) / (180 * c ** 1.5 )
504- meter_1deg_lng = (
505- math .pi * earth_radius * math .cos (lat * math .pi / 180 ) / (180 * math .sqrt (c ))
506- )
457+ meter_1deg_lng = math .pi * earth_radius * math .cos (lat * math .pi / 180 ) / (180 * math .sqrt (c ))
507458 lats = np .sin (np .radians (np .arange (0 , 361 , 1 ))) * radius / meter_1deg_lat + lat
508459 lngs = np .cos (np .radians (np .arange (0 , 361 , 1 ))) * radius / meter_1deg_lng + lng
509460 return np .array ([lats , lngs ]).T
0 commit comments