点不在 shapefile 上绘制
Point not plotting over a shapefile
我正在尝试用波兰的 Powiaty(县)和克拉科夫所在的点绘制一个 shapefile。这是我正在使用的代码。由于某种原因,该点没有出现在图中。
这是我获得波兰 Powiaty 的 shapefile 的地方:https://www.gis-support.pl/downloads/Powiaty.zip?_ga=2.76556755.796824239.1647028537-1312541903.1646793893
library(sf)
# Load shapefile
poland_Powiaty <- st_read("~/Powiaty.shp")
### Krakow's coordinates
krakow <- data.frame(x=19.940000819941382,
y=50.06197893970872)
point <- st_as_sf(krakow, coords = c("x", "y"))
# Set the CRS of shapefile
st_crs(point) <- "+proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80
+towgs84=0,0,0,0,0,0,0 +units=m +no_defs " # assign CRS to point
plot(poland_Powiaty[2])
plot(point,col="black",pch=19, cex=3,add=T)
有谁知道我怎样才能将克拉科夫添加到地图中?
你很接近,但 crs 似乎是问题所在。
*编辑为完整的代表:
library(sf)
library(dplyr) #for the pipe %>%
library(ggplot2)
# data from: https://gis-support.pl/baza-wiedzy-2/dane-do-pobrania/granice-administracyjne/
# file downloaded: https://www.gis-support.pl/downloads/Powiaty.zip
# read shapefile, retain only geometry column
poland_Powiaty <- read_sf('Powiaty.shp') %>% #change filename as needed
st_geometry()
# In latitude/longitude
krakow <- data.frame(x=19.940000819941382,
y=50.06197893970872) %>%
st_as_sf(coords = c('x', 'y'))
# set crs to 4326 for lat/lon
krakow <- st_set_crs(krakow, 4326)
# transform krakow to have same crs as poland_Powiaty
krakow <- st_transform(krakow, st_crs(poland_Powiaty))
# base plot
plot(poland_Powiaty)
plot(krakow, add = T, pch = 15, col = 'red')
## ggplot2
ggplot() +
geom_sf(data = poland_Powiaty) +
geom_sf(data = krakow, color = 'red', size = 3)
由 reprex package (v0.3.0)
创建于 2022-03-11
我正在尝试用波兰的 Powiaty(县)和克拉科夫所在的点绘制一个 shapefile。这是我正在使用的代码。由于某种原因,该点没有出现在图中。
这是我获得波兰 Powiaty 的 shapefile 的地方:https://www.gis-support.pl/downloads/Powiaty.zip?_ga=2.76556755.796824239.1647028537-1312541903.1646793893
library(sf)
# Load shapefile
poland_Powiaty <- st_read("~/Powiaty.shp")
### Krakow's coordinates
krakow <- data.frame(x=19.940000819941382,
y=50.06197893970872)
point <- st_as_sf(krakow, coords = c("x", "y"))
# Set the CRS of shapefile
st_crs(point) <- "+proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80
+towgs84=0,0,0,0,0,0,0 +units=m +no_defs " # assign CRS to point
plot(poland_Powiaty[2])
plot(point,col="black",pch=19, cex=3,add=T)
有谁知道我怎样才能将克拉科夫添加到地图中?
你很接近,但 crs 似乎是问题所在。
*编辑为完整的代表:
library(sf)
library(dplyr) #for the pipe %>%
library(ggplot2)
# data from: https://gis-support.pl/baza-wiedzy-2/dane-do-pobrania/granice-administracyjne/
# file downloaded: https://www.gis-support.pl/downloads/Powiaty.zip
# read shapefile, retain only geometry column
poland_Powiaty <- read_sf('Powiaty.shp') %>% #change filename as needed
st_geometry()
# In latitude/longitude
krakow <- data.frame(x=19.940000819941382,
y=50.06197893970872) %>%
st_as_sf(coords = c('x', 'y'))
# set crs to 4326 for lat/lon
krakow <- st_set_crs(krakow, 4326)
# transform krakow to have same crs as poland_Powiaty
krakow <- st_transform(krakow, st_crs(poland_Powiaty))
# base plot
plot(poland_Powiaty)
plot(krakow, add = T, pch = 15, col = 'red')
## ggplot2
ggplot() +
geom_sf(data = poland_Powiaty) +
geom_sf(data = krakow, color = 'red', size = 3)
由 reprex package (v0.3.0)
创建于 2022-03-11