DEV Community

Gcasases
Gcasases

Posted on

Who sees the sea? Tracing the sea view of 158,060 buildings in 46 minutes with open data, Python and Numba

Every listing on the Spanish coast says "sea views". Almost none says how much sea, from which floor, or past which building. We wanted a number for every façade and every floor of every home on the Costa del Sol, from open data only. This is how the model works, with the core of the ray tracer in a few lines of Numba.

The full working paper, with figures, validation and how to cite it, is on CASAS.ES Research. The data and code are on Zenodo: 10.5281/zenodo.24106387 (paper: 10.5281/zenodo.24106403).

The result first

For the 158,060 residential buildings of 21 municipalities, from Casares to Nerja:

  • 52 % see some sea from their top floor;
  • 36 % have an open view, 20° of sea or more;
  • on terrain alone, with no buildings in the way, 73 % would have that open view from the top floor. The buildings take it from about half of them.

Two open datasets

The model uses nothing but:

  1. Cadastral buildings (Dirección General del Catastro, INSPIRE): the footprint of each building part and its number of floors above ground.
  2. Terrain models of the national mapping agency (IGN): MDT05, a 5 m grid derived from LiDAR, for the first 2 km, and MDT25 for the relief out to 30 km.

No point clouds, no surface model of buildings, no coastline file. How CASAS.ES builds its figures in general is in the methodology.

The model in four steps

1. A surface of terrain plus blocks. MDT05 is resampled to a 2 m grid. Every building part with N floors becomes a solid block of height 3.0 m × N + 1.0 m above the terrain of each cell, so roofs follow the slope. Sea cells are those at 0.2 m or less that connect to the edge of the grid: the sea finds itself, no vector needed.

2. Free façades and viewpoints. Walk the outer ring of each block, drop party walls (edges less than 0.30 m from another building), group the free edges by octant and keep octants with at least 3 m of free façade. Each one gets a viewpoint 0.75 m outside the middle of its longest edge, at three floors: 1, ⌈N/2⌉ and N. Eye height is the terrain + 3.0 m × (floor − 1) + 1.6 m. That gives 1.35 million viewpoints for the coast.

3. Lines of sight. From each viewpoint, 86 rays leave at ±1°, ±3°, … ±85° from the façade normal. Each ray stands for 2°, so a score runs from 0° to 172°. Up to 2 km a ray walks buildings and terrain on the 2 m grid, with a step of max(1 m, 0.004 × d), 8 m at 2 km. From 2 to 30 km it walks the relief on MDT25 every 25 m. At each sample the tangent of the elevation angle includes Earth curvature and refraction (k = 0.13). A sea cell counts as seen when the sea surface, at z = 0, rises above every tangent before it on the same ray.

4. Degrees of sea. Per viewpoint: 2° for each ray that reaches the sea. A building sees the sea from a level when its best façade scores 4° or more (two rays), and has an open view at 20° or more.

The kernel

A simplified version of the near field (the full model, with the far field, is in the Zenodo release):

import math
import numpy as np
from numba import njit, prange

R_EARTH = 6_371_000.0
K_REFR = 0.13                                # refraction coefficient
KCURV = (1 - K_REFR) / (2 * R_EARTH)         # drop of the surface: KCURV * d**2

@njit(parallel=True, cache=True)
def degrees_of_sea(ox, oy, oz, normal, own, dsm, dtm, block, sea, x0, ytop, res, d_near=2000.0):
    """Degrees of sea for each viewpoint: 86 rays at ±1°, ±3°, … ±85° from the façade
    normal, each worth 2°. Near field only (buildings + terrain on one grid)."""
    H, W = dsm.shape
    out = np.zeros(ox.shape[0])
    for i in prange(ox.shape[0]):                # one viewpoint per thread
        for k in range(86):
            off = -85.0 + 2.0 * k
            az = math.radians(normal[i] + off)
            sx, sy = math.sin(az), math.cos(az)
            tmax = -1e9                          # highest tangent seen so far on this ray
            d = 1.0
            while d < d_near:
                c = int((ox[i] + sx * d - x0) / res)
                r = int((ytop - (oy[i] + sy * d)) / res)
                if r < 0 or c < 0 or r >= H or c >= W:
                    break
                drop = KCURV * d * d
                if sea[r, c] and (0.0 - drop - oz[i]) / d > tmax:
                    out[i] += 2.0                # the sea surface (z = 0) shows above everything before it
                    break
                # the viewpoint's own block counts as terrain, so its wall does not hide its view
                z = dtm[r, c] if block[r, c] == own[i] else dsm[r, c]
                t = (z - drop - oz[i]) / d
                if t > tmax:
                    tmax = t
                d += max(1.0, 0.004 * d)         # 1 m steps near the façade, 8 m at 2 km
    return out
Enter fullscreen mode Exit fullscreen mode

Two details matter more than they look:

  • The own block is transparent. The viewpoint sits 0.75 m outside its wall; without this rule, the first cells of a ray would hit its own building. Other blocks of the same building still block it.
  • The sea is a surface, not a cell value. Testing the sea at z = 0, rather than at the terrain value of the cell, keeps the beach in front from being hidden by its own 0.2 m.

How long it takes

The coast was cut into 159 tiles of 4 km, each with a 2 km margin of neighbouring buildings and terrain. On two virtual cores (Intel Xeon at 2.1 GHz, 8 GB, Python 3.11, NumPy 2.4, Numba 0.68):

  • ray tracing: 2,504 s;
  • data preparation: 237 s;
  • in all, 46 minutes, or 9.6 core-hours per million buildings.

A pilot of 6 × 5.5 km of Marbella, 52,971 viewpoints, runs in 71 s.

Does it see what people see?

  • Against a LiDAR surface, in the pilot: 96.7 % of viewpoints fall in the same class at 20°. What the cadastre does not record (rooftop structures, extensions) lowers the sea seen from the top floor by 6.1–9.0° on average.
  • Against photographs of 93 homes for sale, labelled blind: an AUC of 0.80 (95 % CI 0.70–0.89). At 20° the score is sensitive (0.90) but not specific (0.45).
  • Dwellings are harder than buildings: the cadastre gives the number of dwellings, not their floors or façades. Under the main allocation rule, 42 % of dwellings are on a floor with open-view potential; across the scenarios tested, 12–45 %.

The scores are modelled potential, not observed views. Trees are out except in the tree variants, and so are balconies.

Municipality by municipality

Share of residential buildings that see the sea from the top floor, west to east (Table 5 of the paper). Each name links to its page on CASAS.ES:

Municipality Buildings Some sea, top floor Open view (≥ 20°), top floor
Casares 2,249 40 % 28 %
Manilva 2,981 76 % 60 %
Estepona 8,390 63 % 47 %
Benahavís 2,388 82 % 72 %
Istán 904 24 % 19 %
Marbella 16,765 75 % 57 %
Ojén 1,146 54 % 28 %
Alhaurín el Grande 9,104 4 % 0 %
Mijas 16,743 64 % 48 %
Fuengirola 5,713 34 % 22 %
Alhaurín de la Torre 8,443 36 % 9 %
Benalmádena 6,670 79 % 63 %
Torremolinos 3,710 68 % 49 %
Málaga 40,147 32 % 18 %
Totalán 324 31 % 1 %
Rincón de la Victoria 5,094 81 % 63 %
Vélez-Málaga 12,677 55 % 40 %
Algarrobo 1,545 53 % 40 %
Torrox 4,225 66 % 47 %
Frigiliana 2,264 72 % 56 %
Nerja 6,578 74 % 57 %
Costa del Sol 158,060 52 % 36 %

Fuengirola is the clearest case of buildings hiding the sea: on terrain alone, 96 % of its buildings would have an open view from the top floor; with its own blocks in the way, 22 %.

Data and code

  • Data (CC BY 4.0): one row per building and one per façade viewpoint and floor, with the 14-character cadastral reference, on Zenodo: 10.5281/zenodo.24106387.
  • Code (MIT): the model, the scripts behind every table and figure, a test area with its expected output and a pinned environment, in the same release. bash reproduce.sh tables checks the main numbers in minutes.
  • Every street of the coast, 9,500 of them, with a link to its page: streets of the coast.

The next articles in this series go street by street. The first CASAS.ES dataset on dev.to, the 103 municipalities of Málaga in a CSV, is here.

Top comments (1)

Collapse
 
aryanna profile image
Aryanna •

Read this twice. One thing I keep circling: the 93 homes are for sale, so your labels come from the same listings that claim sea views. A listing photo is a frame the seller picked. So specificity 0.45 may be measuring how often the seller oversells, not how generous the geometry is. Do you have any label from a window nobody chose, street-level imagery or a survey of the same facade?