ระบบพิกัดอ้างอิงและการฉายแผนที่
แปลงระหว่าง CRS ด้วย st_crs() และ st_transform() อย่างถูกต้อง
ระบบพิกัดอ้างอิงและการฉายแผนที่ เป็นบทเรียน R Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน R Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส R Academy มีบทเรียนทั้งหมด 4 บทเรียน
เหตุใด CRS จึงสำคัญ
ระบบอ้างอิงพิกัด (CRS) บอก R ว่าตัวเลขในคอลัมน์เรขาคณิตหมายถึงอะไร เช่น องศาละติจูด/ลองจิจูด เมตรในกริดที่ฉายแล้ว หรือความหมายอื่น การใช้ข้อมูลจาก CRS ต่างกันโดยไม่ฉายใหม่จะทำให้การเชื่อมข้อมูลเชิงพื้นที่ล้มเหลวโดยไม่แสดงข้อผิดพลาด และทำให้การคำนวณระยะทางไม่ถูกต้อง
library(sf)
# Two sf objects in the same CRS: operations work
nc <- read_sf(system.file('shape/nc.shp', package = 'sf'))
# sf will error or warn when CRS do not match during operations
# This is the CRS metadata attached to the object
crs <- st_crs(nc)
cat('EPSG:', crs$epsg, '\n')
cat('Input is geographic (lon/lat)?', st_is_longlat(nc), '\n')
cat('Units:', crs$units_gdal, '\n')st_crs: การอ่านข้อมูล CRS
st_crs(sf_obj) ส่งคืนออบเจ็กต์ crs ที่มีรหัส EPSG สตริง WKT สตริง Proj4 และข้อมูลหน่วย คุณยังสามารถส่งรหัส EPSG แบบจำนวนเต็มโดยตรงเพื่อเรียกดูนิยาม CRS ได้
library(sf)
nc <- read_sf(system.file('shape/nc.shp', package = 'sf'))
# Inspect the CRS object
crs <- st_crs(nc)
cat('Class:', class(crs), '\n')
cat('EPSG:', crs$epsg, '\n')
cat('Proj4:', substr(crs$proj4string, 1, 60), '...\n')
# Lookup a CRS by EPSG code
wgs84 <- st_crs(4326)
web_mercator <- st_crs(3857)
cat('\nWGS84 name:', wgs84$Name, '\n')
cat('Web Mercator name:', web_mercator$Name, '\n')EPSG:4326 — WGS84
EPSG:4326 คือ ระบบภูมิสารสนเทศโลก 1984 ซึ่งเป็นมาตรฐานสำหรับพิกัด GPS พิกัดใช้หน่วยองศาทศนิยม: ลองจิจูด (X) อยู่ในช่วง −180 ถึง 180 และละติจูด (Y) อยู่ในช่วง −90 ถึง 90 นี่คือ CRS แบบภูมิศาสตร์ ไม่ใช่แบบฉาย
library(sf)
# Create points in WGS84 (GPS coordinates)
cities <- data.frame(
city = c('London', 'Tokyo', 'Sydney', 'New York'),
lon = c(-0.118, 139.692, 151.209, -74.006),
lat = c(51.509, 35.689, -33.869, 40.713)
)
cities_sf <- st_as_sf(cities,
coords = c('lon', 'lat'),
crs = 4326
)
cat('CRS:', st_crs(cities_sf)$Name, '\n')
cat('Units:', st_crs(cities_sf)$units_gdal, '\n')
# Naive distance in degrees (not meaningful for real distances)
d <- st_distance(cities_sf[1, ], cities_sf[2, ])
cat('London-Tokyo distance (degrees):', round(as.numeric(d), 2), '\n')EPSG:3857 — Web Mercator
EPSG:3857 คือการฉาย Web Mercator ที่ Google Maps, OpenStreetMap และ Leaflet ใช้ พิกัดมีหน่วยเป็นเมตร โดยมีศูนย์กลางอยู่ที่เส้นเมริเดียนแรกและเส้นศูนย์สูตร การฉายนี้ทำให้พื้นที่ใกล้ขั้วโลกบิดเบี้ยว แต่เหมาะสำหรับการแสดงแผนที่บนเว็บ
library(sf)
cities <- data.frame(
city = c('London', 'Tokyo'),
lon = c(-0.118, 139.692),
lat = c(51.509, 35.689)
)
cities_sf <- st_as_sf(cities, coords = c('lon', 'lat'), crs = 4326)
# Transform to Web Mercator
cities_merc <- st_transform(cities_sf, 3857)
cat('WGS84 coords (degrees):\n')
print(st_coordinates(cities_sf))
cat('\nWeb Mercator coords (metres):\n')
print(st_coordinates(cities_merc))
# Distance is now in metres
d <- st_distance(cities_merc[1, ], cities_merc[2, ])
cat('\nLondon-Tokyo distance (km):', round(as.numeric(d) / 1000, 0), '\n')st_set_crs: การกำหนด CRS
st_set_crs(sf_obj, crs) กำหนด CRS โดยไม่แปลงพิกัด ใช้ฟังก์ชันนี้เฉพาะเมื่อข้อมูลเมทาดาทาของ CRS หายไปหรือไม่ถูกต้อง หากต้องการฉายพิกัดใหม่ไปยัง CRS อื่น ให้ใช้ st_transform() แทน
library(sf)
# Create geometry with NO CRS
pts <- st_sfc(
st_point(c(-74.006, 40.713)),
st_point(c(-87.629, 41.878))
)
cat('CRS before:', is.na(st_crs(pts)), '(NA = no CRS)\n')
# Assign WGS84 (no coordinate transformation)
pts_wgs84 <- st_set_crs(pts, 4326)
cat('CRS after assignment:', st_crs(pts_wgs84)$epsg, '\n')
# Compare with st_transform (DOES transform coordinates)
pts_sf <- st_sf(id = 1:2, geometry = pts_wgs84)
pts_merc <- st_transform(pts_sf, 3857)
cat('Original lon:', st_coordinates(pts_sf)[1, 'X'], '\n')
cat('Mercator X: ', st_coordinates(pts_merc)[1, 'X'], '\n')st_transform: การฉายใหม่
st_transform(sf_obj, crs) ฉายเรขาคณิตทั้งหมดใหม่จาก CRS ปัจจุบันไปยัง CRS เป้าหมาย โดยใช้การแปลงเชิงภูมิสารสนเทศอย่างครบถ้วน อาร์กิวเมนต์ crs รับจำนวนเต็ม EPSG สตริง Proj4 หรือสตริง WKT
library(sf)
nc <- read_sf(system.file('shape/nc.shp', package = 'sf'))
cat('Original CRS:', st_crs(nc)$epsg, '\n')
# Re-project to WGS84
nc_wgs84 <- st_transform(nc, 4326)
cat('Transformed CRS:', st_crs(nc_wgs84)$epsg, '\n')
# Re-project to a local projected CRS (NC State Plane)
nc_stateplane <- st_transform(nc, 32119) # NAD83 / North Carolina
cat('State Plane CRS:', st_crs(nc_stateplane)$Name, '\n')
# Area calculation is more accurate in a projected CRS
area_deg <- st_area(nc[1, ])
area_m2 <- st_area(nc_stateplane[1, ])
cat('Area (degrees^2):', format(area_deg, big.mark = ','), '\n')
cat('Area (m^2): ', format(area_m2, big.mark = ','), '\n')การเลือกการฉายที่เหมาะสม
การฉายแต่ละแบบเหมาะกับคุณสมบัติที่แตกต่างกัน ได้แก่ รักษาพื้นที่ (เช่น Mollweide) รักษารูปร่าง (Mercator) และรักษาระยะทาง สำหรับการวิเคราะห์ในพื้นที่ ให้ใช้ CRS แบบฉายในหน่วยเมตร สำหรับภาพรวมระดับโลก WGS84 ใช้ได้ และสำหรับแผนที่บนเว็บ ให้ใช้ Web Mercator
library(sf)
# Common EPSG codes cheat sheet
epsg_table <- data.frame(
EPSG = c(4326, 3857, 4269, 32637, 27700),
Name = c(
'WGS84 (GPS, global)',
'Web Mercator (web maps)',
'NAD83 (North America geographic)',
'UTM zone 37N (Middle East)',
'British National Grid (UK)'
),
Units = c('degrees', 'metres', 'degrees', 'metres', 'metres')
)
print(epsg_table, row.names = FALSE)
# Look up any EPSG online: https://epsg.io/<code>
cat('\nTip: use st_crs(epsg_code)$Name to verify a code\n')สตริง Proj4
ก่อนที่รหัส EPSG จะกลายเป็นมาตรฐาน CRS ถูกกำหนดด้วย สตริง Proj4 ซึ่งเป็นสตริงพารามิเตอร์แบบกระชับที่ส่งให้ไลบรารี PROJ คุณยังคงใช้สตริงเหล่านี้กับ st_transform() ได้ แต่ควรใช้รหัส EPSG มากกว่าเพื่อความชัดเจนและความเข้ากันได้ในอนาคต
library(sf)
nc <- read_sf(system.file('shape/nc.shp', package = 'sf'))
# WGS84 as Proj4 string
wgs84_proj4 <- '+proj=longlat +datum=WGS84 +no_defs'
nc_wgs84 <- st_transform(nc, crs = wgs84_proj4)
cat('Using Proj4 string:\n', wgs84_proj4, '\n')
cat('Result CRS EPSG:', st_crs(nc_wgs84)$epsg, '\n')
# Compare: equivalent to st_transform(nc, 4326)
cat('Same as EPSG 4326?',
identical(st_crs(nc_wgs84), st_crs(st_transform(nc, 4326))), '\n')การตรวจสอบความเท่ากันของ CRS
ก่อนการเชื่อมข้อมูลเชิงพื้นที่หรือการซ้อนทับใด ๆ ให้ตรวจสอบว่าออบเจ็กต์ทั้งสองใช้ CRS เดียวกันด้วย st_crs(a) == st_crs(b) หากแตกต่างกัน ให้แปลงออบเจ็กต์หนึ่งให้ตรงกับอีกออบเจ็กต์ก่อนดำเนินการต่อ
library(sf)
# Two objects in different CRS
nc_nad27 <- read_sf(system.file('shape/nc.shp', package = 'sf'))
nc_wgs84 <- st_transform(nc_nad27, 4326)
cat('Same CRS?', st_crs(nc_nad27) == st_crs(nc_wgs84), '\n')
# Always check before join/overlay
align_crs <- function(a, b) {
if (st_crs(a) != st_crs(b)) {
message('CRS mismatch! Transforming b to match a.')
b <- st_transform(b, st_crs(a))
}
list(a = a, b = b)
}
aligned <- align_crs(nc_nad27, nc_wgs84)
cat('After alignment - same CRS?',
st_crs(aligned$a) == st_crs(aligned$b), '\n')การคำนวณระยะทางด้วย CRS
ระยะทางที่คำนวณบน CRS แบบภูมิศาสตร์ (องศา) ไม่มีความหมาย ควรฉายใหม่ไปยัง CRS แบบฉายที่รักษาระยะทางหรือพื้นที่ก่อน หรือใช้การคำนวณระยะทางตามเส้นโค้งโลกในตัวของ sf ซึ่งจัดการ WGS84 ได้อย่างถูกต้องผ่านไลบรารี PROJ
library(sf)
# City points in WGS84
cities <- data.frame(
city = c('Paris', 'Berlin', 'Madrid', 'Rome'),
lon = c(2.350, 13.404, -3.702, 12.496),
lat = c(48.865, 52.520, 40.417, 41.902)
)
cities_sf <- st_as_sf(cities, coords = c('lon', 'lat'), crs = 4326)
# sf computes geodesic distances on WGS84 automatically
dist_matrix <- st_distance(cities_sf)
rownames(dist_matrix) <- cities$city
colnames(dist_matrix) <- cities$city
# Convert to km
dist_km <- round(as.numeric(dist_matrix) / 1000)
mat_km <- matrix(dist_km, 4, 4, dimnames = list(cities$city, cities$city))
print(mat_km)กระบวนการทำงาน CRS แบบครบถ้วน
กระบวนการทำงานแบบครบถ้วนมีดังนี้: โหลดข้อมูล GPS ดิบ กำหนดให้เป็น WGS84 ฉายใหม่ไปยัง CRS เชิงเมตริกเฉพาะพื้นที่เพื่อคำนวณพื้นที่และระยะทางอย่างแม่นยำ ดำเนินการวิเคราะห์ แล้วฉายกลับเป็น WGS84 เพื่อส่งออกสำหรับแผนที่บนเว็บ
library(sf)
# Load and assign CRS
nc <- read_sf(system.file('shape/nc.shp', package = 'sf'))
nc_wgs84 <- st_transform(nc, 4326)
# Re-project to NC State Plane (metres) for accurate areas
nc_proj <- st_transform(nc, 32119)
# Compute area per county in km2
nc_proj$area_km2 <- as.numeric(st_area(nc_proj)) / 1e6
# Top 5 largest counties
top5 <- nc_proj[order(-nc_proj$area_km2), c('NAME', 'area_km2')]
cat('5 Largest NC Counties (km2):\n')
print(head(as.data.frame(top5)[, c('NAME', 'area_km2')], 5))
# Back to WGS84 for output
nc_output <- st_transform(nc_proj, 4326)
cat('\nOutput CRS:', st_crs(nc_output)$epsg, '\n')ตรวจสอบความเข้าใจอย่างรวดเร็ว
คุณต้องคำนวณพื้นที่ของฟีเจอร์รูปหลายเหลี่ยมให้ถูกต้อง (เป็นตารางเมตร) โดยขณะนี้ข้อมูลอยู่ใน WGS84 (EPSG:4326) คุณควรทำอย่างไร
ทบทวน: CRS และการฉาย
ประเด็นสำคัญ:
st_crs(sf_obj)อ่านเมทาดาทาของ CRS โดยควรใช้รหัส EPSG เพื่อระบุ CRS- EPSG:4326 = WGS84 (องศา สำหรับ GPS); EPSG:3857 = Web Mercator (เมตร สำหรับแผนที่บนเว็บ)
st_set_crs()กำหนด CRS โดยไม่แปลงพิกัด ส่วนst_transform()จะฉายพิกัดใหม่- ตรวจสอบความเท่ากันของ CRS ด้วย
st_crs(a) == st_crs(b)ก่อนการดำเนินการเชิงพื้นที่เสมอ - สำหรับระยะทางและพื้นที่ที่แม่นยำ ให้ฉายใหม่ไปยัง CRS แบบฉายในหน่วยเมตริกก่อน
- สตริง Proj4 ยังคงใช้งานได้ แต่รหัส EPSG กระชับและอ่านเข้าใจง่ายกว่า
library(sf)
nc <- read_sf(system.file('shape/nc.shp', package = 'sf'))
# CRS pipeline
cat('Original:', st_crs(nc)$epsg, '\n')
nc_wgs84 <- st_transform(nc, 4326)
nc_proj <- st_transform(nc_wgs84, 32119)
cat('WGS84:', st_crs(nc_wgs84)$epsg, '\n')
cat('Projected:', st_crs(nc_proj)$epsg, '\n')เรียนรู้ R ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 43
- บทเรียน
- 159
คำถามที่พบบ่อย
บทเรียน “ระบบพิกัดอ้างอิงและการฉายแผนที่” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ระบบพิกัดอ้างอิงและการฉายแผนที่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส R Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส R Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ระบบพิกัดอ้างอิงและการฉายแผนที่”
แปลงระหว่าง CRS ด้วย st_crs() และ st_transform() อย่างถูกต้อง คุณปฏิบัติ R Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน R Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน R Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ระบบพิกัดอ้างอิงและการฉายแผนที่” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน R Academy นี้ได้ไหม
ได้ บทเรียน R Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ฟีเจอร์เชิงเรขาคณิตอย่างง่ายด้วยแพ็กเกจ sf
- ระบบพิกัดอ้างอิงและการฉายแผนที่
- การเชื่อมตารางและการดำเนินการเชิงพื้นที่
- แผนที่เชิงโต้ตอบด้วย leaflet