Remove old ol.parser.GeoJSON

This commit is contained in:
Tom Payne
2014-01-18 20:23:37 +01:00
parent 637bd77531
commit 785c5a0339
6 changed files with 0 additions and 1239 deletions

View File

@@ -1,56 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<link rel="stylesheet" href="../css/ol.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
<title>Vector layer example</title>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./"><img src="../resources/logo.png"> OpenLayers 3 Examples</a>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map"></div>
</div>
</div>
<div class="row-fluid">
<div class="span4">
<h4 id="title">Vector layer example</h4>
<p id="shortdesc">Example of a countries vector layer with country information on hover and country labels at higher zoom levels.</p>
<div id="docs">
<p>See the <a href="vector-layer.js" target="_blank">vector-layer.js source</a> to see how this is done.</p>
</div>
<div id="tags">vector, geojson, style</div>
</div>
<div class="span4 offset4">
<div id="info" class="alert alert-success">
&nbsp;
</div>
</div>
</div>
</div>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="loader.js?id=vector-layer" type="text/javascript"></script>
<script src="../resources/example-behaviour.js" type="text/javascript"></script>
</body>
</html>

View File

@@ -1,88 +0,0 @@
goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.expr');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.parser.GeoJSON');
goog.require('ol.source.MapQuestOpenAerial');
goog.require('ol.source.Vector');
goog.require('ol.style.Fill');
goog.require('ol.style.Rule');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
goog.require('ol.style.Text');
var raster = new ol.layer.Tile({
source: new ol.source.MapQuestOpenAerial()
});
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
parser: new ol.parser.GeoJSON(),
url: 'data/countries.geojson'
}),
style: new ol.style.Style({rules: [
new ol.style.Rule({
symbolizers: [
new ol.style.Fill({
color: 'white',
opacity: 0.6
}),
new ol.style.Stroke({
color: '#319FD3',
opacity: 1
})
]
}),
new ol.style.Rule({
maxResolution: 5000,
symbolizers: [
new ol.style.Text({
color: 'black',
text: ol.expr.parse('name'),
fontFamily: 'Calibri,sans-serif',
fontSize: 12,
stroke: new ol.style.Stroke({
color: 'white',
width: 3
})
})
]
})
]})
});
var map = new ol.Map({
layers: [raster, vector],
renderer: ol.RendererHint.CANVAS,
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 1
})
});
var displayFeatureInfo = function(pixel) {
map.getFeatures({
pixel: pixel,
layers: [vector],
success: function(featuresByLayer) {
var features = featuresByLayer[0];
document.getElementById('info').innerHTML = features.length > 0 ?
features[0].getId() + ': ' + features[0].get('name') :
'&nbsp;';
}
});
};
$(map.getViewport()).on('mousemove', function(evt) {
var pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(pixel);
});
map.on('singleclick', function(evt) {
var pixel = evt.getPixel();
displayFeatureInfo(pixel);
});

View File

@@ -1 +0,0 @@
@exportSymbol ol.parser.GeoJSON

View File

@@ -1,419 +0,0 @@
goog.provide('ol.parser.GeoJSON');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.Feature');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.parser.Parser');
goog.require('ol.parser.ReadFeaturesResult');
goog.require('ol.parser.StringFeatureParser');
/**
* Read and write [GeoJSON](http://geojson.org/)
*
* @constructor
* @implements {ol.parser.StringFeatureParser}
* @extends {ol.parser.Parser}
* @todo stability experimental
*/
ol.parser.GeoJSON = function() {};
goog.inherits(ol.parser.GeoJSON, ol.parser.Parser);
goog.addSingletonGetter(ol.parser.GeoJSON);
/**
* Parse a GeoJSON string.
* @param {string} str GeoJSON string.
* @return {ol.Feature|Array.<ol.Feature>|
* ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
* of geometries.
*/
ol.parser.GeoJSON.prototype.read = function(str) {
var json = /** @type {GeoJSONObject} */ (JSON.parse(str));
return this.parse_(json);
};
/**
* Parse a GeoJSON string.
* @param {string} str GeoJSON string.
* @return {ol.Feature|Array.<ol.Feature>|
* ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
* of geometries.
*/
ol.parser.GeoJSON.read = function(str) {
return ol.parser.GeoJSON.getInstance().read(str);
};
/**
* Parse a GeoJSON feature collection.
* @param {string} str GeoJSON feature collection.
* @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/
ol.parser.GeoJSON.prototype.readFeaturesFromString = function(str) {
var json = /** @type {GeoJSONFeatureCollection} */ (JSON.parse(str));
return this.parseAsFeatureCollection_(json);
};
/**
* Parse a GeoJSON feature collection from decoded JSON.
* @param {GeoJSONFeatureCollection} object GeoJSON feature collection decoded
* from JSON.
* @return {ol.parser.ReadFeaturesResult} Features and metadata.
*/
ol.parser.GeoJSON.prototype.readFeaturesFromObject = function(object) {
return this.parseAsFeatureCollection_(object);
};
/**
* Parse any GeoJSON object.
*
* @param {GeoJSONObject} json GeoJSON object.
* @return {ol.Feature|Array.<ol.Feature>|
* ol.geom.Geometry|Array.<ol.geom.Geometry>} Parsed geometry or array
* of geometries.
* @private
*/
ol.parser.GeoJSON.prototype.parse_ = function(json) {
var result;
if (json.type === 'FeatureCollection') {
result = this.parseFeatureCollection_(
/** @type {GeoJSONFeatureCollection} */ (json));
} else if (json.type === 'Feature') {
result = this.parseFeature_(
/** @type {GeoJSONFeature} */ (json));
} else if (json.type === 'GeometryCollection') {
result = this.parseGeometryCollection_(
/** @type {GeoJSONGeometryCollection} */ (json));
} else {
// we've been called with a geometry or an unknown object
// create a feature to get shared vertices handling
var feature = this.parseFeature_(
/** @type {GeoJSONFeature} */ ({type: 'Feature', geometry: json}));
result = feature.getGeometry();
}
return result;
};
/**
* @param {GeoJSONObject} json GeoJSON object.
* @return {ol.parser.ReadFeaturesResult} Parsed object coerced into array of
* features.
* @private
*/
ol.parser.GeoJSON.prototype.parseAsFeatureCollection_ = function(json) {
var obj = this.parse_(json);
var features = [];
var feature;
if (obj instanceof ol.Feature) {
features = [obj];
} else if (obj instanceof ol.geom.Geometry) {
feature = new ol.Feature();
feature.setGeometry(obj);
features = [feature];
} else if (goog.isArray(obj)) {
var item, geomArray;
for (var i = 0, ii = obj.length; i < ii; ++i) {
item = obj[i];
geomArray = geomArray || (item instanceof ol.geom.Geometry);
if (!geomArray) {
goog.asserts.assert(item instanceof ol.Feature, 'expected feature');
features = obj;
break;
} else {
feature = new ol.Feature();
feature.setGeometry(item);
features[i] = feature;
}
}
}
var projection = 'EPSG:4326';
if (goog.isDefAndNotNull(json.crs)) {
var crs = json.crs;
if (crs.type === 'name') {
projection = (/** GeoJSONCRSName */ (crs.properties)).name;
}
}
return {features: features, metadata: {projection: projection}};
};
/**
* @param {GeoJSONFeature} json GeoJSON feature.
* @return {ol.Feature} Parsed feature.
* @private
*/
ol.parser.GeoJSON.prototype.parseFeature_ = function(json) {
var geomJson = json.geometry,
geometry = null;
var feature = new ol.Feature(json.properties);
if (goog.isDef(json.id)) {
feature.setId(json.id);
}
if (geomJson) {
var type = geomJson.type;
switch (type) {
case 'Point':
geometry = this.parsePoint_(geomJson);
break;
case 'LineString':
geometry = this.parseLineString_(geomJson);
break;
case 'Polygon':
geometry = this.parsePolygon_(geomJson);
break;
case 'MultiPoint':
geometry = this.parseMultiPoint_(geomJson);
break;
case 'MultiLineString':
geometry = this.parseMultiLineString_(geomJson);
break;
case 'MultiPolygon':
geometry = this.parseMultiPolygon_(geomJson);
break;
default:
throw new Error('Bad geometry type: ' + type);
}
feature.setGeometry(geometry);
}
return feature;
};
/**
* @param {GeoJSONFeatureCollection} json GeoJSON feature collection.
* @return {Array.<ol.Feature>} Parsed array of features.
* @private
*/
ol.parser.GeoJSON.prototype.parseFeatureCollection_ = function(json) {
var features = json.features,
len = features.length,
result = new Array(len),
i;
for (i = 0; i < len; ++i) {
result[i] = this.parseFeature_(/** @type {GeoJSONFeature} */ (features[i]));
}
return result;
};
/**
* @param {GeoJSONGeometryCollection} json GeoJSON geometry collection.
* @return {Array.<ol.geom.Geometry>} Parsed array of geometries.
* @private
*/
ol.parser.GeoJSON.prototype.parseGeometryCollection_ = function(json) {
var geometries = json.geometries,
len = geometries.length,
result = new Array(len),
i;
for (i = 0; i < len; ++i) {
result[i] = this.parse_(/** @type {GeoJSONGeometry} */ (geometries[i]));
}
return result;
};
/**
* @param {GeoJSONGeometry} json GeoJSON linestring.
* @return {ol.geom.LineString} Parsed linestring.
* @private
*/
ol.parser.GeoJSON.prototype.parseLineString_ = function(json) {
return new ol.geom.LineString(json.coordinates);
};
/**
* @param {GeoJSONGeometry} json GeoJSON multi-linestring.
* @return {ol.geom.MultiLineString} Parsed multi-linestring.
* @private
*/
ol.parser.GeoJSON.prototype.parseMultiLineString_ = function(json) {
return new ol.geom.MultiLineString(json.coordinates);
};
/**
* @param {GeoJSONGeometry} json GeoJSON multi-point.
* @return {ol.geom.MultiPoint} Parsed multi-point.
* @private
*/
ol.parser.GeoJSON.prototype.parseMultiPoint_ = function(json) {
return new ol.geom.MultiPoint(json.coordinates);
};
/**
* @param {GeoJSONGeometry} json GeoJSON multi-polygon.
* @return {ol.geom.MultiPolygon} Parsed multi-polygon.
* @private
*/
ol.parser.GeoJSON.prototype.parseMultiPolygon_ = function(json) {
return new ol.geom.MultiPolygon(json.coordinates);
};
/**
* @param {GeoJSONGeometry} json GeoJSON point.
* @return {ol.geom.Point} Parsed point.
* @private
*/
ol.parser.GeoJSON.prototype.parsePoint_ = function(json) {
return new ol.geom.Point(json.coordinates);
};
/**
* @param {GeoJSONGeometry} json GeoJSON polygon.
* @return {ol.geom.Polygon} Parsed polygon.
* @private
*/
ol.parser.GeoJSON.prototype.parsePolygon_ = function(json) {
return new ol.geom.Polygon(json.coordinates);
};
/**
* @param {ol.geom.Geometry} geometry Geometry to encode.
* @return {GeoJSONGeometry} GeoJSON geometry.
* @private
*/
ol.parser.GeoJSON.prototype.encodeGeometry_ = function(geometry) {
var type = geometry.getType();
return /** @type {GeoJSONGeometry} */({
type: goog.object.findKey(ol.parser.GeoJSON.GeometryType,
function(value, key) {
return value === type;
}
),
coordinates: geometry.getCoordinates()
});
};
/**
* @param {ol.geom.GeometryCollection} collection Geometry collection to
* encode.
* @return {GeoJSONGeometryCollection} GeoJSON geometry collection.
* @private
*/
ol.parser.GeoJSON.prototype.encodeGeometryCollection_ = function(collection) {
var geometries = [];
var components = collection.getComponents();
for (var i = 0, ii = components.length; i < ii; ++i) {
geometries.push(this.encodeGeometry_(components[i]));
}
return /** @type {GeoJSONGeometryCollection} */({
type: 'GeometryCollection',
geometries: geometries
});
};
/**
* @param {Array.<ol.Feature>} collection Feature collection to encode.
* @return {GeoJSONFeatureCollection} GeoJSON feature collection.
* @private
*/
ol.parser.GeoJSON.prototype.encodeFeatureCollection_ = function(collection) {
var features = [];
for (var i = 0, ii = collection.length; i < ii; ++i) {
features.push(this.encodeFeature_(collection[i]));
}
return /** @type {GeoJSONFeatureCollection} */({
type: 'FeatureCollection',
features: features
});
};
/**
* @param {ol.Feature} feature Feature to encode.
* @return {GeoJSONFeature} GeoJSON feature.
* @private
*/
ol.parser.GeoJSON.prototype.encodeFeature_ = function(feature) {
var geometry = feature.getGeometry(),
properties = feature.getAttributes(true);
return /** @type {GeoJSONFeature} */({
type: 'Feature',
properties: properties,
geometry: this.encodeGeometry_(geometry)
});
};
/**
* @param {ol.geom.GeometryCollection|ol.geom.Geometry|Array.<ol.Feature>|
* ol.Feature} obj The object to encode.
* @return {string} The GeoJSON as string.
* @private
*/
ol.parser.GeoJSON.prototype.encode_ = function(obj) {
var result;
if (obj instanceof ol.geom.GeometryCollection) {
result = this.encodeGeometryCollection_(obj);
} else if (obj instanceof ol.geom.Geometry) {
result = this.encodeGeometry_(obj);
} else if (obj instanceof ol.Feature) {
result = this.encodeFeature_(obj);
} else if (goog.isArray(obj)) {
result = this.encodeFeatureCollection_(obj);
}
return JSON.stringify(result);
};
/**
* Write out a geometry, geometry collection, feature or an array of features
* as a GeoJSON string.
* @param {ol.geom.Geometry|ol.geom.GeometryCollection|ol.Feature|
* Array.<ol.Feature>} obj The object to encode.
* @return {string} GeoJSON for the geometry.
*/
ol.parser.GeoJSON.write = function(obj) {
return ol.parser.GeoJSON.getInstance().write(obj);
};
/**
* Write out a geometry, geometry collection, feature or an array of features
* as a GeoJSON string.
* @param {ol.geom.Geometry|ol.geom.GeometryCollection|ol.Feature|
* Array.<ol.Feature>} obj The object to encode.
* @return {string} GeoJSON for the geometry.
*/
ol.parser.GeoJSON.prototype.write = function(obj) {
return this.encode_(obj);
};
/**
* @enum {ol.geom.GeometryType}
*/
ol.parser.GeoJSON.GeometryType = {
'Point': ol.geom.GeometryType.POINT,
'LineString': ol.geom.GeometryType.LINE_STRING,
'Polygon': ol.geom.GeometryType.POLYGON,
'MultiPoint': ol.geom.GeometryType.MULTI_POINT,
'MultiLineString': ol.geom.GeometryType.MULTI_LINE_STRING,
'MultiPolygon': ol.geom.GeometryType.MULTI_POLYGON,
'GeometryCollection': ol.geom.GeometryType.GEOMETRY_COLLECTION
};

View File

@@ -1,494 +0,0 @@
goog.provide('ol.test.parser.GeoJSON');
describe('ol.parser.GeoJSON', function() {
var parser = new ol.parser.GeoJSON();
var data = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {
'LINK_ID': 573730499,
'RP_TYPE': 14,
'RP_FUNC': 0,
'DIRECTION': 2,
'LOGKOD': '',
'CHANGED': '',
'USERID': '',
'ST_NAME': '',
'L_REFADDR': '',
'L_NREFADDR': '',
'R_REFADDR': '',
'R_NREFADDR': '',
'SPEED_CAT': '7',
'ZIPCODE': '59330',
'SHAPE_LEN': 46.3826
},
'geometry': {
'type': 'LineString',
'coordinates': [
[1549497.66985, 6403707.96],
[1549491.1, 6403710.1],
[1549488.03995, 6403716.7504],
[1549488.5401, 6403724.5504],
[1549494.37985, 6403733.54],
[1549499.6799, 6403738.0504],
[1549506.22, 6403739.2504]
]
}
}, {
'type': 'Feature',
'properties': {
'LINK_ID': 30760556,
'RP_TYPE': 12,
'RP_FUNC': 1,
'DIRECTION': 0,
'LOGKOD': '',
'CHANGED': '',
'USERID': '',
'ST_NAME': 'BRUNNSGATAN',
'L_REFADDR': '24',
'L_NREFADDR': '16',
'R_REFADDR': '',
'R_NREFADDR': '',
'SPEED_CAT': '7',
'ZIPCODE': '59330',
'SHAPE_LEN': 70.3106
},
'geometry': {
'type': 'LineString',
'coordinates': [
[1549754.2769, 6403854.8024],
[1549728.45985, 6403920.2]
]
}
}
]
};
describe('#write()', function() {
it('encodes point', function() {
var point = new ol.geom.Point([10, 20]);
var geojson = parser.write(point);
expect(point.getCoordinates()).to.eql(
parser.read(geojson).getCoordinates());
});
it('encodes linestring', function() {
var linestring = new ol.geom.LineString([[10, 20], [30, 40]]);
var geojson = parser.write(linestring);
expect(linestring.getCoordinates()).to.eql(
parser.read(geojson).getCoordinates());
});
it('encodes polygon', function() {
var outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],
inner1 = [[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]],
inner2 = [[8, 8], [9, 8], [9, 9], [8, 9], [8, 8]];
var polygon = new ol.geom.Polygon([outer, inner1, inner2]);
var geojson = parser.write(polygon);
expect(polygon.getCoordinates()).to.eql(
parser.read(geojson).getCoordinates());
});
it('encodes geometry collection', function() {
var collection = new ol.geom.GeometryCollection([
new ol.geom.Point([10, 20]),
new ol.geom.LineString([[30, 40], [50, 60]])
]);
var geojson = parser.write(collection);
var got = parser.read(geojson);
var components = collection.getComponents();
expect(components.length).to.equal(got.length);
for (var i = 0, ii = components.length; i < ii; ++i) {
expect(components[i].getCoordinates()).to.eql(got[i].getCoordinates());
}
});
it('encodes feature collection', function() {
var str = JSON.stringify(data),
array = parser.read(str);
var geojson = parser.write(array);
var result = parser.read(geojson);
expect(array.length).to.equal(result.length);
var got, exp, gotAttr, expAttr;
for (var i = 0, ii = array.length; i < ii; ++i) {
got = array[i];
exp = result[i];
expect(got.getGeometry().getCoordinates()).to.eql(
exp.getGeometry().getCoordinates());
gotAttr = got.getAttributes();
delete gotAttr.geometry;
expAttr = exp.getAttributes();
delete expAttr.geometry;
expect(gotAttr).to.eql(expAttr);
}
});
});
describe('#read()', function() {
it('parses point', function() {
var str = JSON.stringify({
type: 'Point',
coordinates: [10, 20]
});
var obj = parser.read(str);
expect(obj).to.be.a(ol.geom.Point);
expect(obj.getCoordinates()).to.eql([10, 20]);
});
it('parses linestring', function() {
var str = JSON.stringify({
type: 'LineString',
coordinates: [[10, 20], [30, 40]]
});
var obj = parser.read(str);
expect(obj).to.be.a(ol.geom.LineString);
expect(obj.getCoordinates()).to.eql([[10, 20], [30, 40]]);
});
it('parses polygon', function() {
var outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],
inner1 = [[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]],
inner2 = [[8, 8], [9, 8], [9, 9], [8, 9], [8, 8]],
str = JSON.stringify({
type: 'Polygon',
coordinates: [outer, inner1, inner2]
});
var obj = parser.read(str);
expect(obj).to.be.a(ol.geom.Polygon);
var rings = obj.getRings();
expect(rings.length).to.be(3);
expect(rings[0]).to.be.a(ol.geom.LinearRing);
expect(rings[1]).to.be.a(ol.geom.LinearRing);
expect(rings[2]).to.be.a(ol.geom.LinearRing);
});
it('parses geometry collection', function() {
var str = JSON.stringify({
type: 'GeometryCollection',
geometries: [
{type: 'Point', coordinates: [10, 20]},
{type: 'LineString', coordinates: [[30, 40], [50, 60]]}
]
});
var array = parser.read(str);
expect(array.length).to.be(2);
expect(array[0]).to.be.a(ol.geom.Point);
expect(array[1]).to.be.a(ol.geom.LineString);
});
it('parses feature collection', function() {
var str = JSON.stringify(data),
array = parser.read(str);
expect(array.length).to.be(2);
var first = array[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('LINK_ID')).to.be(573730499);
var firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(ol.geom.LineString);
var second = array[1];
expect(second).to.be.a(ol.Feature);
expect(second.get('ST_NAME')).to.be('BRUNNSGATAN');
var secondGeom = second.getGeometry();
expect(secondGeom).to.be.a(ol.geom.LineString);
});
it('parses countries.geojson', function(done) {
afterLoadText('spec/ol/parser/geojson/countries.geojson', function(text) {
var result = parser.read(text);
expect(result.length).to.be(179);
var first = result[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('name')).to.be('Afghanistan');
expect(first.getId()).to.be('AFG');
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');
expect(last.getId()).to.be('ZWE');
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);
done();
});
});
});
describe('#parseAsFeatureCollection_()', function() {
it('generates an array of features for FeatureCollection', function() {
var parser = new ol.parser.GeoJSON();
var json = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {
foo: 'bar'
},
geometry: {
type: 'Point',
coordinates: [1, 2]
}
}, {
type: 'Feature',
properties: {
bam: 'baz'
},
geometry: {
type: 'LineString',
coordinates: [[1, 2], [3, 4]]
}
}]
};
var result = parser.parseAsFeatureCollection_(json);
var features = result.features;
expect(features.length).to.be(2);
var first = features[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('foo')).to.be('bar');
expect(first.getGeometry()).to.be.a(ol.geom.Point);
var second = features[1];
expect(second).to.be.a(ol.Feature);
expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('reads named crs from top-level object', function() {
var parser = new ol.parser.GeoJSON();
var json = {
type: 'FeatureCollection',
crs: {
type: 'name',
properties: {
name: 'EPSG:1234'
}
},
features: [{
type: 'Feature',
properties: {
foo: 'bar'
},
geometry: {
type: 'Point',
coordinates: [1, 2]
}
}, {
type: 'Feature',
properties: {
bam: 'baz'
},
geometry: {
type: 'LineString',
coordinates: [[1, 2], [3, 4]]
}
}]
};
var result = parser.parseAsFeatureCollection_(json);
var features = result.features;
expect(features.length).to.be(2);
var first = features[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('foo')).to.be('bar');
expect(first.getGeometry()).to.be.a(ol.geom.Point);
var second = features[1];
expect(second).to.be.a(ol.Feature);
expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(result.metadata.projection).to.be('EPSG:1234');
});
it('accepts null crs', function() {
var parser = new ol.parser.GeoJSON();
var json = {
type: 'FeatureCollection',
crs: null,
features: [{
type: 'Feature',
properties: {
foo: 'bar'
},
geometry: {
type: 'Point',
coordinates: [1, 2]
}
}, {
type: 'Feature',
properties: {
bam: 'baz'
},
geometry: {
type: 'LineString',
coordinates: [[1, 2], [3, 4]]
}
}]
};
var result = parser.parseAsFeatureCollection_(json);
var features = result.features;
expect(features.length).to.be(2);
var first = features[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('foo')).to.be('bar');
expect(first.getGeometry()).to.be.a(ol.geom.Point);
var second = features[1];
expect(second).to.be.a(ol.Feature);
expect(second.get('bam')).to.be('baz');
expect(second.getGeometry()).to.be.a(ol.geom.LineString);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('generates an array of features for Feature', function() {
var parser = new ol.parser.GeoJSON();
var json = {
type: 'Feature',
properties: {
bam: 'baz'
},
geometry: {
type: 'LineString',
coordinates: [[1, 2], [3, 4]]
}
};
var result = parser.parseAsFeatureCollection_(json);
var features = result.features;
expect(features.length).to.be(1);
var first = features[0];
expect(first).to.be.a(ol.Feature);
expect(first.get('bam')).to.be('baz');
expect(first.getGeometry()).to.be.a(ol.geom.LineString);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('generates an array of features for GeometryCollection', function() {
var parser = new ol.parser.GeoJSON();
var json = {
type: 'GeometryCollection',
geometries: [{
type: 'Point',
coordinates: [1, 2]
}, {
type: 'LineString',
coordinates: [[3, 4], [5, 6]]
}, {
type: 'Polygon',
coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]]
}]
};
var result = parser.parseAsFeatureCollection_(json);
var features = result.features;
expect(features.length).to.be(3);
expect(features[0].getGeometry()).to.be.a(ol.geom.Point);
expect(features[1].getGeometry()).to.be.a(ol.geom.LineString);
expect(features[2].getGeometry()).to.be.a(ol.geom.Polygon);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('generates an array of features for Point', function() {
var parser = new ol.parser.GeoJSON();
var json = {
type: 'Point',
coordinates: [1, 2]
};
var result = parser.parseAsFeatureCollection_(json);
var features = result.features;
expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.Point);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('generates an array of features for LineString', function() {
var parser = new ol.parser.GeoJSON();
var json = {
type: 'LineString',
coordinates: [[3, 4], [5, 6]]
};
var result = parser.parseAsFeatureCollection_(json);
var features = result.features;
expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.LineString);
expect(result.metadata.projection).to.be('EPSG:4326');
});
it('generates an array of features for Polygon', function() {
var parser = new ol.parser.GeoJSON();
var json = {
type: 'Polygon',
coordinates: [[[7, 8], [9, 10], [11, 12], [7, 8]]]
};
var result = parser.parseAsFeatureCollection_(json);
var features = result.features;
expect(features.length).to.be(1);
expect(features[0].getGeometry()).to.be.a(ol.geom.Polygon);
expect(result.metadata.projection).to.be('EPSG:4326');
});
});
});
goog.require('ol.Feature');
goog.require('ol.extent');
goog.require('ol.geom.GeometryCollection');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.LineString');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.parser.GeoJSON');

File diff suppressed because one or more lines are too long