flatgeobuf
Version:
A performant binary encoding for geographic data
16 lines • 8 kB
JavaScript
import{readFileSync as t}from"node:fs";import e from"ol/Feature.js";import a from"ol/format/GeoJSON.js";import o from"ol/format/WKT.js";import{describe as l,expect as i,it as n}from"vitest";import{deserialize as r,serialize as s}from"./ol.js";import{arrayToStream as u,takeAsync as y}from"./streams/utils.js";let g=new o,c=new a,P=t=>c.writeFeatures(t);function p(t){return M([t])}function M(t){return t.map(t=>g.readGeometry(t)).map((t,a)=>{let o=new e({geometry:t});return o.setId(a),o})}l("ol module",()=>{l("Geometry roundtrips",()=>{n("Point",async()=>{let t=p("POINT(1.2 -2.1)"),e=s(t);i(P(await y(r(e)))).to.equal(P(t))}),n("Point via stream",async()=>{let t=p("POINT(1.2 -2.1)"),e=u(s(t));i(P(await y(r(e)))).to.equal(P(t))}),n("Point Z",async()=>{let t=p("POINT Z(1.2 -2.1 5)"),e=s(t);i(P(await y(r(e)))).to.equal(P(t))}),n("Point M",async()=>{let t=p("POINT M(1.2 -2.1 5)"),e=s(t);i(P(await y(r(e)))).to.equal(P(t))}),n("Point ZM",async()=>{let t=p("POINT ZM(1.2 -2.1 5 12.8)"),e=s(t);i(P(await y(r(e)))).to.equal(P(t))}),n("Points",async()=>{let t=M(["POINT(1.2 -2.1)","POINT(2.4 -4.8)"]);i(P(await y(r(s(t))))).to.equal(P(t))}),n("Points Z",async()=>{let t=M(["POINT Z(1.2 -2.1 5)","POINT Z(2.4 -4.8 5.6)"]);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPoint",async()=>{let t=p("MULTIPOINT(10 40, 40 30, 20 20, 30 10)");i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPoint Z",async()=>{let t=p("MULTIPOINT Z(10 40 0.5, 40 30 0.5, 20 20 0.5, 30 10 0.5)");i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPoint ZM",async()=>{let t=p("MULTIPOINT ZM(10 40 0.5 1, 40 30 0.5 1, 20 20 0.5 1, 30 10 0.5 1)");i(P(await y(r(s(t))))).to.equal(P(t))}),n("LineString",async()=>{let t=p("LINESTRING(1.2 -2.1, 2.4 -4.8)");i(P(await y(r(s(t))))).to.equal(P(t))}),n("LineString Z",async()=>{let t=p("LINESTRING Z(1.2 -2.1 5, 2.4 -4.8 0.5)");i(P(await y(r(s(t))))).to.equal(P(t))}),n("LineString ZM",async()=>{let t=p("LINESTRING ZM(1.2 -2.1 5 1, 2.4 -4.8 0.5 1)");i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiLineString",async()=>{let t=p(`MULTILINESTRING((10 10, 20 20, 10 40),
(40 40, 30 30, 40 20, 30 10), (50 50, 60 60, 50 90))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiLineString Z",async()=>{let t=p(`MULTILINESTRING Z((10 10 5, 20 20 0.5, 10 40 12.1),
(40 40 4, 30 30 3, 40 20 2, 30 10 1), (50 50 5, 60 60 6, 50 90 9))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiLineString ZM",async()=>{let t=p(`MULTILINESTRING ZM((10 10 5 1, 20 20 0.5 1, 10 40 12.1 1),
(40 40 4 1, 30 30 3 1, 40 20 2 1, 30 10 1 2), (50 50 5 1, 60 60 6 1, 50 90 9 1))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiLineStringSinglePart",async()=>{let t=p("MULTILINESTRING((1.2 -2.1, 2.4 -4.8))");i(P(await y(r(s(t))))).to.equal(P(t))}),n("Polygon",async()=>{let t=p("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))");i(P(await y(r(s(t))))).to.equal(P(t))}),n("Polygon Z",async()=>{let t=p("POLYGON Z((30 10 12.1, 40 40 4, 20 40 4, 10 20 0.5, 30 10 12))");i(P(await y(r(s(t))))).to.equal(P(t))}),n("Polygon ZM",async()=>{let t=p("POLYGON ZM((30 10 12.1 1, 40 40 4 1, 20 40 4 1, 10 20 0.5 1, 30 10 12 1))");i(P(await y(r(s(t))))).to.equal(P(t))}),n("PolygonWithHole",async()=>{let t=p(`POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),
(20 30, 35 35, 30 20, 20 30))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("PolygonWithHole Z",async()=>{let t=p(`POLYGON Z ((35 10 4, 45 45 4, 15 40 4, 10 20 4, 35 10 4),
(20 30 4, 35 35 4, 30 20 4, 20 30 4))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("PolygonWithHole ZM",async()=>{let t=p(`POLYGON ZM ((35 10 4 1, 45 45 4 1, 15 40 4 1, 10 20 4 1, 35 10 4 1),
(20 30 4 1, 35 35 4 1, 30 20 4 1, 20 30 4 1))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("PolygonWithTwoHoles",async()=>{let t=p(`POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),
(20 30, 35 35, 30 20, 20 30), (20 30, 35 35, 30 20, 20 30))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygon",async()=>{let t=p(`MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),
((15 5, 40 10, 10 20, 5 10, 15 5)))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygon Z",async()=>{let t=p(`MULTIPOLYGON Z (((30 20 1, 45 40 3, 10 40 4, 30 20 6)),
((15 5 1, 40 10 1, 10 20 1, 5 10 1, 15 5 1)))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygon ZM",async()=>{let t=p(`MULTIPOLYGON ZM (((30 20 1 2, 45 40 3 2, 10 40 4 2, 30 20 6 2)),
((15 5 1 2, 40 10 1 2, 10 20 1 2, 5 10 1 2, 15 5 1 2)))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygonWithHole",async()=>{let t=p(`MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),
((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygonWithHole Z",async()=>{let t=p(`MULTIPOLYGON Z(((40 40 1, 20 45 1, 45 30 1, 40 40 1)),
((20 35 1, 10 30 1, 10 10 1, 30 5 1, 45 20 1, 20 35 1), (30 20 1, 20 15 1, 20 25 1, 30 20 1)))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygonWithHole ZM",async()=>{let t=p(`MULTIPOLYGON ZM (((40 40 1 2, 20 45 1 2, 45 30 1 2, 40 40 1 2)),
((20 35 1 2, 10 30 1 2, 10 10 1 2, 30 5 1 2, 45 20 1 2, 20 35 1 2), (30 20 1 2, 20 15 1 2, 20 25 1 2, 30 20 1 2)))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygonSinglePart",async()=>{let t=p("MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)))");i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygonSinglePartWithHole",async()=>{let t=p(`MULTIPOLYGON (((35 10, 45 45, 15 40, 10 20, 35 10),
(20 30, 35 35, 30 20, 20 30))))`);i(P(await y(r(s(t))))).to.equal(P(t))}),n("MultiPolygon with two holes",async()=>{let t={type:"FeatureCollection",features:[{type:"Feature",id:0,properties:{test:1},geometry:{type:"MultiPolygon",coordinates:[[[[102,2],[103,2],[103,3],[102,3],[102,2]]],[[[100,0],[101,0],[101,1],[100,1],[100,0]],[[100.2,.2],[100.8,.2],[100.8,.8],[100.2,.8],[100.2,.2]]]]}}]};i(JSON.parse(P(await y(r(s(c.readFeatures(t))))))).to.deep.equal(t)}),n("Should parse UScounties fgb produced from GDAL array buffer",async()=>{let e=new Uint8Array(t("./test/data/UScounties.fgb")),a=await y(r(e));for(let t of(i(a.length).to.eq(3221),a))i(t.getGeometry().getCoordinates()?.length).to.be.greaterThan(0)}),n("Should parse countries fgb produced from GDAL array buffer",async()=>{let e=new Uint8Array(t("./test/data/countries.fgb")),a=await y(r(e));for(let t of(i(a.length).to.eq(179),a))i(t.getGeometry().getCoordinates()?.length).to.be.greaterThan(0)}),n("Should parse countries fgb produced from GDAL stream",async()=>{let e=u(new Uint8Array(t("./test/data/countries.fgb")).buffer),a=await y(r(e));for(let t of(i(a.length).to.eq(179),a))i(t.getGeometry().getCoordinates()?.length).to.be.greaterThan(0)}),n("Bahamas",async()=>{let t={type:"FeatureCollection",features:[{type:"Feature",id:0,properties:{name:"The Bahamas"},geometry:{type:"MultiPolygon",coordinates:[[[[-77.53466,23.75975],[-77.78,23.71],[-78.03405,24.28615],[-78.40848,24.57564],[-78.19087,25.2103],[-77.89,25.17],[-77.54,24.34],[-77.53466,23.75975]]],[[[-77.82,26.58],[-78.91,26.42],[-78.98,26.79],[-78.51,26.87],[-77.85,26.84],[-77.82,26.58]]],[[[-77,26.59],[-77.17255,25.87918],[-77.35641,26.00735],[-77.34,26.53],[-77.78802,26.92516],[-77.79,27.04],[-77,26.59]]]]}}]};i(JSON.parse(P(await y(r(s(c.readFeatures(t))))))).to.deep.equal(t)}),n("Heterogeneous geometry types",async()=>{let t={type:"FeatureCollection",features:[{type:"Feature",id:0,properties:{name:"A"},geometry:{type:"Point",coordinates:[-77.53466,23.75975]}},{type:"Feature",id:1,properties:{name:"B"},geometry:{type:"LineString",coordinates:[[-77.53466,23.75975],[-77,26.59]]}}]};i(JSON.parse(P(await y(r(s(c.readFeatures(t))))))).to.deep.equal(t)}),n("Heterogeneous geometry types Z",async()=>{let t={type:"FeatureCollection",features:[{type:"Feature",id:0,properties:{name:"A"},geometry:{type:"Point",coordinates:[-77.53466,23.75975,12]}},{type:"Feature",id:1,properties:{name:"B"},geometry:{type:"LineString",coordinates:[[-77.53466,23.75975,12],[-77,26.59,12]]}}]};i(JSON.parse(P(await y(r(s(c.readFeatures(t))))))).to.deep.equal(t)})})});
//# sourceMappingURL=ol.spec.js.map