-
Notifications
You must be signed in to change notification settings - Fork 0
/
melt_join_to_shape.R
164 lines (124 loc) · 4.17 KB
/
melt_join_to_shape.R
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
#install.packages("reshape2")
library(reshape2)
library(foreign)
library(sp) # raster data
library(rgdal)
#install.packages("foreach")
library(foreach)
#rm(list=ls())
in.folder<-("C:/Data/wdpa_desig/scratch/national/precise_reshape_dbfs_sr")
in.folder2<-("C:/Data/wdpa_desig/scratch/national/fnets/fnets.gdb")
setwd(in.folder)
out.folder<-("C:/Data/wdpa_desig/scratch/national/precise_output_fcs")
# listDBF=list.files(getwd(),full.names=TRUE)
# listDBF
filenames <- Sys.glob("*.csv")
filenames
filenames<-filenames[lapply(filenames,function(x) length(grep("sum_",x,value=FALSE))) == 1]
fName<-filenames[2:2]
fName
f<-read.csv(fName)
head(f)
#gsub(".dbf",".csv","john.dbf")
#gsub(".dbf",".csv",paste0(out.folder,"/",filenames[i]))
length<-length(filenames)
#(filenames)
#length = 7
tmp.all=NULL
tmp = read.csv(filenames[1])
filenames
#filenameEdt<-gsub(".csv","", filename)
# The input file geodatabase
fgdb = in.folder2
# List all feature classes in a file geodatabase
subset(ogrDrivers(), grepl("GDB", name))
fc_list = ogrListLayers(fgdb)
print(fc_list)
# Read the feature class
#fc = readOGR(dsn=fgdb,layer="fnet_AFG")
fc_list<-fc_list[lapply(fc_list,function(x) length(grep("fnet_",x,value=FALSE))) == 0]
#fc_list<-fc_list[lapply(fc_list,function(x) length(grep("CHN",x,value=FALSE))) == 0]
fc_list<-fc_list[lapply(fc_list,function(x) length(grep("BRA",x,value=FALSE))) == 0]
fc_list<-fc_list[lapply(fc_list,function(x) length(grep("CAN",x,value=FALSE))) == 0]
fc_list<-fc_list[lapply(fc_list,function(x) length(grep("AUS",x,value=FALSE))) == 0]
fc_list<-fc_list[lapply(fc_list,function(x) length(grep("IND",x,value=FALSE))) == 0]
fc_list<-fc_list[lapply(fc_list,function(x) length(grep("RUS",x,value=FALSE))) == 0]
fc_list<-fc_list[lapply(fc_list,function(x) length(grep("USA",x,value=FALSE))) == 0]
fc_list[194]
#if precise version then $sum should be $countSum and vice versa if normal outputs
#loop through and join unpivoted tables to shapes for viewing
for (i in 194:length){
setwd(in.folder)
print(i)
gridStats=read.csv(filenames[i])
filenamesEdt<-gsub(".csv","", filenames[i])
setwd(in.folder2)
print (filenamesEdt)
# filenamesEdt<-strsplit(filenamesEdt, "_")
# filenamesEdt<-unlist(filenamesEdt)
# filenamesEdt<-filenamesEdt[4]
filenamesEdt<-gsub("sum_","fnetwdpa_", filenamesEdt)
filenamesEdt<-gsub("_preciseIntUnionFC","", filenamesEdt)
print (filenamesEdt)
outfile<-paste0(out.folder,"/",filenamesEdt,".shp")
print(outfile)
# Read the feature class
gridShape = readOGR(dsn=fgdb,layer=filenamesEdt)
print(head(gridShape@data))
#gridShape = readOGR(".", filenamesEdt)
joinGrid<-merge(gridShape, gridStats,by.x="GID", by.y="GID")
#print(head(joinGrid@data))
joinGrid<-subset(joinGrid,joinGrid@data$countSum>0)
droplevels(joinGrid@data)
print(str(joinGrid@data))
if(!file.exists(outfile)){
writeOGR(joinGrid, out.folder, filenamesEdt, "ESRI Shapefile")
}
rm(gridShape)
rm(joinGrid)
rm(gridStats)
}
if(!file.exists(outfile)){
# Read the feature class
gridShape = readOGR(dsn=fgdb,layer=filenamesEdt)
print(head(gridShape@data))
#gridShape = readOGR(".", filenamesEdt)
joinGrid<-merge(gridShape, gridStats,by.x="GID", by.y="GID")
#print(head(joinGrid@data))
joinGrid<-subset(joinGrid,joinGrid@data$countSum>0)
droplevels(joinGrid@data)
print(str(joinGrid@data))
writeOGR(joinGrid, out.folder, filenamesEdt, "ESRI Shapefile")
rm(gridShape)
rm(joinGrid)
rm(gridStats)
}
joinGrid<-subset(joinGrid,joinGrid@data$sum>0)
droplevels(joinGrid@data)
str(joinGrid@data)
plot(joinGrid)
head(gridShape@data)
head(gridStats)
gridShape=fc
plot(gridShape)
gridShape$GID<-1:nrow(gridShape)
head(gridShape@data)
head(gridStats)
#gridShape = readOGR(".", "fnet_ESP")
require(rgdal)
#################################
# # The input file geodatabase
# fgdb = "C:/Data/wdpa_desig/scratch/grids/grids.gdb"
#
# # List all feature classes in a file geodatabase
# subset(ogrDrivers(), grepl("GDB", name))
# fc_list = ogrListLayers(fgdb)
# print(fc_list)
#
# # Read the feature class
# fc = readOGR(dsn=fgdb,layer="fnetwdpa_YEM")
#
# # Determine the FC extent, projection, and attribute information
# summary(fc)
# View the feature class
plot(fc)