]> marstr Code Repo - marstr/orthographic.git/commitdiff
Change to zoom level 16, attempt to generate contour maps for each OSM tag. Not yet...
authorMarcus Str. <marcus@marstr.online>
Fri, 17 Jan 2025 19:31:56 +0000 (20:31 +0100)
committerMarcus Str. <marcus@marstr.online>
Fri, 17 Jan 2025 19:31:56 +0000 (20:31 +0100)
defines.py
maskgen.py
orthographic.py
osmxml.py

index 13a76bb85afb13cb5dc0b0501a7b94071a482d13..0e5e024c546013598794163d61829b1c690366a0 100644 (file)
 # stored.\r
 #mstr_datafolder = "M:/Developer/Projects/orthographic/"\r
 mstr_datafolder = "D:/Developer/Projects/orthographic/"\r
+#mstr_datafolder = "/home/marcus/Developer/Projects/orthographic/"\r
 # Switched to Linux, so path is amended\r
 \r
 \r
 # API endpoint to acquire OSM data (bonus: I have my own)\r
-mstr_osm_endpoint = "https://marstr.online/osm/v1/"\r
+#mstr_osm_endpoint = "https://marstr.online/osm/v1/"\r
+mstr_osm_endpoint = "http://localhost/og.php"\r
 \r
 # Define the texture resolution you want to have your photos at.\r
 mstr_photores = 2048\r
@@ -100,7 +102,7 @@ mstr_xp_normal_maps = [
 # How much of a tile we need for each zoom level. The higher\r
 # the zoom level, the smaller the area to generate a mask of - but also\r
 # higher detail.\r
-mstr_zl_16 = 0.08\r
+mstr_zl_16 = 0.064\r
 mstr_zl_17 = 0.048\r
 mstr_zl_18 = 0.016\r
 mstr_zl_19 = 0.008\r
index b8080c2a04efbf040ead4b6fb433f342e1af95ee..12f1061b715338594e9e6311dd55b25a9bb05c6a 100644 (file)
@@ -37,6 +37,7 @@ class mstr_maskgen:
         self._vstep = vstep\r
         self._scale = 1 / math.cos(math.radians(self._box[0]))\r
         self._isline = isline\r
+        self._zoomlevel = mstr_zl_16\r
 \r
         #mstr_msg("maskgen", "Intialized mask gen.")\r
 \r
@@ -98,8 +99,8 @@ class mstr_maskgen:
         bbox.append(self._box[0] + ((self._box[1]-1) * self._vstep))\r
         bbox.append(self._box[0] + ((self._box[1]-1) * self._vstep) + self._vstep)\r
         # Longitude\r
-        bbox.append(self._box[2] + ((self._box[3]-1) * mstr_zl_18))\r
-        bbox.append(self._box[2] + ((self._box[3]-1) * mstr_zl_18) + mstr_zl_18)\r
+        bbox.append(self._box[2] + ((self._box[3]-1) * self._zoomlevel))\r
+        bbox.append(self._box[2] + ((self._box[3]-1) * self._zoomlevel) + self._zoomlevel)\r
 \r
         # Building levels, if this is a building\r
         bld_levels = 0\r
@@ -213,7 +214,7 @@ class mstr_maskgen:
         # of the building in this loop.\r
         if mstr_shadow_enabled == True and is_prep == False:\r
             if self._tag == "building":\r
-                mpp = meters_per_pixel(self._tile_width) * mstr_zl_18\r
+                mpp = meters_per_pixel(self._tile_width) * self._zoomlevel\r
                 pix_per_floor = mstr_shadow_floor_h / mpp\r
                 total_pix = pix_per_floor * bld_levels\r
                 shift = int(total_pix)\r
index defe6afed3175c325559c8a622325e24d73887a9..929bf95432720559da811983dc9e748855c53abc 100644 (file)
@@ -30,6 +30,7 @@ class mstr_orthographic:
 \r
     # Constructor of class. Takes longitude and latitude.\r
     def __init__(self, lat, lng, outfolder, pwd, prep=False):\r
+        self._zoomlevel = mstr_zl_16\r
         self._lat  = lat\r
         self._long = lng\r
         self._output = outfolder\r
@@ -68,7 +69,7 @@ class mstr_orthographic:
     # orthos can only be a power of 2, such as 2048x2048\r
     def _findVerticalStepping(self):\r
         scale = 1 / math.cos(math.radians(self._lat))\r
-        maxlat = (1 / scale) * mstr_zl_18\r
+        maxlat = (1 / scale) * self._zoomlevel\r
         return maxlat\r
     \r
 \r
@@ -90,7 +91,7 @@ class mstr_orthographic:
         bb_lat = self._lat\r
         bb_lng = self._long\r
         bb_lat_edge = self._lat+self._vstep\r
-        bb_lng_edge = self._long+mstr_zl_18\r
+        bb_lng_edge = self._long+self._zoomlevel\r
         cur_tile_x = 1\r
         cur_tile_y = 1\r
         osmxml = mstr_osmxml(0,0)\r
@@ -109,7 +110,7 @@ class mstr_orthographic:
             bb_lat = bb_lat + self._vstep\r
             mlat = mlat+1\r
         while bb_lng < self._long + 1:\r
-            bb_lng = bb_lng + mstr_zl_18\r
+            bb_lng = bb_lng + self._zoomlevel\r
             mlng = mlng+1\r
         mstr_msg("orthographic", "Max lat tile: " + str(mlat) + " - max lng tile: " + str(mlng))\r
         maxlatlng = [ mlat, mlng ]\r
@@ -130,8 +131,8 @@ class mstr_orthographic:
                 \r
                 # Adjust longitude coordinates\r
                 cur_tile_x = cur_tile_x+1\r
-                bb_lng = bb_lng + mstr_zl_18\r
-                bb_lng_edge = bb_lng_edge + mstr_zl_18\r
+                bb_lng = bb_lng + self._zoomlevel\r
+                bb_lng_edge = bb_lng_edge + self._zoomlevel\r
                 mstr_msg("orthographic", "Adjustment of longitude performed")\r
                 # Adjust peak longitude tile number\r
                 if cur_tile_x > top_lng:\r
@@ -141,7 +142,7 @@ class mstr_orthographic:
             cur_tile_y = cur_tile_y+1\r
             cur_tile_x = 1\r
             bb_lng = self._long\r
-            bb_lng_edge = self._long + mstr_zl_18\r
+            bb_lng_edge = self._long + self._zoomlevel\r
             bb_lat = bb_lat + self._vstep\r
             bb_lat_edge = bb_lat_edge + self._vstep\r
             mstr_msg("orthographic", "Adjustment of latitude performed")\r
@@ -159,14 +160,14 @@ class mstr_orthographic:
         bb_lat = self._lat\r
         bb_lng = self._long\r
         bb_lat_edge = self._lat+self._vstep\r
-        bb_lng_edge = self._long+mstr_zl_18\r
+        bb_lng_edge = self._long+self._zoomlevel\r
         mlat = 1\r
         mlng = 1\r
         while bb_lat < self._lat + 1:\r
             bb_lat = bb_lat + self._vstep\r
             mlat = mlat+1\r
         while bb_lng < self._long + 1:\r
-            bb_lng = bb_lng + mstr_zl_18\r
+            bb_lng = bb_lng + self._zoomlevel\r
             mlng = mlng+1\r
         mstr_msg("orthographic", "Max lat tile: " + str(mlat) + " - max lng tile: " + str(mlng))\r
         maxlatlng = [ mlat, mlng ]\r
@@ -197,7 +198,7 @@ class mstr_orthographic:
         bb_lat = self._lat\r
         bb_lng = self._long\r
         bb_lat_edge = self._lat+self._vstep\r
-        bb_lng_edge = self._long+mstr_zl_18\r
+        bb_lng_edge = self._long+self._zoomlevel\r
 \r
         # We need to know the highest possible latitude and longitude tile numbers,\r
         # in case we render at the edge\r
@@ -207,7 +208,7 @@ class mstr_orthographic:
             bb_lat = bb_lat + self._vstep\r
             mlat = mlat+1\r
         while bb_lng < self._long + 1:\r
-            bb_lng = bb_lng + mstr_zl_18\r
+            bb_lng = bb_lng + self._zoomlevel\r
             mlng = mlng+1\r
         mstr_msg("orthographic", "Max lat tile: " + str(mlat) + " - max lng tile: " + str(mlng))\r
         maxlatlng = [ mlat, mlng ]\r
@@ -217,9 +218,9 @@ class mstr_orthographic:
             if os.path.isfile(ddsf) == False:\r
                 # Reset these two\r
                 bb_lat = self._lat + ((grid_lat-1)*self._vstep)\r
-                bb_lng = self._long + ((grid_lng-1)*mstr_zl_18)\r
+                bb_lng = self._long + ((grid_lng-1)*self._zoomlevel)\r
                 bb_lat_edge = self._lat + ((grid_lat-1)*self._vstep) + self._vstep\r
-                bb_lng_edge = self._long + ((grid_lng-1)*mstr_zl_18) + mstr_zl_18\r
+                bb_lng_edge = self._long + ((grid_lng-1)*self._zoomlevel) + self._zoomlevel\r
 \r
                 osmxml = mstr_osmxml()\r
                 osmxml.adjust_bbox(bb_lat, bb_lng, bb_lat_edge, bb_lng_edge)\r
@@ -355,7 +356,7 @@ class mstr_orthographic:
         bb_lat = self._lat\r
         bb_lng = self._long\r
         bb_lat_edge = self._lat+self._vstep\r
-        bb_lng_edge = self._long+mstr_zl_18\r
+        bb_lng_edge = self._long+self._zoomlevel\r
         cur_tile_x = 1\r
         cur_tile_y = 1\r
         #osmxml = mstr_osmxml(0,0)\r
@@ -374,7 +375,7 @@ class mstr_orthographic:
             bb_lat = bb_lat + self._vstep\r
             mlat = mlat+1\r
         while bb_lng < self._long + 1:\r
-            bb_lng = bb_lng + mstr_zl_18\r
+            bb_lng = bb_lng + self._zoomlevel\r
             mlng = mlng+1\r
         mstr_msg("orthographic", "Max lat tile: " + str(mlat) + " - max lng tile: " + str(mlng))\r
         maxlatlng = [ mlat, mlng ]\r
@@ -387,8 +388,8 @@ class mstr_orthographic:
         contours = []\r
 \r
         # Generate images for all resources\r
-        cnt_w = mlng * 20\r
-        cnt_h = mlat * 20\r
+        cnt_w = mlng * 80\r
+        cnt_h = mlat * 80\r
         for l in mstr_ortho_layers:\r
              if l[0] != "building":\r
                 cntimg = Image.new("RGBA", (cnt_w, cnt_h), (255,255,255,255))\r
@@ -401,6 +402,11 @@ class mstr_orthographic:
         # stress on OSM servers and my server, we will do acquire the data\r
         # only for the current processed part of the tile.\r
 \r
+        # Total number of tiles to be generated\r
+        total_tiles = mlng * mlat\r
+        nextstep = 20\r
+        mstr_important_msg("orthographic", "Generating map data required to build orthos")\r
+\r
         for lat_grid in range(1, maxlatlng[0]+1):\r
             for lng_grid in range(1, maxlatlng[1]+1):\r
                 # Adjust bounding box\r
@@ -422,7 +428,7 @@ class mstr_orthographic:
                         mg = mstr_maskgen( [self._lat, cur_tile_y, self._long, cur_tile_x], self._vstep, layer[0], layer[1], layer[2])\r
                         mask = mg._build_mask(osmxml, is_prep=True) # We need an object here\r
 \r
-                        mask = mask.resize((20,20), Image.Resampling.BILINEAR)\r
+                        mask = mask.resize((80,80), Image.Resampling.BILINEAR)\r
                         idx = 0\r
                         for c in contours:\r
                             if c[0] == layer[0] and c[1] == layer[1]:\r
@@ -430,8 +436,8 @@ class mstr_orthographic:
                             else:\r
                                 idx=idx+1\r
                         \r
-                        cnx = (lng_grid-1) * 20\r
-                        cny = cnt_h - (lat_grid * 20)\r
+                        cnx = (lng_grid-1) * 80\r
+                        cny = cnt_h - (lat_grid * 80)\r
                         contours[idx][2].alpha_composite(mask, dest=(cnx,cny))\r
 \r
                         #tp = mstr_tileprep(self._lat, self._long, lat_grid, lng_grid, layer[0], layer[1], mask, False)\r
@@ -441,18 +447,25 @@ class mstr_orthographic:
 \r
                 # Adjust longitude coordinates\r
                 cur_tile_x = cur_tile_x+1\r
-                bb_lng = bb_lng + mstr_zl_18\r
-                bb_lng_edge = bb_lng_edge + mstr_zl_18\r
+                bb_lng = bb_lng + self._zoomlevel\r
+                bb_lng_edge = bb_lng_edge + self._zoomlevel\r
                 mstr_msg("orthographic", "Adjustment of longitude performed")\r
                 # Adjust peak longitude tile number\r
                 if cur_tile_x > top_lng:\r
                     top_lng = cur_tile_x\r
 \r
+                totaldata = glob.glob("D:/Developer/webserver/htdocs/server/osm/*.xml")\r
+                progress = int( (len(totaldata) / total_tiles) * 100 )\r
+\r
+                if progress >= nextstep:\r
+                    mstr_important_msg("orthographic", str(progress) + " percent complete")\r
+                    nextstep = nextstep + 20\r
+\r
             # Adjust latitude and all other values when we get here\r
             cur_tile_y = cur_tile_y+1\r
             cur_tile_x = 1\r
             bb_lng = self._long\r
-            bb_lng_edge = self._long + mstr_zl_18\r
+            bb_lng_edge = self._long + self._zoomlevel\r
             bb_lat = bb_lat + self._vstep\r
             bb_lat_edge = bb_lat_edge + self._vstep\r
             mstr_msg("orthographic", "Adjustment of latitude performed")\r
@@ -526,12 +539,12 @@ class mstr_orthographic:
         bb_lat = self._lat\r
         bb_lng = self._long\r
         bb_lat_edge = self._lat+self._vstep\r
-        bb_lng_edge = self._long+mstr_zl_18\r
+        bb_lng_edge = self._long+self._zoomlevel\r
         while bb_lat < self._lat + 1:\r
             bb_lat = bb_lat + self._vstep\r
             mlat = mlat+1\r
         while bb_lng < self._long + 1:\r
-            bb_lng = bb_lng + mstr_zl_18\r
+            bb_lng = bb_lng + self._zoomlevel\r
             mlng = mlng+1\r
         mstr_msg("orthographic", "Max lat tile: " + str(mlat) + " - max lng tile: " + str(mlng))\r
         maxlatlng = [ mlat, mlng ]\r
index 85ebdca3e70016c126ddbaf3df4b4a2ee657d915..7c4c1504bee1f14f20466d2ccd3c275482d9dc9d 100644 (file)
--- a/osmxml.py
+++ b/osmxml.py
@@ -63,42 +63,56 @@ class mstr_osmxml:
     # Acquire XMLs in chunks, then store them\r
     def acquire_osm(self, v, h):\r
         mstr_msg("osmxml", "Acquiring OSM data for " + str(self._lat)+","+str(self._lng)+" - "+str(self._curB_lat)+","+str(self._curB_lng))\r
-        \r
-        # We will use our self-hosted API for this.\r
-        parse = False\r
-        while parse == False:\r
-            data = { \r
-                "bbox": { \r
-                        "lat": str(self._lat),\r
-                        "lng": str(self._lng),\r
-                        "lat_b": str(self._curB_lat),\r
-                        "lng_b": str(self._curB_lng)\r
-                    },\r
-                    "tile_lat": str(self._lat),\r
-                    "tile_lng": str(self._lng),\r
-                    "square_lat": str(v),\r
-                    "square_lng": str(h)\r
-                    }\r
-            r = requests.post(mstr_osm_endpoint, json=data)\r
 \r
-            try:\r
-                # Attempt to parse the XML string\r
-                dom = xml.dom.minidom.parseString(r.content)\r
+        # We switched to a local storage model to ease load on the server. We will check if we already have the data we need.\r
+        # If not, go an acquire it.\r
+        fn = mstr_datafolder + "z_orthographic/data/+++++/osm/" + str(self._lat) + "-" + str(v) + "_"  + str(self._lng) + "-" + str(h) + ".xml"\r
+        if os.path.isfile(fn) == False:\r
+            # We will use our self-hosted API for this.\r
+            parse = False\r
+            while parse == False:\r
+                data = { \r
+                    "bbox": { \r
+                            "lat": str(self._lat),\r
+                            "lng": str(self._lng),\r
+                            "lat_b": str(self._curB_lat),\r
+                            "lng_b": str(self._curB_lng)\r
+                        },\r
+                        "tile_lat": str(self._lat),\r
+                        "tile_lng": str(self._lng),\r
+                        "square_lat": str(v),\r
+                        "square_lng": str(h)\r
+                        }\r
+                r = requests.post(mstr_osm_endpoint, json=data)\r
+\r
+                try:\r
+                    # Attempt to parse the XML string\r
+                    dom = xml.dom.minidom.parseString(r.content)\r
+\r
+                    # Check if the DOM object has a document element\r
+                    if dom.documentElement:\r
+                        # Store the content in memory\r
+                        self._xmlcontent = r.content\r
+                        self._xmldata = xml.dom.minidom.parseString(self._xmlcontent)\r
+                        self._xmlcontent = ""  # Clear\r
+                        parse = True\r
 \r
-                # Check if the DOM object has a document element\r
-                if dom.documentElement:\r
-                    # Store the content in memory\r
-                    self._xmlcontent = r.content\r
-                    self._xmldata = xml.dom.minidom.parseString(self._xmlcontent)\r
-                    self._xmlcontent = ""  # Clear\r
-                    parse = True\r
+                except ExpatError as e:\r
+                    parse = False\r
+                    time.sleep(1)\r
+                except Exception as e:\r
+                    parse = False\r
+                    time.sleep(1)\r
+        else:\r
+            # Attempt to parse the XML string\r
+            dom = xml.dom.minidom.parse(fn)\r
 \r
-            except ExpatError as e:\r
-                parse = False\r
-                time.sleep(1)\r
-            except Exception as e:\r
-                parse = False\r
-                time.sleep(1)\r
+            # Check if the DOM object has a document element\r
+            if dom.documentElement:\r
+                # Store the content in memory\r
+                self._xmlcontent = r.content\r
+                self._xmldata = xml.dom.minidom.parseString(self._xmlcontent)\r
+                self._xmlcontent = ""  # Clear\r
 \r
 \r
     # Get all nodes from the specified OSM file\r