This repository was archived by the owner on Jan 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcontourJsonReader.js
139 lines (126 loc) · 4.41 KB
/
contourJsonReader.js
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
/* global File */
/**
* Create a new instance of class contourJsonReader
*
* @class
* @extends geo.fileReader
* @returns {geo.contourJsonReader}
*/
import geo from 'geojs';
import _ from 'underscore';
window.geo = geo;
geo.contourJsonReader = function (arg) {
'use strict';
if (!(this instanceof geo.contourJsonReader)) {
return new geo.contourJsonReader(arg); // eslint-disable-line new-cap
}
var that = this;
geo.fileReader.call(this, arg);
this.canRead = function (file) {
if (file instanceof File) {
return (file.type === 'application/json' || file.name.match(/\.json$/));
} else if (_.isString(file)) {
try {
JSON.parse(file);
} catch (e) {
return false;
}
return true;
}
try {
if (Array.isArray(that._featureArray(file))) {
return true;
}
} catch (e) {}
return false;
};
this._readObject = function (file, done, progress) {
var object;
function onDone(fileString) {
if (!_.isString(fileString)) {
done(false);
}
// We have two possibilities here:
// 1) fileString is a JSON string or is
// a URL.
try {
object = JSON.parse(fileString);
done(object);
} catch (e) {
if (!object) {
$.ajax({
type: 'GET',
url: fileString,
dataType: 'text'
}).done(function (data) {
object = JSON.parse(data);
done(object);
}).fail(function () {
done(false);
});
}
}
}
if (file instanceof File) {
that._getString(file, onDone, progress);
} else if (_.isString(file)) {
onDone(file);
} else {
done(file);
}
};
this._getStyle = function (spec) {
return spec.properties;
};
this.read = function (file, done, progress) {
function _done(data) {
var contour = that.layer().createFeature('contour')
.data(data.position || data.values)
.style({
opacity: 0.75
})
.contour({
gridWidth: data.gridWidth,
gridHeight: data.gridHeight,
/* The color range doesn't have to be linear:
rangeValues: [0, 25, 50, 75, 100, 125, 250, 500, 750, 2000],
*/
/* Or, you could plot iso-contour lines using a varying opacity:
rangeValues: [100, 100, 200, 200, 300, 300, 400, 400, 500, 500],
opacityRange: [1, 0, 1, 0, 1, 0, 1, 0, 1],
*/
/* You can make smooth contours instead of stepped contours:
stepped: false,
*/
min: 0
});
if (data.position) {
contour
.position(function (d) { return {x: d.x, y: d.y, z: d.z}; })
.style({
value: function (d) { return d.z > -9999 ? d.z : null; }
/* You can get better contours if you set a minimum value and set
* sea locations to a small negative number:
value: function (d) { return d.z > -9999 ? d.z : -10; }
*/
});
} else {
contour
.style({
value: function (d) { return d > -9999 ? d : null; }
})
.contour({
/* The geometry can be specified using 0-point coordinates and deltas
* since it is a regular grid. */
x0: data.x0, y0: data.y0, dx: data.dx, dy: data.dy
});
}
if (done) {
done(contour);
}
}
that._readObject(file, _done, progress);
};
};
geo.inherit(geo.contourJsonReader, geo.fileReader);
geo.registerFileReader('contourJsonReader', geo.contourJsonReader);