From d18e367fe4a865ebad4a6fd7e4ed168017c2e426 Mon Sep 17 00:00:00 2001
From: frederik <frederik.anilmarkus@gmail.com>
Date: Thu, 8 Feb 2024 16:22:50 +0100
Subject: [PATCH] add custom name support for osm maps and terrain

Signed-off-by: frederik <frederik.anilmarkus@gmail.com>
---
 app/__init__.py       |  9 ++++++---
 app/blender.py        | 14 +++++++++++---
 app/command_line.py   |  3 ++-
 gui/__init__.py       | 12 ++++++++++++
 parse/osm/__init__.py |  2 ++
 terrain/__init__.py   | 13 +++++++++++--
 6 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/app/__init__.py b/app/__init__.py
index f36dcc86..56f04a15 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -71,15 +71,18 @@ def download(self, url, filepath, data=None):
             f.write(u.read())
         print("Saving the file to %s..." % filepath)
     
-    def downloadOsmFile(self, osmDir, minLon, minLat, maxLon, maxLat):
+    def downloadOsmFile(self, osmDir, minLon, minLat, maxLon, maxLat, customName):
         # find a file name for the OSM file
         osmFileName = BaseApp.osmFileName % ""
         counter = 1
         while True:
             osmFilepath = os.path.realpath( os.path.join(osmDir, osmFileName) )
             if os.path.isfile(osmFilepath):
-                counter += 1
-                osmFileName = BaseApp.osmFileName % "_%s" % counter
+                if customName != "":
+                    osmFileName = BaseApp.osmFileName % "_%s" % customName
+                else:
+                    counter += 1
+                    osmFileName = BaseApp.osmFileName % "_%s" % counter
             else:
                 break
         self.osmFilepath = osmFilepath
diff --git a/app/blender.py b/app/blender.py
index 89a03337..c9d1ecf0 100644
--- a/app/blender.py
+++ b/app/blender.py
@@ -112,6 +112,7 @@ class BlenderApp(BaseApp):
     
     voidValue = -32768
     voidSubstitution = 0
+    customName = ""
     
     def __init__(self):
         super().__init__()
@@ -198,7 +199,7 @@ def initOsm(self, op, context):
         ]
         
         if addon.osmSource == "server":
-            self.downloadOsmFile(osmDir, self.minLon, self.minLat, self.maxLon, self.maxLat)
+            self.downloadOsmFile(osmDir, self.minLon, self.minLat, self.maxLon, self.maxLat, self.customName)
         else:
             self.osmFilepath = os.path.realpath(bpy.path.abspath(self.osmFilepath))
 
@@ -531,10 +532,17 @@ def importTerrain(self, context):
             v[2] -= minHeight
         
         # create a mesh object in Blender
-        mesh = bpy.data.meshes.new("Terrain")
+        if context.scene.blosm.terrainName != "Terrain":
+            mesh = bpy.data.meshes.new(context.scene.blosm.terrainName)
+        else:
+            mesh = bpy.data.meshes.new("Terrain")
         mesh.from_pydata(verts, [], indices)
         mesh.update()
-        obj = bpy.data.objects.new("Terrain", mesh)
+        if context.scene.blosm.terrainName != "Terrain":
+            obj = bpy.data.objects.new(context.scene.blosm.terrainName, mesh)
+        else:
+            obj = bpy.data.objects.new("Terrain", mesh)
+            
         obj["height_offset"] = minHeight
         context.scene.collection.objects.link(obj)
         context.scene.blosm.terrainObject = obj.name
diff --git a/app/command_line.py b/app/command_line.py
index a8b1c9b7..2e0eea22 100644
--- a/app/command_line.py
+++ b/app/command_line.py
@@ -42,6 +42,7 @@ def initArgParser(self):
         argParser.add_argument("--osmFilepath", help="Path to an OSM file")
         argParser.add_argument("--osmDir", help="A directory for the downloaded OSM files")
         argParser.add_argument("--setupScript", help="The path to a custom setup script")
+        argParser.add_argument("--customName", help="A custom name for the downloaded OSM file", default="")
         
         argParser.add_argument("--buildings", action='store_true', help="Import buildings", default=False)
         argParser.add_argument("--highways", action='store_true', help="Import roads and paths", default=False)
@@ -76,7 +77,7 @@ def initOsm(self):
         if self.osmFilepath:
             self.osmFilepath = os.path.realpath(self.osmFilepath)
         else:
-            self.downloadOsmFile(self.osmDir, self.minLon, self.minLat, self.maxLon, self.maxLat)
+            self.downloadOsmFile(self.osmDir, self.minLon, self.minLat, self.maxLon, self.maxLat, self.customName)
 
     def render(self):
         logger = self.logger
diff --git a/gui/__init__.py b/gui/__init__.py
index f1370520..3d4d8a0f 100644
--- a/gui/__init__.py
+++ b/gui/__init__.py
@@ -863,6 +863,12 @@ class BlosmProperties(bpy.types.PropertyGroup):
         default = 3.
     )
     
+    customName: bpy.props.StringProperty(
+        name = "Custom name",
+        description = "Custom name for the imported objects",
+        default = ""
+    )
+    
     defaultLevels: bpy.props.CollectionProperty(type = BlosmDefaultLevelsEntry)
     
     defaultLevelsIndex: bpy.props.IntProperty(
@@ -922,6 +928,12 @@ class BlosmProperties(bpy.types.PropertyGroup):
         description="Primitive type used for the terrain mesh: quad or triangle",
         default="quad"
     )
+    
+    terrainName: bpy.props.StringProperty(
+        name="Terrain name",
+        description="Name for the terrain object",
+        default="Terrain"
+    )
 
     # Number of vertex reduction
     # The Reduction Ratio is a divider of 1200
diff --git a/parse/osm/__init__.py b/parse/osm/__init__.py
index cee2a91b..3978cbb8 100644
--- a/parse/osm/__init__.py
+++ b/parse/osm/__init__.py
@@ -77,6 +77,8 @@ def __init__(self, app):
         self.minLon = 0.
         self.maxLon = 0.
     
+        self.customName = ""
+        
     def addCondition(self, condition, layerId=None, manager=None, renderer=None):
         self.conditions.append(
             (condition, manager, renderer, layerId)
diff --git a/terrain/__init__.py b/terrain/__init__.py
index fce6c882..5a4833e7 100644
--- a/terrain/__init__.py
+++ b/terrain/__init__.py
@@ -261,9 +261,18 @@ def createFlatTerrain(minLon, minLat, maxLon, maxLat, projection, context):
                 vertsCounter += 1
         
         # create a mesh object in Blender
-        mesh = bpy.data.meshes.new("Terrain")
+        if context.scene.blosm.terrainName != "Terrain":
+            mesh = bpy.data.meshes.new(context.scene.blosm.terrainName)
+        else:
+            mesh = bpy.data.meshes.new("Terrain")
+
         mesh.from_pydata(verts, [], indices)
         mesh.update()
-        obj = bpy.data.objects.new("Terrain", mesh)
+
+        if context.scene.blosm.terrainName != "Terrain":
+            obj = bpy.data.objects.new(context.scene.blosm.terrainName, mesh)
+        else:
+            obj = bpy.data.objects.new("Terrain", mesh)
+
         bpy.context.scene.collection.objects.link(obj)
         return obj.name
\ No newline at end of file