]> marstr Code Repo - marstr/orthographic.git/commitdiff
Contrast values moved into separate file to make sure values only appear once. Approp...
authorMarcus Str. <marcus@marstr.online>
Fri, 20 Dec 2024 13:05:48 +0000 (14:05 +0100)
committerMarcus Str. <marcus@marstr.online>
Fri, 20 Dec 2024 13:05:48 +0000 (14:05 +0100)
defines.py
layergen.py
photogen.py
tileprep.py

index 4aaf21e3c80ad5aee090f84820fa568479fed853..47299192d1f60157590761966112896fecd47c37 100644 (file)
@@ -232,10 +232,10 @@ mstr_mask_blur = [
     ("landuse", "farmland", 10),\r
     ("landuse", "farmyard", 10),\r
     # Z-Order 2\r
-    ("landuse", "forest", 10),\r
-    ("leisure", "nature_reserve", 10),\r
-    ("natural", "wood", 10),\r
-    ("natural", "tree_row", 10),\r
+    ("landuse", "forest", 12),\r
+    ("leisure", "nature_reserve", 12),\r
+    ("natural", "wood", 12),\r
+    ("natural", "tree_row", 12),\r
     ("landuse", "military", 15),\r
     # Z-Order 3\r
     ("natural", "bare_rock", 25),\r
index 8ef320814bdf309c61db004ac3d299aae33f84ab..f9fc7021db8d9e9d682dde0040d597af28f7e258 100644 (file)
@@ -146,20 +146,21 @@ class mstr_layergen:
 \r
 \r
     # Find layer contrast to apply, if any\r
-    def findLayerContrast(self):\r
+    def findLayerContrast(self, res):\r
 \r
         contrast = 0\r
 \r
         # The already existing source data\r
-        srcfile = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/" + str(self._lat_number) + "_" + str(\r
-            self._lng_number)\r
+        srcfile = mstr_datafolder + "z_orthographic/data/" + self._latlngfld + "/ctrdata"\r
 \r
         # Let's open the file and find our entry\r
         with open(srcfile) as file:\r
             for line in file:\r
                 linedata = line.split(" ")\r
-                if linedata[0] == self._tag and linedata[1] == self._value:\r
-                    contrast = int(linedata[4])\r
+                if len(linedata) > 1: # Make sure we don't break at last line of data file\r
+                    if linedata[0] == self._tag and linedata[1] == self._value and linedata[2] == ctr:\r
+                        contrast = int(linedata[3])\r
+                        break\r
 \r
         return contrast\r
 \r
@@ -287,13 +288,18 @@ class mstr_layergen:
 \r
             # Determine which sources to use.\r
             src = self.findLayerSource()\r
+            srcstr = ""\r
+            for s in range(len(src)):\r
+                srcstr = srcstr + str(src[s])\r
+                if s < len(src)-1:\r
+                    srcstr = strsrc + ","\r
 \r
             # Patch and border sources. There can only be one for each.\r
             brd_src = None\r
             ptc_src = []\r
 \r
             # Find this layer's predetermined contrast\r
-            lyr_contrast = self.findLayerContrast()\r
+            lyr_contrast = self.findLayerContrast(srcstr)\r
             if lyr_contrast != 0:\r
                 mstr_msg("layergen", "Applying contrast value: " + str(lyr_contrast))\r
 \r
index 0dbdb7bd8dc0e7637a56b11ddd15d4b363a84ee5..a4e46728f35b73d725a3647c76197697c75a91a0 100644 (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) + "_"\r
 \r
         # Correct layers\r
-        #mstr_msg("photogen", "Correcting layer order issues")\r
-        #layers = self.correctLayerIssues(layers)\r
+        mstr_msg("photogen", "Correcting layer order issues")\r
+        layers = self.correctLayerIssues(layers)\r
 \r
         # First, we walk through all layers and blend them on top of each other, in order\r
         mstr_msg("photogen", "Merging layers")\r
@@ -202,7 +202,7 @@ class mstr_photogen:
 \r
         # One more thing...\r
         mstr_msg("photogen", "Adding features to layers")\r
-        self.addTreesToFeatures(layers)\r
+        self.addTreesToFeatures(layers, waterlayers)\r
 \r
         # Throw missing buildings on top\r
         lyr = 0\r
@@ -337,7 +337,15 @@ class mstr_photogen:
 \r
     # This used to be in layergen and solves the problem of roads being rendered above trees.\r
     # It is the only solution that worked, after some research.\r
-    def addTreesToFeatures(self, layers):\r
+    def addTreesToFeatures(self, layers, wtr):\r
+\r
+        # Generate a combined water layer. Order and appearance does not matter.\r
+        wtrlyr = Image.new("RGBA", (self._imgsize, self._imgsize))\r
+        for l in wtr:\r
+            wtrlyr.alpha_composite(l)\r
+\r
+        # Preload the water layer for comparison\r
+        wtrpix = wtrlyr.load()\r
 \r
         # Walk through list of layers to decide where to add the trees\r
         curlyr = 0\r
@@ -353,7 +361,8 @@ class mstr_photogen:
                     lx = randrange(0, self._imgsize)\r
                     ly = randrange(0, self._imgsize)\r
                     lp = lyrmask[lx,ly]\r
-                    if lp[3] == 255:\r
+                    wp = wtrpix[lx,ly]\r
+                    if lp[3] == 255 and wp[3] == 0: # Exclude water bodies from tree placement\r
                         tree = self.generate_tree()\r
                         trees.alpha_composite(tree, (lx, ly))\r
 \r
@@ -401,8 +410,8 @@ class mstr_photogen:
     def correctLayerIssues(self, layers):\r
 \r
         # First the residential/forest dilemma\r
-        residential = 0\r
-        forest = 0\r
+        residential = -1\r
+        forest = -1\r
 \r
         curlyr = 0\r
         for lyr in self._lyrnames:\r
@@ -412,7 +421,22 @@ class mstr_photogen:
                 forest = curlyr\r
             curlyr = curlyr+1\r
 \r
-        layers[forest].alpha_composite(layers[residential])\r
+        # Make sure we hit the correct layers with correct content!\r
+        # Only do something if both are found.\r
+        if residential != -1 and forest != -1:\r
+            rsd_pix = layers[residential].load()\r
+            frs_pix = layers[forest].load()\r
+            rsd_adj = Image.new("RGBA", (self._imgsize, self._imgsize))\r
+            adj_pix = rsd_adj.load()\r
+\r
+            for y in range(0, self._tile.height):\r
+                for x in range(0, self._tile.width):\r
+                    fp = frs_pix[x,y]\r
+                    rp = rsd_pix[x,y]\r
+                    if rp[3] > 0 and fp[3] > 0:\r
+                        adj_pix[x,y] = (rp[0], rp[1], rp[2], rp[3])\r
+\r
+            layers[forest].alpha_composite(rsd_adj)\r
 \r
         return layers\r
 \r
index a43123ee6b1fc3dd3d250d8db92b3046e3031e2a..09c5c1c8d5b5064e0b93e5644d26234743e885e3 100644 (file)
@@ -132,25 +132,6 @@ class mstr_tileprep:
         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
     def _getResourceTouch(self, tv, th):
         touch = ""
@@ -184,51 +165,69 @@ class mstr_tileprep:
 
 
     # 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)
         fnlines = []
-        contrast = ctr
         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()
             curline = 0
+
             for ln in fnlines:
                 l = ln.split(" ")
                 if len(l) > 1:
                     if l[0] == self._tag and l[1] == self._value:
                         l[2] = res
-                    l[4] = l[4].replace("\n", "")
-                    #contrast = l[4]
-
-                    # Find contrast values for some tags
-                    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
+
+                    fnlines[curline] = l[0] + " " + l[1] + " " + l[2] + " " + l[3]
                 
                 curline = curline+1
 
             lines = ""
             for l in range(len(fnlines)):
                 lines = lines + fnlines[l]
-                if l < len(lines)-1: lines = lines+"\n"
 
             with open(df, 'w') as textfile:
                 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
     # 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
 
         # 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)
         end_reached = False
@@ -251,17 +250,15 @@ class mstr_tileprep:
             while edge_end[0] == 0:
                 resinfo[0] = self._getResourceTouch(tv, th)
                 resinfo[1] = self._getResourceInfo(tv, th)
-                resinfo[2] = self._getResourceContrast(tv, th)
 
                 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[2] = self._getResourceContrast(tv, th)
 
                 if "t" in resinfo[0]:
                     tv=tv+1
                     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:
                     edge_end[0] = 1
 
@@ -272,17 +269,15 @@ class mstr_tileprep:
             while edge_end[1] == 0:
                 resinfo[0] = self._getResourceTouch(tv, th)
                 resinfo[1] = self._getResourceInfo(tv, th)
-                resinfo[2] = self._getResourceContrast(tv, th)
 
                 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[2] = self._getResourceContrast(tv, th)
 
                 if "r" in resinfo[0]:
                     th=th+1
                     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:
                     edge_end[1] = 1
 
@@ -293,17 +288,15 @@ class mstr_tileprep:
             while edge_end[2] == 0:
                 resinfo[0] = self._getResourceTouch(tv, th)
                 resinfo[1] = self._getResourceInfo(tv, th)
-                resinfo[2] = self._getResourceContrast(tv, th)
 
                 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[2] = self._getResourceContrast(tv, th)
 
                 if "b" in resinfo[0]:
                     tv=tv-1
                     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:
                     edge_end[2] = 1
 
@@ -314,17 +307,15 @@ class mstr_tileprep:
             while edge_end[3] == 0:
                 resinfo[0] = self._getResourceTouch(tv, th)
                 resinfo[1] = self._getResourceInfo(tv, th)
-                resinfo[2] = self._getResourceContrast(tv, th)
 
                 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[2] = self._getResourceContrast(tv, th)
 
                 if "l" in resinfo[0]:
                     th=th-1
                     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:
                     edge_end[3] = 1