Add binary support to ol.source.VectorFile

This commit is contained in:
Tom Payne
2014-02-17 17:42:06 +01:00
parent ef82965c0a
commit e40b9adde0
2 changed files with 39 additions and 8 deletions

View File

@@ -860,6 +860,7 @@
/**
* @typedef {Object} olx.source.VectorFileOptions
* @property {ArrayBuffer|undefined} arrayBuffer Array buffer.
* @property {Array.<ol.Attribution>|undefined} attributions Attributions.
* @property {Document|undefined} doc Document.
* @property {ol.Extent|undefined} extent Extent.

View File

@@ -4,8 +4,13 @@
goog.provide('ol.source.VectorFile');
goog.require('goog.asserts');
goog.require('goog.dispose');
goog.require('goog.events');
goog.require('goog.net.EventType');
goog.require('goog.net.XhrIo');
goog.require('goog.net.XhrIo.ResponseType');
goog.require('goog.userAgent');
goog.require('ol.BrowserFeature');
goog.require('ol.format.FormatType');
goog.require('ol.proj');
goog.require('ol.source.State');
@@ -53,17 +58,38 @@ ol.source.VectorFile = function(opt_options) {
this.readFeatures_(options.text);
}
if (goog.isDef(options.arrayBuffer)) {
this.readFeatures_(options.arrayBuffer);
}
if (goog.isDef(options.url) || goog.isDef(options.urls)) {
this.setState(ol.source.State.LOADING);
var handleXhrIo = goog.bind(this.handleXhrIo_, this);
var type = this.format.getType();
var responseType;
if (type == ol.format.FormatType.BINARY &&
ol.BrowserFeature.HAS_ARRAY_BUFFER) {
responseType = goog.net.XhrIo.ResponseType.ARRAY_BUFFER;
} else {
responseType = goog.net.XhrIo.ResponseType.TEXT;
}
var xhrIo;
if (goog.isDef(options.url)) {
goog.net.XhrIo.send(options.url, handleXhrIo);
xhrIo = new goog.net.XhrIo();
xhrIo.setResponseType(responseType);
goog.events.listen(xhrIo, goog.net.EventType.COMPLETE,
goog.bind(this.handleXhrIo_, this));
xhrIo.send(options.url);
}
if (goog.isDef(options.urls)) {
var urls = options.urls;
var i, ii;
for (i = 0, ii = urls.length; i < ii; ++i) {
goog.net.XhrIo.send(urls[i], handleXhrIo);
xhrIo = new goog.net.XhrIo();
xhrIo.setResponseType(responseType);
goog.events.listen(xhrIo, goog.net.EventType.COMPLETE,
goog.bind(this.handleXhrIo_, this));
xhrIo.send(urls[i]);
}
}
}
@@ -77,13 +103,16 @@ goog.inherits(ol.source.VectorFile, ol.source.Vector);
* @private
*/
ol.source.VectorFile.prototype.handleXhrIo_ = function(event) {
var xhrIo = /** @type {goog.net.XhrIo} */ (event.target);
var xhrIo = event.target;
goog.asserts.assertInstanceof(xhrIo, goog.net.XhrIo);
if (xhrIo.isSuccess()) {
var type = this.format.getType();
/** @type {Document|Node|Object|string|undefined} */
/** @type {ArrayBuffer|Document|Node|Object|string|undefined} */
var source;
if (type == ol.format.FormatType.BINARY) {
// FIXME
if (type == ol.format.FormatType.BINARY &&
ol.BrowserFeature.HAS_ARRAY_BUFFER) {
source = xhrIo.getResponse();
goog.asserts.assertInstanceof(source, ArrayBuffer);
} else if (type == ol.format.FormatType.JSON) {
source = xhrIo.getResponseJson();
} else if (type == ol.format.FormatType.TEXT) {
@@ -98,6 +127,7 @@ ol.source.VectorFile.prototype.handleXhrIo_ = function(event) {
} else {
goog.asserts.fail();
}
goog.dispose(xhrIo);
if (goog.isDef(source)) {
this.readFeatures_(source);
} else {
@@ -111,7 +141,7 @@ ol.source.VectorFile.prototype.handleXhrIo_ = function(event) {
/**
* @param {Document|Node|Object|string} source Source.
* @param {ArrayBuffer|Document|Node|Object|string} source Source.
* @private
*/
ol.source.VectorFile.prototype.readFeatures_ = function(source) {