-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoilAPI.js
More file actions
172 lines (150 loc) · 3.97 KB
/
SoilAPI.js
File metadata and controls
172 lines (150 loc) · 3.97 KB
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
function get(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
String.prototype.format = function() {
var res = this;
for (i = 0; i < arguments.length; i++) {
res = res.replace('{' + i + '}', arguments[i].toString());
}
return res;
}
function getProperties(lat, lon){
var ans = JSON.parse(get('https://rest.soilgrids.org/query?lon={0}&lat={1}&attributes=TAXNWRB,PHIHOX'.format(lon, lat)));
return ans['properties'];
}
function soilCompare(a, b) {
if (a.part < b.part)
return 1;
if (a.part > b.part)
return -1;
return 0;
}
function getSoilInfo(lat, lon, lim=5){
var props = getProperties(lat, lon);
var soil_dict = props['TAXNWRB'];
var soils = new Array();
for (soil in soil_dict) {
soils.push({name:soil, part:soil_dict[soil]});
}
soils = soils.sort(soilCompare);
res_soils = [];
for (i = 0; (i < soils.length) && (soils[i].part >= lim); i++)
res_soils.push(soils[i])
for (i in props['PHIHOX']['M']) {
var acidity = props['PHIHOX']['M'][i];
break;
}
return {'acidity':acidity, 'soils':res_soils};
}
var allplus = 0, wheat = 0, rice = 0, melon = 0, winter = 0, maize = 0
function changesWithSoils(soilName){
var s = soilName.toLowerCase()
if (s.includes('histosol')){
allplus-=0.7
}
if (s.includes('andosol')){
rice+=2
maize+=1
}
if (s.includes('arenosols')){
melon+=1
maize+=1
}
if (s.includes('vertisols')){
allplus-=0.3
}
if (s.includes('fluvisols')){
winter+=1.5
maize+=1.5
melon+=1.5
}
if (s.includes('gleysols')){
rice+=2.5
allplus-=0.5
}
if (s.includes('leptosols')){
//nothing
}
if (s.includes('regosols')){
wheat+=1.3
rice+=1.3
maize+=1.3
}
if (s.includes('cambisols')){
allplus+=1.5
}
if (s.includes('solonchaks')){
allplus-=1.7
}
if (s.includes('solonetz')){
allplus-=1.6
}
if (s.includes('gypsisols')){
allplus+=0.5
}
if (s.includes('calcisols')){
allplus+=0.5
}
if (s.includes('kastanozems')){
allplus+=1
}
if (s.includes('chernozems')){
allplus+=2
}
if (s.includes('phaeozems')){
wheat+=1
}
if (s.includes('luvisols')){
allplus+=1
}
if (s.includes('podzols')){
allplus+=1
}
}
function getGoodCultures(lat,lon) {
var cultures = new Object()
cultures['Ячмень'] = [6.0 , 7.5, 0]
cultures['Гречиха'] = [4.7 , 7.5, 0]
cultures['Кукуруза'] = [6.0 , 7.5, 0]
cultures['Рис'] = [5.0 , 6.6, 0]
cultures['Рожь озимая'] = [5.0 , 7.7, 0]
cultures['Пшеница озимая'] = [6.3 , 7.5, 0]
cultures['Подсолнечник'] = [6.0 , 6.8, 0]
cultures['Свекла сахарная'] = [7.0 , 7.5, 0]
cultures['Картофель'] = [4.5 , 6.3, 0]
cultures['Арбуз и дыня'] = [4.0 , 5.5, 0]
var addToAll = 0
var soils = getSoilInfo(lat,lon)
for (soilNum in soils['soils']) {
changesWithSoils(soils['soils'][soilNum]['name'])
if (soils['soils'][soilNum]['part']>=40){
allplus*=2
wheat*=2
winter*=2
rice*=2
melon*=2
maize*=2
}
cultures['Ячмень'][2]+=allplus+wheat
cultures['Гречиха'][2]+=allplus+wheat
cultures['Кукуруза'][2]+=allplus+maize
cultures['Рис'][2]+=allplus+rice
cultures['Рожь озимая'][2]+=allplus+winter
cultures['Пшеница озимая'][2]+=allplus+winter
cultures['Подсолнечник'][2]+=allplus+wheat
cultures['Свекла сахарная'][2]+=allplus+maize
cultures['Картофель'][2]+=allplus+maize
cultures['Арбуз и дыня'][2]+=allplus+melon
allplus = 0, wheat = 0, rice = 0, melon = 0, winter = 0, maize = 0
}
for (culture in cultures) {
if (cultures[culture][0] <= soils['acidity'] && soils['acidity'] <= cultures[culture][1]){
cultures[culture][2]+=1.5
}
}
return cultures
}