-
Notifications
You must be signed in to change notification settings - Fork 0
/
地图数据设计.R
executable file
·130 lines (107 loc) · 3.5 KB
/
地图数据设计.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
library(tidyverse)
library(sf)
albers_proj = "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"
read_sf("gz_2010_us_050_00_20m/gz_2010_us_050_00_20m.shp") %>%
st_transform(albers_proj) -> counties
read_sf("gz_2010_us_040_00_20m/gz_2010_us_040_00_20m.shp") %>%
st_transform(albers_proj) -> states
usmapdata::us_map("states") %>%
st_transform(albers_proj) -> statesmap
usmapdata::us_map("counties") %>%
st_transform(albers_proj) -> countiesmap
# 旋转函数
rotation = function(a){
r = a * pi / 180 # 角度转弧度
matrix(c(cos(r), sin(r), -sin(r), cos(r)),
nrow = 2, ncol = 2)
}
# 变换阿拉斯加
states %>%
filter(STATE == "02") %>%
mutate(geometry = geometry * rotation(-50) / 2 + c(300000, -2000000)) %>%
st_set_crs(albers_proj) -> alaska
# 变换夏威夷
states %>%
filter(STATE == "15") %>%
mutate(geometry = geometry * rotation(-35) + c(3600000, 1800000)) %>%
st_set_crs(albers_proj) -> Hawaii
ggplot(statesmap) +
geom_sf() +
geom_sf(data = alaska, color = "red") +
geom_sf(data = Hawaii, color = "red")
# 保存为 geojson 文件
bind_rows(
subset(states, !STATE %in% c("15", "02")),
alaska, Hawaii
) -> newstates
# newstates %>%
# select(fips = STATE) %>%
# st_transform(4326) %>%
# write_sf("newstates.geojson")
# 分隔线
st_linestring(matrix(c(-121.201171875, 32.76880048488168,
-98.7890625, 25.958044673317843),
nrow = 2, byrow = T)) %>%
st_sfc() %>%
st_sf() %>%
st_set_crs(4326) %>%
st_transform(albers_proj) -> line1
st_linestring(matrix(c(-111.5523, 30.62574,
-106.3916015625, 22.63429269379353),
nrow = 2, byrow = T)) %>%
st_sfc() %>%
st_sf() %>%
st_set_crs(4326) %>%
st_transform(albers_proj) -> line2
ggplot(statesmap) +
geom_sf() +
geom_sf(data = alaska, color = "red") +
geom_sf(data = Hawaii, color = "red") +
geom_sf(data = line1, color = "green") +
geom_sf(data = line2, color = "green")
line1 %>% st_intersection(line2) %>%
st_transform(4326)
st_combine(c(line1$geometry, line2$geometry)) -> separateline
separateline %>%
write_rds("separateline.rds")
# 保存 shp 数据
newstates %>%
select(fips = STATE) %>%
left_join(st_drop_geometry(statesmap)) %>%
select(colnames(st_drop_geometry(statesmap)), everything()) -> newstates
dir.create("us_states")
newstates %>%
write_sf("us_states/us_states.shp")
dir.create("separate_line")
separateline %>%
write_sf("separate_line/separate_line.shp")
# county map
# 变换阿拉斯加
counties %>%
filter(STATE == "02") %>%
mutate(geometry = geometry * rotation(-50) / 2 + c(300000, -2000000)) %>%
st_set_crs(albers_proj) -> alaska_county
# 变换夏威夷
counties %>%
filter(STATE == "15") %>%
mutate(geometry = geometry * rotation(-35) + c(3600000, 1800000)) %>%
st_set_crs(albers_proj) -> Hawaii_county
ggplot(countiesmap) +
geom_sf() +
geom_sf(data = alaska_county, color = "red") +
geom_sf(data = Hawaii_county, color = "red")
# 保存为 geojson 文件
bind_rows(
subset(counties, !STATE %in% c("15", "02")),
alaska_county, Hawaii_county
) -> newcounties
newcounties %>%
mutate(fips = paste0(STATE, COUNTY)) %>%
select(fips) %>%
left_join(st_drop_geometry(countiesmap)) %>%
select(colnames(st_drop_geometry(countiesmap)),
everything()) -> newcounties
# 保存 shp 数据
dir.create("us_counties")
newcounties %>%
write_sf("us_counties/us_counties.shp")