From f206adbf0adca4a87f796c68c0af8750a94dcd8e Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Wed, 7 Jan 2026 12:25:15 +0000
Subject: [PATCH 1/2] =?UTF-8?q?direction=5Fid=E3=81=8CNULL=E3=81=AE?=
 =?UTF-8?q?=E3=83=88=E3=83=AA=E3=83=83=E3=83=97=E3=81=AB=E5=AD=98=E5=9C=A8?=
 =?UTF-8?q?=E3=81=99=E3=82=8B=E3=83=90=E3=82=B9=E5=81=9C=E3=82=82=E6=AD=A3?=
 =?UTF-8?q?=E3=81=97=E3=81=8F=E5=8F=96=E3=82=8A=E8=BE=BC=E3=82=80=E3=82=88?=
 =?UTF-8?q?=E3=81=86=E4=BF=AE=E6=AD=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GTFSの仕様ではdirection_idはオプショナルであり、NULLの場合でも
バス停は有効な停留所として扱うべきである。

これまではvariant_only_with_neighbors CTEで「direction_id IS NOT NULL」
のトリップにのみ存在するバス停をフィルタリングしていたため、
早81の原宿駅前や渋谷駅東口などのバス停がレスポンスから除外されていた。
---
 stationapi/src/import.rs | 10 ----------
 1 file changed, 10 deletions(-)

diff --git a/stationapi/src/import.rs b/stationapi/src/import.rs
index 6f4f2e77..c7c300c2 100644
--- a/stationapi/src/import.rs
+++ b/stationapi/src/import.rs
@@ -1636,7 +1636,6 @@ async fn build_stop_route_mapping(
                )
            ),
            -- Find variant-only stops (not on main trip) with their neighbor info
-           -- Exclude stops that ONLY appear on NULL direction_id trips (loop routes)
            variant_only_with_neighbors AS (
                SELECT DISTINCT ON (vts.parent_stop_id, vts.route_id)
                    vts.parent_stop_id,
@@ -1650,15 +1649,6 @@ async fn build_stop_route_mapping(
                    WHERE mts.parent_stop_id = vts.parent_stop_id
                      AND mts.route_id = vts.route_id
                )
-               -- Only include stops that appear on at least one non-NULL direction_id trip
-               AND EXISTS (
-                   SELECT 1 FROM gtfs_trips gt2
-                   JOIN gtfs_stop_times gst2 ON gt2.trip_id = gst2.trip_id
-                   JOIN gtfs_stops gs2 ON gst2.stop_id = gs2.stop_id
-                   WHERE gt2.route_id = vts.route_id
-                     AND COALESCE(gs2.parent_station, gs2.stop_id) = vts.parent_stop_id
-                     AND gt2.direction_id IS NOT NULL
-               )
                ORDER BY vts.parent_stop_id, vts.route_id, vts.stop_sequence
            ),
            -- Recursive CTE to find the nearest main-trip stop by following prev chain

From 82a20109b246f62dd6d871269d9d4ec1d2c1036b Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Wed, 7 Jan 2026 12:42:29 +0000
Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=90=E3=83=AA=E3=82=A2=E3=83=B3?=
 =?UTF-8?q?=E3=83=88=E3=83=90=E3=82=B9=E5=81=9C=E3=81=AE=E4=BD=8D=E7=BD=AE?=
 =?UTF-8?q?=E6=8E=A8=E5=AE=9A=E3=81=A7=E3=83=A1=E3=82=A4=E3=83=B3=E3=83=88?=
 =?UTF-8?q?=E3=83=AA=E3=83=83=E3=83=97=E3=81=AB=E5=AD=98=E5=9C=A8=E3=81=99?=
 =?UTF-8?q?=E3=82=8B=E9=9A=A3=E6=8E=A5=E3=83=90=E3=82=B9=E5=81=9C=E3=82=92?=
 =?UTF-8?q?=E5=84=AA=E5=85=88?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

variant_only_with_neighborsでバス停の隣接情報を選択する際に、
隣接バス停（prev_stop_id/next_stop_id）がメイントリップに存在する
レコードを優先するよう修正。

これにより、原宿駅前などのバス停がより正確な位置に挿入される。
---
 stationapi/src/import.rs | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/stationapi/src/import.rs b/stationapi/src/import.rs
index c7c300c2..1b991878 100644
--- a/stationapi/src/import.rs
+++ b/stationapi/src/import.rs
@@ -1636,6 +1636,7 @@ async fn build_stop_route_mapping(
                )
            ),
            -- Find variant-only stops (not on main trip) with their neighbor info
+           -- Prioritize records where neighbors exist on main trip for better position estimation
            variant_only_with_neighbors AS (
                SELECT DISTINCT ON (vts.parent_stop_id, vts.route_id)
                    vts.parent_stop_id,
@@ -1644,12 +1645,25 @@ async fn build_stop_route_mapping(
                    vts.prev_stop_id,
                    vts.next_stop_id
                FROM variant_trip_stops_with_neighbors vts
+               LEFT JOIN main_trip_stops mts_prev
+                   ON vts.prev_stop_id = mts_prev.parent_stop_id
+                   AND vts.route_id = mts_prev.route_id
+               LEFT JOIN main_trip_stops mts_next
+                   ON vts.next_stop_id = mts_next.parent_stop_id
+                   AND vts.route_id = mts_next.route_id
                WHERE NOT EXISTS (
                    SELECT 1 FROM main_trip_stops mts
                    WHERE mts.parent_stop_id = vts.parent_stop_id
                      AND mts.route_id = vts.route_id
                )
-               ORDER BY vts.parent_stop_id, vts.route_id, vts.stop_sequence
+               ORDER BY vts.parent_stop_id, vts.route_id,
+                        -- Prioritize records where neighbors exist on main trip
+                        CASE
+                            WHEN mts_prev.parent_stop_id IS NOT NULL AND mts_next.parent_stop_id IS NOT NULL THEN 0
+                            WHEN mts_prev.parent_stop_id IS NOT NULL OR mts_next.parent_stop_id IS NOT NULL THEN 1
+                            ELSE 2
+                        END,
+                        vts.stop_sequence
            ),
            -- Recursive CTE to find the nearest main-trip stop by following prev chain
            prev_chain AS (
