-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
208 lines (155 loc) · 5.27 KB
/
index.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
var url = require('url')
var querystring = require('querystring')
var mongoose = require('mongoose')
var tiletype = require('tiletype')
var TileSchema = require('./schema').TileSchema
var TilesetSchema = require('./schema').TilesetSchema
var protocol = 'foxgis+mongodb:'
function FoxgisSource(uri, callback) {
if (typeof uri === 'string') {
uri = url.parse(uri, true)
uri.pathname = querystring.unescape(uri.pathname)
}
if (typeof uri.query === 'string') {
uri.query = querystring.parse(uri.query)
}
if (uri.protocol !== protocol) {
return callback(new Error('Bad uri protocol "' + uri.protocol + '". Must be ' + protocol + '.'))
}
if (!uri.query.tileset_id) {
return callback(new Error('Must specify "tileset_id" with querystring'))
}
this.query = uri.query
this.tileset_id = uri.query.tileset_id
var _this = this
uri.protocol = 'mongodb:'
this._db = mongoose.createConnection()
this._db.open(url.format(uri), function(err) {
if (err) {
return callback(err)
}
var tiles = 'tiles_' + _this.tileset_id
var grids = 'grids_' + _this.tileset_id
_this.Tile = _this._db.model(tiles, TileSchema, tiles)
_this.Grid = _this._db.model(grids, TileSchema, grids)
_this.Tileset = _this._db.model('Tileset', TilesetSchema)
_this.open = true
callback(null, _this)
})
}
FoxgisSource.registerProtocols = function(tilelive) {
tilelive.protocols['foxgis+mongodb:'] = FoxgisSource
}
FoxgisSource.list = function(filepath, callback) {
return callback(new Error(".list not implemented for " + protocol))
}
FoxgisSource.findID = function(filepath, id, callback) {
return callback(new Error(".findID not implemented for " + protocol))
}
FoxgisSource.prototype.getTile = function(z, x, y, callback) {
if (typeof callback !== 'function') throw new Error('Callback needed')
if (!this.open) return callback(new Error('FoxgisSource not yet loaded'))
this.Tile.findOne({
zoom_level: +z,
tile_column: +x,
tile_row: +y
}, function(err, tile) {
if (err) {
return callback(err)
}
if (!tile) {
return callback(new Error('Tile does not exist'))
}
if (!tile.tile_data || !Buffer.isBuffer(tile.tile_data)) {
return callback(new Error('Tile is invalid'))
}
var headers = tiletype.headers(tile.tile_data)
return callback(null, tile.tile_data, headers)
})
}
FoxgisSource.prototype.getGrid = function(z, x, y, callback) {
if (typeof callback !== 'function') throw new Error('Callback needed')
if (!this.open) return callback(new Error('FoxgisSource not yet loaded'))
this.Grid.findOne({
zoom_level: +z,
tile_column: +x,
tile_row: +y
}, function(err, grid) {
if (err) {
return callback(err)
}
if (!grid) {
return callback(new Error('Grid does not exist'))
}
try {
var grid_json = JSON.parse(grid.tile_data)
} catch (err) {
return callback(new Error('Grid is invalid:' + err.message))
}
var headers = { 'Content-Type': 'text/javascript' }
return callback(null, grid_json)
})
}
FoxgisSource.prototype.getInfo = function(callback) {
if (typeof callback !== 'function') throw new Error('Callback needed')
if (!this.open) return callback(new Error('FoxgisSource not yet loaded'))
if (this._info) return callback(null, this._info)
var _this = this
this.Tileset.findOne({ tileset_id: this.tileset_id }, '-_id -__v', function(err, tileset) {
if (err) {
callback(err)
}
if (!tileset) {
callback(new Error('Info does not exist'))
}
_this._info = tileset.toJSON()
return callback(null, _this._info)
})
}
FoxgisSource.prototype.startWriting = function(callback) {
return callback(null)
}
FoxgisSource.prototype.stopWriting = function(callback) {
return callback(null)
}
FoxgisSource.prototype.close = function(callback) {
this._db.close(callback)
}
FoxgisSource.prototype.putTile = function(z, x, y, tile, callback) {
if (typeof callback !== 'function') throw new Error('Callback needed')
if (!this.open) return callback(new Error('FoxgisSource not yet loaded'))
if (!Buffer.isBuffer(tile)) return callback(new Error('Image needs to be a Buffer'))
this.Tile.findOneAndUpdate({
zoom_level: +z,
tile_column: +x,
tile_row: +y
}, { tile_data: tile }, { upsert: true }, callback)
}
FoxgisSource.prototype.putGrid = function(z, x, y, grid, callback) {
if (typeof callback !== 'function') throw new Error('Callback needed')
if (!this.open) return callback(new Error('FoxgisSource not yet loaded'))
this.Grid.findOneAndUpdate({
zoom_level: +z,
tile_column: +x,
tile_row: +y
}, { tile_data: grid }, { upsert: true }, callback)
}
FoxgisSource.prototype.putInfo = function(info, callback) {
if (typeof callback !== 'function') throw new Error('Callback needed')
if (!this.open) return callback(new Error('FoxgisSource not yet loaded'))
var _this = this
info.scheme = 'xyz'
Object.keys(this.query).forEach(function(key) {
info[key] = _this.query[key]
})
this.Tileset.findOneAndUpdate({
tileset_id: this.tileset_id
}, info, { upsert: true, new: true, setDefaultsOnInsert: true }, function(err, tileset) {
if (err) {
return callback(err)
}
delete _this._info
_this.getInfo(callback)
})
}
module.exports = FoxgisSource