Contrast values moved into separate file to make sure values only appear once. Appropriate adjustments toward that in layergen. Made sure trees do not get placed in water in photogen.

This commit is contained in:
Marcus Str. 2024-12-20 14:05:48 +01:00
parent b29bc3b0d2
commit 01e53bdcde
4 changed files with 98 additions and 77 deletions

View File

@ -232,10 +232,10 @@ mstr_mask_blur = [
("landuse", "farmland", 10), ("landuse", "farmland", 10),
("landuse", "farmyard", 10), ("landuse", "farmyard", 10),
# Z-Order 2 # Z-Order 2
("landuse", "forest", 10), ("landuse", "forest", 12),
("leisure", "nature_reserve", 10), ("leisure", "nature_reserve", 12),
("natural", "wood", 10), ("natural", "wood", 12),
("natural", "tree_row", 10), ("natural", "tree_row", 12),
("landuse", "military", 15), ("landuse", "military", 15),
# Z-Order 3 # Z-Order 3
("natural", "bare_rock", 25), ("natural", "bare_rock", 25),

View File

@ -146,20 +146,21 @@ class mstr_layergen:
# Find layer contrast to apply, if any # Find layer contrast to apply, if any
def findLayerContrast(self): def findLayerContrast(self, res):
contrast = 0 contrast = 0
# The already existing source data # The already existing source data
srcfile = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(self._lat_number) + "_" + str( srcfile = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/ctrdata"
self._lng_number)
# Let's open the file and find our entry # Let's open the file and find our entry
with open(srcfile) as file: with open(srcfile) as file:
for line in file: for line in file:
linedata = line.split(" ") linedata = line.split(" ")
if linedata[0] == self._tag and linedata[1] == self._value: if len(linedata) > 1: # Make sure we don't break at last line of data file
contrast = int(linedata[4]) if linedata[0] == self._tag and linedata[1] == self._value and linedata[2] == ctr:
contrast = int(linedata[3])
break
return contrast return contrast
@ -287,13 +288,18 @@ class mstr_layergen:
# Determine which sources to use. # Determine which sources to use.
src = self.findLayerSource() src = self.findLayerSource()
srcstr = ""
for s in range(len(src)):
srcstr = srcstr + str(src[s])
if s < len(src)-1:
srcstr = strsrc + ","
# Patch and border sources. There can only be one for each. # Patch and border sources. There can only be one for each.
brd_src = None brd_src = None
ptc_src = [] ptc_src = []
# Find this layer's predetermined contrast # Find this layer's predetermined contrast
lyr_contrast = self.findLayerContrast() lyr_contrast = self.findLayerContrast(srcstr)
if lyr_contrast != 0: if lyr_contrast != 0:
mstr_msg("layergen", "Applying contrast value: " + str(lyr_contrast)) mstr_msg("layergen", "Applying contrast value: " + str(lyr_contrast))

View File

@ -52,8 +52,8 @@ class mstr_photogen:
#root_filename = mstr_datafolder + "/_cache/" + str(self._lat) + "-" + str(self._ty) + "_" + str(self._lng) + "-" + str(self._tx) + "_" #root_filename = mstr_datafolder + "/_cache/" + str(self._lat) + "-" + str(self._ty) + "_" + str(self._lng) + "-" + str(self._tx) + "_"
# Correct layers # Correct layers
#mstr_msg("photogen", "Correcting layer order issues") mstr_msg("photogen", "Correcting layer order issues")
#layers = self.correctLayerIssues(layers) layers = self.correctLayerIssues(layers)
# First, we walk through all layers and blend them on top of each other, in order # First, we walk through all layers and blend them on top of each other, in order
mstr_msg("photogen", "Merging layers") mstr_msg("photogen", "Merging layers")
@ -202,7 +202,7 @@ class mstr_photogen:
# One more thing... # One more thing...
mstr_msg("photogen", "Adding features to layers") mstr_msg("photogen", "Adding features to layers")
self.addTreesToFeatures(layers) self.addTreesToFeatures(layers, waterlayers)
# Throw missing buildings on top # Throw missing buildings on top
lyr = 0 lyr = 0
@ -337,7 +337,15 @@ class mstr_photogen:
# This used to be in layergen and solves the problem of roads being rendered above trees. # This used to be in layergen and solves the problem of roads being rendered above trees.
# It is the only solution that worked, after some research. # It is the only solution that worked, after some research.
def addTreesToFeatures(self, layers): def addTreesToFeatures(self, layers, wtr):
# Generate a combined water layer. Order and appearance does not matter.
wtrlyr = Image.new("RGBA", (self._imgsize, self._imgsize))
for l in wtr:
wtrlyr.alpha_composite(l)
# Preload the water layer for comparison
wtrpix = wtrlyr.load()
# Walk through list of layers to decide where to add the trees # Walk through list of layers to decide where to add the trees
curlyr = 0 curlyr = 0
@ -353,7 +361,8 @@ class mstr_photogen:
lx = randrange(0, self._imgsize) lx = randrange(0, self._imgsize)
ly = randrange(0, self._imgsize) ly = randrange(0, self._imgsize)
lp = lyrmask[lx,ly] lp = lyrmask[lx,ly]
if lp[3] == 255: wp = wtrpix[lx,ly]
if lp[3] == 255 and wp[3] == 0: # Exclude water bodies from tree placement
tree = self.generate_tree() tree = self.generate_tree()
trees.alpha_composite(tree, (lx, ly)) trees.alpha_composite(tree, (lx, ly))
@ -401,8 +410,8 @@ class mstr_photogen:
def correctLayerIssues(self, layers): def correctLayerIssues(self, layers):
# First the residential/forest dilemma # First the residential/forest dilemma
residential = 0 residential = -1
forest = 0 forest = -1
curlyr = 0 curlyr = 0
for lyr in self._lyrnames: for lyr in self._lyrnames:
@ -412,7 +421,22 @@ class mstr_photogen:
forest = curlyr forest = curlyr
curlyr = curlyr+1 curlyr = curlyr+1
layers[forest].alpha_composite(layers[residential]) # Make sure we hit the correct layers with correct content!
# Only do something if both are found.
if residential != -1 and forest != -1:
rsd_pix = layers[residential].load()
frs_pix = layers[forest].load()
rsd_adj = Image.new("RGBA", (self._imgsize, self._imgsize))
adj_pix = rsd_adj.load()
for y in range(0, self._tile.height):
for x in range(0, self._tile.width):
fp = frs_pix[x,y]
rp = rsd_pix[x,y]
if rp[3] > 0 and fp[3] > 0:
adj_pix[x,y] = (rp[0], rp[1], rp[2], rp[3])
layers[forest].alpha_composite(rsd_adj)
return layers return layers

View File

@ -132,25 +132,6 @@ class mstr_tileprep:
return src return src
# Get existing contrast value
def _getResourceContrast(self, tv, th):
# This either remains 0 or a string different to "0" in the end
ctr = "0"
df = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(tv) + "_" + str(th)
fnlines = []
if os.path.isfile(df) == True: # It is possible that the requested file does not yet exist
with open(df) as textfile:
fnlines = textfile.readlines()
for ln in fnlines:
l = ln.split(" ")
if l[0] == self._tag and l[1] == self._value:
l[4] = l[4].replace("\n", "")
ctr = l[4]
return ctr
# Find the edge touch info # Find the edge touch info
def _getResourceTouch(self, tv, th): def _getResourceTouch(self, tv, th):
touch = "" touch = ""
@ -184,51 +165,69 @@ class mstr_tileprep:
# Store the required resource information into the appropriate tile # Store the required resource information into the appropriate tile
def _storeResourceInfo(self, tile_v, tile_h, res, ctr): def _storeResourceInfo(self, tile_v, tile_h, res):
# Separate file for contrast values
ctrdata = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/ctrdata"
df = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(tile_v) + "_" + str(tile_h) df = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(tile_v) + "_" + str(tile_h)
fnlines = [] fnlines = []
contrast = ctr
if os.path.isfile(df) == True: # It is possible that the requested file does not yet exist if os.path.isfile(df) == True: # It is possible that the requested file does not yet exist
with open(df) as textfile: with open(df) as textfile:
fnlines = textfile.readlines() fnlines = textfile.readlines()
curline = 0 curline = 0
for ln in fnlines: for ln in fnlines:
l = ln.split(" ") l = ln.split(" ")
if len(l) > 1: if len(l) > 1:
if l[0] == self._tag and l[1] == self._value: if l[0] == self._tag and l[1] == self._value:
l[2] = res l[2] = res
l[4] = l[4].replace("\n", "")
#contrast = l[4]
# Find contrast values for some tags fnlines[curline] = l[0] + " " + l[1] + " " + l[2] + " " + l[3]
if contrast == "0":
if (
(l[0] == "landuse" and l[1] == "forest") or
(l[0] == "landuse" and l[1] == "meadow") or
(l[0] == "landuse" and l[1] == "grass") or
(l[0] == "leisure" and l[1] == "nature_reserve") or
(l[0] == "natural" and l[1] == "grassland") or
(l[0] == "landuse" and l[1] == "greenfield") or
(l[0] == "natural" and l[1] == "heath") or
(l[0] == "natural" and l[1] == "wetland") or
(l[0] == "leisure" and l[1] == "park") or
(l[0] == "building")
):
contrast = str(randrange(1, 4))
l[4] = l[4].replace("\n", "")
fnlines[curline] = l[0] + " " + l[1] + " " + l[2] + " " + l[3] + " " + contrast
curline = curline+1 curline = curline+1
lines = "" lines = ""
for l in range(len(fnlines)): for l in range(len(fnlines)):
lines = lines + fnlines[l] lines = lines + fnlines[l]
if l < len(lines)-1: lines = lines+"\n"
with open(df, 'w') as textfile: with open(df, 'w') as textfile:
textfile.write(lines) textfile.write(lines)
# Take care of contrast value
ctrcontent = []
if os.path.isfile(ctrdata) == True:
with open(ctrdata) as textfile:
ctrcontent = textfile.readlines()
# Compare if we already have a data string for this particular resource
ctrpresent = False
if len(ctrcontent) > 0:
for ln in range(0, len(ctrcontent)):
cnt = ctrcontent[ln].split(" ")
if cnt[0] == self._tag and cnt[1] == self._value and cnt[2] == res:
ctrpresent = True
break
# Should there not be a contrast value for this resource, add it
if ctrpresent == False:
if (
(self._tag == "landuse" and self._value == "forest") or
(self._tag == "landuse" and self._value == "meadow") or
(self._tag == "landuse" and self._value == "grass") or
(self._tag == "leisure" and self._value == "nature_reserve") or
(self._tag == "natural" and self._value == "grassland") or
(self._tag == "landuse" and self._value == "greenfield") or
(self._tag == "natural" and self._value == "heath") or
(self._tag == "natural" and self._value == "wetland") or
(self._tag == "leisure" and self._value == "park") or
(self._tag == "building")
):
contrast = str(randrange(1, 4))
newline = self._tag + " " + self._value + " " + res + " " + str(contrast) + "\n"
with open(ctrdata, 'a') as textfile:
textfile.write(newline)
# Walk through the now existing data files and make sure we always pick the correct # Walk through the now existing data files and make sure we always pick the correct
# sources for every tile, thus evading previous edge detection errors # sources for every tile, thus evading previous edge detection errors
@ -238,7 +237,7 @@ class mstr_tileprep:
edge_end = [0,0,0,0] # top, right, bottom, left edge_end = [0,0,0,0] # top, right, bottom, left
# Array with info we need # Array with info we need
resinfo = ["", "", ""] # Touch, Resource numbers, contrast resinfo = ["", ""] # Touch, Resource numbers, contrast
# Go through everything until the end is reached (no more options left) # Go through everything until the end is reached (no more options left)
end_reached = False end_reached = False
@ -251,17 +250,15 @@ class mstr_tileprep:
while edge_end[0] == 0: while edge_end[0] == 0:
resinfo[0] = self._getResourceTouch(tv, th) resinfo[0] = self._getResourceTouch(tv, th)
resinfo[1] = self._getResourceInfo(tv, th) resinfo[1] = self._getResourceInfo(tv, th)
resinfo[2] = self._getResourceContrast(tv, th)
if resinfo[1] == "0": if resinfo[1] == "0":
self._storeResourceInfo(tv, th, self._selectResources(), resinfo[2]) self._storeResourceInfo(tv, th, self._selectResources())
resinfo[1] = self._getResourceInfo(tv, th) resinfo[1] = self._getResourceInfo(tv, th)
resinfo[2] = self._getResourceContrast(tv, th)
if "t" in resinfo[0]: if "t" in resinfo[0]:
tv=tv+1 tv=tv+1
rinfo = self._getResourceInfo(tv, th) rinfo = self._getResourceInfo(tv, th)
if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1], resinfo[2]) if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1])
else: else:
edge_end[0] = 1 edge_end[0] = 1
@ -272,17 +269,15 @@ class mstr_tileprep:
while edge_end[1] == 0: while edge_end[1] == 0:
resinfo[0] = self._getResourceTouch(tv, th) resinfo[0] = self._getResourceTouch(tv, th)
resinfo[1] = self._getResourceInfo(tv, th) resinfo[1] = self._getResourceInfo(tv, th)
resinfo[2] = self._getResourceContrast(tv, th)
if resinfo[1] == "0": if resinfo[1] == "0":
self._storeResourceInfo(tv, th, self._selectResources(), resinfo[2]) self._storeResourceInfo(tv, th, self._selectResources())
resinfo[1] = self._getResourceInfo(tv, th) resinfo[1] = self._getResourceInfo(tv, th)
resinfo[2] = self._getResourceContrast(tv, th)
if "r" in resinfo[0]: if "r" in resinfo[0]:
th=th+1 th=th+1
rinfo = self._getResourceInfo(tv, th) rinfo = self._getResourceInfo(tv, th)
if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1], resinfo[2]) if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1])
else: else:
edge_end[1] = 1 edge_end[1] = 1
@ -293,17 +288,15 @@ class mstr_tileprep:
while edge_end[2] == 0: while edge_end[2] == 0:
resinfo[0] = self._getResourceTouch(tv, th) resinfo[0] = self._getResourceTouch(tv, th)
resinfo[1] = self._getResourceInfo(tv, th) resinfo[1] = self._getResourceInfo(tv, th)
resinfo[2] = self._getResourceContrast(tv, th)
if resinfo[1] == "0": if resinfo[1] == "0":
self._storeResourceInfo(tv, th, self._selectResources(), resinfo[2]) self._storeResourceInfo(tv, th, self._selectResources())
resinfo[1] = self._getResourceInfo(tv, th) resinfo[1] = self._getResourceInfo(tv, th)
resinfo[2] = self._getResourceContrast(tv, th)
if "b" in resinfo[0]: if "b" in resinfo[0]:
tv=tv-1 tv=tv-1
rinfo = self._getResourceInfo(tv, th) rinfo = self._getResourceInfo(tv, th)
if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1], resinfo[2]) if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1])
else: else:
edge_end[2] = 1 edge_end[2] = 1
@ -314,17 +307,15 @@ class mstr_tileprep:
while edge_end[3] == 0: while edge_end[3] == 0:
resinfo[0] = self._getResourceTouch(tv, th) resinfo[0] = self._getResourceTouch(tv, th)
resinfo[1] = self._getResourceInfo(tv, th) resinfo[1] = self._getResourceInfo(tv, th)
resinfo[2] = self._getResourceContrast(tv, th)
if resinfo[1] == "0": if resinfo[1] == "0":
self._storeResourceInfo(tv, th, self._selectResources(), resinfo[2]) self._storeResourceInfo(tv, th, self._selectResources())
resinfo[1] = self._getResourceInfo(tv, th) resinfo[1] = self._getResourceInfo(tv, th)
resinfo[2] = self._getResourceContrast(tv, th)
if "l" in resinfo[0]: if "l" in resinfo[0]:
th=th-1 th=th-1
rinfo = self._getResourceInfo(tv, th) rinfo = self._getResourceInfo(tv, th)
if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1], resinfo[2]) if rinfo == "0": self._storeResourceInfo(tv, th, resinfo[1])
else: else:
edge_end[3] = 1 edge_end[3] = 1