Library used to execute:
library(ggvis)
library(shapes)
##
## Attaching package: 'shapes'
## The following object is masked from 'package:stats':
##
## sigma
library(matlib)
library(rgeos)
## rgeos version: 0.3-23, (SVN revision 546)
## GEOS runtime version: 3.5.0-CAPI-1.9.0 r4084
## Linking to sp version: 1.2-4
## Polygon checking: TRUE
library(plyr)
library(optR)
## Loaded optR Version: 1.2.5
Input data (3 different wings):
setwd("C:/Users/Admin/Documents/Intern/practice")
data <- read.csv("MyData.csv")
data1 <- data.matrix(data[,c("ID","X","Y","isBoundary","isLandmark")])
head(data)
## X Y IMAGE ID Scale isBoundary isLandmark
## 1 193 1053 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 2 580 1127 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 3 1173 1229 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 4 1619 1285 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 5 1697 1278 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
## 6 2106 1229 F_ST03_07.21.11_LT_02.tif 4 0.001099 1 0
Caculate distance from one point to centroid: 1. Centroid is calculated by the average of the sum of all the boundary points:
#Centroid
centroids <- aggregate(data[, 1:2], list(data$ID, data$isBoundary ), mean)
centroid1 <- centroids[4:6,c("Group.1","X","Y")]
centroid <- rename(centroid1,c("Group.1"="ID","X"="X-centroid","Y"="Y-centroid"))
data3 <- merge(data1,centroid, by="ID")
head(data3)
## ID X Y isBoundary isLandmark X-centroid Y-centroid
## 1 4 193 1053 1 0 1489.556 930.6111
## 2 4 580 1127 1 0 1489.556 930.6111
## 3 4 1173 1229 1 0 1489.556 930.6111
## 4 4 1619 1285 1 0 1489.556 930.6111
## 5 4 1697 1278 1 0 1489.556 930.6111
## 6 4 2106 1229 1 0 1489.556 930.6111
#Distance
a = data.frame()
for (i in 1:length(data3[,1])){
#print(i)
distance <- sqrt((data3[i,2] - data3[i,6])^2 + (data3[i,3] -data3[i,7])^2)
a <- rbind(a, distance)
#print(distance)
}
colnames(a) <- c("Distance")
X <- cbind(data3,a)
d1 <- X[,c("ID","X","Y","X-centroid","Y-centroid","Distance","isBoundary","isLandmark")]
head(d1)
## ID X Y X-centroid Y-centroid Distance isBoundary isLandmark
## 1 4 193 1053 1489.556 930.6111 1302.3192 1 0
## 2 4 580 1127 1489.556 930.6111 930.5159 1 0
## 3 4 1173 1229 1489.556 930.6111 435.0211 1 0
## 4 4 1619 1285 1489.556 930.6111 377.2895 1 0
## 5 4 1697 1278 1489.556 930.6111 404.6137 1 0
## 6 4 2106 1229 1489.556 930.6111 684.8647 1 0
Distance from one point to the boundary:
#Split data
data_split <- split(d1, d1$ID)
data_split
## $`4`
## ID X Y X-centroid Y-centroid Distance isBoundary isLandmark
## 1 4 193 1053 1489.556 930.6111 1302.31922 1 0
## 2 4 580 1127 1489.556 930.6111 930.51593 1 0
## 3 4 1173 1229 1489.556 930.6111 435.02109 1 0
## 4 4 1619 1285 1489.556 930.6111 377.28948 1 0
## 5 4 1697 1278 1489.556 930.6111 404.61369 1 0
## 6 4 2106 1229 1489.556 930.6111 684.86472 1 0
## 7 4 2301 1109 1489.556 930.6111 830.82169 1 1
## 8 4 2378 1025 1489.556 930.6111 893.44434 1 1
## 9 4 2417 924 1489.556 930.6111 927.46801 1 0
## 10 4 2323 832 1489.556 930.6111 839.25788 1 1
## 11 4 2168 720 1489.556 930.6111 710.38293 1 1
## 12 4 1963 620 1489.556 930.6111 566.24103 1 1
## 13 4 1775 558 1489.556 930.6111 469.37999 1 1
## 14 4 1734 550 1489.556 930.6111 452.34711 1 1
## 15 4 1400 527 1489.556 930.6111 413.42729 1 1
## 16 4 618 775 1489.556 930.6111 885.33830 1 1
## 17 4 295 898 1489.556 930.6111 1195.00061 1 0
## 18 4 72 1012 1489.556 930.6111 1419.89010 1 0
## 19 4 510 1021 1489.556 930.6111 983.71705 0 0
## 20 4 520 988 1489.556 930.6111 971.25252 0 0
## 21 4 600 961 1489.556 930.6111 890.07447 0 0
## 22 4 571 918 1489.556 930.6111 918.64212 0 0
## 23 4 584 881 1489.556 930.6111 906.91352 0 0
## 24 4 573 841 1489.556 930.6111 920.92575 0 0
## 25 4 539 838 1489.556 930.6111 955.05638 0 0
## 26 4 547 814 1489.556 930.6111 949.74161 0 0
## 27 4 835 906 1489.556 930.6111 655.01808 0 1
## 28 4 876 941 1489.556 930.6111 613.64350 0 1
## 29 4 878 978 1489.556 930.6111 613.38887 0 1
## 30 4 784 1086 1489.556 930.6111 722.46408 0 1
## 31 4 1224 1041 1489.556 930.6111 287.58557 0 1
## 32 4 1241 845 1489.556 930.6111 262.88615 0 1
## 33 4 1419 575 1489.556 930.6111 362.54289 0 0
## 34 4 1455 701 1489.556 930.6111 232.19679 0 0
## 35 4 1529 935 1489.556 930.6111 39.68786 0 0
## 36 4 1560 939 1489.556 930.6111 70.94218 0 0
## 37 4 1496 1133 1489.556 930.6111 202.49146 0 1
## 38 4 1570 1111 1489.556 930.6111 197.51319 0 0
## 39 4 1736 1201 1489.556 930.6111 365.84835 0 0
## 40 4 1640 1238 1489.556 930.6111 342.23013 0 0
## 41 4 1799 1217 1489.556 930.6111 421.63309 0 0
## 42 4 1814 1207 1489.556 930.6111 426.21006 0 0
## 43 4 2020 1021 1489.556 930.6111 538.09057 0 0
## 44 4 1715 1017 1489.556 930.6111 241.42957 0 0
## 45 4 1998 1051 1489.556 930.6111 522.50286 0 0
## 46 4 2170 931 1489.556 930.6111 680.44456 0 0
## 47 4 2041 726 1489.556 930.6111 588.18082 0 0
## 48 4 1777 845 1489.556 930.6111 299.92261 0 0
## 49 4 2180 836 1489.556 930.6111 696.89654 0 0
## 50 4 2333 929 1489.556 930.6111 843.44598 0 0
## 51 4 2278 1019 1489.556 930.6111 793.38341 0 0
## 52 4 2190 765 1489.556 930.6111 719.75653 0 0
## 53 4 215 1010 1489.556 930.6111 1277.02563 0 0
## 54 4 230 978 1489.556 930.6111 1260.44671 0 0
## 55 4 1558 566 1489.556 930.6111 370.97965 0 0
##
## $`5`
## ID X Y X-centroid Y-centroid Distance isBoundary isLandmark
## 56 5 273 1129 1624.571 1082.81 1352.3605 1 0
## 57 5 576 1203 1624.571 1082.81 1055.4373 1 0
## 58 5 954 1258 1624.571 1082.81 693.0785 1 0
## 59 5 1181 1299 1624.571 1082.81 493.4510 1 0
## 60 5 1539 1371 1624.571 1082.81 300.6264 1 0
## 61 5 1895 1407 1624.571 1082.81 422.1742 1 1
## 62 5 2157 1381 1624.571 1082.81 610.2440 1 0
## 63 5 2258 1340 1624.571 1082.81 683.6510 1 1
## 64 5 2411 1238 1624.571 1082.81 801.5946 1 1
## 65 5 2485 1158 1624.571 1082.81 863.7077 1 1
## 66 5 2515 1070 1624.571 1082.81 890.5207 1 1
## 67 5 2427 992 1624.571 1082.81 807.5506 1 1
## 68 5 2280 898 1624.571 1082.81 680.9854 1 1
## 69 5 2190 859 1624.571 1082.81 608.1120 1 0
## 70 5 2059 808 1624.571 1082.81 514.0510 1 1
## 71 5 1846 736 1624.571 1082.81 411.4699 1 1
## 72 5 1652 710 1624.571 1082.81 373.8172 1 0
## 73 5 1286 771 1624.571 1082.81 460.2780 1 0
## 74 5 813 939 1624.571 1082.81 824.2144 1 0
## 75 5 533 1025 1624.571 1082.81 1093.1012 1 0
## 76 5 786 1147 1624.571 1082.81 841.0246 1 0
## 77 5 713 1109 1624.571 1082.81 911.9476 0 0
## 78 5 792 1082 1624.571 1082.81 832.5718 0 0
## 79 5 756 1041 1624.571 1082.81 869.5771 0 0
## 80 5 784 1014 1624.571 1082.81 843.3831 0 0
## 81 5 776 986 1624.571 1082.81 854.0758 0 0
## 82 5 970 1219 1624.571 1082.81 668.5893 0 1
## 83 5 972 1201 1624.571 1082.81 663.1881 0 0
## 84 5 921 1209 1624.571 1082.81 714.7984 0 0
## 85 5 905 1166 1624.571 1082.81 724.3643 0 0
## 86 5 1116 1107 1624.571 1082.81 509.1464 0 1
## 87 5 1114 1078 1624.571 1082.81 510.5941 0 1
## 88 5 1060 1039 1624.571 1082.81 566.2686 0 1
## 89 5 1281 830 1624.571 1082.81 426.5606 0 0
## 90 5 1431 1164 1624.571 1082.81 209.9090 0 1
## 91 5 1474 1186 1624.571 1082.81 182.5377 0 0
## 92 5 1455 994 1624.571 1082.81 191.4200 0 1
## 93 5 1691 863 1624.571 1082.81 229.6279 0 0
## 94 5 1715 1260 1624.571 1082.81 198.9316 0 1
## 95 5 1891 1377 1624.571 1082.81 396.9033 0 0
## 96 5 1852 1305 1624.571 1082.81 317.9503 0 0
## 97 5 1949 1227 1624.571 1082.81 355.0279 0 0
## 98 5 2159 1328 1624.571 1082.81 587.9900 0 0
## 99 5 2002 916 1624.571 1082.81 412.6472 0 0
## 100 5 2208 1078 1624.571 1082.81 583.4484 0 0
## 101 5 2350 1076 1624.571 1082.81 725.4605 0 0
## 102 5 2172 912 1624.571 1082.81 573.4579 0 0
## 103 5 1930 1323 1624.571 1082.81 388.5590 0 0
##
## $`6`
## ID X Y X-centroid Y-centroid Distance isBoundary isLandmark
## 104 6 238 1037 1365.174 962.6957 1129.62036 1 0
## 105 6 385 1070 1365.174 962.6957 986.02998 1 0
## 106 6 504 1092 1365.174 962.6957 870.82726 1 0
## 107 6 1007 1197 1365.174 962.6957 428.00360 1 0
## 108 6 1249 1244 1365.174 962.6957 304.34933 1 0
## 109 6 1619 1289 1365.174 962.6957 413.40320 1 0
## 110 6 1705 1287 1365.174 962.6957 469.73937 1 1
## 111 6 2067 1238 1365.174 962.6957 753.89146 1 1
## 112 6 2153 1193 1365.174 962.6957 820.79841 1 0
## 113 6 2239 1123 1365.174 962.6957 888.40842 1 1
## 114 6 2309 1037 1365.174 962.6957 946.74644 1 1
## 115 6 2344 955 1365.174 962.6957 978.85634 1 1
## 116 6 2270 859 1365.174 962.6957 910.74861 1 1
## 117 6 2127 769 1365.174 962.6957 786.06424 1 1
## 118 6 1910 683 1365.174 962.6957 612.42561 1 1
## 119 6 1683 607 1365.174 962.6957 477.00400 1 1
## 120 6 1509 591 1365.174 962.6957 398.55188 1 0
## 121 6 1183 626 1365.174 962.6957 382.82019 1 0
## 122 6 907 716 1365.174 962.6957 520.36725 1 0
## 123 6 874 726 1365.174 962.6957 545.23082 1 0
## 124 6 391 869 1365.174 962.6957 978.66935 1 0
## 125 6 189 924 1365.174 962.6957 1176.81028 1 0
## 126 6 537 1010 1365.174 962.6957 829.52380 1 0
## 127 6 545 984 1365.174 962.6957 820.45056 0 0
## 128 6 584 922 1365.174 962.6957 782.23323 0 0
## 129 6 588 906 1365.174 962.6957 779.23917 0 0
## 130 6 610 894 1365.174 962.6957 758.29198 0 0
## 131 6 602 869 1365.174 962.6957 768.90396 0 0
## 132 6 561 863 1365.174 962.6957 810.33012 0 0
## 133 6 565 843 1365.174 962.6957 809.07684 0 0
## 134 6 616 967 1365.174 962.6957 749.18628 0 0
## 135 6 799 1088 1365.174 962.6957 579.87419 0 1
## 136 6 889 992 1365.174 962.6957 477.07477 0 1
## 137 6 893 957 1365.174 962.6957 472.20826 0 1
## 138 6 846 918 1365.174 962.6957 521.09428 0 1
## 139 6 1224 877 1365.174 962.6957 165.14787 0 1
## 140 6 1218 1062 1365.174 962.6957 177.54299 0 1
## 141 6 1322 765 1365.174 962.6957 202.35503 0 0
## 142 6 1200 671 1365.174 962.6957 335.21452 0 0
## 143 6 1431 748 1365.174 962.6957 224.56023 0 0
## 144 6 1312 886 1365.174 962.6957 93.32571 0 0
## 145 6 1269 1092 1365.174 962.6957 161.14911 0 0
## 146 6 1320 1201 1365.174 962.6957 242.54823 0 0
## 147 6 1507 1145 1365.174 962.6957 230.97514 0 1
## 148 6 1597 1242 1365.174 962.6957 362.97969 0 0
## 149 6 1636 1252 1365.174 962.6957 396.28749 0 0
## 150 6 1617 1195 1365.174 962.6957 342.61011 0 0
## 151 6 1683 1211 1365.174 962.6957 403.32180 0 0
## 152 6 1789 1215 1365.174 962.6957 493.24034 0 0
## 153 6 1826 881 1365.174 962.6957 468.01160 0 0
## 154 6 1580 632 1365.174 962.6957 394.34739 0 0
## 155 6 2102 1037 1365.174 962.6957 740.56318 0 0
## 156 6 2096 951 1365.174 962.6957 730.91967 0 0
## 157 6 2176 869 1365.174 962.6957 816.22167 0 0
intersection_point = function(a, data){
slope = (data[2,2] - data[1,2])/(data[[2]]-data[[1]])
c1 = - ((data[2,2] - slope*data[2,1]))
A <- matrix(c(1,slope,0,-1), nrow=2, ncol=2)
b <- matrix(cbind(a[1],c1), nrow=2, ncol=1)
Z1<-optR(A, b, method="gauss")
output1 <- matrix(unlist(Z1[3]), nrow=1 , ncol =2)
return(output1)
}
distanceToBoundary= function(a, b){
distance <- sqrt((b[1]- a[1])^2 + (b[2]-a[2])^2)
return(distance)
}
find_intersection = function(point,data){
#X-coordinates
temp = matrix(ncol = 2)
temp = temp[-1,]
for (i in 1:(nrow(data)-1)){
if ( (i ==1) && (((data[i,1] < point[1]) && (data[nrow(data),1] > point[1])) || ((data[i,1] > point[1]) && (data[nrow(data),1] < point[1])) )){
temp <- rbind(temp,data[i,],data[i+1,])
}
if (((data[i,1] < point[1]) && (data[i+1,1] > point[1])) || ((data[i,1] > point[1]) && (data[i+1,1] < point[1])) ){
temp <- rbind(temp,data[i,],data[i+1,])
}
}
#y-coordinates
temp1 = matrix(ncol=2)
temp1 = temp1[-1,]
for (j in 1:(nrow(data)-1)){
if ( (j ==1) && (((data[j,2] < point[2]) && (data[nrow(data),2] > point[2])) || ((data[j,2] > point[2]) && (data[nrow(data),2] < point[2])) )){
temp1 <- rbind(temp1,data[j,],data[j+1,])
}
if (((data[j,2] < point[2]) && (data[j+1,2] > point[2])) || ((data[j,2] > point[2]) && (data[j+1,2] < point[2])) ){
temp1 <- rbind(temp1,data[j,],data[j+1,])
}
}
result= rbind(temp, temp1)
output = matrix(ncol=1, nrow=1)
output = output[-1,]
for (k in 1:(nrow(result))){
if(k%%2 != 0 ){
tempx <- data.matrix(result[k:(k+1),])
inter_point <- intersection_point(point,tempx)
z <- data.matrix(inter_point)
temp5<-distanceToBoundary(point,z)
output <- rbind(output, temp5)
}
}
return(output)
}
Test one boundary point:
wings <- data.frame(data_split[[1]])
boundary <- split(wings, wings$isBoundary)
temp3 <- data.matrix(boundary[[2]]) #isBoundary is 1
z <- data.matrix(wings[20,2:3])
r <- find_intersection(z,temp3[,2:3])
r
## [,1]
## temp5 127.5271
## temp5 175.6811
## temp5 4848.7436
## temp5 205.0224
The results are the distance from the north, south, east, west.