You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R使用tmap绘图仅显示泉水点位无基督城区域,如何添加兴趣面?

问题原因

你没有将基督城的行政面图层添加到绘图序列中,该问题并非地理编码错误导致,只需按以下步骤调整代码即可。
首先要注意图层投影统一:你通过geocode_OSM得到的泉水点位投影为EPSG:4326,而原本读取的行政边界TA图层投影为EPSG:2193,需要先统一投影避免空间错位。

修改后代码

# 原有加载包部分无需改动
library(sf)
library(tmap)
library(tidyverse)
library(rmapshaper)
library(dplyr)
library(units)
library(terra) 
library(rmapshaper)
library(tidyr)
library(tmaptools)

# 读取泉水点位数据
springs <- st_read("Spring_Locations.geojson")

# 读取行政边界数据
TA <- st_read("territorial-authority-2021-generalised.gpkg")

# 转换泉水点位投影
springs <- springs %>% st_transform(crs = "EPSG:2193")

# 筛选保留需要的字段并重命名
TA <- TA %>% select(TA_code = TA2021_V1_00, TA_name = TA2021_V1_00_NAME, land_area_sq_km = LAND_AREA_SQ_KM,
                    area_sq_km = AREA_SQ_KM, shape_length = Shape_Length, geometry = geom)

# 筛选基督城范围内的泉水点位
springs_chch <- springs %>%
  st_filter(TA %>% filter(TA_name == "Christchurch City"))

# 地理编码
springs_chch_loc2 <- geocode_OSM(springs_chch$ROAD_OR_STREET, crs = 4326, as.sf = TRUE)

# ----- 新增部分:提取基督城行政面并统一投影 -----
chch_boundary <- TA %>% 
  filter(TA_name == "Christchurch City") %>% 
  st_transform(crs = 4326)

# ----- 调整绘图逻辑:先加面图层,再加点位图层 -----
tm_shape(chch_boundary, bbox = "Christchurch") +
  # 绘制基督城行政面,可自定义填充色、边框样式
  tm_polygons(col = "white", border.col = "gray40", lwd = 1) +
  # 添加泉水点位图层
  tm_shape(springs_chch_loc2) +
  tm_dots(col = "red",
          title = "基督城泉水点位",
          alpha = 1) +
  tm_scale_bar(position = c("right", "bottom"), 
             breaks = c(0,1,2)) +
  tm_credits("数据来源:坎特伯雷地区委员会,2016", 
           position=c("right", "bottom"), 
           size = 0.5) +
  tm_layout(bg.color = "lightcyan", 
            frame = T, 
            legend.position = c("left","top"),
            title = "基督城泉水点位分布", 
            title.size = 1,
            title.fontface = "bold",
            legend.title.size = 0.8,
            legend.text.size = 0.6)

可选优化

如果需要更详细的街道底图,可以在绘图代码最开头添加OSM底图图层:

# 需先将tmap切换为交互模式,静态模式加载底图需提前下载瓦片
tmap_mode("view")
tm_basemap("OpenStreetMap.Mapnik") +
  # 后续接上面的行政面、点位图层代码即可

你原来的输出效果:
效果展示图

内容的提问来源于stack exchange,提问作者scumbagsurfer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 13:18:05