-
Notifications
You must be signed in to change notification settings - Fork 0
/
DotaV2.rmd
195 lines (153 loc) · 5.04 KB
/
DotaV2.rmd
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
---
title: "Dota VGA"
output: html_document
---
```{r}
library(grid)
library(png)
library(magick)
library(tidyverse)
library(dplyr)
#spatial
library(rgdal)
library(sp)
library(raster)
library(terra)
library(sf)
```
#1) Make Individual Rasters
Read Raw Files
```{r}
all_files <- list.files(path = "Map Files", pattern = '.png$', full.names = T, include.dirs = T) %>%
map(image_read)
#check extraction
namelist = list.files(path = "Map Files", pattern = '.png$', full.names = T, include.dirs = T)
names = sub(".*Map Files/map_data_", "", sub(".png.*", "", namelist))
names
```
Prepare Rasters
```{r}
for (i in 1:length(all_files)){
version = names[i]
img = all_files[[i]]
#break files
topology = image_crop(img, geometry = "260x260")
trees = image_crop(img, geometry = "260x260+260")
boundaries = image_crop(img, geometry = "260x260+520")
nowards_1 = image_crop(img, geometry = "260x260+780")
nowards_2 = image_crop(img, geometry = "260x260+1040")
#make transparent
trees = image_transparent(trees,"white")
#boundaries = image_transparent(boundaries,"white")
#combine and make scale and transparent #original 260 x 260
dota = image_mosaic(c(boundaries,trees)) #%>% image_scale(geometry = "4096")
image_write(dota,paste("VGA Analysis/Rasters/",names[i],"_raster.png",sep=""))
}
```
#2) Convert Rasters to Polygons
```{r}
#find filepaths for rasters
raster_names = list.files(path = "VGA Analysis/Rasters", pattern = '.png$', full.names = T, include.dirs = T)
#for (i in 1:1){
for (i in 1:length(raster_names)){
#covert to raster
r <- rast(raster_names[i])
p <- as.polygons(r)
final <- flip(p, "vertical")
#write polygons
st_write(st_as_sf(final), paste("VGA Analysis/Polygons/",names[i],".mif",sep="") ,driver= "MapInfo File")
#write polylines
pl = readOGR(paste("VGA Analysis/Polygons/",names[i],".mif",sep=""), verbose = FALSE)
li = as(pl, 'SpatialLinesDataFrame')
writeOGR(li, paste("VGA Analysis/Polylines/",names[i],".mif",sep=""), verbose = FALSE, layer = names[i], driver= "MapInfo File")
}
```
3) (MANUALLY) IMPORT AND FILL INTO GRAPHS
#4) Run VGA
```{r}
#Manual Process, Import and to Grid
all_graphs = list.files(path = "VGA Analysis/Graph_Import/", pattern = '.graph$', full.names = T, include.dirs = T)
#Make a copy of original data
file.copy(file.path(all_graphs), "VGA Analysis/Graph_Processed")
#New Directory
new_files = list.files(path = "VGA Analysis/Graph_Processed/", pattern = '.graph$', full.names = T, include.dirs = T)
#Process VGA
for (dir in new_files){
#Make VGA
rdepthmap::makeVGAGraph(dir)
vers = parse_number(dir)
#Run VGA
rdepthmap::VGA(dir, vgaMode = "visibility-global", radii = c("n"))
}
```
-----------------------------------------------------------------------------------------------------
#5) Collect Data
```{r include=FALSE}
new_files = list.files(path = "VGA Analysis/Graph_Processed/", pattern = '.graph$', full.names = T, include.dirs = T)
#create dataframe
data_list = list()
#collect different maps in list
for (dir in new_files){
#version = parse_number(dir)
data = rdepthmap::getPointmapData(dir)$map
data_list = append(data_list,data)
print(version)
}
```
#6) Trial Plot (700x432)
default plot
```{r}
for (i in 1:length(new_files)){
version = parse_number(new_files[i])
a = as(data_list[[i]],"SpatialPixelsDataFrame")
b = as(a,"SpatialPixelsDataFrame")
plot(b[, "Visual.Integration..HH."]) + title(paste0("v",version))
}
```
plot by fixed values
```{r}
for (i in 1:length(new_files)){
version = parse_number(new_files[i])
a = as(data_list[[i]],"SpatialPixelsDataFrame")
b = as(a,"SpatialPixelsDataFrame")
png(filename=paste("VGA Analysis/Diagrams/v",version,".png",sep = ""))
plot(b[, "Visual.Integration..HH."],zlim = c(2,14)) + title(paste0("v",version))
dev.off()
}
```
Build Animation
```{r}
library(animation)
library(magick)
## make sure ImageMagick has been installed in your system
saveGIF({
for (i in 1:length(new_files)){
version = parse_number(new_files[i])
a = as(data_list[[i]],"SpatialPixelsDataFrame")
b = as(a,"SpatialPixelsDataFrame")
plot(b[, "Visual.Integration..HH."],zlim = c(2,14)) + title(paste0("v",version))
}
}, interval = 1, ani.width =700, ani.height = 432)
```
# 7a) Saving back to MIF for QGIS (Usual Size)
```{r}
new_files
for (i in 1:length(new_files)){
version = parse_number(new_files[i])
a = as(data_list[[i]],"SpatialPixelsDataFrame")
b = as(a,"SpatialPixelsDataFrame")
writeOGR(obj=b, dsn=paste0("VGA Analysis/GIS Files/",version,".mif"), layer="00", driver="MapInfo File") # this is in geographical projection
}
```
# 7b) Saving back to MIF for QGIS (Rescaled Size)
```{r}
for (i in 1:length(new_files)){
version = parse_number(new_files[i])
a = as(data_list[[i]],"SpatialPixelsDataFrame")
b = as(a,"SpatialPointsDataFrame")
sc = 4096/260
tf = SimilarityTransformation(as.data.frame(b),parameters=c(sc,0,sc,0))
e = applyTransformation(tf,b)
writeOGR(obj=e, dsn=paste0("VGA Analysis/GIS Files/Stretched/",version,".mif"), layer="00", driver="MapInfo File") # this is in geographical projection
}
```