找到R中的直线和三角形之间的交点

时间:2017-04-25 13:26:38

标签: r gps geospatial intersection

我试图找到路径和三角形的交点。输出可以是具有与三角形相交的路径的经度或纬度点集的简单矩阵。这些点可以在图中突出显示。有人可以帮我吗?

# path 01 
longitude <- round(c(48.7188021250007,
                 48.7188133749999,
                 48.7188291249998,
                 48.7188336250004), 8);

Latitude <- round(c (2.39514523661229,
                 2.39512477447308, 
                 2.39472235230961,
                 2.39467460730113), 8);

# path 02
longitude_2 <- round(c(48.71881,
                   48.71882,
                   48.71883,
                   48.71884), 8);

Latitude_2 <- round(c (2.39479,
                   2.3947,
                   2.39469396980779,
                   2.3945), 8);

plot(longitude,Latitude,ylim=range(c(Latitude,Latitude_2)),
xlim=range(c(longitude,longitude_2)), type="l",col="blue")
lines(longitude_2,Latitude_2,col="darkgreen")
title(main = "Two different paths");
legend("right", legend=c("path A", "path B"),
   col=c("blue", "darkgreen"), lty=1:1, cex=0.8, 
   box.lty=0)


# Drawing polygon 

lon <- c(48.71882,48.71883, 48.71884);
lat <- c (2.3945, 2.39478,  2.39479);
x <- cbind(lon, lat)
polygon(x, col='lightblue')

plot

1 个答案:

答案 0 :(得分:1)

我认为您可以在创建SpatialLines和SpatialPolygons后使用rgeos包中的gIntersection函数。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class='online' id='id_1'>Btn 1</button>
<button class='online' id='id_2'>Btn 2</button>
<button class='online' id='id_3'>Btn 3</button>

或者,如果您想要使用正确缩放的相同绘图,您可以使用以下方法重新设计:

require(sp)
require(rgeos)
longitude <- round(c(48.7188021250007,
                     48.7188133749999,
                     48.7188291249998,
                     48.7188336250004), 8);

Latitude <- round(c (2.39514523661229,
                     2.39512477447308, 
                     2.39472235230961,
                     2.39467460730113), 8);

longitude_2 <- round(c(48.71881,
                       48.71882,
                       48.71883,
                       48.71884), 8);

Latitude_2 <- round(c (2.39479,
                       2.3947,
                       2.39469396980779,
                       2.3945), 8);

lon <- c(48.71882,48.71883, 48.71884);
lat <- c (2.3945, 2.39478,  2.39479);


lines <- SpatialLines(list(Lines(list(Line(cbind(longitude, Latitude)),
                                      Line(cbind(longitude_2, Latitude_2))), 1)))

pol_1 <- SpatialPolygons(list(Polygons(list(Polygon(coords = cbind(lon, lat))),1)))

result <- gIntersection(lines, pol_1)

plot(lines, col = "red")
plot(pol_1, col = "blue", add = T)
plot(result, col = "green", add = T)