Add ol.format.GeoJSON
This commit is contained in:
@@ -247,7 +247,7 @@
|
||||
|
||||
/**
|
||||
* @typedef {Object} ol.format.GeoJSONOptions
|
||||
* @property {ol.proj.ProjectionLike} projection Projection.
|
||||
* @property {ol.proj.ProjectionLike} defaultProjection Default projection.
|
||||
*/
|
||||
|
||||
/**
|
||||
|
||||
1
src/ol/format/geojsonformat.exports
Normal file
1
src/ol/format/geojsonformat.exports
Normal file
@@ -0,0 +1 @@
|
||||
@exportClass ol.format.GeoJSON ol.format.GeoJSONOptions
|
||||
365
src/ol/format/geojsonformat.js
Normal file
365
src/ol/format/geojsonformat.js
Normal file
@@ -0,0 +1,365 @@
|
||||
// FIXME coordinate order
|
||||
// FIXME reprojection
|
||||
// FIXME GeometryCollection
|
||||
|
||||
goog.provide('ol.format.GeoJSON');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.object');
|
||||
goog.require('ol.Feature');
|
||||
goog.require('ol.format.JSON');
|
||||
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.proj');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {ol.format.JSON}
|
||||
* @param {ol.format.GeoJSONOptions=} opt_options Options.
|
||||
*/
|
||||
ol.format.GeoJSON = function(opt_options) {
|
||||
|
||||
var options = goog.isDef(opt_options) ? opt_options : {};
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {ol.proj.Projection}
|
||||
*/
|
||||
this.defaultProjection_ = ol.proj.get(options.defaultProjection ?
|
||||
options.defaultProjection : 'EPSG:4326');
|
||||
|
||||
};
|
||||
goog.inherits(ol.format.GeoJSON, ol.format.JSON);
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONGeometry} object Object.
|
||||
* @private
|
||||
* @return {ol.geom.Geometry} Geometry.
|
||||
*/
|
||||
ol.format.GeoJSON.readGeometry_ = function(object) {
|
||||
var geometryReader = ol.format.GeoJSON.GEOMETRY_READERS_[object.type];
|
||||
goog.asserts.assert(goog.isDef(geometryReader));
|
||||
return geometryReader(object);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONGeometry} object Object.
|
||||
* @private
|
||||
* @return {ol.geom.Point} Point.
|
||||
*/
|
||||
ol.format.GeoJSON.readPointGeometry_ = function(object) {
|
||||
goog.asserts.assert(object.type == 'Point');
|
||||
return new ol.geom.Point(object.coordinates);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONGeometry} object Object.
|
||||
* @private
|
||||
* @return {ol.geom.LineString} LineString.
|
||||
*/
|
||||
ol.format.GeoJSON.readLineStringGeometry_ = function(object) {
|
||||
goog.asserts.assert(object.type == 'LineString');
|
||||
return new ol.geom.LineString(object.coordinates);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONGeometry} object Object.
|
||||
* @private
|
||||
* @return {ol.geom.MultiLineString} MultiLineString.
|
||||
*/
|
||||
ol.format.GeoJSON.readMultiLineStringGeometry_ = function(object) {
|
||||
goog.asserts.assert(object.type == 'MultiLineString');
|
||||
return new ol.geom.MultiLineString(object.coordinates);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONGeometry} object Object.
|
||||
* @private
|
||||
* @return {ol.geom.MultiPoint} MultiPoint.
|
||||
*/
|
||||
ol.format.GeoJSON.readMultiPointGeometry_ = function(object) {
|
||||
goog.asserts.assert(object.type == 'MultiPoint');
|
||||
return new ol.geom.MultiPoint(object.coordinates);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONGeometry} object Object.
|
||||
* @private
|
||||
* @return {ol.geom.MultiPolygon} MultiPolygon.
|
||||
*/
|
||||
ol.format.GeoJSON.readMultiPolygonGeometry_ = function(object) {
|
||||
goog.asserts.assert(object.type == 'MultiPolygon');
|
||||
return new ol.geom.MultiPolygon(object.coordinates);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {GeoJSONGeometry} object Object.
|
||||
* @private
|
||||
* @return {ol.geom.Polygon} Polygon.
|
||||
*/
|
||||
ol.format.GeoJSON.readPolygonGeometry_ = function(object) {
|
||||
goog.asserts.assert(object.type == 'Polygon');
|
||||
return new ol.geom.Polygon(object.coordinates);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.geom.Geometry} geometry Geometry.
|
||||
* @private
|
||||
* @return {GeoJSONGeometry} GeoJSON geometry.
|
||||
*/
|
||||
ol.format.GeoJSON.writeGeometry_ = function(geometry) {
|
||||
var geometryWriter = ol.format.GeoJSON.GEOMETRY_WRITERS_[geometry.getType()];
|
||||
goog.asserts.assert(goog.isDef(geometryWriter));
|
||||
return geometryWriter(geometry);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.geom.Geometry} geometry Geometry.
|
||||
* @private
|
||||
* @return {GeoJSONGeometry} GeoJSON geometry.
|
||||
*/
|
||||
ol.format.GeoJSON.writeLineStringGeometry_ = function(geometry) {
|
||||
goog.asserts.assertInstanceof(geometry, ol.geom.LineString);
|
||||
return /** @type {GeoJSONGeometry} */ ({
|
||||
'type': 'LineString',
|
||||
'coordinates': geometry.getCoordinates()
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.geom.Geometry} geometry Geometry.
|
||||
* @private
|
||||
* @return {GeoJSONGeometry} GeoJSON geometry.
|
||||
*/
|
||||
ol.format.GeoJSON.writeMultiLineStringGeometry_ = function(geometry) {
|
||||
goog.asserts.assertInstanceof(geometry, ol.geom.MultiLineString);
|
||||
goog.asserts.assert(
|
||||
geometry.getType() == ol.geom.GeometryType.MULTI_LINE_STRING);
|
||||
return /** @type {GeoJSONGeometry} */ ({
|
||||
'type': 'MultiLineString',
|
||||
'coordinates': geometry.getCoordinates()
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.geom.Geometry} geometry Geometry.
|
||||
* @private
|
||||
* @return {GeoJSONGeometry} GeoJSON geometry.
|
||||
*/
|
||||
ol.format.GeoJSON.writeMultiPointGeometry_ = function(geometry) {
|
||||
goog.asserts.assertInstanceof(geometry, ol.geom.MultiPoint);
|
||||
return /** @type {GeoJSONGeometry} */ ({
|
||||
'type': 'MultiPoint',
|
||||
'coordinates': geometry.getCoordinates()
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.geom.Geometry} geometry Geometry.
|
||||
* @private
|
||||
* @return {GeoJSONGeometry} GeoJSON geometry.
|
||||
*/
|
||||
ol.format.GeoJSON.writeMultiPolygonGeometry_ = function(geometry) {
|
||||
goog.asserts.assertInstanceof(geometry, ol.geom.MultiPolygon);
|
||||
return /** @type {GeoJSONGeometry} */ ({
|
||||
'type': 'MultiPolygon',
|
||||
'coordinates': geometry.getCoordinates()
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.geom.Geometry} geometry Geometry.
|
||||
* @private
|
||||
* @return {GeoJSONGeometry} GeoJSON geometry.
|
||||
*/
|
||||
ol.format.GeoJSON.writePointGeometry_ = function(geometry) {
|
||||
goog.asserts.assertInstanceof(geometry, ol.geom.Point);
|
||||
return /** @type {GeoJSONGeometry} */ ({
|
||||
'type': 'Point',
|
||||
'coordinates': geometry.getCoordinates()
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.geom.Geometry} geometry Geometry.
|
||||
* @private
|
||||
* @return {GeoJSONGeometry} GeoJSON geometry.
|
||||
*/
|
||||
ol.format.GeoJSON.writePolygonGeometry_ = function(geometry) {
|
||||
goog.asserts.assertInstanceof(geometry, ol.geom.Polygon);
|
||||
return /** @type {GeoJSONGeometry} */ ({
|
||||
'type': 'Polygon',
|
||||
'coordinates': geometry.getCoordinates()
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @const
|
||||
* @private
|
||||
* @type {Object.<string, function(GeoJSONGeometry): ol.geom.Geometry>}
|
||||
*/
|
||||
ol.format.GeoJSON.GEOMETRY_READERS_ = {
|
||||
'Point': ol.format.GeoJSON.readPointGeometry_,
|
||||
'LineString': ol.format.GeoJSON.readLineStringGeometry_,
|
||||
'Polygon': ol.format.GeoJSON.readPolygonGeometry_,
|
||||
'MultiPoint': ol.format.GeoJSON.readMultiPointGeometry_,
|
||||
'MultiLineString': ol.format.GeoJSON.readMultiLineStringGeometry_,
|
||||
'MultiPolygon': ol.format.GeoJSON.readMultiPolygonGeometry_
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @const
|
||||
* @private
|
||||
* @type {Object.<string, function(ol.geom.Geometry): GeoJSONGeometry>}
|
||||
*/
|
||||
ol.format.GeoJSON.GEOMETRY_WRITERS_ = {
|
||||
'Point': ol.format.GeoJSON.writePointGeometry_,
|
||||
'LineString': ol.format.GeoJSON.writeLineStringGeometry_,
|
||||
'Polygon': ol.format.GeoJSON.writePolygonGeometry_,
|
||||
'MultiPoint': ol.format.GeoJSON.writeMultiPointGeometry_,
|
||||
'MultiLineString': ol.format.GeoJSON.writeMultiLineStringGeometry_,
|
||||
'MultiPolygon': ol.format.GeoJSON.writeMultiPolygonGeometry_
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.prototype.readFeatureFromObject = function(object) {
|
||||
var geoJSONFeature = /** @type {GeoJSONFeature} */ (object);
|
||||
goog.asserts.assert(geoJSONFeature.type == 'Feature');
|
||||
var geometry = ol.format.GeoJSON.readGeometry_(geoJSONFeature.geometry);
|
||||
var feature = new ol.Feature(geometry);
|
||||
if (goog.isDef(geoJSONFeature.id)) {
|
||||
feature.setId(geoJSONFeature.id);
|
||||
}
|
||||
if (goog.isDef(geoJSONFeature.properties)) {
|
||||
feature.setValues(geoJSONFeature.properties);
|
||||
}
|
||||
return feature;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.prototype.readFeaturesFromObject = function(object) {
|
||||
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
|
||||
if (geoJSONObject.type == 'Feature') {
|
||||
return [this.readFeatureFromObject(object)];
|
||||
} else if (geoJSONObject.type == 'FeatureCollection') {
|
||||
var geoJSONFeatureCollection = /** @type {GeoJSONFeatureCollection} */
|
||||
(object);
|
||||
/** @type {Array.<ol.Feature>} */
|
||||
var features = [];
|
||||
var geoJSONFeatures = geoJSONFeatureCollection.features;
|
||||
var i, ii;
|
||||
for (i = 0, ii = geoJSONFeatures.length; i < ii; ++i) {
|
||||
features.push(this.readFeatureFromObject(geoJSONFeatures[i]));
|
||||
}
|
||||
return features;
|
||||
} else {
|
||||
goog.asserts.fail();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.prototype.readGeometryFromObject = function(object) {
|
||||
return ol.format.GeoJSON.readGeometry_(
|
||||
/** @type {GeoJSONGeometry} */ (object));
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.prototype.readProjection = function(object) {
|
||||
var geoJSONObject = /** @type {GeoJSONObject} */ (object);
|
||||
var crs = geoJSONObject.crs;
|
||||
if (goog.isDefAndNotNull(crs)) {
|
||||
if (crs.type == 'name') {
|
||||
return ol.proj.get(crs.properties.name);
|
||||
} else {
|
||||
goog.asserts.fail();
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return this.defaultProjection_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.prototype.writeFeatureObject = function(feature) {
|
||||
var object = {
|
||||
'type': 'Feature'
|
||||
};
|
||||
var id = feature.getId();
|
||||
if (goog.isDefAndNotNull(id)) {
|
||||
goog.object.set(object, 'id', id);
|
||||
}
|
||||
var geometry = feature.getGeometry();
|
||||
if (goog.isDefAndNotNull(geometry)) {
|
||||
goog.object.set(
|
||||
object, 'geometry', ol.format.GeoJSON.writeGeometry_(geometry));
|
||||
}
|
||||
var properties = feature.getProperties();
|
||||
goog.object.remove(properties, 'geometry');
|
||||
if (!goog.object.isEmpty(properties)) {
|
||||
goog.object.set(object, 'properties', properties);
|
||||
}
|
||||
return object;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.prototype.writeFeaturesObject = function(features) {
|
||||
var objects = [];
|
||||
var i, ii;
|
||||
for (i = 0, ii = features.length; i < ii; ++i) {
|
||||
objects.push(this.writeFeatureObject(features[i]));
|
||||
}
|
||||
return /** @type {GeoJSONFeatureCollection} */ ({
|
||||
'type': 'FeatureCollection',
|
||||
'features': objects
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
ol.format.GeoJSON.prototype.writeGeometryObject =
|
||||
ol.format.GeoJSON.writeGeometry_;
|
||||
181
test/spec/ol/format/geojson/countries.geojson
Normal file
181
test/spec/ol/format/geojson/countries.geojson
Normal file
File diff suppressed because one or more lines are too long
470
test/spec/ol/format/geojsonformat.test.js
Normal file
470
test/spec/ol/format/geojsonformat.test.js
Normal file
@@ -0,0 +1,470 @@
|
||||
goog.provide('ol.test.reader.GeoJSON');
|
||||
|
||||
|
||||
describe('ol.format.GeoJSON', function() {
|
||||
|
||||
var format;
|
||||
beforeEach(function() {
|
||||
format = new ol.format.GeoJSON();
|
||||
});
|
||||
|
||||
var pointGeoJSON = {
|
||||
'type': 'Feature',
|
||||
'geometry': {
|
||||
'type': 'Point',
|
||||
'coordinates': [102.0, 0.5]
|
||||
},
|
||||
'properties': {
|
||||
'prop0': 'value0'
|
||||
}
|
||||
};
|
||||
|
||||
var lineStringGeoJSON = {
|
||||
'type': 'Feature',
|
||||
'geometry': {
|
||||
'type': 'LineString',
|
||||
'coordinates': [
|
||||
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
|
||||
]
|
||||
},
|
||||
'properties': {
|
||||
'prop0': 'value0',
|
||||
'prop1': 0.0
|
||||
}
|
||||
};
|
||||
|
||||
var polygonGeoJSON = {
|
||||
'type': 'Feature',
|
||||
'geometry': {
|
||||
'type': 'Polygon',
|
||||
'coordinates': [[
|
||||
[100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0]
|
||||
]]
|
||||
},
|
||||
'properties': {
|
||||
'prop0': 'value0',
|
||||
'prop1': {'this': 'that'}
|
||||
}
|
||||
};
|
||||
|
||||
var featureCollectionGeoJSON = {
|
||||
'type': 'FeatureCollection',
|
||||
'features': [pointGeoJSON, lineStringGeoJSON, polygonGeoJSON]
|
||||
};
|
||||
|
||||
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('#readFeature', function() {
|
||||
|
||||
it('can read a single point feature', function() {
|
||||
var feature = format.readFeature(pointGeoJSON);
|
||||
expect(feature).to.be.an(ol.Feature);
|
||||
var geometry = feature.getGeometry();
|
||||
expect(geometry).to.be.an(ol.geom.Point);
|
||||
expect(geometry.getCoordinates()).to.eql([102.0, 0.5]);
|
||||
expect(feature.get('prop0')).to.be('value0');
|
||||
});
|
||||
|
||||
it('can read a single line string feature', function() {
|
||||
var feature = format.readFeature(lineStringGeoJSON);
|
||||
expect(feature).to.be.an(ol.Feature);
|
||||
var geometry = feature.getGeometry();
|
||||
expect(geometry).to.be.an(ol.geom.LineString);
|
||||
expect(geometry.getCoordinates()).to.eql(
|
||||
[[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]]);
|
||||
expect(feature.get('prop0')).to.be('value0');
|
||||
expect(feature.get('prop1')).to.be(0.0);
|
||||
});
|
||||
|
||||
it('can read a single polygon feature', function() {
|
||||
var feature = format.readFeature(polygonGeoJSON);
|
||||
expect(feature).to.be.an(ol.Feature);
|
||||
var geometry = feature.getGeometry();
|
||||
expect(geometry).to.be.an(ol.geom.Polygon);
|
||||
expect(geometry.getCoordinates()).to.eql([[
|
||||
[100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0]
|
||||
]]);
|
||||
expect(feature.get('prop0')).to.be('value0');
|
||||
expect(feature.get('prop1')).to.eql({'this': 'that'});
|
||||
});
|
||||
|
||||
it('can read a feature collection', function() {
|
||||
var features = format.readFeatures(featureCollectionGeoJSON);
|
||||
expect(features).to.have.length(3);
|
||||
expect(features[0].getGeometry()).to.be.an(ol.geom.Point);
|
||||
expect(features[1].getGeometry()).to.be.an(ol.geom.LineString);
|
||||
expect(features[2].getGeometry()).to.be.an(ol.geom.Polygon);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#readFeatures', function() {
|
||||
|
||||
it('parses feature collection', function() {
|
||||
var str = JSON.stringify(data),
|
||||
array = format.readFeatures(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/format/geojson/countries.geojson', function(text) {
|
||||
var result = format.readFeatures(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.getExtent(),
|
||||
[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.getExtent(),
|
||||
[25.264226, -22.271612, 32.849861, -15.507787]))
|
||||
.to.be(true);
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('generates an array of features for Feature', function() {
|
||||
|
||||
var format = new ol.format.GeoJSON();
|
||||
var json = {
|
||||
type: 'Feature',
|
||||
properties: {
|
||||
bam: 'baz'
|
||||
},
|
||||
geometry: {
|
||||
type: 'LineString',
|
||||
coordinates: [[1, 2], [3, 4]]
|
||||
}
|
||||
};
|
||||
var features = format.readFeatures(json);
|
||||
|
||||
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(format.readProjection(json)).to.be(ol.proj.get('EPSG:4326'));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#readGeometry', function() {
|
||||
|
||||
it('parses point', function() {
|
||||
var str = JSON.stringify({
|
||||
type: 'Point',
|
||||
coordinates: [10, 20]
|
||||
});
|
||||
|
||||
var obj = format.readGeometry(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 = format.readGeometry(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 = format.readGeometry(str);
|
||||
expect(obj).to.be.a(ol.geom.Polygon);
|
||||
var rings = obj.getLinearRings();
|
||||
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.skip('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 = format.readGeometry(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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#readProjection', function() {
|
||||
|
||||
it('reads named crs from top-level object', function() {
|
||||
|
||||
var json = {
|
||||
type: 'FeatureCollection',
|
||||
crs: {
|
||||
type: 'name',
|
||||
properties: {
|
||||
name: 'EPSG:3857'
|
||||
}
|
||||
},
|
||||
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 features = format.readFeatures(json);
|
||||
|
||||
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(format.readProjection(json)).to.be(ol.proj.get('EPSG:3857'));
|
||||
|
||||
});
|
||||
|
||||
it('accepts null crs', function() {
|
||||
|
||||
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 features = format.readFeatures(json);
|
||||
|
||||
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(format.readProjection(json)).to.be(ol.proj.get('EPSG:4326'));
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#writeFeatures', function() {
|
||||
|
||||
it('encodes feature collection', function() {
|
||||
var str = JSON.stringify(data),
|
||||
array = format.readFeatures(str);
|
||||
var geojson = format.writeFeatures(array);
|
||||
var result = format.readFeatures(geojson);
|
||||
expect(array.length).to.equal(result.length);
|
||||
var got, exp, gotProp, expProp;
|
||||
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());
|
||||
gotProp = got.getProperties();
|
||||
delete gotProp.geometry;
|
||||
expProp = exp.getProperties();
|
||||
delete expProp.geometry;
|
||||
expect(gotProp).to.eql(expProp);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#writeGeometry', function() {
|
||||
|
||||
it('encodes point', function() {
|
||||
var point = new ol.geom.Point([10, 20]);
|
||||
var geojson = format.writeGeometry(point);
|
||||
expect(point.getCoordinates()).to.eql(
|
||||
format.readGeometry(geojson).getCoordinates());
|
||||
});
|
||||
|
||||
it('encodes linestring', function() {
|
||||
var linestring = new ol.geom.LineString([[10, 20], [30, 40]]);
|
||||
var geojson = format.writeGeometry(linestring);
|
||||
expect(linestring.getCoordinates()).to.eql(
|
||||
format.readGeometry(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 = format.writeGeometry(polygon);
|
||||
expect(polygon.getCoordinates()).to.eql(
|
||||
format.readGeometry(geojson).getCoordinates());
|
||||
});
|
||||
|
||||
it.skip('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 = format.writeGeometry(collection);
|
||||
var got = format.readGeometry(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());
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
goog.require('ol.Feature');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.format.GeoJSON');
|
||||
goog.require('ol.geom.LineString');
|
||||
goog.require('ol.geom.LinearRing');
|
||||
goog.require('ol.geom.Point');
|
||||
goog.require('ol.geom.Polygon');
|
||||
goog.require('ol.proj');
|
||||
Reference in New Issue
Block a user