-
Notifications
You must be signed in to change notification settings - Fork 19
/
raster.js
224 lines (185 loc) · 5.9 KB
/
raster.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var gdal = require('gdal');
var mapnik = require('mapnik');
var srs = require('srs');
var invalid = require('./invalid');
var path = require('path');
var utils = require('./utils');
mapnik.register_default_input_plugins();
module.exports = Raster;
function Raster(filepath) {
var geotransform;
var filelist;
this.filepath = filepath;
this.details = {};
this.basename = path.basename(this.filepath, path.extname(this.filepath));
try {
this.mapnikDatasource = new mapnik.Datasource({
type: 'gdal',
file: filepath
});
this.gdalDatasource = gdal.open(filepath);
if (this.gdalDatasource.driver.description === 'VRT') {
filelist = this.gdalDatasource.getFileList();
if (filelist.length === 1) throw invalid('VRT file does not reference existing source files.');
}
}
catch (err) {
throw invalid('Invalid raster: could not open the file');
}
try {
this.details.width = this.gdalDatasource.rasterSize.x;
this.details.height = this.gdalDatasource.rasterSize.y;
}
catch (err) {
throw invalid('Invalid raster: could not read image dimensions');
}
try {
geotransform = this.gdalDatasource.geoTransform;
}
catch (err) {
throw invalid('Invalid raster: could not read georeferencing information');
}
if (!geotransform) throw invalid('could not read georeferencing information');
var ratio = Math.abs(geotransform[1] / geotransform[5]);
if (geotransform[1] <= 0 || geotransform[5] >= 0) throw invalid('Invalid raster: Invalid pixelsize in geotransform array');
if (ratio < 0.5 || ratio > 2) throw invalid('Invalid raster: Invalid pixelsize in geotransform array');
if (geotransform[2] !== 0 || geotransform[4] !== 0) throw invalid('Invalid raster: Invalid rotation value in geotransform array');
this.details.pixelSize = [geotransform[1], geotransform[5]];
this.details.origin = [geotransform[0], geotransform[3]];
try { this.projection = srs.parse(this.gdalDatasource.srs.toWKT()).proj4; }
catch (err) {
throw invalid('Invalid raster: could not read spatial reference information');
}
if (this.projection === '') throw invalid('Invalid projection');
}
Raster.validFileType = ['tif', 'vrt'];
Raster.prototype.detailsName = 'raster';
Raster.prototype.dstype = 'gdal';
Raster.prototype.getProjection = function(callback) {
return callback(null, this.projection);
};
Raster.prototype.getCenter = function(callback) {
this.getExtent(function(err, extent) {
if (err) return callback(err);
return callback(null, [
0.5 * (extent[0] + extent[2]),
0.5 * (extent[1] + extent[3])
]);
});
};
Raster.prototype.getExtent = function(callback) {
var current = new mapnik.Projection(this.projection);
var wgs84 = new mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs');
var extent;
var transform;
try {
extent = this.mapnikDatasource.extent();
}
catch (err) {
return callback(
invalid('Invalid raster: could not read extent')
);
}
if (current !== wgs84) {
transform = new mapnik.ProjTransform(current, wgs84);
extent = transform.forward(extent);
}
return callback(null, extent);
};
Raster.prototype.getBands = function() {
var numBands = this.gdalDatasource.bands.count();
var bandInfo;
var bands = [];
var i;
var band;
for (i = 1; i <= numBands; i++) {
band = this.gdalDatasource.bands.get(i);
try {
bandInfo = {
scale: band.scale,
unitType: band.unitType,
rasterDatatype: band.dataType,
categoryNames: band.categoryNames,
hasArbitraryOverviews: band.hasArbitraryOverviews,
overviews: band.overviews,
nodata: band.noDataValue,
id: band.id,
blockSize: band.blockSize,
color: band.colorInterpretation
};
}
catch (err) {
throw invalid('Invalid raster: could not read band information');
}
try {
bandInfo.stats = band.getStatistics(true, true);
}
catch (err) {
// ignore--this is non-fatal
}
bands.push(bandInfo);
}
return bands;
};
Raster.prototype.getLayers = function(callback) {
return callback(null, [this.basename]);
};
/**
* Detects unit type of the gdal source, using the srs/projection
* @param srs
* @returns unit type (String)
*/
Raster.prototype.getZooms = function(callback) {
var pixelSize = utils.convertToMeters(this.details.pixelSize, utils.getUnitType(this.projection));
var spatialResolutions = utils.getSpatialResolutions();
/*
* Threshold weight = the amount to shift the break;
* 0 = no shift (always upsamples), 1 = full shift (always downsamples)
* 0.25 threshold means that we'll upsample 75% of the time
*/
var thresholdWeight = 0.25;
var _this = this;
var validSpatialResolutions = utils.getValidSpatialResolutions(spatialResolutions, _this.details.pixelSize[0], thresholdWeight);
this.details.pixelSize = pixelSize;
return callback(
null,
Math.max(0, validSpatialResolutions.length - 6),
validSpatialResolutions.length
);
};
Raster.prototype.getDetails = function(callback) {
var details = this.details;
var ref;
try {
details.width = this.gdalDatasource.rasterSize.x;
details.height = this.gdalDatasource.rasterSize.y;
}
catch (err) {
return callback(
invalid('Invalid raster: could not read image dimensions', err)
);
}
try {
details.bandCount = this.gdalDatasource.bands.count();
details.bands = this.getBands();
details.nodata = this.details.bands[0].nodata;
}
catch (err) {
return callback(
invalid('Invalid raster: could not get band information', err)
);
}
try {
ref = gdal.SpatialReference.fromProj4(this.projection);
details.units = {
linear: ref.getLinearUnits(),
angular: ref.getAngularUnits()
};
}
catch (err) {
return callback(
invalid('Invalid raster: could not read spatial reference information', err)
);
}
return callback(null, details);
};