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

View File

@@ -1,256 +0,0 @@
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 {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', 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', function (done) {
afterLoadText('spec/ol/format/topojson/simple.json', function (text) {
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,
]);
done();
});
});
it('parses simple.json and transforms', function (done) {
afterLoadText('spec/ol/format/topojson/simple.json', function (text) {
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'),
],
]);
done();
});
});
it('parses world-110m.json', function (done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function (text) {
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,
]);
done();
});
});
it("sets the topology's child names as feature property", function (done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function (text) {
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');
done();
});
});
it("only parses features from specified topology's children", function (done) {
afterLoadText('spec/ol/format/topojson/world-110m.json', function (text) {
const format = new TopoJSON({
layers: ['land'],
});
const features = format.readFeatures(text);
expect(features.length).to.be(1);
done();
});
});
});
});

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;

View File

@@ -1,16 +1,18 @@
import EsriJSON from '../../../../../src/ol/format/EsriJSON.js';
import Feature from '../../../../../src/ol/Feature.js';
import LineString from '../../../../../src/ol/geom/LineString.js';
import LinearRing from '../../../../../src/ol/geom/LinearRing.js';
import MultiLineString from '../../../../../src/ol/geom/MultiLineString.js';
import MultiPoint from '../../../../../src/ol/geom/MultiPoint.js';
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../../src/ol/geom/Point.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js';
import {equals} from '../../../../../src/ol/extent.js';
import {get as getProjection, transform} from '../../../../../src/ol/proj.js';
import EsriJSON from '../../../../src/ol/format/EsriJSON.js';
import Feature from '../../../../src/ol/Feature.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import LinearRing from '../../../../src/ol/geom/LinearRing.js';
import MultiLineString from '../../../../src/ol/geom/MultiLineString.js';
import MultiPoint from '../../../../src/ol/geom/MultiPoint.js';
import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import fse from 'fs-extra';
import {equals} from '../../../../src/ol/extent.js';
import {get as getProjection, transform} from '../../../../src/ol/proj.js';
describe('ol.format.EsriJSON', function () {
describe('ol/format/EsriJSON.js', function () {
let format;
beforeEach(function () {
format = new EsriJSON();
@@ -374,42 +376,43 @@ describe('ol.format.EsriJSON', function () {
expect(secondGeom).to.be.a(LineString);
});
it('parses ksfields.geojson', function (done) {
afterLoadText('spec/ol/format/esrijson/ksfields.json', function (text) {
const result = format.readFeatures(text);
expect(result.length).to.be(9);
it('parses ksfields.geojson', async () => {
const text = await fse.readFile(
'test/node/ol/format/EsriJSON/ksfields.json',
{encoding: 'utf8'}
);
const result = format.readFeatures(text);
expect(result.length).to.be(9);
const first = result[0];
expect(first).to.be.a(Feature);
expect(first.get('field_name')).to.be('EUDORA');
expect(first.getId()).to.be(6406);
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(Polygon);
expect(
equals(firstGeom.getExtent(), [
-10585772.743554419,
4712365.161160459,
-10579560.16462974,
4716567.373073828,
])
).to.be(true);
const first = result[0];
expect(first).to.be.a(Feature);
expect(first.get('field_name')).to.be('EUDORA');
expect(first.getId()).to.be(6406);
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(Polygon);
expect(
equals(firstGeom.getExtent(), [
-10585772.743554419,
4712365.161160459,
-10579560.16462974,
4716567.373073828,
])
).to.be(true);
const last = result[8];
expect(last).to.be.a(Feature);
expect(last.get('field_name')).to.be('FEAGINS');
expect(last.getId()).to.be(6030);
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(
equals(lastGeom.getExtent(), [
-10555714.026858449,
4576511.565880965,
-10553671.199322715,
4578554.9934867555,
])
).to.be(true);
done();
});
const last = result[8];
expect(last).to.be.a(Feature);
expect(last.get('field_name')).to.be('FEAGINS');
expect(last.getId()).to.be(6030);
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(
equals(lastGeom.getExtent(), [
-10555714.026858449,
4576511.565880965,
-10553671.199322715,
4578554.9934867555,
])
).to.be(true);
});
});

View File

@@ -1,22 +1,24 @@
import Circle from '../../../../../src/ol/geom/Circle.js';
import Feature from '../../../../../src/ol/Feature.js';
import GeoJSON from '../../../../../src/ol/format/GeoJSON.js';
import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js';
import LineString from '../../../../../src/ol/geom/LineString.js';
import LinearRing from '../../../../../src/ol/geom/LinearRing.js';
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../../src/ol/geom/Point.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js';
import Circle from '../../../../src/ol/geom/Circle.js';
import Feature from '../../../../src/ol/Feature.js';
import GeoJSON from '../../../../src/ol/format/GeoJSON.js';
import GeometryCollection from '../../../../src/ol/geom/GeometryCollection.js';
import LineString from '../../../../src/ol/geom/LineString.js';
import LinearRing from '../../../../src/ol/geom/LinearRing.js';
import MultiPolygon from '../../../../src/ol/geom/MultiPolygon.js';
import Point from '../../../../src/ol/geom/Point.js';
import Polygon from '../../../../src/ol/geom/Polygon.js';
import expect from '../../expect.js';
import fse from 'fs-extra';
import {
Projection,
fromLonLat,
get as getProjection,
toLonLat,
transform,
} from '../../../../../src/ol/proj.js';
import {equals} from '../../../../../src/ol/extent.js';
} from '../../../../src/ol/proj.js';
import {equals} from '../../../../src/ol/extent.js';
describe('ol.format.GeoJSON', function () {
describe('ol/format/GeoJSON.js', function () {
let format;
beforeEach(function () {
format = new GeoJSON();
@@ -341,45 +343,43 @@ describe('ol.format.GeoJSON', function () {
expect(geometry).to.be.an(Polygon);
});
it('parses countries.geojson', function (done) {
afterLoadText(
'spec/ol/format/geojson/countries.geojson',
function (text) {
const result = format.readFeatures(text);
expect(result.length).to.be(179);
const first = result[0];
expect(first).to.be.a(Feature);
expect(first.get('name')).to.be('Afghanistan');
expect(first.getId()).to.be('AFG');
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(Polygon);
expect(
equals(firstGeom.getExtent(), [
60.52843,
29.318572,
75.158028,
38.486282,
])
).to.be(true);
const last = result[178];
expect(last).to.be.a(Feature);
expect(last.get('name')).to.be('Zimbabwe');
expect(last.getId()).to.be('ZWE');
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(
equals(lastGeom.getExtent(), [
25.264226,
-22.271612,
32.849861,
-15.507787,
])
).to.be(true);
done();
}
it('parses countries.geojson', async () => {
const text = await fse.readFile(
'test/node/ol/format/GeoJSON/countries.geojson',
{encoding: 'utf8'}
);
const result = format.readFeatures(text);
expect(result.length).to.be(179);
const first = result[0];
expect(first).to.be.a(Feature);
expect(first.get('name')).to.be('Afghanistan');
expect(first.getId()).to.be('AFG');
const firstGeom = first.getGeometry();
expect(firstGeom).to.be.a(Polygon);
expect(
equals(firstGeom.getExtent(), [
60.52843,
29.318572,
75.158028,
38.486282,
])
).to.be(true);
const last = result[178];
expect(last).to.be.a(Feature);
expect(last.get('name')).to.be('Zimbabwe');
expect(last.getId()).to.be('ZWE');
const lastGeom = last.getGeometry();
expect(lastGeom).to.be.a(Polygon);
expect(
equals(lastGeom.getExtent(), [
25.264226,
-22.271612,
32.849861,
-15.507787,
])
).to.be(true);
});
it('generates an array of features for Feature', function () {

View File

@@ -1,9 +1,10 @@
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 {get as getProjection, transform} from '../../../../../src/ol/proj.js';
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', function () {
describe('ol/format/Polyline.js', function () {
let format;
let points;
let flatPoints, encodedFlatPoints, flippedFlatPoints;

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);
});
});
});

View File

@@ -1,10 +1,11 @@
import Feature from '../../../../../src/ol/Feature.js';
import GeometryCollection from '../../../../../src/ol/geom/GeometryCollection.js';
import Point from '../../../../../src/ol/geom/Point.js';
import SimpleGeometry from '../../../../../src/ol/geom/SimpleGeometry.js';
import WKB from '../../../../../src/ol/format/WKB.js';
import WKT from '../../../../../src/ol/format/WKT.js';
import {transform} from '../../../../../src/ol/proj.js';
import Feature from '../../../../src/ol/Feature.js';
import GeometryCollection from '../../../../src/ol/geom/GeometryCollection.js';
import Point from '../../../../src/ol/geom/Point.js';
import SimpleGeometry from '../../../../src/ol/geom/SimpleGeometry.js';
import WKB from '../../../../src/ol/format/WKB.js';
import WKT from '../../../../src/ol/format/WKT.js';
import expect from '../../expect.js';
import {transform} from '../../../../src/ol/proj.js';
const patterns = [
[
@@ -957,7 +958,7 @@ const patterns = [
],
];
describe('ol.format.WKB', function () {
describe('ol/format/WKB.js', function () {
const format = new WKB();
describe('#readProjection(string)', function () {

View File

@@ -1,9 +1,10 @@
import Feature from '../../../../../src/ol/Feature.js';
import Point from '../../../../../src/ol/geom/Point.js';
import WKT from '../../../../../src/ol/format/WKT.js';
import {transform} from '../../../../../src/ol/proj.js';
import Feature from '../../../../src/ol/Feature.js';
import Point from '../../../../src/ol/geom/Point.js';
import WKT from '../../../../src/ol/format/WKT.js';
import expect from '../../expect.js';
import {transform} from '../../../../src/ol/proj.js';
describe('ol.format.WKT', function () {
describe('ol/format/WKT.js', function () {
let format = new WKT();
describe('#readProjectionFromText', function () {