#### Step 1:建立資料庫並啟用 PostGIS - 在 Neon 建立新資料庫後,進入 SQL Editor - 執行以下指令安裝 PostGIS: ```sql CREATE EXTENSION IF NOT EXISTS postgis; ``` 這時候資料庫就能支援 geometry / geography 欄位。 #### Step 2:用 shp2pgsql 匯入 .shp 假設你有一個登山路線的 `.shp` 檔: ```bash shp2pgsql -I -s 3826 hiking_map.shp hiking_map | psql $DATABASE_URL ``` - `-s 3826` 表示這筆資料是台灣 TWD97 平面座標 - 匯入後,Neon 就會有一張 `hiking_map` 表,裡面有 `geom` 欄位儲存路線 #### Step 3:查詢 GeoJSON 格式(給前端用) 在 API(NestJS / Express)中可以這樣撈: ```sql SELECT id, ST_AsGeoJSON(ST_Transform(geom, 4326)) AS geometry FROM hiking_map; ``` > 注意:Leaflet 等地圖工具僅支援 EPSG:4326(WGS84 經緯度),匯入後須轉換 SRID 你可以把查詢結果轉成這種格式回傳: ```json { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [...] }, "properties": { "id": 1 } } ] } ``` --- 這樣就能把地理資料存進 Neon,並透過 Next.js + Leaflet 在網頁上畫出圖層。