Remove use of shared structures in feature parsers

This commit is contained in:
Tim Schaub
2013-09-25 15:11:19 +02:00
parent 2850c761cf
commit e1ba1d8887
8 changed files with 116 additions and 547 deletions

View File

@@ -2,7 +2,6 @@ goog.provide('ol.parser.AsyncObjectFeatureParser');
goog.provide('ol.parser.AsyncStringFeatureParser'); goog.provide('ol.parser.AsyncStringFeatureParser');
goog.provide('ol.parser.DomFeatureParser'); goog.provide('ol.parser.DomFeatureParser');
goog.provide('ol.parser.ObjectFeatureParser'); goog.provide('ol.parser.ObjectFeatureParser');
goog.provide('ol.parser.ReadFeaturesOptions');
goog.provide('ol.parser.ReadFeaturesResult'); goog.provide('ol.parser.ReadFeaturesResult');
goog.provide('ol.parser.StringFeatureParser'); goog.provide('ol.parser.StringFeatureParser');
@@ -18,7 +17,6 @@ ol.parser.DomFeatureParser = function() {};
/** /**
* @param {Element|Document} node Document or element node. * @param {Element|Document} node Document or element node.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.DomFeatureParser.prototype.readFeaturesFromNode = ol.parser.DomFeatureParser.prototype.readFeaturesFromNode =
@@ -34,7 +32,6 @@ ol.parser.ObjectFeatureParser = function() {};
/** /**
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.ObjectFeatureParser.prototype.readFeaturesFromObject = ol.parser.ObjectFeatureParser.prototype.readFeaturesFromObject =
@@ -50,7 +47,6 @@ ol.parser.StringFeatureParser = function() {};
/** /**
* @param {string} data String data. * @param {string} data String data.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.StringFeatureParser.prototype.readFeaturesFromString = ol.parser.StringFeatureParser.prototype.readFeaturesFromString =
@@ -68,7 +64,6 @@ ol.parser.AsyncStringFeatureParser = function() {};
* @param {string} data String data. * @param {string} data String data.
* @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* called after parsing. * called after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.AsyncStringFeatureParser.prototype.readFeaturesFromStringAsync = ol.parser.AsyncStringFeatureParser.prototype.readFeaturesFromStringAsync =
goog.abstractMethod; goog.abstractMethod;
@@ -85,30 +80,17 @@ ol.parser.AsyncObjectFeatureParser = function() {};
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* called after parsing. * called after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.AsyncObjectFeatureParser.prototype.readFeaturesFromObjectAsync = ol.parser.AsyncObjectFeatureParser.prototype.readFeaturesFromObjectAsync =
goog.abstractMethod; goog.abstractMethod;
/**
* @typedef {function(ol.Feature, ol.geom.GeometryType):ol.geom.SharedVertices}
*/
ol.parser.ReadFeaturesCallback;
/** /**
* @typedef {{projection: ol.proj.ProjectionLike}} * @typedef {{projection: ol.proj.ProjectionLike}}
*/ */
ol.parser.ReadFeaturesMetadata; ol.parser.ReadFeaturesMetadata;
/**
* @typedef {{callback: ol.parser.ReadFeaturesCallback}}
*/
ol.parser.ReadFeaturesOptions;
/** /**
* @typedef {{features: Array.<ol.Feature>, * @typedef {{features: Array.<ol.Feature>,
* metadata: ol.parser.ReadFeaturesMetadata}} * metadata: ol.parser.ReadFeaturesMetadata}}

View File

@@ -12,9 +12,7 @@ goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.Parser'); goog.require('ol.parser.Parser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.ReadFeaturesResult'); goog.require('ol.parser.ReadFeaturesResult');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
@@ -60,13 +58,11 @@ ol.parser.GeoJSON.read = function(str) {
/** /**
* Parse a GeoJSON feature collection. * Parse a GeoJSON feature collection.
* @param {string} str GeoJSON feature collection. * @param {string} str GeoJSON feature collection.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GeoJSON.prototype.readFeaturesFromString = ol.parser.GeoJSON.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
var json = /** @type {GeoJSONFeatureCollection} */ (JSON.parse(str)); var json = /** @type {GeoJSONFeatureCollection} */ (JSON.parse(str));
return this.parseAsFeatureCollection_(json, opt_options); return this.parseAsFeatureCollection_(json);
}; };
@@ -74,43 +70,38 @@ ol.parser.GeoJSON.prototype.readFeaturesFromString =
* Parse a GeoJSON feature collection from decoded JSON. * Parse a GeoJSON feature collection from decoded JSON.
* @param {GeoJSONFeatureCollection} object GeoJSON feature collection decoded * @param {GeoJSONFeatureCollection} object GeoJSON feature collection decoded
* from JSON. * from JSON.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GeoJSON.prototype.readFeaturesFromObject = ol.parser.GeoJSON.prototype.readFeaturesFromObject = function(object) {
function(object, opt_options) { return this.parseAsFeatureCollection_(object);
return this.parseAsFeatureCollection_(object, opt_options);
}; };
/** /**
* Parse any GeoJSON object. Note that this method should not be called * Parse any GeoJSON object.
* recursively due to the shared vertex creation.
* *
* @param {GeoJSONObject} json GeoJSON object. * @param {GeoJSONObject} json GeoJSON object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.Feature|Array.<ol.Feature>| * @return {ol.Feature|Array.<ol.Feature>|
* ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array * ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
* of geometries. * of geometries.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parse_ = function(json, opt_options) { ol.parser.GeoJSON.prototype.parse_ = function(json) {
var result; var result;
if (json.type === 'FeatureCollection') { if (json.type === 'FeatureCollection') {
result = this.parseFeatureCollection_( result = this.parseFeatureCollection_(
/** @type {GeoJSONFeatureCollection} */ (json), opt_options); /** @type {GeoJSONFeatureCollection} */ (json));
} else if (json.type === 'Feature') { } else if (json.type === 'Feature') {
result = this.parseFeature_( result = this.parseFeature_(
/** @type {GeoJSONFeature} */ (json), opt_options); /** @type {GeoJSONFeature} */ (json));
} else if (json.type === 'GeometryCollection') { } else if (json.type === 'GeometryCollection') {
result = this.parseGeometryCollection_( result = this.parseGeometryCollection_(
/** @type {GeoJSONGeometryCollection} */ (json), opt_options); /** @type {GeoJSONGeometryCollection} */ (json));
} else { } else {
// we've been called with a geometry or an unknown object // we've been called with a geometry or an unknown object
// create a feature to get shared vertices handling // create a feature to get shared vertices handling
var feature = this.parseFeature_( var feature = this.parseFeature_(
/** @type {GeoJSONFeature} */ ({type: 'Feature', geometry: json}), /** @type {GeoJSONFeature} */ ({type: 'Feature', geometry: json}));
opt_options);
result = feature.getGeometry(); result = feature.getGeometry();
} }
return result; return result;
@@ -119,14 +110,12 @@ ol.parser.GeoJSON.prototype.parse_ = function(json, opt_options) {
/** /**
* @param {GeoJSONObject} json GeoJSON object. * @param {GeoJSONObject} json GeoJSON object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Parsed object coerced into array of * @return {ol.parser.ReadFeaturesResult} Parsed object coerced into array of
* features. * features.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json, ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json) {
opt_options) { var obj = this.parse_(json);
var obj = this.parse_(json, opt_options);
var features = []; var features = [];
var feature; var feature;
if (obj instanceof ol.Feature) { if (obj instanceof ol.Feature) {
@@ -164,45 +153,36 @@ ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json,
/** /**
* @param {GeoJSONFeature} json GeoJSON feature. * @param {GeoJSONFeature} json GeoJSON feature.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Read options.
* @return {ol.Feature} Parsed feature. * @return {ol.Feature} Parsed feature.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseFeature_ = function(json, opt_options) { ol.parser.GeoJSON.prototype.parseFeature_ = function(json) {
var geomJson = json.geometry, var geomJson = json.geometry,
geometry = null, geometry = null;
options = opt_options || {};
var feature = new ol.Feature(json.properties); var feature = new ol.Feature(json.properties);
if (goog.isDef(json.id)) { if (goog.isDef(json.id)) {
feature.setId(json.id); feature.setId(json.id);
} }
if (geomJson) { if (geomJson) {
var type = geomJson.type; var type = geomJson.type;
var callback = options.callback;
var sharedVertices;
if (callback) {
goog.asserts.assert(type in ol.parser.GeoJSON.GeometryType,
'Bad geometry type: ' + type);
sharedVertices = callback(feature, ol.parser.GeoJSON.GeometryType[type]);
}
switch (type) { switch (type) {
case 'Point': case 'Point':
geometry = this.parsePoint_(geomJson, sharedVertices); geometry = this.parsePoint_(geomJson);
break; break;
case 'LineString': case 'LineString':
geometry = this.parseLineString_(geomJson, sharedVertices); geometry = this.parseLineString_(geomJson);
break; break;
case 'Polygon': case 'Polygon':
geometry = this.parsePolygon_(geomJson, sharedVertices); geometry = this.parsePolygon_(geomJson);
break; break;
case 'MultiPoint': case 'MultiPoint':
geometry = this.parseMultiPoint_(geomJson, sharedVertices); geometry = this.parseMultiPoint_(geomJson);
break; break;
case 'MultiLineString': case 'MultiLineString':
geometry = this.parseMultiLineString_(geomJson, sharedVertices); geometry = this.parseMultiLineString_(geomJson);
break; break;
case 'MultiPolygon': case 'MultiPolygon':
geometry = this.parseMultiPolygon_(geomJson, sharedVertices); geometry = this.parseMultiPolygon_(geomJson);
break; break;
default: default:
throw new Error('Bad geometry type: ' + type); throw new Error('Bad geometry type: ' + type);
@@ -215,20 +195,17 @@ ol.parser.GeoJSON.prototype.parseFeature_ = function(json, opt_options) {
/** /**
* @param {GeoJSONFeatureCollection} json GeoJSON feature collection. * @param {GeoJSONFeatureCollection} json GeoJSON feature collection.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Read options.
* @return {Array.<ol.Feature>} Parsed array of features. * @return {Array.<ol.Feature>} Parsed array of features.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseFeatureCollection_ = function( ol.parser.GeoJSON.prototype.parseFeatureCollection_ = function(json) {
json, opt_options) {
var features = json.features, var features = json.features,
len = features.length, len = features.length,
result = new Array(len), result = new Array(len),
i; i;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
result[i] = this.parseFeature_( result[i] = this.parseFeature_(/** @type {GeoJSONFeature} */ (features[i]));
/** @type {GeoJSONFeature} */ (features[i]), opt_options);
} }
return result; return result;
}; };
@@ -236,20 +213,17 @@ ol.parser.GeoJSON.prototype.parseFeatureCollection_ = function(
/** /**
* @param {GeoJSONGeometryCollection} json GeoJSON geometry collection. * @param {GeoJSONGeometryCollection} json GeoJSON geometry collection.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Read options.
* @return {Array.<ol.geom.Geometry>} Parsed array of geometries. * @return {Array.<ol.geom.Geometry>} Parsed array of geometries.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseGeometryCollection_ = function(json, ol.parser.GeoJSON.prototype.parseGeometryCollection_ = function(json) {
opt_options) {
var geometries = json.geometries, var geometries = json.geometries,
len = geometries.length, len = geometries.length,
result = new Array(len), result = new Array(len),
i; i;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
result[i] = this.parse_(/** @type {GeoJSONGeometry} */ (geometries[i]), result[i] = this.parse_(/** @type {GeoJSONGeometry} */ (geometries[i]));
opt_options);
} }
return result; return result;
}; };
@@ -257,68 +231,61 @@ ol.parser.GeoJSON.prototype.parseGeometryCollection_ = function(json,
/** /**
* @param {GeoJSONGeometry} json GeoJSON linestring. * @param {GeoJSONGeometry} json GeoJSON linestring.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.LineString} Parsed linestring. * @return {ol.geom.LineString} Parsed linestring.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseLineString_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parseLineString_ = function(json) {
return new ol.geom.LineString(json.coordinates, opt_vertices); return new ol.geom.LineString(json.coordinates);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON multi-linestring. * @param {GeoJSONGeometry} json GeoJSON multi-linestring.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.MultiLineString} Parsed multi-linestring. * @return {ol.geom.MultiLineString} Parsed multi-linestring.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseMultiLineString_ = function( ol.parser.GeoJSON.prototype.parseMultiLineString_ = function(json) {
json, opt_vertices) { return new ol.geom.MultiLineString(json.coordinates);
return new ol.geom.MultiLineString(json.coordinates, opt_vertices);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON multi-point. * @param {GeoJSONGeometry} json GeoJSON multi-point.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.MultiPoint} Parsed multi-point. * @return {ol.geom.MultiPoint} Parsed multi-point.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseMultiPoint_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parseMultiPoint_ = function(json) {
return new ol.geom.MultiPoint(json.coordinates, opt_vertices); return new ol.geom.MultiPoint(json.coordinates);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON multi-polygon. * @param {GeoJSONGeometry} json GeoJSON multi-polygon.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.MultiPolygon} Parsed multi-polygon. * @return {ol.geom.MultiPolygon} Parsed multi-polygon.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parseMultiPolygon_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parseMultiPolygon_ = function(json) {
return new ol.geom.MultiPolygon(json.coordinates, opt_vertices); return new ol.geom.MultiPolygon(json.coordinates);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON point. * @param {GeoJSONGeometry} json GeoJSON point.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Point} Parsed point. * @return {ol.geom.Point} Parsed point.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parsePoint_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parsePoint_ = function(json) {
return new ol.geom.Point(json.coordinates, opt_vertices); return new ol.geom.Point(json.coordinates);
}; };
/** /**
* @param {GeoJSONGeometry} json GeoJSON polygon. * @param {GeoJSONGeometry} json GeoJSON polygon.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Polygon} Parsed polygon. * @return {ol.geom.Polygon} Parsed polygon.
* @private * @private
*/ */
ol.parser.GeoJSON.prototype.parsePolygon_ = function(json, opt_vertices) { ol.parser.GeoJSON.prototype.parsePolygon_ = function(json) {
return new ol.geom.Polygon(json.coordinates, opt_vertices); return new ol.geom.Polygon(json.coordinates);
}; };

View File

@@ -9,7 +9,6 @@ goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.parser.DomFeatureParser'); goog.require('ol.parser.DomFeatureParser');
goog.require('ol.parser.ObjectFeatureParser'); goog.require('ol.parser.ObjectFeatureParser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
goog.require('ol.parser.XML'); goog.require('ol.parser.XML');
@@ -58,14 +57,7 @@ ol.parser.GPX = function(opt_options) {
parseFloat(node.getAttribute('lat'))]; parseFloat(node.getAttribute('lat'))];
this.readChildNodes(node, properties); this.readChildNodes(node, properties);
var feature = new ol.Feature(properties); var feature = new ol.Feature(properties);
var sharedVertices; var geometry = new ol.geom.Point(coordinates);
if (this.readFeaturesOptions_) {
var callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, ol.geom.GeometryType.POINT);
}
}
var geometry = new ol.geom.Point(coordinates, sharedVertices);
feature.setGeometry(geometry); feature.setGeometry(geometry);
obj.features.push(feature); obj.features.push(feature);
} }
@@ -82,15 +74,7 @@ ol.parser.GPX = function(opt_options) {
}; };
this.readChildNodes(node, container); this.readChildNodes(node, container);
var feature = new ol.Feature(container.properties); var feature = new ol.Feature(container.properties);
var sharedVertices; var geometry = new ol.geom.LineString(container.geometry.coordinates);
if (this.readFeaturesOptions_) {
var callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, type);
}
}
var geometry = new ol.geom.LineString(container.geometry.coordinates,
sharedVertices);
feature.setGeometry(geometry); feature.setGeometry(geometry);
obj.features.push(feature); obj.features.push(feature);
} }
@@ -255,12 +239,9 @@ ol.parser.GPX.prototype.read = function(data) {
/** /**
* Parse a GPX document provided as a string. * Parse a GPX document provided as a string.
* @param {string} str GPX document. * @param {string} str GPX document.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GPX.prototype.readFeaturesFromString = ol.parser.GPX.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(str); return this.read(str);
}; };
@@ -268,24 +249,18 @@ ol.parser.GPX.prototype.readFeaturesFromString =
/** /**
* Parse a GPX document provided as a DOM structure. * Parse a GPX document provided as a DOM structure.
* @param {Element|Document} node Document or element node. * @param {Element|Document} node Document or element node.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GPX.prototype.readFeaturesFromNode = ol.parser.GPX.prototype.readFeaturesFromNode = function(node) {
function(node, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(node); return this.read(node);
}; };
/** /**
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.GPX.prototype.readFeaturesFromObject = ol.parser.GPX.prototype.readFeaturesFromObject = function(obj) {
function(obj, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(obj); return this.read(obj);
}; };

View File

@@ -20,11 +20,9 @@ goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.AsyncObjectFeatureParser'); goog.require('ol.parser.AsyncObjectFeatureParser');
goog.require('ol.parser.AsyncStringFeatureParser'); goog.require('ol.parser.AsyncStringFeatureParser');
goog.require('ol.parser.DomFeatureParser'); goog.require('ol.parser.DomFeatureParser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
goog.require('ol.parser.XML'); goog.require('ol.parser.XML');
goog.require('ol.style.Fill'); goog.require('ol.style.Fill');
@@ -104,7 +102,6 @@ ol.parser.KML = function(opt_options) {
}, },
'Placemark': function(node, obj) { 'Placemark': function(node, obj) {
var container = {properties: {}}; var container = {properties: {}};
var sharedVertices, callback;
var id = node.getAttribute('id'); var id = node.getAttribute('id');
this.readChildNodes(node, container); this.readChildNodes(node, container);
if (goog.isDef(container.track)) { if (goog.isDef(container.track)) {
@@ -132,15 +129,7 @@ ol.parser.KML = function(opt_options) {
} }
var geom = track.points[i]; var geom = track.points[i];
if (geom) { if (geom) {
sharedVertices = undefined; var geometry = this.createGeometry_({geometry: geom});
if (this.readFeaturesOptions_) {
callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, geom.type);
}
}
var geometry = this.createGeometry_({geometry: geom},
sharedVertices);
if (goog.isDef(geometry)) { if (goog.isDef(geometry)) {
feature.setGeometry(geometry); feature.setGeometry(geometry);
} }
@@ -159,14 +148,7 @@ ol.parser.KML = function(opt_options) {
feature.setId(id); feature.setId(id);
} }
if (container.geometry) { if (container.geometry) {
sharedVertices = undefined; geometry = this.createGeometry_(container);
if (this.readFeaturesOptions_) {
callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, container.geometry.type);
}
}
geometry = this.createGeometry_(container, sharedVertices);
if (goog.isDef(geometry)) { if (goog.isDef(geometry)) {
feature.setGeometry(geometry); feature.setGeometry(geometry);
} }
@@ -850,11 +832,8 @@ goog.inherits(ol.parser.KML, ol.parser.XML);
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is * @param {function(ol.parser.ReadFeaturesResult)} callback Callback which is
* called after parsing. * called after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.KML.prototype.readFeaturesFromObjectAsync = ol.parser.KML.prototype.readFeaturesFromObjectAsync = function(obj, callback) {
function(obj, callback, opt_options) {
this.readFeaturesOptions_ = opt_options;
this.read(obj, callback); this.read(obj, callback);
}; };
@@ -863,11 +842,8 @@ ol.parser.KML.prototype.readFeaturesFromObjectAsync =
* @param {string} str String data. * @param {string} str String data.
* @param {function(ol.parser.ReadFeaturesResult)} * @param {function(ol.parser.ReadFeaturesResult)}
* callback Callback which is called after parsing. * callback Callback which is called after parsing.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
*/ */
ol.parser.KML.prototype.readFeaturesFromStringAsync = ol.parser.KML.prototype.readFeaturesFromStringAsync = function(str, callback) {
function(str, callback, opt_options) {
this.readFeaturesOptions_ = opt_options;
this.read(str, callback); this.read(str, callback);
}; };
@@ -875,12 +851,9 @@ ol.parser.KML.prototype.readFeaturesFromStringAsync =
/** /**
* Parse a KML document provided as a string. * Parse a KML document provided as a string.
* @param {string} str KML document. * @param {string} str KML document.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.KML.prototype.readFeaturesFromString = ol.parser.KML.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
this.readFeaturesOptions_ = opt_options;
return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(str)); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(str));
}; };
@@ -888,24 +861,18 @@ ol.parser.KML.prototype.readFeaturesFromString =
/** /**
* Parse a KML document provided as a DOM structure. * Parse a KML document provided as a DOM structure.
* @param {Element|Document} node Document or element node. * @param {Element|Document} node Document or element node.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.KML.prototype.readFeaturesFromNode = ol.parser.KML.prototype.readFeaturesFromNode = function(node) {
function(node, opt_options) {
this.readFeaturesOptions_ = opt_options;
return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(node)); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(node));
}; };
/** /**
* @param {Object} obj Object representing features. * @param {Object} obj Object representing features.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Feature reading options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.KML.prototype.readFeaturesFromObject = ol.parser.KML.prototype.readFeaturesFromObject = function(obj) {
function(obj, opt_options) {
this.readFeaturesOptions_ = opt_options;
return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(obj)); return /** @type {ol.parser.ReadFeaturesResult} */ (this.read(obj));
}; };
@@ -1028,52 +995,47 @@ ol.parser.KML.prototype.applyStyle_ = function(feature, styles,
/** /**
* @private * @private
* @param {Object} container Geometry container. * @param {Object} container Geometry container.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Geometry} The geometry created. * @return {ol.geom.Geometry} The geometry created.
*/ */
ol.parser.KML.prototype.createGeometry_ = function(container, ol.parser.KML.prototype.createGeometry_ = function(container) {
opt_vertices) {
var geometry = null, coordinates, i, ii; var geometry = null, coordinates, i, ii;
switch (container.geometry.type) { switch (container.geometry.type) {
case ol.geom.GeometryType.POINT: case ol.geom.GeometryType.POINT:
geometry = new ol.geom.Point(container.geometry.coordinates, geometry = new ol.geom.Point(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.LINESTRING: case ol.geom.GeometryType.LINESTRING:
geometry = new ol.geom.LineString(container.geometry.coordinates, geometry = new ol.geom.LineString(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.POLYGON: case ol.geom.GeometryType.POLYGON:
geometry = new ol.geom.Polygon(container.geometry.coordinates, geometry = new ol.geom.Polygon(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.MULTIPOINT: case ol.geom.GeometryType.MULTIPOINT:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiPoint(coordinates, opt_vertices); geometry = new ol.geom.MultiPoint(coordinates);
break; break;
case ol.geom.GeometryType.MULTILINESTRING: case ol.geom.GeometryType.MULTILINESTRING:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiLineString(coordinates, opt_vertices); geometry = new ol.geom.MultiLineString(coordinates);
break; break;
case ol.geom.GeometryType.MULTIPOLYGON: case ol.geom.GeometryType.MULTIPOLYGON:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiPolygon(coordinates, opt_vertices); geometry = new ol.geom.MultiPolygon(coordinates);
break; break;
case ol.geom.GeometryType.GEOMETRYCOLLECTION: case ol.geom.GeometryType.GEOMETRYCOLLECTION:
var geometries = []; var geometries = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
geometries.push(this.createGeometry_({ geometries.push(this.createGeometry_({
geometry: container.geometry.parts[i] geometry: container.geometry.parts[i]
}, opt_vertices)); }));
} }
geometry = new ol.geom.GeometryCollection(geometries); geometry = new ol.geom.GeometryCollection(geometries);
break; break;

View File

@@ -303,15 +303,7 @@ ol.parser.ogc.GML = function(opt_options) {
var feature = new ol.Feature(container.properties); var feature = new ol.Feature(container.properties);
var geom = container.geometry; var geom = container.geometry;
if (geom) { if (geom) {
var sharedVertices = undefined; var geometry = this.createGeometry({geometry: geom});
if (this.readFeaturesOptions_) {
var callback = this.readFeaturesOptions_.callback;
if (callback) {
sharedVertices = callback(feature, geom.type);
}
}
var geometry = this.createGeometry({geometry: geom},
sharedVertices);
if (goog.isDef(geometry)) { if (goog.isDef(geometry)) {
feature.setGeometry(geometry); feature.setGeometry(geometry);
} }
@@ -551,57 +543,51 @@ ol.parser.ogc.GML.prototype.readNode = function(node, obj, opt_first) {
/** /**
* @param {Object} container Geometry container. * @param {Object} container Geometry container.
* @param {ol.geom.SharedVertices=} opt_vertices Shared vertices.
* @return {ol.geom.Geometry} The geometry created. * @return {ol.geom.Geometry} The geometry created.
*/ */
// TODO use a mixin since this is also used in the KML parser // TODO use a mixin since this is also used in the KML parser
ol.parser.ogc.GML.prototype.createGeometry = function(container, ol.parser.ogc.GML.prototype.createGeometry = function(container) {
opt_vertices) {
var geometry = null, coordinates, i, ii; var geometry = null, coordinates, i, ii;
switch (container.geometry.type) { switch (container.geometry.type) {
case ol.geom.GeometryType.POINT: case ol.geom.GeometryType.POINT:
geometry = new ol.geom.Point(container.geometry.coordinates, geometry = new ol.geom.Point(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.LINEARRING: case ol.geom.GeometryType.LINEARRING:
geometry = new ol.geom.LinearRing(container.geometry.coordinates, geometry = new ol.geom.LinearRing(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.LINESTRING: case ol.geom.GeometryType.LINESTRING:
geometry = new ol.geom.LineString(container.geometry.coordinates, geometry = new ol.geom.LineString(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.POLYGON: case ol.geom.GeometryType.POLYGON:
geometry = new ol.geom.Polygon(container.geometry.coordinates, geometry = new ol.geom.Polygon(container.geometry.coordinates);
opt_vertices);
break; break;
case ol.geom.GeometryType.MULTIPOINT: case ol.geom.GeometryType.MULTIPOINT:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiPoint(coordinates, opt_vertices); geometry = new ol.geom.MultiPoint(coordinates);
break; break;
case ol.geom.GeometryType.MULTILINESTRING: case ol.geom.GeometryType.MULTILINESTRING:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiLineString(coordinates, opt_vertices); geometry = new ol.geom.MultiLineString(coordinates);
break; break;
case ol.geom.GeometryType.MULTIPOLYGON: case ol.geom.GeometryType.MULTIPOLYGON:
coordinates = []; coordinates = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
coordinates.push(container.geometry.parts[i].coordinates); coordinates.push(container.geometry.parts[i].coordinates);
} }
geometry = new ol.geom.MultiPolygon(coordinates, opt_vertices); geometry = new ol.geom.MultiPolygon(coordinates);
break; break;
case ol.geom.GeometryType.GEOMETRYCOLLECTION: case ol.geom.GeometryType.GEOMETRYCOLLECTION:
var geometries = []; var geometries = [];
for (i = 0, ii = container.geometry.parts.length; i < ii; i++) { for (i = 0, ii = container.geometry.parts.length; i < ii; i++) {
geometries.push(this.createGeometry({ geometries.push(this.createGeometry({
geometry: container.geometry.parts[i] geometry: container.geometry.parts[i]
}, opt_vertices)); }));
} }
geometry = new ol.geom.GeometryCollection(geometries); geometry = new ol.geom.GeometryCollection(geometries);
break; break;
@@ -615,12 +601,9 @@ ol.parser.ogc.GML.prototype.createGeometry = function(container,
/** /**
* Parse a GML document provided as a string. * Parse a GML document provided as a string.
* @param {string} str GML document. * @param {string} str GML document.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.ogc.GML.prototype.readFeaturesFromString = ol.parser.ogc.GML.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
this.readFeaturesOptions_ = opt_options;
return this.read(str); return this.read(str);
}; };

View File

@@ -3,7 +3,6 @@ goog.provide('ol.parser.TopoJSON');
goog.require('ol.Coordinate'); goog.require('ol.Coordinate');
goog.require('ol.CoordinateArray'); goog.require('ol.CoordinateArray');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString'); goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.MultiPoint');
@@ -11,7 +10,6 @@ goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.parser.Parser'); goog.require('ol.parser.Parser');
goog.require('ol.parser.ReadFeaturesOptions');
goog.require('ol.parser.StringFeatureParser'); goog.require('ol.parser.StringFeatureParser');
@@ -23,18 +21,7 @@ goog.require('ol.parser.StringFeatureParser');
* @implements {ol.parser.StringFeatureParser} * @implements {ol.parser.StringFeatureParser}
* @extends {ol.parser.Parser} * @extends {ol.parser.Parser}
*/ */
ol.parser.TopoJSON = function() { ol.parser.TopoJSON = function() {};
/**
* Common feature for all shared vertex creation.
* // TODO: make feature optional in shared vertex callback
*
* @type {ol.Feature}
* @private
*/
this.feature_ = new ol.Feature();
};
goog.inherits(ol.parser.TopoJSON, ol.parser.Parser); goog.inherits(ol.parser.TopoJSON, ol.parser.Parser);
goog.addSingletonGetter(ol.parser.TopoJSON); goog.addSingletonGetter(ol.parser.TopoJSON);
@@ -65,6 +52,10 @@ ol.parser.TopoJSON.prototype.concatenateArcs_ = function(indices, arcs) {
} }
coordinates.push.apply(coordinates, arc); coordinates.push.apply(coordinates, arc);
} }
// provide fresh copies of coordinate arrays
for (var j = 0, jj = coordinates.length; j < jj; ++j) {
coordinates[j] = coordinates[j].slice();
}
return coordinates; return coordinates;
}; };
@@ -84,17 +75,17 @@ ol.parser.TopoJSON.prototype.read = function(str) {
* Create features from a TopoJSON topology string. * Create features from a TopoJSON topology string.
* *
* @param {string} str TopoJSON topology string. * @param {string} str TopoJSON topology string.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.TopoJSON.prototype.readFeaturesFromString = ol.parser.TopoJSON.prototype.readFeaturesFromString = function(str) {
function(str, opt_options) {
var topology = /** @type {TopoJSONTopology} */ (JSON.parse(str)); var topology = /** @type {TopoJSONTopology} */ (JSON.parse(str));
if (topology.type !== 'Topology') { if (topology.type !== 'Topology') {
throw new Error('Not a "Topology" type object'); throw new Error('Not a "Topology" type object');
} }
return {features: this.readFeaturesFromTopology_(topology, opt_options), return {
metadata: {projection: 'EPSG:4326'}}; features: this.readFeaturesFromTopology_(topology),
metadata: {projection: 'EPSG:4326'}
};
}; };
@@ -102,16 +93,16 @@ ol.parser.TopoJSON.prototype.readFeaturesFromString =
* Create features from a TopoJSON topology object. * Create features from a TopoJSON topology object.
* *
* @param {TopoJSONTopology} topology TopoJSON topology object. * @param {TopoJSONTopology} topology TopoJSON topology object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.parser.ReadFeaturesResult} Features and metadata. * @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/ */
ol.parser.TopoJSON.prototype.readFeaturesFromObject = ol.parser.TopoJSON.prototype.readFeaturesFromObject = function(topology) {
function(topology, opt_options) {
if (topology.type !== 'Topology') { if (topology.type !== 'Topology') {
throw new Error('Not a "Topology" type object'); throw new Error('Not a "Topology" type object');
} }
return {features: this.readFeaturesFromTopology_(topology, opt_options), return {
metadata: {projection: 'EPSG:4326'}}; features: this.readFeaturesFromTopology_(topology),
metadata: {projection: 'EPSG:4326'}
};
}; };
@@ -122,32 +113,30 @@ ol.parser.TopoJSON.prototype.readFeaturesFromObject =
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {Array.<number>} scale Scale for each dimension. * @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension. * @param {Array.<number>} translate Translation for each dimension.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.Feature} Feature. * @return {ol.Feature} Feature.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readFeatureFromGeometry_ = function(object, arcs, ol.parser.TopoJSON.prototype.readFeatureFromGeometry_ = function(object, arcs,
scale, translate, opt_options) { scale, translate) {
var geometry; var geometry;
var type = object.type; var type = object.type;
if (type === 'Point') { if (type === 'Point') {
geometry = this.readPoint_(/** @type {TopoJSONPoint} */ (object), scale, geometry = this.readPoint_(/** @type {TopoJSONPoint} */ (object), scale,
translate, opt_options); translate);
} else if (type === 'LineString') { } else if (type === 'LineString') {
geometry = this.readLineString_(/** @type {TopoJSONLineString} */ (object), geometry = this.readLineString_(/** @type {TopoJSONLineString} */ (object),
arcs, opt_options); arcs);
} else if (type === 'Polygon') { } else if (type === 'Polygon') {
geometry = this.readPolygon_(/** @type {TopoJSONPolygon} */ (object), arcs, geometry = this.readPolygon_(/** @type {TopoJSONPolygon} */ (object), arcs);
opt_options);
} else if (type === 'MultiPoint') { } else if (type === 'MultiPoint') {
geometry = this.readMultiPoint_(/** @type {TopoJSONMultiPoint} */ (object), geometry = this.readMultiPoint_(/** @type {TopoJSONMultiPoint} */ (object),
scale, translate, opt_options); scale, translate);
} else if (type === 'MultiLineString') { } else if (type === 'MultiLineString') {
geometry = this.readMultiLineString_( geometry = this.readMultiLineString_(
/** @type {TopoJSONMultiLineString} */(object), arcs, opt_options); /** @type {TopoJSONMultiLineString} */(object), arcs);
} else if (type === 'MultiPolygon') { } else if (type === 'MultiPolygon') {
geometry = this.readMultiPolygon_( geometry = this.readMultiPolygon_(
/** @type {TopoJSONMultiPolygon} */ (object), arcs, opt_options); /** @type {TopoJSONMultiPolygon} */ (object), arcs);
} else { } else {
throw new Error('Unsupported geometry type: ' + type); throw new Error('Unsupported geometry type: ' + type);
} }
@@ -168,18 +157,17 @@ ol.parser.TopoJSON.prototype.readFeatureFromGeometry_ = function(object, arcs,
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {Array.<number>} scale Scale for each dimension. * @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension. * @param {Array.<number>} translate Translation for each dimension.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {Array.<ol.Feature>} Array of features. * @return {Array.<ol.Feature>} Array of features.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readFeaturesFromGeometryCollection_ = function( ol.parser.TopoJSON.prototype.readFeaturesFromGeometryCollection_ = function(
collection, arcs, scale, translate, opt_options) { collection, arcs, scale, translate) {
var geometries = collection.geometries; var geometries = collection.geometries;
var num = geometries.length; var num = geometries.length;
var features = new Array(num); var features = new Array(num);
for (var i = 0; i < num; ++i) { for (var i = 0; i < num; ++i) {
features[i] = this.readFeatureFromGeometry_(geometries[i], arcs, scale, features[i] = this.readFeatureFromGeometry_(geometries[i], arcs, scale,
translate, opt_options); translate);
} }
return features; return features;
}; };
@@ -187,12 +175,10 @@ ol.parser.TopoJSON.prototype.readFeaturesFromGeometryCollection_ = function(
/** /**
* @param {TopoJSONTopology} topology TopoJSON object. * @param {TopoJSONTopology} topology TopoJSON object.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {Array.<ol.Feature>} Parsed features. * @return {Array.<ol.Feature>} Parsed features.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function( ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function(topology) {
topology, opt_options) {
var transform = topology.transform; var transform = topology.transform;
var scale = transform.scale; var scale = transform.scale;
var translate = transform.translate; var translate = transform.translate;
@@ -204,11 +190,11 @@ ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function(
if (objects[key].type === 'GeometryCollection') { if (objects[key].type === 'GeometryCollection') {
features.push.apply(features, this.readFeaturesFromGeometryCollection_( features.push.apply(features, this.readFeaturesFromGeometryCollection_(
/** @type {TopoJSONGeometryCollection} */ (objects[key]), /** @type {TopoJSONGeometryCollection} */ (objects[key]),
arcs, scale, translate, opt_options)); arcs, scale, translate));
} else { } else {
features.push(this.readFeatureFromGeometry_( features.push(this.readFeatureFromGeometry_(
/** @type {TopoJSONGeometry} */ (objects[key]), /** @type {TopoJSONGeometry} */ (objects[key]),
arcs, scale, translate, opt_options)); arcs, scale, translate));
} }
} }
return features; return features;
@@ -220,20 +206,12 @@ ol.parser.TopoJSON.prototype.readFeaturesFromTopology_ = function(
* *
* @param {TopoJSONLineString} object TopoJSON object. * @param {TopoJSONLineString} object TopoJSON object.
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.LineString} Geometry. * @return {ol.geom.LineString} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readLineString_ = function(object, arcs, ol.parser.TopoJSON.prototype.readLineString_ = function(object, arcs) {
opt_options) {
var coordinates = this.concatenateArcs_(object.arcs, arcs); var coordinates = this.concatenateArcs_(object.arcs, arcs);
// TODO: make feature optional in callback return new ol.geom.LineString(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.LINESTRING);
}
return new ol.geom.LineString(coordinates, sharedVertices);
}; };
@@ -242,26 +220,17 @@ ol.parser.TopoJSON.prototype.readLineString_ = function(object, arcs,
* *
* @param {TopoJSONMultiLineString} object TopoJSON object. * @param {TopoJSONMultiLineString} object TopoJSON object.
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.MultiLineString} Geometry. * @return {ol.geom.MultiLineString} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readMultiLineString_ = function(object, arcs, ol.parser.TopoJSON.prototype.readMultiLineString_ = function(object, arcs) {
opt_options) {
var array = object.arcs; // I'm out of good names var array = object.arcs; // I'm out of good names
var num = array.length; var num = array.length;
var coordinates = new Array(num); var coordinates = new Array(num);
for (var i = 0; i < num; ++i) { for (var i = 0; i < num; ++i) {
coordinates[i] = this.concatenateArcs_(array[i], arcs); coordinates[i] = this.concatenateArcs_(array[i], arcs);
} }
// TODO: make feature optional in callback return new ol.geom.MultiLineString(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_,
ol.geom.GeometryType.MULTILINESTRING);
}
return new ol.geom.MultiLineString(coordinates, sharedVertices);
}; };
@@ -271,23 +240,16 @@ ol.parser.TopoJSON.prototype.readMultiLineString_ = function(object, arcs,
* @param {TopoJSONMultiPoint} object TopoJSON object. * @param {TopoJSONMultiPoint} object TopoJSON object.
* @param {Array.<number>} scale Scale for each dimension. * @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension. * @param {Array.<number>} translate Translation for each dimension.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.MultiPoint} Geometry. * @return {ol.geom.MultiPoint} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readMultiPoint_ = function(object, scale, ol.parser.TopoJSON.prototype.readMultiPoint_ = function(object, scale,
translate, opt_options) { translate) {
var coordinates = object.coordinates; var coordinates = object.coordinates;
for (var i = 0, ii = coordinates.length; i < ii; ++i) { for (var i = 0, ii = coordinates.length; i < ii; ++i) {
this.transformVertex_(coordinates[i], scale, translate); this.transformVertex_(coordinates[i], scale, translate);
} }
// TODO: make feature optional in callback return new ol.geom.MultiPoint(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.MULTIPOINT);
}
return new ol.geom.MultiPoint(coordinates, sharedVertices);
}; };
@@ -296,12 +258,10 @@ ol.parser.TopoJSON.prototype.readMultiPoint_ = function(object, scale,
* *
* @param {TopoJSONMultiPolygon} object TopoJSON object. * @param {TopoJSONMultiPolygon} object TopoJSON object.
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.MultiPolygon} Geometry. * @return {ol.geom.MultiPolygon} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readMultiPolygon_ = function(object, arcs, ol.parser.TopoJSON.prototype.readMultiPolygon_ = function(object, arcs) {
opt_options) {
var array = object.arcs; var array = object.arcs;
var numPolys = array.length; var numPolys = array.length;
var coordinates = new Array(numPolys); var coordinates = new Array(numPolys);
@@ -317,13 +277,7 @@ ol.parser.TopoJSON.prototype.readMultiPolygon_ = function(object, arcs,
} }
coordinates[i] = ringCoords; coordinates[i] = ringCoords;
} }
// TODO: make feature optional in callback return new ol.geom.MultiPolygon(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.MULTIPOLYGON);
}
return new ol.geom.MultiPolygon(coordinates, sharedVertices);
}; };
@@ -333,21 +287,13 @@ ol.parser.TopoJSON.prototype.readMultiPolygon_ = function(object, arcs,
* @param {TopoJSONPoint} object TopoJSON object. * @param {TopoJSONPoint} object TopoJSON object.
* @param {Array.<number>} scale Scale for each dimension. * @param {Array.<number>} scale Scale for each dimension.
* @param {Array.<number>} translate Translation for each dimension. * @param {Array.<number>} translate Translation for each dimension.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.Point} Geometry. * @return {ol.geom.Point} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readPoint_ = function(object, scale, translate, ol.parser.TopoJSON.prototype.readPoint_ = function(object, scale, translate) {
opt_options) {
var coordinates = object.coordinates; var coordinates = object.coordinates;
this.transformVertex_(coordinates, scale, translate); this.transformVertex_(coordinates, scale, translate);
// TODO: make feature optional in callback return new ol.geom.Point(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.POINT);
}
return new ol.geom.Point(coordinates, sharedVertices);
}; };
@@ -356,25 +302,17 @@ ol.parser.TopoJSON.prototype.readPoint_ = function(object, scale, translate,
* *
* @param {TopoJSONPolygon} object TopoJSON object. * @param {TopoJSONPolygon} object TopoJSON object.
* @param {Array.<ol.CoordinateArray>} arcs Array of arcs. * @param {Array.<ol.CoordinateArray>} arcs Array of arcs.
* @param {ol.parser.ReadFeaturesOptions=} opt_options Reader options.
* @return {ol.geom.Polygon} Geometry. * @return {ol.geom.Polygon} Geometry.
* @private * @private
*/ */
ol.parser.TopoJSON.prototype.readPolygon_ = function(object, arcs, ol.parser.TopoJSON.prototype.readPolygon_ = function(object, arcs) {
opt_options) {
var array = object.arcs; // I'm out of good names var array = object.arcs; // I'm out of good names
var num = array.length; var num = array.length;
var coordinates = new Array(num); var coordinates = new Array(num);
for (var i = 0; i < num; ++i) { for (var i = 0; i < num; ++i) {
coordinates[i] = this.concatenateArcs_(array[i], arcs); coordinates[i] = this.concatenateArcs_(array[i], arcs);
} }
// TODO: make feature optional in callback return new ol.geom.Polygon(coordinates);
var callback = opt_options && opt_options.callback;
var sharedVertices;
if (callback) {
sharedVertices = callback(this.feature_, ol.geom.GeometryType.POLYGON);
}
return new ol.geom.Polygon(coordinates, sharedVertices);
}; };

View File

@@ -218,74 +218,11 @@ describe('ol.parser.GeoJSON', function() {
}); });
}); });
it('parses countries.geojson with shared vertices', function() {
afterLoadText('spec/ol/parser/geojson/countries.geojson', function(text) {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var result = parser.readFeaturesFromString(text,
{callback: callback}).features;
expect(result.length).to.be(179);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(21344);
var first = result[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('name')).to.be('Afghanistan');
var firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(ol.geom.Polygon);
expect(ol.extent.equals(firstGeom.getBounds(),
[60.52843, 29.318572, 75.158028, 38.486282]))
.to.be(true);
var last = result[178];
expect(last).to.be.a(ol.Feature);
expect(last.get('name')).to.be('Zimbabwe');
var lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(ol.geom.Polygon);
expect(ol.extent.equals(lastGeom.getBounds(),
[25.264226, -22.271612, 32.849861, -15.507787]))
.to.be(true);
});
});
}); });
describe('#parseAsFeatureCollection_()', function() { describe('#parseAsFeatureCollection_()', function() {
it('generates an array of features for FeatureCollection', function() { it('generates an array of features for FeatureCollection', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -310,8 +247,7 @@ describe('ol.parser.GeoJSON', function() {
} }
}] }]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(2); expect(features.length).to.be(2);
@@ -326,30 +262,10 @@ describe('ol.parser.GeoJSON', function() {
expect(second.get('bam')).to.be('baz'); expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString); expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('reads named crs from top-level object', function() { it('reads named crs from top-level object', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -380,8 +296,7 @@ describe('ol.parser.GeoJSON', function() {
} }
}] }]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(2); expect(features.length).to.be(2);
@@ -396,30 +311,10 @@ describe('ol.parser.GeoJSON', function() {
expect(second.get('bam')).to.be('baz'); expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString); expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:1234'); expect(result.metadata.projection).to.be('EPSG:1234');
}); });
it('accepts null crs', function() { it('accepts null crs', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -445,8 +340,7 @@ describe('ol.parser.GeoJSON', function() {
} }
}] }]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(2); expect(features.length).to.be(2);
@@ -461,30 +355,10 @@ describe('ol.parser.GeoJSON', function() {
expect(second.get('bam')).to.be('baz'); expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString); expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for Feature', function() { it('generates an array of features for Feature', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -497,8 +371,7 @@ describe('ol.parser.GeoJSON', function() {
coordinates: [[1, 2], [3, 4]] coordinates: [[1, 2], [3, 4]]
} }
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(1); expect(features.length).to.be(1);
@@ -508,30 +381,10 @@ describe('ol.parser.GeoJSON', function() {
expect(first.get('bam')).to.be('baz'); expect(first.get('bam')).to.be('baz');
expect(first.getGeometry()).to.be.a(ol.geom.LineString); expect(first.getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for GeometryCollection', function() { it('generates an array of features for GeometryCollection', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
@@ -547,8 +400,7 @@ describe('ol.parser.GeoJSON', function() {
coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]] coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]]
}] }]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(3); expect(features.length).to.be(3);
@@ -557,124 +409,57 @@ describe('ol.parser.GeoJSON', function() {
expect(features[1].getGeometry()).to.be.a(ol.geom.LineString); expect(features[1].getGeometry()).to.be.a(ol.geom.LineString);
expect(features[2].getGeometry()).to.be.a(ol.geom.Polygon); expect(features[2].getGeometry()).to.be.a(ol.geom.Polygon);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(8);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for Point', function() { it('generates an array of features for Point', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
type: 'Point', type: 'Point',
coordinates: [1, 2] coordinates: [1, 2]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(1); expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.Point); expect(features[0].getGeometry()).to.be.a(ol.geom.Point);
expect(pointVertices.coordinates.length).to.be(2);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for LineString', function() { it('generates an array of features for LineString', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
type: 'LineString', type: 'LineString',
coordinates: [[3, 4], [5, 6]] coordinates: [[3, 4], [5, 6]]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(1); expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.LineString); expect(features[0].getGeometry()).to.be.a(ol.geom.LineString);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(4);
expect(polygonVertices.coordinates.length).to.be(0);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
it('generates an array of features for Polygon', function() { it('generates an array of features for Polygon', function() {
var pointVertices = new ol.geom.SharedVertices();
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var parser = new ol.parser.GeoJSON(); var parser = new ol.parser.GeoJSON();
var json = { var json = {
type: 'Polygon', type: 'Polygon',
coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]] coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]]
}; };
var result = parser.parseAsFeatureCollection_(json, var result = parser.parseAsFeatureCollection_(json);
{callback: callback});
var features = result.features; var features = result.features;
expect(features.length).to.be(1); expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.Polygon); expect(features[0].getGeometry()).to.be.a(ol.geom.Polygon);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(8);
expect(result.metadata.projection).to.be('EPSG:4326'); expect(result.metadata.projection).to.be('EPSG:4326');
}); });
@@ -690,5 +475,4 @@ goog.require('ol.geom.LinearRing');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.GeoJSON'); goog.require('ol.parser.GeoJSON');

View File

@@ -56,33 +56,12 @@ describe('ol.parser.TopoJSON', function() {
describe('#readFeaturesFromString()', function() { describe('#readFeaturesFromString()', function() {
it('parses world-110m.geojson with shared vertices', function(done) { it('parses world-110m.geojson', function(done) {
afterLoadText('spec/ol/parser/topojson/world-110m.json', function(text) { afterLoadText('spec/ol/parser/topojson/world-110m.json', function(text) {
var pointVertices = new ol.geom.SharedVertices(); var result = parser.readFeaturesFromString(text);
var lineVertices = new ol.geom.SharedVertices();
var polygonVertices = new ol.geom.SharedVertices();
var lookup = {
'point': pointVertices,
'linestring': lineVertices,
'polygon': polygonVertices,
'multipoint': pointVertices,
'multilinstring': lineVertices,
'multipolygon': polygonVertices
};
var callback = function(feature, type) {
return lookup[type];
};
var result = parser.readFeaturesFromString(text, {callback: callback});
expect(result.features.length).to.be(178); expect(result.features.length).to.be(178);
expect(pointVertices.coordinates.length).to.be(0);
expect(lineVertices.coordinates.length).to.be(0);
expect(polygonVertices.coordinates.length).to.be(31400);
var first = result.features[0]; var first = result.features[0];
expect(first).to.be.a(ol.Feature); expect(first).to.be.a(ol.Feature);
var firstGeom = first.getGeometry(); var firstGeom = first.getGeometry();
@@ -110,6 +89,5 @@ describe('ol.parser.TopoJSON', function() {
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
goog.require('ol.parser.Parser'); goog.require('ol.parser.Parser');
goog.require('ol.parser.TopoJSON'); goog.require('ol.parser.TopoJSON');