Merge pull request #2407 from ahocevar/format-projection

Options for feature readers and writers to support transforms
This commit is contained in:
Tobias Sauerwein
2014-08-21 17:58:10 +02:00
29 changed files with 1282 additions and 218 deletions

View File

@@ -174,6 +174,36 @@ describe('ol.format.GeoJSON', function() {
expect(features[2].getGeometry()).to.be.an(ol.geom.Polygon);
});
it('can read and transform a point', function() {
var feature = format.readFeatures(pointGeoJSON, {
featureProjection: 'EPSG:3857'
});
expect(feature[0].getGeometry()).to.be.an(ol.geom.Point);
expect(feature[0].getGeometry().getCoordinates()).to.eql(
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
});
it('can read and transform a feature collection', function() {
var features = format.readFeatures(featureCollectionGeoJSON, {
featureProjection: 'EPSG:3857'
});
expect(features[0].getGeometry()).to.be.an(ol.geom.Point);
expect(features[0].getGeometry().getCoordinates()).to.eql(
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
expect(features[1].getGeometry().getCoordinates()).to.eql([
ol.proj.transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857')
]);
expect(features[2].getGeometry().getCoordinates()).to.eql([[
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857')
]]);
});
it('can create a feature with a specific geometryName', function() {
var feature = new ol.format.GeoJSON({geometryName: 'the_geom'}).
readFeature(pointGeoJSON);
@@ -452,6 +482,22 @@ describe('ol.format.GeoJSON', function() {
}
});
it('transforms and encodes feature collection', function() {
var str = JSON.stringify(data),
array = format.readFeatures(str);
var geojson = format.writeFeatures(array, {
featureProjection: 'EPSG:3857'
});
var result = format.readFeatures(geojson);
var got, exp;
for (var i = 0, ii = array.length; i < ii; ++i) {
got = array[i];
exp = result[i];
expect(got.getGeometry().transform('EPSG:3857', 'EPSG:4326')
.getCoordinates()).to.eql(exp.getGeometry().getCoordinates());
}
});
});
describe('#writeGeometry', function() {
@@ -507,6 +553,20 @@ describe('ol.format.GeoJSON', function() {
});
});
it('transforms and encodes a point', function() {
var point = new ol.geom.Point([2, 3]);
var geojson = format.writeGeometry(point, {
featureProjection: 'EPSG:3857'
});
var newPoint = format.readGeometry(geojson, {
featureProjection: 'EPSG:3857'
});
expect(point.getCoordinates()[0]).to.eql(newPoint.getCoordinates()[0]);
expect(
Math.abs(point.getCoordinates()[1] - newPoint.getCoordinates()[1]))
.to.be.lessThan(0.0000001);
});
});
});

View File

@@ -1,19 +1,20 @@
goog.provide('ol.test.format.GML');
var readGeometry = function(format, text) {
var readGeometry = function(format, text, opt_options) {
var doc = ol.xml.load(text);
// we need an intermediate node for testing purposes
var node = goog.dom.createElement(goog.dom.TagName.PRE);
node.appendChild(doc.documentElement);
return format.readGeometryFromNode(node);
return format.readGeometryFromNode(node, opt_options);
};
describe('ol.format.GML', function() {
var format, formatWGS84;
var format, formatWGS84, formatNoSrs;
beforeEach(function() {
format = new ol.format.GML({srsName: 'CRS:84'});
formatWGS84 = new ol.format.GML({srsName: 'urn:x-ogc:def:crs:EPSG:4326'});
formatNoSrs = new ol.format.GML();
});
describe('#readGeometry', function() {
@@ -33,6 +34,44 @@ describe('ol.format.GML', function() {
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
it('can read, transform and write a point geometry', function() {
var config = {
featureProjection: 'EPSG:3857'
};
var text =
'<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:pos>1 2</gml:pos>' +
'</gml:Point>';
var g = readGeometry(format, text, config);
expect(g).to.be.an(ol.geom.Point);
var coordinates = g.getCoordinates();
expect(coordinates.splice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
config.dataProjection = 'CRS:84';
var serialized = format.writeGeometry(g, config);
var pos = serialized.firstElementChild.firstElementChild.textContent;
var coordinate = pos.split(' ');
expect(coordinate[0]).to.roughlyEqual(1, 1e-9);
expect(coordinate[1]).to.roughlyEqual(2, 1e-9);
});
it('can detect SRS, read and transform a point geometry', function() {
var config = {
featureProjection: 'EPSG:3857'
};
var text =
'<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:pos>1 2</gml:pos>' +
'</gml:Point>';
var g = readGeometry(formatNoSrs, text, config);
expect(g).to.be.an(ol.geom.Point);
var coordinates = g.getCoordinates();
expect(coordinates.splice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
});
it('can read and write a point geometry in EPSG:4326', function() {
var text =
'<gml:Point xmlns:gml="http://www.opengis.net/gml" ' +
@@ -63,6 +102,32 @@ describe('ol.format.GML', function() {
expect(serialized.firstElementChild).to.xmleql(ol.xml.load(text));
});
it('can read, transform and write a linestring geometry', function() {
var config = {
dataProjection: 'CRS:84',
featureProjection: 'EPSG:3857'
};
var text =
'<gml:LineString xmlns:gml="http://www.opengis.net/gml" ' +
' srsName="CRS:84">' +
' <gml:posList>1 2 3 4</gml:posList>' +
'</gml:LineString>';
var g = readGeometry(format, text, config);
expect(g).to.be.an(ol.geom.LineString);
var coordinates = g.getCoordinates();
expect(coordinates[0].slice(0, 2)).to.eql(
ol.proj.transform([1, 2], 'CRS:84', 'EPSG:3857'));
expect(coordinates[1].slice(0, 2)).to.eql(
ol.proj.transform([3, 4], 'CRS:84', 'EPSG:3857'));
var serialized = format.writeGeometry(g, config);
var poss = serialized.firstElementChild.firstElementChild.textContent;
var coordinate = poss.split(' ');
expect(coordinate[0]).to.roughlyEqual(1, 1e-9);
expect(coordinate[1]).to.roughlyEqual(2, 1e-9);
expect(coordinate[2]).to.roughlyEqual(3, 1e-9);
expect(coordinate[3]).to.roughlyEqual(4, 1e-9);
});
it('can read and write a linestring geometry in EPSG:4326', function() {
var text =
'<gml:LineString xmlns:gml="http://www.opengis.net/gml" ' +
@@ -779,7 +844,7 @@ describe('ol.format.GML', function() {
describe('when parsing more than one geometry', function() {
var features, feature;
var features;
before(function(done) {
afterLoadText('spec/ol/format/gml/more-geoms.xml', function(xml) {
try {
@@ -805,7 +870,7 @@ describe('ol.format.GML', function() {
describe('when parsing an attribute name equal to featureType', function() {
var features, feature;
var features;
before(function(done) {
afterLoadText('spec/ol/format/gml/repeated-name.xml', function(xml) {
try {
@@ -842,3 +907,4 @@ goog.require('ol.geom.MultiPolygon');
goog.require('ol.xml');
goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon');
goog.require('ol.proj');

View File

@@ -1,6 +1,5 @@
goog.provide('ol.test.format.GPX');
describe('ol.format.GPX', function() {
var format;
@@ -80,6 +79,34 @@ describe('ol.format.GPX', function() {
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can transform, read and write a rte', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <rte>' +
' <rtept lat="1" lon="2"/>' +
' <rtept lat="5" lon="6"/>' +
' </rte>' +
'</gpx>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.LineString);
var p1 = ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857');
p1.push(0, 0);
var p2 = ol.proj.transform([6, 5], 'EPSG:4326', 'EPSG:3857');
p2.push(0, 0);
expect(g.getCoordinates()).to.eql([p1, p2]);
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
var serialized = format.writeFeatures(fs, {
featureProjection: 'EPSG:3857'
});
expect(serialized).to.xmleql(ol.xml.load(text));
});
});
describe('trk', function() {
@@ -181,6 +208,42 @@ describe('ol.format.GPX', function() {
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can tranform, read and write a trk with a trkseg', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <trk>' +
' <trkseg>' +
' <trkpt lat="1" lon="2">' +
' <ele>3</ele>' +
' <time>2010-01-10T09:29:12Z</time>' +
' </trkpt>' +
' <trkpt lat="5" lon="6">' +
' <ele>7</ele>' +
' <time>2010-01-10T09:30:12Z</time>' +
' </trkpt>' +
' </trkseg>' +
' </trk>' +
'</gpx>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.MultiLineString);
var p1 = ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857');
p1.push(3, 1263115752);
var p2 = ol.proj.transform([6, 5], 'EPSG:4326', 'EPSG:3857');
p2.push(7, 1263115812);
expect(g.getCoordinates()).to.eql([[p1, p2]]);
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
var serialized = format.writeFeatures(fs, {
featureProjection: 'EPSG:3857'
});
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can read and write a trk with multiple trksegs', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
@@ -243,6 +306,29 @@ describe('ol.format.GPX', function() {
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can transform, read and write a wpt', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
' <wpt lat="1" lon="2"/>' +
'</gpx>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
var expectedPoint = ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857');
expectedPoint.push(0, 0);
expect(g.getCoordinates()).to.eql(expectedPoint);
expect(g.getLayout()).to.be(ol.geom.GeometryLayout.XYZM);
var serialized = format.writeFeatures(fs, {
featureProjection: 'EPSG:3857'
});
expect(serialized).to.xmleql(ol.xml.load(text));
});
it('can read and write a wpt with ele', function() {
var text =
'<gpx xmlns="http://www.topografix.com/GPX/1/1">' +
@@ -471,4 +557,5 @@ goog.require('ol.format.GPX');
goog.require('ol.geom.LineString');
goog.require('ol.geom.MultiLineString');
goog.require('ol.geom.Point');
goog.require('ol.proj');
goog.require('ol.xml');

View File

@@ -0,0 +1,121 @@
goog.provide('ol.test.format.IGC');
describe('ol.format.IGC', function() {
var format;
var igc =
'AFLY05094\n' +
'HFDTE190411\n' +
'HFFXA100\n' +
'HFPLTPILOT:Tom Payne\n' +
'HFGTYGLIDERTYPE:Axis Mercury\n' +
'HFGIDGLIDERID:\n' +
'HFDTM100GPSDATUM:WGS84\n' +
'HFGPSGPS:FURUNO GH-80\n' +
'HFRFWFIRMWAREVERSION:1.22\n' +
'HFRHWHARDWAREVERSION:1.00\n' +
'HFFTYFRTYPE:FLYTEC,5020\n' +
'I013638TAS\n' +
'B0848484556256N00651095EA0205102039000\n' +
'B0855534556037N00651011EA0259302513000\n' +
'B0903354554964N00648049EA0272402758000\n' +
'GAB890A77AFE5CE63979AF6B1BED7F07D\n' +
'G62BB282E44D63A1149EF2F5E8AF6F2F1\n' +
'GEC14381987B15F81003EDE1E01A47843\n' +
'G60189641B00B00800019000000000000';
beforeEach(function() {
format = new ol.format.IGC();
});
describe('#readFeature', function() {
it('does not read invalid features', function() {
expect(format.readFeature('invalid')).to.be(null);
});
it('does read a feature', function() {
var feature = format.readFeature(igc);
expect(feature).to.be.an(ol.Feature);
var geom = feature.getGeometry();
expect(geom.getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(geom.getCoordinates()).to.eql([
[6.851583333333333, 45.9376, 1303202928],
[6.850183333333334, 45.93395, 1303203353],
[6.800816666666667, 45.916066666666666, 1303203815]]);
});
it('does transform and read a feature', function() {
var feature = format.readFeature(igc, {
featureProjection: 'EPSG:3857'
});
expect(feature).to.be.an(ol.Feature);
var geom = feature.getGeometry();
expect(geom.getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
var expectedPoint1 = ol.proj.transform(
[6.851583333333333, 45.9376], 'EPSG:4326', 'EPSG:3857');
expectedPoint1.push(1303202928);
var expectedPoint2 = ol.proj.transform(
[6.850183333333334, 45.93395], 'EPSG:4326', 'EPSG:3857');
expectedPoint2.push(1303203353);
var expectedPoint3 = ol.proj.transform(
[6.800816666666667, 45.916066666666666], 'EPSG:4326', 'EPSG:3857');
expectedPoint3.push(1303203815);
expect(geom.getCoordinates()).to.eql(
[expectedPoint1, expectedPoint2, expectedPoint3]);
});
});
describe('#readFeatures', function() {
it('does not read invalid features', function() {
expect(format.readFeatures('invalid')).to.be.empty();
});
it('does read features', function() {
var features = format.readFeatures(igc);
expect(features.length).to.eql(1);
var feature = features[0];
expect(feature).to.be.an(ol.Feature);
var geom = feature.getGeometry();
expect(geom.getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
expect(geom.getCoordinates()).to.eql([
[6.851583333333333, 45.9376, 1303202928],
[6.850183333333334, 45.93395, 1303203353],
[6.800816666666667, 45.916066666666666, 1303203815]]);
});
it('does transform and read features', function() {
var features = format.readFeatures(igc, {
featureProjection: 'EPSG:3857'
});
expect(features.length).to.eql(1);
var feature = features[0];
expect(feature).to.be.an(ol.Feature);
var geom = feature.getGeometry();
expect(geom.getType()).to.eql(ol.geom.GeometryType.LINE_STRING);
var expectedPoint1 = ol.proj.transform(
[6.851583333333333, 45.9376], 'EPSG:4326', 'EPSG:3857');
expectedPoint1.push(1303202928);
var expectedPoint2 = ol.proj.transform(
[6.850183333333334, 45.93395], 'EPSG:4326', 'EPSG:3857');
expectedPoint2.push(1303203353);
var expectedPoint3 = ol.proj.transform(
[6.800816666666667, 45.916066666666666], 'EPSG:4326', 'EPSG:3857');
expectedPoint3.push(1303203815);
expect(geom.getCoordinates()).to.eql(
[expectedPoint1, expectedPoint2, expectedPoint3]);
});
});
});
goog.require('ol.format.IGC');
goog.require('ol.Feature');
goog.require('ol.geom.GeometryType');
goog.require('ol.proj');

View File

@@ -1,29 +0,0 @@
goog.provide('ol.test.format.IGC');
describe('ol.format.IGC', function() {
var format;
beforeEach(function() {
format = new ol.format.IGC();
});
describe('#readFeature', function() {
it('does not read invalid features', function() {
expect(format.readFeature('invalid')).to.be(null);
});
});
describe('#readFeatures', function() {
it('does not read invalid features', function() {
expect(format.readFeatures('invalid')).to.be.empty();
});
});
});
goog.require('ol.format.IGC');

View File

@@ -119,6 +119,76 @@ describe('ol.format.KML', function() {
expect(g.getCoordinates()).to.eql([1, 2, 3]);
});
it('can transform and read Point geometries', function() {
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</kml>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857');
expectedPoint.push(3);
expect(g.getCoordinates()).to.eql(expectedPoint);
});
it('can read a single Point geometry', function() {
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</kml>';
var f = format.readFeature(text);
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([1, 2, 3]);
});
it('can transform and read a single Point geometry', function() {
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</kml>';
var f = format.readFeature(text, {
featureProjection: 'EPSG:3857'
});
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857');
expectedPoint.push(3);
expect(g.getCoordinates()).to.eql(expectedPoint);
});
it('can write XY Point geometries', function() {
var layout = ol.geom.GeometryLayout.XY;
var point = new ol.geom.Point([1, 2], layout);
@@ -159,6 +229,29 @@ describe('ol.format.KML', function() {
expect(node).to.xmleql(ol.xml.load(text));
});
it('can transform and write XYZ Point geometries', function() {
var layout = ol.geom.GeometryLayout.XYZ;
var point = new ol.geom.Point([1, 2, 3], layout).transform(
'EPSG:4326', 'EPSG:3857');
var features = [new ol.Feature(point)];
var node = format.writeFeatures(features, {
featureProjection: 'EPSG:3857'
});
var text =
'<kml xmlns="http://www.opengis.net/kml/2.2"' +
' xmlns:gx="http://www.google.com/kml/ext/2.2"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xsi:schemaLocation="http://www.opengis.net/kml/2.2' +
' https://developers.google.com/kml/schema/kml22gx.xsd">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</kml>';
expect(node).to.xmleql(ol.xml.load(text));
});
it('can write XYM Point geometries', function() {
var layout = ol.geom.GeometryLayout.XYM;
var point = new ol.geom.Point([1, 2, 100], layout);
@@ -1890,6 +1983,28 @@ describe('ol.format.KML', function() {
expect(fs[0]).to.be.an(ol.Feature);
});
it('can transform and read a single feature from a Document', function() {
var text =
'<Document xmlns="http://earth.google.com/kml/2.2">' +
' <Placemark>' +
' <Point>' +
' <coordinates>1,2,3</coordinates>' +
' </Point>' +
' </Placemark>' +
'</Document>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(1);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
var expectedPoint = ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857');
expectedPoint.push(3);
expect(g.getCoordinates()).to.eql(expectedPoint);
});
it('can read a multiple features from a Document', function() {
var text =
'<Document xmlns="http://earth.google.com/kml/2.2">' +
@@ -2317,6 +2432,7 @@ goog.require('ol.geom.Polygon');
goog.require('ol.style.Fill');
goog.require('ol.style.Icon');
goog.require('ol.style.IconOrigin');
goog.require('ol.proj');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
goog.require('ol.style.Text');

View File

@@ -0,0 +1,105 @@
goog.provide('ol.test.format.OSMXML');
describe('ol.format.OSMXML', function() {
var format;
beforeEach(function() {
format = new ol.format.OSMXML();
});
describe('#readFeatures', function() {
it('can read an empty document', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(0);
});
it('can read nodes', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(2);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([2, 1]);
});
it('can read nodes and ways', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
' <way id="3">' +
' <tag k="name" v="3"/>' +
' <nd ref="1" />' +
' <nd ref="2" />' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text);
expect(fs).to.have.length(3);
var point = fs[0];
expect(point).to.be.an(ol.Feature);
var g = point.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql([2, 1]);
var line = fs[2];
expect(line).to.be.an(ol.Feature);
g = line.getGeometry();
expect(g).to.be.an(ol.geom.LineString);
expect(g.getCoordinates()).to.eql([[2, 1], [4, 3]]);
});
it('can transform and read nodes', function() {
var text =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<osm version="0.6" generator="my hand">' +
' <node id="1" lat="1" lon="2">' +
' <tag k="name" v="1"/>' +
' </node>' +
' <node id="2" lat="3" lon="4">' +
' <tag k="name" v="2"/>' +
' </node>' +
'</osm>';
var fs = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(fs).to.have.length(2);
var f = fs[0];
expect(f).to.be.an(ol.Feature);
var g = f.getGeometry();
expect(g).to.be.an(ol.geom.Point);
expect(g.getCoordinates()).to.eql(
ol.proj.transform([2, 1], 'EPSG:4326', 'EPSG:3857'));
});
});
});
goog.require('goog.dom.xml');
goog.require('ol.Feature');
goog.require('ol.format.OSMXML');
goog.require('ol.geom.Point');
goog.require('ol.geom.LineString');
goog.require('ol.proj');

View File

@@ -11,13 +11,17 @@ describe('ol.format.Polyline', function() {
function resetTestingData() {
format = new ol.format.Polyline();
points = [[38.50000, -120.20000],
[40.70000, -120.95000],
[43.25200, -126.45300]];
flatPoints = [38.50000, -120.20000,
40.70000, -120.95000,
43.25200, -126.45300];
encodedFlatPoints = '_p~iF~ps|U_ulLnnqC_mqNvxq`@';
points = [[-120.20000, 38.50000],
[-120.95000, 40.70000],
[-126.45300, 43.25200]];
flatPoints = [-120.20000, 38.50000,
-120.95000, 40.70000,
-126.45300, 43.25200];
encodedFlatPoints = '~ps|U_p~iFnnqC_ulLvxq`@_mqN';
points3857 = [
ol.proj.transform([-120.20000, 38.50000], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([-120.95000, 40.70000], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([-126.45300, 43.25200], 'EPSG:4326', 'EPSG:3857')];
floats = [0.00, 0.15, -0.01, -0.16, 0.16, 0.01];
smallFloats = [0.00000, 0.00015, -0.00001, -0.00016, 0.00016, 0.00001];
@@ -253,6 +257,16 @@ describe('ol.format.Polyline', function() {
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('transforms and returns the expected feature', function() {
var feature = format.readFeature(encodedFlatPoints, {
featureProjection: 'EPSG:3857'
});
expect(feature).to.be.an(ol.Feature);
var geometry = feature.getGeometry();
expect(geometry).to.be.an(ol.geom.LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readFeatures', function() {
@@ -268,6 +282,19 @@ describe('ol.format.Polyline', function() {
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('transforms and returns the expected features', function() {
var features = format.readFeatures(encodedFlatPoints, {
featureProjection: 'EPSG:3857'
});
expect(features).to.be.an(Array);
expect(features).to.have.length(1);
var feature = features[0];
expect(feature).to.be.an(ol.Feature);
var geometry = feature.getGeometry();
expect(geometry).to.be.an(ol.geom.LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readGeometry', function() {
@@ -278,6 +305,14 @@ describe('ol.format.Polyline', function() {
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('transforms and returns the expected geometry', function() {
var geometry = format.readGeometry(encodedFlatPoints, {
featureProjection: 'EPSG:3857'
});
expect(geometry).to.be.an(ol.geom.LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readProjection', function() {
@@ -296,6 +331,13 @@ describe('ol.format.Polyline', function() {
expect(format.writeFeature(feature)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function() {
var feature = new ol.Feature(new ol.geom.LineString(points3857));
expect(format.writeFeature(feature, {
featureProjection: 'EPSG:3857'
})).to.be(encodedFlatPoints);
});
});
describe('#writeFeature', function() {
@@ -305,6 +347,13 @@ describe('ol.format.Polyline', function() {
expect(format.writeFeatures(features)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function() {
var features = [new ol.Feature(new ol.geom.LineString(points3857))];
expect(format.writeFeatures(features, {
featureProjection: 'EPSG:3857'
})).to.be(encodedFlatPoints);
});
});
describe('#writeGeometry', function() {
@@ -314,6 +363,13 @@ describe('ol.format.Polyline', function() {
expect(format.writeGeometry(geometry)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function() {
var geometry = new ol.geom.LineString(points3857);
expect(format.writeGeometry(geometry, {
featureProjection: 'EPSG:3857'
})).to.be(encodedFlatPoints);
});
});
});

View File

@@ -89,6 +89,41 @@ describe('ol.format.TopoJSON', function() {
});
});
it('parses simple.json and transforms', function(done) {
afterLoadText('spec/ol/format/topojson/simple.json', function(text) {
var features = format.readFeatures(text, {
featureProjection: 'EPSG:3857'
});
expect(features.length).to.be(3);
var point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(features[0].getGeometry().getCoordinates()).to.eql(
ol.proj.transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857'));
var line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getCoordinates()).to.eql([
ol.proj.transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857')
]);
var polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getCoordinates()).to.eql([[
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
ol.proj.transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857')
]]);
done();
});
});
it('parses world-110m.json', function(done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function(text) {
@@ -123,4 +158,5 @@ goog.require('ol.Feature');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon');
goog.require('ol.format.Feature');
goog.require('ol.proj');
goog.require('ol.format.TopoJSON');

View File

@@ -4,15 +4,17 @@ describe('ol.format.WFS', function() {
describe('when parsing TOPP states GML from WFS', function() {
var features, feature;
var features, feature, xml;
var config = {
'featureNS': 'http://www.openplans.org/topp',
'featureType': 'states'
};
before(function(done) {
proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
afterLoadText('spec/ol/format/wfs/topp-states-wfs.xml', function(xml) {
afterLoadText('spec/ol/format/wfs/topp-states-wfs.xml', function(data) {
try {
var config = {
'featureNS': 'http://www.openplans.org/topp',
'featureType': 'states'
};
xml = data;
features = new ol.format.WFS(config).readFeatures(xml);
} catch (e) {
done(e);
@@ -32,6 +34,20 @@ describe('ol.format.WFS', function() {
expect(feature.getGeometry()).to.be.an(ol.geom.MultiPolygon);
});
it('transforms and creates a polygon for Illinois', function() {
features = new ol.format.WFS(config).readFeatures(xml, {
featureProjection: 'EPSG:3857'
});
feature = features[0];
expect(feature.getId()).to.equal('states.1');
expect(feature.get('STATE_NAME')).to.equal('Illinois');
var geom = feature.getGeometry();
expect(geom).to.be.an(ol.geom.MultiPolygon);
var p = ol.proj.transform([-88.071, 37.511], 'EPSG:4326', 'EPSG:3857');
p.push(0);
expect(geom.getFirstCoordinate()).to.eql(p);
});
});
describe('when parsing FeatureCollection', function() {
@@ -388,3 +404,4 @@ goog.require('ol.geom.MultiPoint');
goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Polygon');
goog.require('ol.format.WFS');
goog.require('ol.proj');

View File

@@ -15,6 +15,21 @@ describe('ol.format.WKT', function() {
expect(geom.getCoordinates()).to.eql([30, 10]);
});
it('Point transformed / read / written correctly', function() {
var wkt = 'POINT(1 2)';
var geom = format.readGeometry(wkt, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(geom.getCoordinates()).to.eql(
ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'));
var newWkt = format.writeGeometry(geom, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(newWkt).to.eql(wkt);
});
it('MultiPoint read / written correctly', function() {
// there are two forms to test
var wkt = 'MULTIPOINT((10 40),(40 30),(20 20),(30 10))';
@@ -231,7 +246,67 @@ describe('ol.format.WKT', function() {
expect(format.writeFeatures(features)).to.eql(wkt);
});
it('Point feature read / written correctly', function() {
var wkt = 'POINT(30 10)';
var feature = format.readFeature(wkt);
var geom = feature.getGeometry();
expect(geom.getCoordinates()).to.eql([30, 10]);
expect(format.writeFeature(feature)).to.eql(wkt);
});
it('Point feature transformed / read / written correctly', function() {
var wkt = 'POINT(1 2)';
var feature = format.readFeature(wkt, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
var geom = feature.getGeometry();
expect(geom.getCoordinates()).to.eql(
ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'));
var newWkt = format.writeFeature(feature, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(newWkt).to.eql(wkt);
});
it('Features read / written correctly', function() {
var wkt = 'GEOMETRYCOLLECTION(POINT(1 2),POINT(3 4))';
var features = format.readFeatures(wkt);
expect(features.length).to.eql(2);
var point1 = features[0].getGeometry();
var point2 = features[1].getGeometry();
expect(point1.getType()).to.eql(ol.geom.GeometryType.POINT);
expect(point2.getType()).to.eql(ol.geom.GeometryType.POINT);
expect(point1.getCoordinates()).to.eql([1, 2]);
expect(point2.getCoordinates()).to.eql([3, 4]);
expect(format.writeFeatures(features)).to.eql(wkt);
});
it('Features transformed / read / written correctly', function() {
var wkt = 'GEOMETRYCOLLECTION(POINT(1 2),POINT(4 5))';
var features = format.readFeatures(wkt, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(features.length).to.eql(2);
var point1 = features[0].getGeometry();
var point2 = features[1].getGeometry();
expect(point1.getType()).to.eql(ol.geom.GeometryType.POINT);
expect(point2.getType()).to.eql(ol.geom.GeometryType.POINT);
expect(point1.getCoordinates()).to.eql(
ol.proj.transform([1, 2], 'EPSG:4326', 'EPSG:3857'));
expect(point2.getCoordinates()).to.eql(
ol.proj.transform([4, 5], 'EPSG:4326', 'EPSG:3857'));
var newWkt = format.writeFeatures(features, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
expect(newWkt).to.eql(wkt);
});
});
goog.require('ol.geom.GeometryType');
goog.require('ol.format.WKT');
goog.require('ol.proj');