flatgeobuf
Version:
A performant binary encoding for geographic data
16 lines • 10.6 kB
JavaScript
import{readFileSync as e}from"node:fs";import t from"ol/Feature.js";import a from"ol/format/GeoJSON.js";import r from"ol/format/WKT.js";import{transform as o}from"ol/proj";import{describe as i,expect as s,it as u}from"vitest";import{deserialize as l,serialize as n}from"./ol.js";import{arrayToStream as w,takeAsync as y}from"./streams/utils.js";let F=new r,P=new a;function g(e){return d([e])}function d(e){return e.map(e=>F.readGeometry(e)).map((e,a)=>{let r=new t({geometry:e});return r.setId(a),r})}i("ol module",()=>{i("Geometry roundtrips",()=>{u("Point",async()=>{let e,t=g("POINT(1.2 -2.1)"),a=n(t);s((e=await y(l(a)),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Point via stream",async()=>{let e,t=g("POINT(1.2 -2.1)"),a=w(n(t));s((e=await y(l(a)),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Point Z",async()=>{let e,t=g("POINT Z(1.2 -2.1 5)"),a=n(t);s((e=await y(l(a)),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Point M",async()=>{let e,t=g("POINT M(1.2 -2.1 5)"),a=n(t);s((e=await y(l(a)),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Point ZM",async()=>{let e,t=g("POINT ZM(1.2 -2.1 5 12.8)"),a=n(t);s((e=await y(l(a)),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Points",async()=>{let e,t=d(["POINT(1.2 -2.1)","POINT(2.4 -4.8)"]);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Points Z",async()=>{let e,t=d(["POINT Z(1.2 -2.1 5)","POINT Z(2.4 -4.8 5.6)"]);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPoint",async()=>{let e,t=g("MULTIPOINT(10 40, 40 30, 20 20, 30 10)");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPoint Z",async()=>{let e,t=g("MULTIPOINT Z(10 40 0.5, 40 30 0.5, 20 20 0.5, 30 10 0.5)");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPoint ZM",async()=>{let e,t=g("MULTIPOINT ZM(10 40 0.5 1, 40 30 0.5 1, 20 20 0.5 1, 30 10 0.5 1)");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("LineString",async()=>{let e,t=g("LINESTRING(1.2 -2.1, 2.4 -4.8)");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("LineString Z",async()=>{let e,t=g("LINESTRING Z(1.2 -2.1 5, 2.4 -4.8 0.5)");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("LineString ZM",async()=>{let e,t=g("LINESTRING ZM(1.2 -2.1 5 1, 2.4 -4.8 0.5 1)");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiLineString",async()=>{let e,t=g(`MULTILINESTRING((10 10, 20 20, 10 40),
(40 40, 30 30, 40 20, 30 10), (50 50, 60 60, 50 90))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiLineString Z",async()=>{let e,t=g(`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))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiLineString ZM",async()=>{let e,t=g(`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))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiLineStringSinglePart",async()=>{let e,t=g("MULTILINESTRING((1.2 -2.1, 2.4 -4.8))");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Polygon",async()=>{let e,t=g("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Polygon Z",async()=>{let e,t=g("POLYGON Z((30 10 12.1, 40 40 4, 20 40 4, 10 20 0.5, 30 10 12))");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("Polygon ZM",async()=>{let e,t=g("POLYGON ZM((30 10 12.1 1, 40 40 4 1, 20 40 4 1, 10 20 0.5 1, 30 10 12 1))");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("PolygonWithHole",async()=>{let e,t=g(`POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),
(20 30, 35 35, 30 20, 20 30))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("PolygonWithHole Z",async()=>{let e,t=g(`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))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("PolygonWithHole ZM",async()=>{let e,t=g(`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))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("PolygonWithTwoHoles",async()=>{let e,t=g(`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))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygon",async()=>{let e,t=g(`MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),
((15 5, 40 10, 10 20, 5 10, 15 5)))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygon Z",async()=>{let e,t=g(`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)))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygon ZM",async()=>{let e,t=g(`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)))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygonWithHole",async()=>{let e,t=g(`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)))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygonWithHole Z",async()=>{let e,t=g(`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)))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygonWithHole ZM",async()=>{let e,t=g(`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)))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygonSinglePart",async()=>{let e,t=g("MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)))");s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygonSinglePartWithHole",async()=>{let e,t=g(`MULTIPOLYGON (((35 10, 45 45, 15 40, 10 20, 35 10),
(20 30, 35 35, 30 20, 20 30))))`);s((e=await y(l(n(t))),P.writeFeatures(e))).to.equal(P.writeFeatures(t))}),u("MultiPolygon with two holes",async()=>{let e,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]]]]}}]};s(JSON.parse((e=await y(l(n(P.readFeatures(t)))),P.writeFeatures(e)))).to.deep.equal(t)}),u("Should parse UScounties fgb produced from GDAL array buffer",async()=>{let t=new Uint8Array(e("./test/data/UScounties.fgb")),a=await y(l(t));for(let e of(s(a.length).to.eq(3221),a))s(e.getGeometry().getCoordinates()?.length).to.be.greaterThan(0)}),u("Should parse countries fgb produced from GDAL array buffer",async()=>{let t=new Uint8Array(e("./test/data/countries.fgb")),a=await y(l(t));for(let e of(s(a.length).to.eq(179),a))s(e.getGeometry().getCoordinates()?.length).to.be.greaterThan(0)}),u("Should parse countries fgb produced from GDAL stream",async()=>{let t=w(new Uint8Array(e("./test/data/countries.fgb")).buffer),a=await y(l(t));for(let e of(s(a.length).to.eq(179),a))s(e.getGeometry().getCoordinates()?.length).to.be.greaterThan(0)}),u("Bahamas",async()=>{let e,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]]]]}}]};s(JSON.parse((e=await y(l(n(P.readFeatures(t)))),P.writeFeatures(e)))).to.deep.equal(t)}),u("Heterogeneous geometry types",async()=>{let e,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]]}}]};s(JSON.parse((e=await y(l(n(P.readFeatures(t)))),P.writeFeatures(e)))).to.deep.equal(t)}),u("Heterogeneous geometry types Z",async()=>{let e,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]]}}]};s(JSON.parse((e=await y(l(n(P.readFeatures(t)))),P.writeFeatures(e)))).to.deep.equal(t)})}),i("Geometry roundtrips with RenderFeatures",()=>{u("Point",async()=>{let e=n(g("POINT(1.2 -2.1)")),t=await y(l(e,void 0,void 0,void 0,void 0,!0));s(t[0].getType()).toEqual("Point"),s(t[0].getFlatCoordinates()).toEqual([1.2,-2.1])}),u("MultiPoint",async()=>{let e=g("MULTIPOINT(10 40, 40 30, 20 20, 30 10)"),t=await y(l(n(e),void 0,void 0,void 0,void 0,!0));s(t[0].getType()).toEqual("MultiPoint"),s(t[0].getFlatCoordinates()).toEqual([10,40,40,30,20,20,30,10])}),u("LineString",async()=>{let e=g("LINESTRING(1.2 -2.1, 2.4 -4.8)"),t=await y(l(n(e),void 0,void 0,void 0,void 0,!0));s(t[0].getType()).toEqual("LineString"),s(t[0].getFlatCoordinates()).toEqual([1.2,-2.1,2.4,-4.8])}),u("Polygon",async()=>{let e=g("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"),t=await y(l(n(e),void 0,void 0,void 0,void 0,!0));s(t[0].getType()).toEqual("Polygon"),s(t[0].getFlatCoordinates()).toEqual([30,10,40,40,20,40,10,20,30,10])})}),i("Transforms geometry to another projection",()=>{i("Point",()=>{let e=g("POINT(13 56)"),t=o([13,56],"EPSG:4326","EPSG:3857");u("Point to Feature",async()=>{let a=n(e);s((await y(l(a,void 0,void 0,void 0,void 0,void 0,"EPSG:4326","EPSG:3857")))[0].getGeometry().getFlatCoordinates()).toEqual(t)}),u("Point to RenderFeature",async()=>{let a=n(e);s((await y(l(a,void 0,void 0,void 0,void 0,!0,"EPSG:4326","EPSG:3857")))[0].getFlatCoordinates()).toEqual(t)})})})});
//# sourceMappingURL=ol.spec.js.map