【问题标题】:Drawing a Circle with a Radius of a Defined Distance in a Map在地图中绘制具有定义距离半径的圆
【发布时间】:2014-04-14 21:46:30
【问题描述】:

我能够绘制地图并为特定点添加标题:

library(maps)
map("state")
text(-80.83,35.19,"Charlotte",cex=.6)

我还可以画一个以该点为中心的圆:

symbols(-80.83,35.19,circles=2, add=TRUE)

但是,我想控制圆的大小。特别是,我想在 data.frame、matrix 或 list 中包含的多个位置周围绘制一个半径为 100 英里的圆。

【问题讨论】:

    标签: r geometry maps geographic-distance


    【解决方案1】:

    Gary 的命题很好地适用于平面地图,但不能应用于“maps”包生成的地图,因为它不关心用于绘制地图的投影。严格如此应用,它会导致绘制一个椭圆(见下文),因为圆半径的单位是度,而不是公里或英里。但是纬度和经度的度数并不对应相同的物理距离。要围绕一个点绘制一个圆或接近圆的东西,其半径是以英里或公里为单位的恒定距离,您需要计算关于投影的校正坐标。使用您的功能并根据http://www.movable-type.co.uk 上的 Chris Veness 解释对其进行调整,您的功能变为:

    library(maps)
    library(mapdata)#For the worldHires database
    library(mapproj)#For the mapproject function
    plotElipse <- function(x, y, r) {#Gary's function ;-)
       angles <- seq(0,2*pi,length.out=360)
       lines(r*cos(angles)+x,r*sin(angles)+y)
    }
    plotCircle <- function(LonDec, LatDec, Km) {#Corrected function
        #LatDec = latitude in decimal degrees of the center of the circle
        #LonDec = longitude in decimal degrees
        #Km = radius of the circle in kilometers
        ER <- 6371 #Mean Earth radius in kilometers. Change this to 3959 and you will have your function working in miles.
        AngDeg <- seq(1:360) #angles in degrees 
        Lat1Rad <- LatDec*(pi/180)#Latitude of the center of the circle in radians
        Lon1Rad <- LonDec*(pi/180)#Longitude of the center of the circle in radians
        AngRad <- AngDeg*(pi/180)#angles in radians
        Lat2Rad <-asin(sin(Lat1Rad)*cos(Km/ER)+cos(Lat1Rad)*sin(Km/ER)*cos(AngRad)) #Latitude of each point of the circle rearding to angle in radians
        Lon2Rad <- Lon1Rad+atan2(sin(AngRad)*sin(Km/ER)*cos(Lat1Rad),cos(Km/ER)-sin(Lat1Rad)*sin(Lat2Rad))#Longitude of each point of the circle rearding to angle in radians
        Lat2Deg <- Lat2Rad*(180/pi)#Latitude of each point of the circle rearding to angle in degrees (conversion of radians to degrees deg = rad*(180/pi) )
        Lon2Deg <- Lon2Rad*(180/pi)#Longitude of each point of the circle rearding to angle in degrees (conversion of radians to degrees deg = rad*(180/pi) )
        polygon(Lon2Deg,Lat2Deg,lty=2)
    }
    map("worldHires", region="belgium")#draw a map of Belgium (yes i am Belgian ;-)
    bruxelles <- mapproject(4.330,50.830)#coordinates of Bruxelles
    points(bruxelles,pch=20,col='blue',cex=2)#draw a blue dot for Bruxelles
    plotCircle(4.330,50.830,50)#Plot a dashed circle of 50 km arround Bruxelles 
    plotElipse(4.330,50.830,0.5)#Tries to plot a plain circle of 50 km arround Bruxelles, but drawn an ellipse
    

    (对不起,我的“声誉”不允许我发布图片 ;-)。编辑:添加了您的图片。

    我希望这会有所帮助。 格雷戈尔

    【讨论】:

      【解决方案2】:

      您可以编写一个函数来自定义您希望圆圈的外观。例如:

      plotCircle <- function(x, y, r) {
        angles <- seq(0,2*pi,length.out=360)
        lines(r*cos(angles)+x,r*sin(angles)+y)
      }
      

      那么如果你在数据框中有一组坐标:

      coords <- data.frame(x = c(-1,0,1), y = c(-1, 0.5, 1))
      

      您可以从一些初始图(地图或空图等)开​​始

      plot(1,type='n',xlim=c(-2,2),ylim=c(-2,2))
      

      然后在坐标列表上调用绘图函数:

      apply(coords,1,function(df) plotCircle(df[1],df[2],.3))
      

      【讨论】:

      • 如何用颜色填充圆圈?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-20
      相关资源
      最近更新 更多