Skip to content

Commit 5d86739

Browse files
committed
Add support for downloading maps from JourneyMap
1 parent a4f9456 commit 5d86739

5 files changed

Lines changed: 94 additions & 7 deletions

File tree

controllers/download.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from core.dynmap import Dynmap
77
from core.dynmapcoords import DynmapCoords
88
from core.gridref import GridRef
9+
from core.journeymap import JourneyMap
910
from core.map import Map
1011
from core.state import State
1112
from models.config import Config
@@ -132,14 +133,55 @@ def download(self):
132133
except Exception:
133134
self.view.display_error('Failed to download tiles')
134135
return
136+
elif user_input['source'] == 'JourneyMap':
137+
if user_input['coord_mode'] == 'mc':
138+
from_region_x = int(input_range['from_x']) >> 4 >> 5
139+
from_region_y = int(input_range['from_z']) >> 4 >> 5
140+
to_region_x = int(input_range['to_x']) >> 4 >> 5
141+
to_region_y = int(input_range['to_z']) >> 4 >> 5
142+
elif user_input['coord_mode'] == 'grid':
143+
try:
144+
from_region_x, from_region_y = GridRef.grid_ref_to_mc(input_range['from'])
145+
except ValueError as e:
146+
self.view.display_error('From grid reference: {}'.format(str(e)))
147+
return
148+
149+
try:
150+
to_region_x, to_region_y = GridRef.grid_ref_to_mc(input_range['to'])
151+
except ValueError as e:
152+
self.view.display_error('To grid reference: {}'.format(str(e)))
153+
return
154+
elif user_input['coord_mode'] == 'dynmap':
155+
self.view.display_error('Dynmap tile coordinates cannot be used with JourneyMap source')
156+
return
157+
else:
158+
self.view.display_error('Invalid coordinate mode')
159+
return
160+
161+
low_region_x = min(from_region_x, to_region_x)
162+
low_region_y = min(from_region_y, to_region_y)
163+
high_region_x = max(from_region_x, to_region_x)
164+
high_region_y = max(from_region_y, to_region_y)
135165

136166
try:
137-
Map.stitch(tiles_dir, user_input['output_dir'], stitch_args, self.update_progress)
167+
stitch_args = JourneyMap.download_tiles(tiles_dir, self.state.selected_server.journeymap_url,
168+
user_input['map_id'], input_range['zoom'], low_region_x,
169+
low_region_y, high_region_x, high_region_y,
170+
self.update_progress)
138171
except Exception:
139-
self.view.display_error('Failed to stitch tiles')
172+
self.view.display_error('Failed to download tiles')
140173
return
174+
else:
175+
self.view.display_error('Invalid source')
176+
return
177+
178+
try:
179+
Map.stitch(tiles_dir, user_input['output_dir'], stitch_args, self.update_progress)
180+
except Exception:
181+
self.view.display_error('Failed to stitch tiles')
182+
return
141183

142-
self.update_progress('Idle', 0, 1)
184+
self.update_progress('Idle', 0, 1)
143185

144186
def update_progress(self, message: str, i, total):
145187
self.progress_msg = message

core/dynmap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import validators
1010

1111
from models.server import Server
12-
from models.tilestitchargs import TileStitchArgs
12+
from models.tilestitchargs import TileStitchArgs, YMode
1313

1414

1515
class Dynmap:
@@ -71,4 +71,4 @@ def download_tiles(tiles_dir: str, dynmap_url: str, world_name: str, map_name: s
7171
url = dynmap_url + Dynmap.get_tile_path(world_name, map_name, zoom, x, y)
7272
urllib.request.urlretrieve(url, os.path.join(tiles_dir, '{}_{}.jpg'.format(x, y)))
7373

74-
return TileStitchArgs(from_x, from_y, to_x, to_y, 128, 128, diff)
74+
return TileStitchArgs(from_x, from_y, to_x, to_y, 128, 128, diff, YMode.DYNMAP)

core/journeymap.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import urllib
3+
from urllib import request
4+
5+
from models.tilestitchargs import TileStitchArgs, YMode
6+
7+
8+
class JourneyMap:
9+
@staticmethod
10+
def get_tile_path(map_type: str, zoom: int, region_x: float, region_y: float):
11+
return '/tiles/tile.png?x={}&z={}&dimension=minecraft:overworld&mapTypeString={}&zoom={}'.format(int(region_x),
12+
int(region_y),
13+
map_type, zoom)
14+
15+
@staticmethod
16+
def download_tiles(tiles_dir: str, journeymap_url: str, map_type: str, zoom: int, from_x, from_y, to_x, to_y,
17+
progress_callback=None) -> TileStitchArgs:
18+
diff = 1
19+
total = ((to_x - from_x) / diff + 1) * ((to_y - from_y) / diff + 1)
20+
i = 0
21+
for x in range(from_x, to_x + 1, diff):
22+
for y in range(from_y, to_y + 1, diff):
23+
i += 1
24+
if progress_callback is not None:
25+
progress_callback('Downloading ({} of {})'.format(i, int(total)), i, total)
26+
27+
url = journeymap_url + JourneyMap.get_tile_path(map_type, zoom, x, y)
28+
urllib.request.urlretrieve(url, os.path.join(tiles_dir, '{}_{}.jpg'.format(x, y)))
29+
30+
return TileStitchArgs(from_x, from_y, to_x, to_y, 512, 512, diff, YMode.JOURNEYMAP)

core/map.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from PIL import Image
44

5-
from models.tilestitchargs import TileStitchArgs
5+
from models.tilestitchargs import TileStitchArgs, YMode
66

77

88
class Map:
@@ -28,6 +28,14 @@ def stitch(tiles_dir: str, output_dir: str, args: TileStitchArgs, progress_callb
2828

2929
tile = Image.open(os.path.join(tiles_dir, '{}_{}.jpg'.format(x, y)))
3030
tile_img = tile.copy()
31-
img.paste(tile_img, (int(img_x), int(height - img_y - args.tile_height)))
31+
32+
if args.y_mode == YMode.DYNMAP:
33+
y_location = int(height - img_y - args.tile_height)
34+
elif args.y_mode == YMode.JOURNEYMAP:
35+
y_location = int(img_y)
36+
else:
37+
raise ValueError("Y Mode invalid")
38+
39+
img.paste(tile_img, (int(img_x), y_location))
3240

3341
img.save(os.path.join(output_dir, 'map.png'))

models/tilestitchargs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
from dataclasses import dataclass
2+
from enum import Enum
3+
4+
5+
class YMode(Enum):
6+
DYNMAP = 0,
7+
JOURNEYMAP = 1
28

39

410
@dataclass(init=True)
@@ -10,3 +16,4 @@ class TileStitchArgs:
1016
tile_width: int
1117
tile_height: int
1218
diff: int
19+
y_mode: YMode

0 commit comments

Comments
 (0)