Run a portion of the format tests in node

This commit is contained in:
Tim Schaub
2021-05-02 09:17:32 -06:00
parent 8ff40c40e8
commit d8142931e2
12 changed files with 383 additions and 371 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ import {
} from '../../../src/ol/coordinate.js';
import {get} from '../../../src/ol/proj.js';
describe('ol.coordinate', function () {
describe('ol/coordinate.js', function () {
describe('#add', function () {
let coordinate, delta;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+388
View File
@@ -0,0 +1,388 @@
import Feature from '../../../../src/ol/Feature.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import Polyline, * as polyline from '../../../../src/ol/format/Polyline.js';
import expect from '../../expect.js';
import {get as getProjection, transform} from '../../../../src/ol/proj.js';
describe('ol/format/Polyline.js', function () {
let format;
let points;
let flatPoints, encodedFlatPoints, flippedFlatPoints;
let floats, smallFloats, encodedFloats;
let signedIntegers, encodedSignedIntegers;
let unsignedIntegers, encodedUnsignedIntegers;
let points3857;
function resetTestingData() {
format = new Polyline();
points = [
[-120.2, 38.5],
[-120.95, 40.7],
[-126.453, 43.252],
];
flatPoints = [-120.2, 38.5, -120.95, 40.7, -126.453, 43.252];
flippedFlatPoints = [38.5, -120.2, 40.7, -120.95, 43.252, -126.453];
encodedFlatPoints = '_p~iF~ps|U_ulLnnqC_mqNvxq`@';
points3857 = [
transform([-120.2, 38.5], 'EPSG:4326', 'EPSG:3857'),
transform([-120.95, 40.7], 'EPSG:4326', 'EPSG:3857'),
transform([-126.453, 43.252], 'EPSG:4326', 'EPSG:3857'),
];
floats = [0.0, 0.15, -0.01, -0.16, 0.16, 0.01];
smallFloats = [0.0, 0.00015, -0.00001, -0.00016, 0.00016, 0.00001];
encodedFloats = '?]@^_@A';
signedIntegers = [0, 15, -1, -16, 16, 1];
encodedSignedIntegers = '?]@^_@A';
unsignedIntegers = [0, 30, 1, 31, 32, 2, 174];
encodedUnsignedIntegers = '?]@^_@AmD';
}
// Reset testing data
beforeEach(resetTestingData);
describe('#readProjectionFromText', function () {
it('returns the default projection', function () {
const projection = format.readProjectionFromText(encodedFlatPoints);
expect(projection).to.eql(getProjection('EPSG:4326'));
});
});
describe('encodeDeltas', function () {
it('returns expected value', function () {
const encodeDeltas = polyline.encodeDeltas;
expect(encodeDeltas(flippedFlatPoints, 2)).to.eql(encodedFlatPoints);
});
});
describe('decodeDeltas', function () {
it('returns expected value', function () {
const decodeDeltas = polyline.decodeDeltas;
expect(decodeDeltas(encodedFlatPoints, 2)).to.eql(flippedFlatPoints);
});
});
describe('encodeFloats', function () {
it('returns expected value', function () {
const encodeFloats = polyline.encodeFloats;
expect(encodeFloats(smallFloats)).to.eql(encodedFloats);
resetTestingData();
expect(encodeFloats(smallFloats, 1e5)).to.eql(encodedFloats);
expect(encodeFloats(floats, 1e2)).to.eql(encodedFloats);
});
});
describe('decodeFloats', function () {
it('returns expected value', function () {
const decodeFloats = polyline.decodeFloats;
expect(decodeFloats(encodedFloats)).to.eql(smallFloats);
expect(decodeFloats(encodedFloats, 1e5)).to.eql(smallFloats);
expect(decodeFloats(encodedFloats, 1e2)).to.eql(floats);
});
});
describe('encodeSignedIntegers', function () {
it('returns expected value', function () {
const encodeSignedIntegers = polyline.encodeSignedIntegers;
expect(encodeSignedIntegers(signedIntegers)).to.eql(
encodedSignedIntegers
);
});
});
describe('decodeSignedIntegers', function () {
it('returns expected value', function () {
const decodeSignedIntegers = polyline.decodeSignedIntegers;
expect(decodeSignedIntegers(encodedSignedIntegers)).to.eql(
signedIntegers
);
});
});
describe('encodeUnsignedIntegers', function () {
it('returns expected value', function () {
const encodeUnsignedIntegers = polyline.encodeUnsignedIntegers;
expect(encodeUnsignedIntegers(unsignedIntegers)).to.eql(
encodedUnsignedIntegers
);
});
});
describe('decodeUnsignedIntegers', function () {
it('returns expected value', function () {
const decodeUnsignedIntegers = polyline.decodeUnsignedIntegers;
expect(decodeUnsignedIntegers(encodedUnsignedIntegers)).to.eql(
unsignedIntegers
);
});
});
describe('encodeFloat', function () {
it('returns expected value', function () {
const encodeFloats = polyline.encodeFloats;
expect(encodeFloats([0.0])).to.eql('?');
expect(encodeFloats([-0.00001])).to.eql('@');
expect(encodeFloats([0.00001])).to.eql('A');
expect(encodeFloats([-0.00002])).to.eql('B');
expect(encodeFloats([0.00002])).to.eql('C');
expect(encodeFloats([0.00015])).to.eql(']');
expect(encodeFloats([-0.00016])).to.eql('^');
expect(encodeFloats([-0.1], 10)).to.eql('@');
expect(encodeFloats([0.1], 10)).to.eql('A');
expect(encodeFloats([(16 * 32) / 1e5])).to.eql('__@');
expect(encodeFloats([(16 * 32 * 32) / 1e5])).to.eql('___@');
// from the "Encoded Polyline Algorithm Format" page at Google
expect(encodeFloats([-179.9832104])).to.eql('`~oia@');
});
});
describe('decodeFloat', function () {
it('returns expected value', function () {
const decodeFloats = polyline.decodeFloats;
expect(decodeFloats('?')).to.eql([0.0]);
expect(decodeFloats('@')).to.eql([-0.00001]);
expect(decodeFloats('A')).to.eql([0.00001]);
expect(decodeFloats('B')).to.eql([-0.00002]);
expect(decodeFloats('C')).to.eql([0.00002]);
expect(decodeFloats(']')).to.eql([0.00015]);
expect(decodeFloats('^')).to.eql([-0.00016]);
expect(decodeFloats('@', 10)).to.eql([-0.1]);
expect(decodeFloats('A', 10)).to.eql([0.1]);
expect(decodeFloats('__@')).to.eql([(16 * 32) / 1e5]);
expect(decodeFloats('___@')).to.eql([(16 * 32 * 32) / 1e5]);
// from the "Encoded Polyline Algorithm Format" page at Google
expect(decodeFloats('`~oia@')).to.eql([-179.98321]);
});
});
describe('encodeSignedInteger', function () {
it('returns expected value', function () {
const encodeSignedIntegers = polyline.encodeSignedIntegers;
expect(encodeSignedIntegers([0])).to.eql('?');
expect(encodeSignedIntegers([-1])).to.eql('@');
expect(encodeSignedIntegers([1])).to.eql('A');
expect(encodeSignedIntegers([-2])).to.eql('B');
expect(encodeSignedIntegers([2])).to.eql('C');
expect(encodeSignedIntegers([15])).to.eql(']');
expect(encodeSignedIntegers([-16])).to.eql('^');
expect(encodeSignedIntegers([16])).to.eql('_@');
expect(encodeSignedIntegers([16 * 32])).to.eql('__@');
expect(encodeSignedIntegers([16 * 32 * 32])).to.eql('___@');
});
});
describe('decodeSignedInteger', function () {
it('returns expected value', function () {
const decodeSignedIntegers = polyline.decodeSignedIntegers;
expect(decodeSignedIntegers('?')).to.eql([0]);
expect(decodeSignedIntegers('@')).to.eql([-1]);
expect(decodeSignedIntegers('A')).to.eql([1]);
expect(decodeSignedIntegers('B')).to.eql([-2]);
expect(decodeSignedIntegers('C')).to.eql([2]);
expect(decodeSignedIntegers(']')).to.eql([15]);
expect(decodeSignedIntegers('^')).to.eql([-16]);
expect(decodeSignedIntegers('_@')).to.eql([16]);
expect(decodeSignedIntegers('__@')).to.eql([16 * 32]);
expect(decodeSignedIntegers('___@')).to.eql([16 * 32 * 32]);
});
});
describe('encodeUnsignedInteger', function () {
it('returns expected value', function () {
const encodeUnsignedInteger = polyline.encodeUnsignedInteger;
expect(encodeUnsignedInteger(0)).to.eql('?');
expect(encodeUnsignedInteger(1)).to.eql('@');
expect(encodeUnsignedInteger(2)).to.eql('A');
expect(encodeUnsignedInteger(30)).to.eql(']');
expect(encodeUnsignedInteger(31)).to.eql('^');
expect(encodeUnsignedInteger(32)).to.eql('_@');
expect(encodeUnsignedInteger(32 * 32)).to.eql('__@');
expect(encodeUnsignedInteger(5 * 32 * 32)).to.eql('__D');
expect(encodeUnsignedInteger(32 * 32 * 32)).to.eql('___@');
// from the "Encoded Polyline Algorithm Format" page at Google
expect(encodeUnsignedInteger(174)).to.eql('mD');
});
});
describe('decodeUnsignedInteger', function () {
it('returns expected value', function () {
const decodeUnsignedIntegers = polyline.decodeUnsignedIntegers;
expect(decodeUnsignedIntegers('?')).to.eql([0]);
expect(decodeUnsignedIntegers('@')).to.eql([1]);
expect(decodeUnsignedIntegers('A')).to.eql([2]);
expect(decodeUnsignedIntegers(']')).to.eql([30]);
expect(decodeUnsignedIntegers('^')).to.eql([31]);
expect(decodeUnsignedIntegers('_@')).to.eql([32]);
expect(decodeUnsignedIntegers('__@')).to.eql([32 * 32]);
expect(decodeUnsignedIntegers('__D')).to.eql([5 * 32 * 32]);
expect(decodeUnsignedIntegers('___@')).to.eql([32 * 32 * 32]);
// from the "Encoded Polyline Algorithm Format" page at Google
expect(decodeUnsignedIntegers('mD')).to.eql([174]);
});
});
describe('#readFeature', function () {
it('returns the expected feature', function () {
const feature = format.readFeature(encodedFlatPoints);
expect(feature).to.be.an(Feature);
const geometry = feature.getGeometry();
expect(geometry).to.be.an(LineString);
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('transforms and returns the expected feature', function () {
const feature = format.readFeature(encodedFlatPoints, {
featureProjection: 'EPSG:3857',
});
expect(feature).to.be.an(Feature);
const geometry = feature.getGeometry();
expect(geometry).to.be.an(LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readFeatures', function () {
it('returns the expected feature', function () {
const features = format.readFeatures(encodedFlatPoints);
expect(features).to.be.an(Array);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.an(Feature);
const geometry = feature.getGeometry();
expect(geometry).to.be.an(LineString);
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('transforms and returns the expected features', function () {
const features = format.readFeatures(encodedFlatPoints, {
featureProjection: 'EPSG:3857',
});
expect(features).to.be.an(Array);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.an(Feature);
const geometry = feature.getGeometry();
expect(geometry).to.be.an(LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readGeometry', function () {
it('returns the expected geometry', function () {
const geometry = format.readGeometry(encodedFlatPoints);
expect(geometry).to.be.an(LineString);
expect(geometry.getFlatCoordinates()).to.eql(flatPoints);
});
it('parses XYZ linestring', function () {
const xyz = polyline.encodeDeltas(
[38.5, -120.2, 100, 40.7, -120.95, 200, 43.252, -126.453, 20],
3
);
const format = new Polyline({
geometryLayout: 'XYZ',
});
const geometry = format.readGeometry(xyz);
expect(geometry.getLayout()).to.eql('XYZ');
expect(geometry.getCoordinates()).to.eql([
[-120.2, 38.5, 100],
[-120.95, 40.7, 200],
[-126.453, 43.252, 20],
]);
});
it('transforms and returns the expected geometry', function () {
const geometry = format.readGeometry(encodedFlatPoints, {
featureProjection: 'EPSG:3857',
});
expect(geometry).to.be.an(LineString);
expect(geometry.getCoordinates()).to.eql(points3857);
});
});
describe('#readProjection', function () {
it('returns the expected projection', function () {
const projection = format.readProjection(encodedFlatPoints);
expect(projection).to.be(getProjection('EPSG:4326'));
});
});
describe('#writeFeature', function () {
it('returns the expected text', function () {
const feature = new Feature(new LineString(points));
expect(format.writeFeature(feature)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function () {
const feature = new Feature(new LineString(points3857));
expect(
format.writeFeature(feature, {
featureProjection: 'EPSG:3857',
})
).to.be(encodedFlatPoints);
});
});
describe('#writeFeature', function () {
it('returns the expected text', function () {
const features = [new Feature(new LineString(points))];
expect(format.writeFeatures(features)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function () {
const features = [new Feature(new LineString(points3857))];
expect(
format.writeFeatures(features, {
featureProjection: 'EPSG:3857',
})
).to.be(encodedFlatPoints);
});
});
describe('#writeGeometry', function () {
it('returns the expected text', function () {
const geometry = new LineString(points);
expect(format.writeGeometry(geometry)).to.be(encodedFlatPoints);
});
it('transforms and returns the expected text', function () {
const geometry = new LineString(points3857);
expect(
format.writeGeometry(geometry, {
featureProjection: 'EPSG:3857',
})
).to.be(encodedFlatPoints);
});
});
});
+262
View File
@@ -0,0 +1,262 @@
import Feature from '../../../../src/ol/Feature.js';
import FeatureFormat from '../../../../src/ol/format/Feature.js';
import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import TopoJSON from '../../../../src/ol/format/TopoJSON.js';
import expect from '../../expect.js';
import fse from 'fs-extra';
import {transform} from '../../../../src/ol/proj.js';
const aruba = {
type: 'Topology',
transform: {
scale: [0.036003600360036005, 0.017361589674592462],
translate: [-180, -89.99892578124998],
},
objects: {
aruba: {
type: 'Polygon',
properties: {
prop0: 'value0',
},
arcs: [[0]],
id: 533,
},
},
arcs: [
[
[3058, 5901],
[0, -2],
[-2, 1],
[-1, 3],
[-2, 3],
[0, 3],
[1, 1],
[1, -3],
[2, -5],
[1, -1],
],
],
};
const zeroId = {
type: 'Topology',
objects: {
foobar: {
type: 'Point',
id: 0,
coordinates: [0, 42],
},
},
};
const nullGeometry = {
type: 'Topology',
objects: {
foobar: {
type: null,
properties: {
prop0: 'value0',
},
id: 533,
},
},
};
describe('ol/format/TopoJSON.js', function () {
let format;
before(function () {
format = new TopoJSON();
});
describe('constructor', function () {
it('creates a new format', function () {
expect(format).to.be.a(FeatureFormat);
expect(format).to.be.a(TopoJSON);
});
});
describe('#readFeaturesFromTopology_()', function () {
it('creates an array of features from a topology', function () {
const features = format.readFeaturesFromObject(aruba);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
const geometry = feature.getGeometry();
expect(geometry).to.be.a(Polygon);
// Parses identifier
expect(feature.getId()).to.be(533);
// Parses properties
expect(feature.get('prop0')).to.be('value0');
expect(geometry.getExtent()).to.eql([
-70.08100810081008,
12.417091709170947,
-69.9009900990099,
12.608069195591469,
]);
});
it('can read a feature with id equal to 0', function () {
const features = format.readFeaturesFromObject(zeroId);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
expect(feature.getId()).to.be(0);
});
it('can read a feature with null geometry', function () {
const features = format.readFeaturesFromObject(nullGeometry);
expect(features).to.have.length(1);
const feature = features[0];
expect(feature).to.be.a(Feature);
expect(feature.getGeometry()).to.be(null);
expect(feature.getId()).to.be(533);
expect(feature.get('prop0')).to.be('value0');
});
});
describe('#readFeatures()', function () {
it('parses simple.json', async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/simple.json',
{encoding: 'utf8'}
);
const features = format.readFeatures(text);
expect(features.length).to.be(3);
const point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(point.getFlatCoordinates()).to.eql([102, 0.5]);
const line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getFlatCoordinates()).to.eql([
102,
0,
103,
1,
104,
0,
105,
1,
]);
const polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getFlatCoordinates()).to.eql([
100,
0,
100,
1,
101,
1,
101,
0,
100,
0,
]);
});
it('parses simple.json and transforms', async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/simple.json',
{encoding: 'utf8'}
);
const features = format.readFeatures(text, {
featureProjection: 'EPSG:3857',
});
expect(features.length).to.be(3);
const point = features[0].getGeometry();
expect(point.getType()).to.be('Point');
expect(features[0].getGeometry().getCoordinates()).to.eql(
transform([102.0, 0.5], 'EPSG:4326', 'EPSG:3857')
);
const line = features[1].getGeometry();
expect(line.getType()).to.be('LineString');
expect(line.getCoordinates()).to.eql([
transform([102.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([103.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([104.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([105.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
]);
const polygon = features[2].getGeometry();
expect(polygon.getType()).to.be('Polygon');
expect(polygon.getCoordinates()).to.eql([
[
transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([100.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([101.0, 1.0], 'EPSG:4326', 'EPSG:3857'),
transform([101.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
transform([100.0, 0.0], 'EPSG:4326', 'EPSG:3857'),
],
]);
});
it('parses world-110m.json', async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/world-110m.json',
{encoding: 'utf8'}
);
const features = format.readFeatures(text);
expect(features.length).to.be(178);
const first = features[0];
expect(first).to.be.a(Feature);
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(MultiPolygon);
expect(firstGeom.getExtent()).to.eql([
-180,
-85.60903777459777,
180,
83.64513000000002,
]);
const last = features[177];
expect(last).to.be.a(Feature);
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(lastGeom.getExtent()).to.eql([
25.26325263252633,
-22.271802279310577,
32.848528485284874,
-15.50833810039586,
]);
});
it("sets the topology's child names as feature property", async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/world-110m.json',
{encoding: 'utf8'}
);
const format = new TopoJSON({
layerName: 'layer',
});
const features = format.readFeatures(text);
expect(features[0].get('layer')).to.be('land');
expect(features[177].get('layer')).to.be('countries');
});
it("only parses features from specified topology's children", async () => {
const text = await fse.readFile(
'test/node/ol/format/TopoJSON/world-110m.json',
{encoding: 'utf8'}
);
const format = new TopoJSON({
layers: ['land'],
});
const features = format.readFeatures(text);
expect(features.length).to.be(1);
});
});
});
+39
View File
@@ -0,0 +1,39 @@
{
"type": "Topology",
"objects": {
"example": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"properties": {
"prop0": "value0"
},
"coordinates": [102, 0.5]
},
{
"type": "LineString",
"properties": {
"prop0": "value0",
"prop1": 0
},
"arcs": [0]
},
{
"type": "Polygon",
"properties": {
"prop0": "value0",
"prop1": {
"this": "that"
}
},
"arcs": [[-2]]
}
]
}
},
"arcs": [
[[102, 0], [103, 1], [104, 0], [105, 1]],
[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]
]
}
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff