-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinputFolder.py
More file actions
93 lines (64 loc) · 2.75 KB
/
inputFolder.py
File metadata and controls
93 lines (64 loc) · 2.75 KB
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
import config
from tile import Tile
import position
import os
class Plane:
''' This represents one zoom level of a region. '''
def __init__(self, mapType, latitudeRange, longitudeRange, zoom):
self.mapType = mapType
self.latitudeRange = latitudeRange
self.longitudeRange = longitudeRange
self.zoom = zoom
def __iter__(self):
for x in self.longitudeRange.atZoom(self.zoom):
for y in self.latitudeRange.atZoom(self.zoom):
tile = Tile(self.mapType, config.STYLE_ID, config.RESOLUTION, self.zoom, x, y)
yield tile
raise StopIteration
def __str__(self):
return "%i, (x: %i, y: %i), (x: %i, y: %i)" %(self.zoom, self.southWest.x, self.southWest.y, self.northEast.x, self.northEast.y)
class Region:
''' This class represents a region of tiles. In the .map file, each line (after the first)
describes a single region. '''
def __init__(self, mapType, line):
fragments = line.split(":") # [zoom, bottomLeftCorner, topRightCorner]
zoom = fragments[0].split("-")
self.minZoom = int(zoom[0])
self.maxZoom = int(zoom[1])
southWest = position.Position(fragments[1])
northEast = position.Position(fragments[2])
self.latitudeRange = position.LatitudeRange(southWest, northEast)
self.longitudeRange = position.LongitudeRange(southWest, northEast)
self.mapType = mapType
def __iter__(self):
for zoom in range(self.minZoom, self.maxZoom+1):
plane = Plane(self.mapType, self.latitudeRange, self.longitudeRange, zoom)
for tile in plane:
yield tile
raise StopIteration
class MapFile:
''' This class represents a .map file. It hides all details about reading the file, and acts
like a generator which returns Region objects, so we can say: "for region in MapFile(path):" '''
def __init__(self, path):
self.path = './input/' + path
with open(self.path, 'r') as f:
lines = f.readlines()
self.mapType = lines[0]
self.regionLines = lines[1:]
def __iter__(self):
for line in self.regionLines:
region = Region(self.mapType, line)
for tile in region:
yield tile
raise StopIteration
class InputFolder:
''' This class represents a folder of .map files." '''
def __init__(self, path):
self.path = path
def __iter__(self):
for filepath in os.listdir(self.path):
if filepath.endswith(".map"):
mapfile = MapFile(filepath)
for tile in mapfile:
yield tile
raise StopIteration