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
+1
View File
@@ -860,6 +860,7 @@
/** /**
* @typedef {Object} olx.source.VectorFileOptions * @typedef {Object} olx.source.VectorFileOptions
* @property {ArrayBuffer|undefined} arrayBuffer Array buffer.
* @property {Array.<ol.Attribution>|undefined} attributions Attributions. * @property {Array.<ol.Attribution>|undefined} attributions Attributions.
* @property {Document|undefined} doc Document. * @property {Document|undefined} doc Document.
* @property {ol.Extent|undefined} extent Extent. * @property {ol.Extent|undefined} extent Extent.
+38 -8
View File
@@ -4,8 +4,13 @@
goog.provide('ol.source.VectorFile'); goog.provide('ol.source.VectorFile');
goog.require('goog.asserts'); 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');
goog.require('goog.net.XhrIo.ResponseType');
goog.require('goog.userAgent'); goog.require('goog.userAgent');
goog.require('ol.BrowserFeature');
goog.require('ol.format.FormatType'); goog.require('ol.format.FormatType');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.source.State'); goog.require('ol.source.State');
@@ -53,17 +58,38 @@ ol.source.VectorFile = function(opt_options) {
this.readFeatures_(options.text); this.readFeatures_(options.text);
} }
if (goog.isDef(options.arrayBuffer)) {
this.readFeatures_(options.arrayBuffer);
}
if (goog.isDef(options.url) || goog.isDef(options.urls)) { if (goog.isDef(options.url) || goog.isDef(options.urls)) {
this.setState(ol.source.State.LOADING); 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)) { 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)) { if (goog.isDef(options.urls)) {
var urls = options.urls; var urls = options.urls;
var i, ii; var i, ii;
for (i = 0, ii = urls.length; i < ii; ++i) { 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 * @private
*/ */
ol.source.VectorFile.prototype.handleXhrIo_ = function(event) { 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()) { if (xhrIo.isSuccess()) {
var type = this.format.getType(); var type = this.format.getType();
/** @type {Document|Node|Object|string|undefined} */ /** @type {ArrayBuffer|Document|Node|Object|string|undefined} */
var source; var source;
if (type == ol.format.FormatType.BINARY) { if (type == ol.format.FormatType.BINARY &&
// FIXME ol.BrowserFeature.HAS_ARRAY_BUFFER) {
source = xhrIo.getResponse();
goog.asserts.assertInstanceof(source, ArrayBuffer);
} else if (type == ol.format.FormatType.JSON) { } else if (type == ol.format.FormatType.JSON) {
source = xhrIo.getResponseJson(); source = xhrIo.getResponseJson();
} else if (type == ol.format.FormatType.TEXT) { } else if (type == ol.format.FormatType.TEXT) {
@@ -98,6 +127,7 @@ ol.source.VectorFile.prototype.handleXhrIo_ = function(event) {
} else { } else {
goog.asserts.fail(); goog.asserts.fail();
} }
goog.dispose(xhrIo);
if (goog.isDef(source)) { if (goog.isDef(source)) {
this.readFeatures_(source); this.readFeatures_(source);
} else { } 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 * @private
*/ */
ol.source.VectorFile.prototype.readFeatures_ = function(source) { ol.source.VectorFile.prototype.readFeatures_ = function(source) {