在r/python/gis中,如何在地图中覆盖多个lat/long坐标,并创建一个变量,指示坐标是否叠加匹配
我有两个带有坐标的数据框。
- 第一个数据帧包含无线电发射器的LAT和长期均匀的无线电波坐标。
- 第二个包含纬度和长期选举区。
我正在尝试i)在BR地图中绘制两个数据范围,以使无线电波覆盖选举区域,ii)合并数据框并创建一个额外的列(coved_by_by_radio_wave
),表示LAT和LONG选举区被无线电波覆盖(因此躺在其中)。 R中的代码:
df_1 <- data.frame(Radio_id=c(13,22),
frequency=c(76.1, 84.1),
transmitter_latitude=c(-8.060833,-23.552222),
transmitter_longitude=c(-34.88111,-46.67528),
radio_name=c('A','B'),
radio_wave=c("-7.312 -34.7498, -7.3233, -34.6224,-7.3571, -34.5029,-7.4123, -34.3949,-7.4872, -34.3017,-7.5795, -34.2261,-7.6864, -34.1704,-7.8047, -34.1363,-7.9308, -34.1248,-8.0608, -34.1363,-8.1909 -34.1704,-8.317, -34.2261,-8.4353, -34.3017,-8.5422, -34.3949,-8.6345, -34.5029,-8.7094, -34.6224,-8.7645, -34.7498,-8.7983, -34.8811,-8.8097, -35.0124,-8.7983, -35.1398,-8.7645 -35.2593,-8.7094, -35.3673,-8.6345, -35.4605,-8.5422, -35.5361,-8.4353, -35.5918,-8.317, -35.626,-8.1909, -35.6375,-8.0608, -35.626,-7.9308, -35.5918,-7.8047, -35.5361,-7.6864, -35.4605,-7.5795, -35.3673,-7.4872, -35.2593,-7.4123, -35.1398,-7.3571, -35.0124,-7.3233, -34.8811",
"-22.9276 -46.5569,-22.937 -46.4422,-22.9652 -46.3346,-23.0112 -46.2373,-23.0737 -46.1533,-23.1507 -46.0851,-23.2399 -46.0349,-23.3386 -46.0042,-23.4438 -45.9939,-23.5522 -46.0042,-23.6607 -46.0349,-23.7659 -46.0851,-23.8646 -46.1533,-23.9537 -46.2373,-24.0307 -46.3346,-24.0932 -46.4422,-24.1392 -46.5569,-24.1674 -46.6753,-24.1769 -46.7936,-24.1674 -46.9083,-24.1392 -47.016,-24.0932 -47.1133,-24.0307 -47.1973,-23.9537 -47.2654,-23.8646 -47.3156,-23.7659 -47.3464,-23.6607 -47.3567,-23.5522 -47.3464,-23.4438 -47.3156,-23.3386 -47.2654,-23.2399 -47.1973,-23.1507 -47.1133,-23.0737 -47.016,-23.0112 -46.9083,-22.9652 -46.7936,-22.937 -46.6753"),
radio_show=c("News","Local hits"))
df_2<- data.frame(election_section=c(13,19),
municipality=c("Abadia de goias", "Agua boa"),
state=c("GO","MT"),
result=c("elected","not elected"),
latitude=c(-18.490394, -23.56),
longitude=c(-47.40096, -46.69))
df_merged_desired<-data.frame(Radio_id=c(13,22),
frequency=c(76.1, 84.1),
transmitter_latitude=c(-8.060833,-23.552222),
transmitter_longitude=c(-34.88111,-46.67528),
radio_name=c('A','B'),
radio_wave=c("-7.312 -34.7498, -7.3233, -34.6224,-7.3571, -34.5029,-7.4123, -34.3949,-7.4872, -34.3017,-7.5795, -34.2261,-7.6864, -34.1704,-7.8047, -34.1363,-7.9308, -34.1248,-8.0608, -34.1363,-8.1909 -34.1704,-8.317, -34.2261,-8.4353, -34.3017,-8.5422, -34.3949,-8.6345, -34.5029,-8.7094, -34.6224,-8.7645, -34.7498,-8.7983, -34.8811,-8.8097, -35.0124,-8.7983, -35.1398,-8.7645 -35.2593,-8.7094, -35.3673,-8.6345, -35.4605,-8.5422, -35.5361,-8.4353, -35.5918,-8.317, -35.626,-8.1909, -35.6375,-8.0608, -35.626,-7.9308, -35.5918,-7.8047, -35.5361,-7.6864, -35.4605,-7.5795, -35.3673,-7.4872, -35.2593,-7.4123, -35.1398,-7.3571, -35.0124,-7.3233, -34.8811",
"-22.9276 -46.5569,-22.937 -46.4422,-22.9652 -46.3346,-23.0112 -46.2373,-23.0737 -46.1533,-23.1507 -46.0851,-23.2399 -46.0349,-23.3386 -46.0042,-23.4438 -45.9939,-23.5522 -46.0042,-23.6607 -46.0349,-23.7659 -46.0851,-23.8646 -46.1533,-23.9537 -46.2373,-24.0307 -46.3346,-24.0932 -46.4422,-24.1392 -46.5569,-24.1674 -46.6753,-24.1769 -46.7936,-24.1674 -46.9083,-24.1392 -47.016,-24.0932 -47.1133,-24.0307 -47.1973,-23.9537 -47.2654,-23.8646 -47.3156,-23.7659 -47.3464,-23.6607 -47.3567,-23.5522 -47.3464,-23.4438 -47.3156,-23.3386 -47.2654,-23.2399 -47.1973,-23.1507 -47.1133,-23.0737 -47.016,-23.0112 -46.9083,-22.9652 -46.7936,-22.937 -46.6753"),
radio_show=c("News","Local hits"),
election_section=c(13,22),
municipality=c("Abadia de goias", "Agua boa"),
state=c("GO","MT"),
result=c("elected","not elected"),
latitude=c(-18.490394, -23.56),
longitude=c(-47.40096, -46.69),
covered_by_radio_wave=c("No","Yes"))
使用GGPLOT
我只能添加一组LAT和我的一个数据范围之一。
library(geobr)
library(ggplot2)
mun <- read_municipality(code_muni="all", year=2010)
ggplot() +
geom_sf(data=mun, fill = NA) + scale_fill_gradientn(colours= brewer.pal(2, "RdYlGn"))+
geom_point(data = df_1, mapping = aes(x = transmitter_longitude, y = transmitter_latitude, colour = factor(radio_show)), size = 1) +
coord_sf()+
theme(panel.grid.major = element_blank(), panel.background = element_blank(), panel.grid.minor = element_blank())
参考 geobr
I have two dataframes with coordinates.
- The first dataframe contains radio transmitters' lat and long as well as their respective radio waves coordinates.
- The second contains lat and long of elections precincts.
I am trying to i) plot both dataframes in a BR map such that the radio waves overlay the elections precincts and ii) merge the dataframes and create an extra column (covered_by_radio_wave
) indicating if the lat and long of the elections precincts are covered (thus lay within) by the radio waves.
Code in R:
df_1 <- data.frame(Radio_id=c(13,22),
frequency=c(76.1, 84.1),
transmitter_latitude=c(-8.060833,-23.552222),
transmitter_longitude=c(-34.88111,-46.67528),
radio_name=c('A','B'),
radio_wave=c("-7.312 -34.7498, -7.3233, -34.6224,-7.3571, -34.5029,-7.4123, -34.3949,-7.4872, -34.3017,-7.5795, -34.2261,-7.6864, -34.1704,-7.8047, -34.1363,-7.9308, -34.1248,-8.0608, -34.1363,-8.1909 -34.1704,-8.317, -34.2261,-8.4353, -34.3017,-8.5422, -34.3949,-8.6345, -34.5029,-8.7094, -34.6224,-8.7645, -34.7498,-8.7983, -34.8811,-8.8097, -35.0124,-8.7983, -35.1398,-8.7645 -35.2593,-8.7094, -35.3673,-8.6345, -35.4605,-8.5422, -35.5361,-8.4353, -35.5918,-8.317, -35.626,-8.1909, -35.6375,-8.0608, -35.626,-7.9308, -35.5918,-7.8047, -35.5361,-7.6864, -35.4605,-7.5795, -35.3673,-7.4872, -35.2593,-7.4123, -35.1398,-7.3571, -35.0124,-7.3233, -34.8811",
"-22.9276 -46.5569,-22.937 -46.4422,-22.9652 -46.3346,-23.0112 -46.2373,-23.0737 -46.1533,-23.1507 -46.0851,-23.2399 -46.0349,-23.3386 -46.0042,-23.4438 -45.9939,-23.5522 -46.0042,-23.6607 -46.0349,-23.7659 -46.0851,-23.8646 -46.1533,-23.9537 -46.2373,-24.0307 -46.3346,-24.0932 -46.4422,-24.1392 -46.5569,-24.1674 -46.6753,-24.1769 -46.7936,-24.1674 -46.9083,-24.1392 -47.016,-24.0932 -47.1133,-24.0307 -47.1973,-23.9537 -47.2654,-23.8646 -47.3156,-23.7659 -47.3464,-23.6607 -47.3567,-23.5522 -47.3464,-23.4438 -47.3156,-23.3386 -47.2654,-23.2399 -47.1973,-23.1507 -47.1133,-23.0737 -47.016,-23.0112 -46.9083,-22.9652 -46.7936,-22.937 -46.6753"),
radio_show=c("News","Local hits"))
df_2<- data.frame(election_section=c(13,19),
municipality=c("Abadia de goias", "Agua boa"),
state=c("GO","MT"),
result=c("elected","not elected"),
latitude=c(-18.490394, -23.56),
longitude=c(-47.40096, -46.69))
df_merged_desired<-data.frame(Radio_id=c(13,22),
frequency=c(76.1, 84.1),
transmitter_latitude=c(-8.060833,-23.552222),
transmitter_longitude=c(-34.88111,-46.67528),
radio_name=c('A','B'),
radio_wave=c("-7.312 -34.7498, -7.3233, -34.6224,-7.3571, -34.5029,-7.4123, -34.3949,-7.4872, -34.3017,-7.5795, -34.2261,-7.6864, -34.1704,-7.8047, -34.1363,-7.9308, -34.1248,-8.0608, -34.1363,-8.1909 -34.1704,-8.317, -34.2261,-8.4353, -34.3017,-8.5422, -34.3949,-8.6345, -34.5029,-8.7094, -34.6224,-8.7645, -34.7498,-8.7983, -34.8811,-8.8097, -35.0124,-8.7983, -35.1398,-8.7645 -35.2593,-8.7094, -35.3673,-8.6345, -35.4605,-8.5422, -35.5361,-8.4353, -35.5918,-8.317, -35.626,-8.1909, -35.6375,-8.0608, -35.626,-7.9308, -35.5918,-7.8047, -35.5361,-7.6864, -35.4605,-7.5795, -35.3673,-7.4872, -35.2593,-7.4123, -35.1398,-7.3571, -35.0124,-7.3233, -34.8811",
"-22.9276 -46.5569,-22.937 -46.4422,-22.9652 -46.3346,-23.0112 -46.2373,-23.0737 -46.1533,-23.1507 -46.0851,-23.2399 -46.0349,-23.3386 -46.0042,-23.4438 -45.9939,-23.5522 -46.0042,-23.6607 -46.0349,-23.7659 -46.0851,-23.8646 -46.1533,-23.9537 -46.2373,-24.0307 -46.3346,-24.0932 -46.4422,-24.1392 -46.5569,-24.1674 -46.6753,-24.1769 -46.7936,-24.1674 -46.9083,-24.1392 -47.016,-24.0932 -47.1133,-24.0307 -47.1973,-23.9537 -47.2654,-23.8646 -47.3156,-23.7659 -47.3464,-23.6607 -47.3567,-23.5522 -47.3464,-23.4438 -47.3156,-23.3386 -47.2654,-23.2399 -47.1973,-23.1507 -47.1133,-23.0737 -47.016,-23.0112 -46.9083,-22.9652 -46.7936,-22.937 -46.6753"),
radio_show=c("News","Local hits"),
election_section=c(13,22),
municipality=c("Abadia de goias", "Agua boa"),
state=c("GO","MT"),
result=c("elected","not elected"),
latitude=c(-18.490394, -23.56),
longitude=c(-47.40096, -46.69),
covered_by_radio_wave=c("No","Yes"))
Using ggplot
I could only add one set of lat and long of one of my dataframes.
library(geobr)
library(ggplot2)
mun <- read_municipality(code_muni="all", year=2010)
ggplot() +
geom_sf(data=mun, fill = NA) + scale_fill_gradientn(colours= brewer.pal(2, "RdYlGn"))+
geom_point(data = df_1, mapping = aes(x = transmitter_longitude, y = transmitter_latitude, colour = factor(radio_show)), size = 1) +
coord_sf()+
theme(panel.grid.major = element_blank(), panel.background = element_blank(), panel.grid.minor = element_blank())
reference geobr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,对于您的第一部分:
i)在BR地图中绘制两个数据框,以使无线电波覆盖选举区
您可以将
radio> radio_wave
坐标转换到多边形(>) SF
)。这有点棘手,因为坐标有点混乱,但是我设法使用自定义功能来完成i)提取坐标ii)使用适当的顺序(LON/LAT)它。请参阅:对于第二个问题 ii)合并数据框并创建一个额外的列(
coveed_by_radio_wave
),指示是否涵盖了选举区域的LAT和LONG RECTINCS /em>,您可以使用st_coverred_by()
创建covee_by_radio_wave
。我注意到您想要的最终数据框架是通过添加
df_1
和df_2
的Cols产生的,因此我对此进行了复制,但请注意,这不是两个数据集的加入。尽管如此,我还是为您制作了。检查如何完成:在2022-06-14创建的 reprex package (v2)。 0.1)
So for your first part:
i) plot both dataframes in a BR map such that the radio waves overlay the elections precincts
You can convert
radio_wave
coordinates onto a polygon (sf
). This is a bit tricky since the coordinates are a bit messy, but I managed to do it with a custom function that i) extract coordinates ii) use the proper order (lon/lat) iii) creating first a linestring and the convex hull of it. See:For the second question ii) merge the dataframes and create an extra column (
covered_by_radio_wave
) indicating if the lat and long of the elections precincts are covered, you can createcovered_by_radio_wave
usingst_coverred_by()
.I noticed that the final data frame that you want is produced by appending cols of
df_1
anddf_2
, so I replicated that, but note that is not a join of the two datasets. Nonetheless, I produced it for you. Check how it can be done:Created on 2022-06-14 by the reprex package (v2.0.1)