อัปโหลด dataset และให้บริการเป็น tile
อัปโหลด GeoJSON เข้าบัญชี MapScale ได้ vector tileset แล้วแสดงบนแผนที่ MapLibre
เริ่มต้น
ก่อนเริ่ม
Dataset คือ GeoJSON FeatureCollection ที่เก็บไว้ในบัญชีของคุณและให้บริการเป็น vector tile รองรับ geometry แบบ Point, MultiPoint, LineString, MultiLineString, Polygon และ MultiPolygon พิกัด WGS84 แบบลองจิจูด/ละติจูด การอัปโหลดและจัดการ dataset ต้องใช้ secret key (sk_) จึงควรเรียกจากเทอร์มินัลหรือเซิร์ฟเวอร์ของคุณ ส่วนการอ่าน tile ในเบราว์เซอร์ใช้ publishable key (pk_) จากบัญชีเดียวกันได้ จำนวน dataset จำนวน feature ต่อ dataset และขนาดไฟล์มีโควตาตามบัญชี
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [100.5018, 13.7563] },
"properties": { "name": "Bangkok", "staff": 42 }
},
{
"type": "Feature",
"geometry": { "type": "Point", "coordinates": [98.9853, 18.7883] },
"properties": { "name": "Chiang Mai", "staff": 12 }
}
]
}อัปโหลด GeoJSON
ส่ง FeatureCollection เป็น request body แบบ POST ค่า slug จะเป็นชื่อ layer ภายใน tile ถ้าไม่ใส่ public=true dataset จะเป็นของบัญชีคุณเท่านั้น
# Save the sample above as branches.geojson, then upload it.
# name is required. slug (the layer name inside the tiles) and public are optional.
curl -X POST "https://api.mapscale.io/v1/datasets?name=Branches&slug=branches" \
-H "x-api-key: sk_live_YOUR_SECRET_KEY" \
-H "content-type: application/json" \
--data-binary @branches.geojson
# The response includes the dataset id, its slug and its tilesetId:
# "tilesetId": "ds_0b3f6f6e-5c6e-4f43-9f43-1d1a1c1e1f10"ตรวจสอบสิ่งที่อัปโหลด
ดูรายการ dataset หรือเรียกทีละรายการด้วย id เพื่อดูสถานะ จำนวน feature ชนิด geometry และขอบเขตพื้นที่
curl "https://api.mapscale.io/v1/datasets" \
-H "x-api-key: sk_live_YOUR_SECRET_KEY"
# One dataset:
curl "https://api.mapscale.io/v1/datasets/DATASET_ID" \
-H "x-api-key: sk_live_YOUR_SECRET_KEY"แสดงบนแผนที่
เพิ่ม vector source ที่ชี้ไปที่ /tiles/v1/ds_{id}/{z}/{x}/{y}.mvt และ layer ที่ตั้ง source-layer เป็น slug ของ dataset MapLibre ตั้ง header ให้คำขอ tile ไม่ได้ จึงต้องส่ง publishable key ผ่านพารามิเตอร์ key
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.css" />
<script src="https://unpkg.com/maplibre-gl@5/dist/maplibre-gl.js"></script>
<div id="map" style="height: 480px"></div>
<script>
// Publishable key from the account that owns the dataset.
const key = "pk_live_YOUR_PUBLISHABLE_KEY"
const tilesetId = "ds_YOUR_DATASET_ID" // tilesetId from the upload response
const slug = "branches" // slug from the upload response
const map = new maplibregl.Map({
container: "map",
style: "https://api.mapscale.io/styles/v1/streets?lang=en&key=" + key,
center: [100.5018, 13.7563],
zoom: 5,
})
map.on("load", () => {
map.addSource("branches", {
type: "vector",
tiles: ["https://api.mapscale.io/tiles/v1/" + tilesetId + "/{z}/{x}/{y}.mvt?key=" + key],
maxzoom: 14,
})
map.addLayer({
id: "branches-circle",
type: "circle",
source: "branches",
"source-layer": slug, // the layer name inside the tiles is the dataset slug
paint: { "circle-color": "#0c4a4a", "circle-radius": 7 },
})
})
</script>ใช้ใน React
ชุดคอมโพเนนต์ React ของ MapScale มี DatasetLayer ที่เพิ่ม vector source และชนิด layer ที่เหมาะกับ geometry ของ dataset และเพิ่มกลับให้เมื่อสลับ style ของแผนที่พื้นฐาน
// npx shadcn@latest add https://mapscale.io/r/geojson-layer.json
import { DatasetLayer } from "@/components/mapscale/dataset-layer"
import { Map } from "@/components/mapscale/map"
export function BranchesMap() {
return (
<div style={{ height: 480 }}>
<Map center={[100.5018, 13.7563]} zoom={5}>
<DatasetLayer
tilesetId="ds_YOUR_DATASET_ID"
sourceLayer="branches"
geometryType="Point"
apiKey={process.env.NEXT_PUBLIC_MAPSCALE_KEY}
/>
</Map>
</div>
)
}ลบ dataset
การลบ dataset จะลบ feature ทั้งหมดและหยุดให้บริการ tile ของ dataset นั้น
curl -X DELETE "https://api.mapscale.io/v1/datasets/DATASET_ID" \
-H "x-api-key: sk_live_YOUR_SECRET_KEY"
# { "id": "...", "deleted": true }