-- Loome tabeli 'links' valides vajalikud andmed OSM 'ways' tabelist
DROP TABLE IF EXISTS links;

CREATE UNLOGGED TABLE links AS
SELECT
  w.id,
  w.source,
  w.target,
  w.osm_id,
  w.highway,
  ST_Length(w.geometry) AS length,
  COALESCE(w.name, w.ref, w.highway, 'Unnamed road') AS name,
  w.oneway,
  w.reverse_cost,
  w.cost,
  w.x1,
  w.y1,
  w.x2,
  w.y2,
  w.geometry
FROM ways w
WHERE w.highway IS NOT NULL;

-- Lisame ruumilise indeksi geomeetriale
CREATE INDEX links_geom_idx ON links USING GIST (geometry);
