orthographic/tileprep.py

151 lines
5.8 KiB
Python

# -------------------------------------------------------------------
# ORTHOGRAPHIC
# Your personal aerial satellite. Always on. At any altitude.*
# Developed by MarStrMind
# License: Open Software License 3.0
# Up to date version always on marstr.online
# -------------------------------------------------------------------
# tileprep.py
# This prepares a zoom level 18 tile for ortho generation. This class
# performs the edge detection, selects the required sources for each
# layer, and stores this information into files which are then used
# by layergen.
# -------------------------------------------------------------------
import glob
from random import randrange
from PIL import Image
from osmxml import *
from maskgen import *
from defines import *
from functions import *
from log import *
from tileinfo import *
class mstr_tileprep:
# For this to work we need some fundamentals.
def __init__(self, lat, lng, v, h, tag, value, mask, is_completion):
self._lat = lat
self._lng = lng
self._tile_h = h
self._tile_v = v
self._tag = tag
self._value = value
self._edges = "" # To be filled by _edgeDetect call
self._source = -1
self._mask = mask
latlngfld = xplane_latlng_folder([lat, lng])
self._tileinfo = mstr_tileinfo(lat, lng, v, h, latlngfld)
# Special case for the final step of an ortho
self._is_completion = is_completion
def _findCorrectTextureFolder(self):
srcfld = []
for lyr in mstr_ortho_layers:
if lyr[0] == self._tag and lyr[1] == self._value:
srcfld.append(lyr[2])
srcfld.append(lyr[3])
break
return srcfld
# Prepare the tile accordingly
def _prepareTile(self):
# Load the mask pixels
mp = self._mask.load()
imgsize = self._mask.width
# Run scan
# Check if pixels touch the borders of the image, and if so -
# make a not of that in the database.
at=False
ar=False
ab=False
al=False
# Top scan
for i in range(0, imgsize-1):
p = mp[i,0]
if p[3] > 0:
at=True
break
# Right scan
for i in range(0, imgsize-1):
p = mp[imgsize-1,i]
if p[3] > 0:
ar=True
break
# Bottom scan
for i in range(0, imgsize-1):
p = mp[i,imgsize-1]
if p[3] > 0:
ab=True
break
# Left scan
for i in range(0, imgsize-1):
p = mp[1,i]
if p[3] > 0:
al=True
break
# Construct adjacency string
adjstr = ""
if at==True: adjstr = adjstr + "t"
if ar==True: adjstr = adjstr + "r"
if ab==True: adjstr = adjstr + "b"
if al==True: adjstr = adjstr + "l"
# Now find out of there is a source from any adjacent tile
adjtiles = findAdjacentTilesTo(self._tile_v, self._tile_h)
sat = []
sar = []
sab = []
sal = []
if self._is_completion == False:
if at == True: sat = self._tileinfo.get_adjacency_for_tag_and_value(adjtiles[0][0], adjtiles[0][1], self._tag, self._value) # Top
if ar == True: sar = self._tileinfo.get_adjacency_for_tag_and_value(adjtiles[1][0], adjtiles[1][1], self._tag, self._value) # Right
if ab == True: sab = self._tileinfo.get_adjacency_for_tag_and_value(adjtiles[2][0], adjtiles[2][1], self._tag, self._value) # Bottom
if al == True: sal = self._tileinfo.get_adjacency_for_tag_and_value(adjtiles[3][0], adjtiles[3][1], self._tag, self._value) # Left
if self._is_completion == True:
if at == True: sat = self._tileinfo.get_adjacency_for_completion(adjtiles[0][0], adjtiles[0][1]) # Top
if ar == True: sar = self._tileinfo.get_adjacency_for_completion(adjtiles[1][0], adjtiles[1][1]) # Right
if ab == True: sab = self._tileinfo.get_adjacency_for_completion(adjtiles[2][0], adjtiles[2][1]) # Bottom
if al == True: sal = self._tileinfo.get_adjacency_for_completion(adjtiles[3][0], adjtiles[3][1]) # Left
if self._source == -1 and len(sat) == 2: self._source = sat[0]
if self._source == -1 and len(sar) == 2: self._source = sar[0]
if self._source == -1 and len(sab) == 2: self._source = sab[0]
if self._source == -1 and len(sal) == 2: self._source = sal[0]
# If there was nothing in the info still, we need to select some source
if self._source == -1:
srcfld = self._findCorrectTextureFolder()
tx = mstr_datafolder + "textures/" + srcfld[0] + "/" + srcfld[1] + "/brd/b*.png"
lst = glob.glob(tx)
if len(lst) == 1: self._source = 1
if len(lst) >= 2: self._source = randrange(1, len(lst)+1)
# Store into DB - but only if there is something to store
if adjstr != "":
if self._is_completion == False:
r = self._tileinfo.get_adjacency_for_tag_and_value(self._tile_v, self._tile_h, self._tag, self._value)
if len(r) == 0:
self._tileinfo.add_adjacency_data(self._tile_v, self._tile_h, self._tag, self._value, self._source, adjstr)
mstr_msg("tileprep", "Adjacency info stored in database")
if self._is_completion == True:
r = self._tileinfo.get_adjacency_for_completion(self._tile_v, self._tile_h)
if len(r) == 0:
self._tileinfo.add_completion_data(self._tile_v, self._tile_h, self._tag, self._value, self._source, adjstr)
mstr_msg("tileprep", "Adjacency info for completion stored in database")