-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
251 lines (202 loc) · 7.67 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from PIL import Image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tqdm
import math
from datetime import datetime
from multiprocessing import Pool
from random import random
import pickle
import cv2
size_x, size_y = 250, 250
radius = 125
print(datetime.now().strftime("%H:%M:%S"))
print("loading noise data")
noise_data = [
[pickle.load(open('../noise/noise1/' + str(i) + '.pickle', 'rb')) for i in range(size_x)],
[pickle.load(open('../noise/noise2/' + str(i) + '.pickle', 'rb')) for i in range(size_x)],
[pickle.load(open('../noise/noise3/' + str(i) + '.pickle', 'rb')) for i in range(size_x)],
]
def noise(x, y, z, i):
return noise_data[i][x][y][z]
def parallelize_dataframe(df, func, n_cores=6):
df_split = np.array_split(df, n_cores)
pool = Pool(n_cores)
df = pd.concat(pool.map(func, df_split))
pool.close()
pool.join()
return df
# normalizing range values
update_range = lambda val, old_min, old_max, new_min, new_max: (((val - old_min) * (new_max - new_min)) / (old_max - old_min)) + new_min
biome_color = {
'ocean': '134074',
'tundra': '669bbc',
'boreal forest': '4f772d',
'grassland': '90a955',
'temperate rainforest': '31572c',
'seasonal forest': '606c38',
'woodland': '283618',
'tropical rainforest': '132a13',
'savanna': 'bb9457',
'desert': 'bb9457',
'beach': 'e9c46a'
}
def asCartesian(rthetaphi):
# convert from pixel height to polar coordinate
theta = rthetaphi[1]
phi = rthetaphi[2]
r = rthetaphi[0]
x = r * math.sin( theta ) * math.cos( phi )
y = r * math.sin( theta ) * math.sin( phi )
z = r * math.cos( theta )
x = update_range(x, -math.pi, math.pi, 0, size_x)
y = update_range(y, -math.pi, math.pi, 0, size_x)
z = update_range(z, -math.pi, math.pi, 0, size_x)
return [int(x),int(y),int(z)]
# get closeness to equator
def equator_temp(x: int) -> float:
"""needs to consider negative temps"""
equator = size_x / 2
percent = x / equator if x < equator else (size_x - x)/equator
category = percent * 10
bonus = category * 3
return -15 + bonus
def add_beach(biome: str, height: float) -> str:
if 'forest' in biome and height < 150:
return 'beach'
return biome
def get_biome(is_land: bool, temperature: float, precipitation: float) -> str:
# https://upload.wikimedia.org/wikipedia/commons/6/68/Climate_influence_on_terrestrial_biome.svg
if not is_land:
return 'ocean'
if temperature < 0:
return 'tundra'
if temperature < 10 and precipitation > 50:
return 'boreal forest'
if temperature < 10:
return 'grassland'
if temperature < 20 and precipitation > 200:
return 'temperate rainforest'
if temperature < 20 and precipitation > 100:
return 'seasonal forest'
if temperature < 20 and precipitation > 50:
return 'woodland'
if temperature < 20:
return 'grassland'
if precipitation > 250:
return 'tropical rainforest'
if precipitation > 50:
return 'savanna'
return 'desert'
def init_df() -> pd.DataFrame:
df = pd.DataFrame(
columns = [
'x',
'y',
'height',
'precipitation',
'temperature',
'is_land',
'biome',
'color'
]
)
# initialize cells
print('initializing')
df['x'] = pd.Series([i for i in range(size_x) for _ in range(size_y)])
df['y'] = pd.Series([i for _ in range(size_x) for i in range(size_y)])
return df
def add_3d_coords(df: pd.DataFrame) -> pd.DataFrame:
print(datetime.now().strftime("%H:%M:%S"))
print('adding 3D cartesian coordinates')
df['lat'] = df.apply(lambda row: update_range(row['x'], 0, size_x, 0, math.pi), axis=1)
df['long'] = df.apply(lambda row: update_range(row['y'], 0, size_x, 0, math.pi * 2), axis=1)
df['3D'] = df.apply(lambda row: asCartesian([1, row['lat'], row['long']]), axis=1)
return df
def add_height_noise(df: pd.DataFrame) -> pd.DataFrame:
# add noise
print(datetime.now().strftime("%H:%M:%S"))
print('adding height noise')
df['height'] = df.apply(lambda row: noise(*row['3D'], i=0), axis=1)
return df
def add_precipitation_noise(df: pd.DataFrame) -> pd.DataFrame:
print(datetime.now().strftime("%H:%M:%S"))
print('adding precipitation noise')
df['precipitation'] = df.apply(lambda row: noise(*row['3D'], i=1), axis=1)
return df
def add_temperature_noise(df: pd.DataFrame) -> pd.DataFrame:
print(datetime.now().strftime("%H:%M:%S"))
print('adding temperature noise')
df['temperature'] = df.apply(lambda row: noise(*row['3D'], i=2), axis=1)
return df
def other_processing(df: pd.DataFrame) -> pd.DataFrame:
print(datetime.now().strftime("%H:%M:%S"))
print('normalizing ranges')
df['height'] = df.apply(lambda row: update_range(row['height'], 0, 1, -11000, 9000), axis=1)
df['precipitation'] = df.apply(lambda row: update_range(row['precipitation'], 0, 1, 0, 500), axis=1)
df['temperature'] = df.apply(lambda row: update_range(row['temperature'], 0, 1, -10, 30), axis=1)
print(datetime.now().strftime("%H:%M:%S"))
print('checking the temperature')
# update temperature based on latitude
df['temperature'] = df.apply(lambda row: row['temperature'] + equator_temp(row['x']), axis=1)
# update temperature based on height
df['temperature'] = df.apply(lambda row: row['temperature'] - row['height']/2000, axis=1)
# set is_land
print(datetime.now().strftime("%H:%M:%S"))
print('finding land')
df['is_land'] = df.apply(lambda row: row['height'] > 0, axis=1)
# set biome
print(datetime.now().strftime("%H:%M:%S"))
print('setting biome')
df["biome"] = df.apply(lambda row: get_biome(row['is_land'], row['temperature'], row['precipitation']), axis=1)
# add beaches
print(datetime.now().strftime("%H:%M:%S"))
print('going to the beach')
df['biome'] = df.apply(lambda row: add_beach(row['biome'], row['height']), axis=1)
# set color
print(datetime.now().strftime("%H:%M:%S"))
print('setting color')
df["color"] = df.apply(lambda row: biome_color[row['biome']], axis=1)
# save
print(datetime.now().strftime("%H:%M:%S"))
return df
def run():
df = init_df()
df = add_3d_coords(df)
df.to_pickle('tmp.pickle')
# df = pd.read_pickle('tmp.pickle')
df = add_height_noise(df)
df.to_pickle('tmp2.pickle')
df = add_precipitation_noise(df)
df.to_pickle('tmp3.pickle')
df = add_temperature_noise(df)
df.to_pickle('tmp4.pickle')
df = other_processing(df)
df.to_pickle('biomes.pickle')
# output results
df_out = df.drop(['color', '3D', 'x', 'y'], axis=1)
df_out.to_json('biomes.json', orient='table', index=False)
return df
def write_map(df):
print('writing image')
image_data = [[[13,40,74] for _ in range(size_x)] for _ in range(size_y)]
for _, row in df.iterrows():
image_data[row['x']][row['y']] = [
int('0x' + row['color'][0:2], 16),
int('0x' + row['color'][2:4], 16),
int('0x' + row['color'][4:6], 16)
]
# write the results to an image
np_data = np.array(image_data)
im = Image.fromarray(np_data.astype(np.uint8))
im.save("./render/client/textures/grid100.jpg")
print(datetime.now().strftime("%H:%M:%S"))
print('scaling image')
img = cv2.imread("./render/client/textures/grid100.jpg", 1)
ratio = 512/size_x
large_image = cv2.resize(img, (0, 0), fx=ratio, fy=ratio)
cv2.imwrite("./render/client/textures/grid512.jpg", large_image)
df = run()
# df = pd.read_pickle('biomes.pickle')
write_map(df)