Transition to image-based contour detection for resource allocation. Allocation of resources not yet implemented with this code. Contour detection requires opencv-python to be present.

This commit is contained in:
Marcus Str. 2025-01-10 12:59:36 +01:00
parent cf25d45789
commit e82ecc9d32
3 changed files with 40 additions and 11 deletions

View File

@ -14,7 +14,7 @@
# tiles will go. This is also the folder where the image generation sources are # tiles will go. This is also the folder where the image generation sources are
# stored. # stored.
#mstr_datafolder = "M:/Developer/Projects/orthographic/" #mstr_datafolder = "M:/Developer/Projects/orthographic/"
mstr_datafolder = "/home/marcus/Data/Developer/Projects/orthographic/" mstr_datafolder = "D:/Developer/Projects/orthographic/"
# Switched to Linux, so path is amended # Switched to Linux, so path is amended

View File

@ -297,9 +297,9 @@ class mstr_orthographic:
mstr_msg("orthographic", "Beginning construction of tile") mstr_msg("orthographic", "Beginning construction of tile")
# Clear cache # Clear cache
tmp = glob.glob(mstr_datafolder + "_cache/*.*") #tmp = glob.glob(mstr_datafolder + "_cache/*.*")
for t in range(0, len(tmp)): #for t in range(0, len(tmp)):
os.remove(tmp[t]) # os.remove(tmp[t])
# We need to know which platform we are on # We need to know which platform we are on
os_platform = os.name os_platform = os.name
@ -383,12 +383,24 @@ class mstr_orthographic:
bb_lat = self._lat bb_lat = self._lat
bb_lng = self._long bb_lng = self._long
# Resource contour maps
contours = []
# Generate images for all resources
cnt_w = mlng * 20
cnt_h = mlat * 20
for l in mstr_ortho_layers:
if l[0] != "building":
cntimg = Image.new("RGBA", (cnt_w, cnt_h), (255,255,255,255))
contours.append([l[0], l[1], cntimg])
mstr_msg("orthographic", "Created base images for landuse contours")
# We will now prepare the graphic tile generation. We do this by only generating # We will now prepare the graphic tile generation. We do this by only generating
# the masks and determine which sources to use in the actual images. # the masks and determine which sources to use in the actual images.
# Previously, I downloaded all XML files in one go - but to ease the # Previously, I downloaded all XML files in one go - but to ease the
# stress on OSM servers and my server, we will do acquire the data # stress on OSM servers and my server, we will do acquire the data
# only for the current processed part of the tile. # only for the current processed part of the tile.
"""
for lat_grid in range(1, maxlatlng[0]+1): for lat_grid in range(1, maxlatlng[0]+1):
for lng_grid in range(1, maxlatlng[1]+1): for lng_grid in range(1, maxlatlng[1]+1):
# Adjust bounding box # Adjust bounding box
@ -410,8 +422,20 @@ class mstr_orthographic:
mg = mstr_maskgen( [self._lat, cur_tile_y, self._long, cur_tile_x], self._vstep, layer[0], layer[1], layer[2]) mg = mstr_maskgen( [self._lat, cur_tile_y, self._long, cur_tile_x], self._vstep, layer[0], layer[1], layer[2])
mask = mg._build_mask(osmxml, is_prep=True) # We need an object here mask = mg._build_mask(osmxml, is_prep=True) # We need an object here
tp = mstr_tileprep(self._lat, self._long, lat_grid, lng_grid, layer[0], layer[1], mask, False) mask = mask.resize((20,20), Image.Resampling.BILINEAR)
tp._determineEdges() idx = 0
for c in contours:
if c[0] == layer[0] and c[1] == layer[1]:
break
else:
idx=idx+1
cnx = (lng_grid-1) * 20
cny = cnt_h - (lat_grid * 20)
contours[idx][2].alpha_composite(mask, dest=(cnx,cny))
#tp = mstr_tileprep(self._lat, self._long, lat_grid, lng_grid, layer[0], layer[1], mask, False)
#tp._determineEdges()
curlyr = curlyr+1 curlyr = curlyr+1
@ -436,14 +460,18 @@ class mstr_orthographic:
if cur_tile_y > top_lat: if cur_tile_y > top_lat:
top_lat = cur_tile_y top_lat = cur_tile_y
exit(1)
""" # Store all contour images
for c in contours:
c[2].save(mstr_datafolder + "_cache/contour_" + c[0] + "_" + c[1] + ".png")
mstr_msg("orthographic", "Saved contour images for each OSM tag")
# We now need to "raytrace" the resources for correct placement # We now need to "raytrace" the resources for correct placement
mstr_msg("orthographic", "Performing resource plamement tracing for tile") mstr_msg("orthographic", "Performing resource plamement tracing for tile")
# Let's set up an array which keeps track of all used tiles, per resource # Let's set up an array which keeps track of all used tiles, per resource
"""
for l in mstr_ortho_layers: for l in mstr_ortho_layers:
self._tiles_visited.append([(l[0], l[1])]) # Store tag and value as tuple for each possible resource self._tiles_visited.append([(l[0], l[1])]) # Store tag and value as tuple for each possible resource
@ -469,6 +497,7 @@ class mstr_orthographic:
tp._setAlreadyVisitedTiles(self._tiles_visited[vstidx]) tp._setAlreadyVisitedTiles(self._tiles_visited[vstidx])
tp._placeTileSources(mlat, mlng) tp._placeTileSources(mlat, mlng)
self.adjust_tiles_visited(vstidx, tp._all_visited) self.adjust_tiles_visited(vstidx, tp._all_visited)
"""
# Adjust the tiles visited for one resource # Adjust the tiles visited for one resource

View File

@ -14,7 +14,7 @@
# ------------------------------------------------------------------- # -------------------------------------------------------------------
import glob import glob
from os import MFD_ALLOW_SEALING #from os import MFD_ALLOW_SEALING
from random import randrange from random import randrange
from PIL import Image from PIL import Image
from osmxml import * from osmxml import *