thematic-earth
Version:
HTML-based, whole-Earth thematic maps using locally hosted data layers
2 lines • 4.59 kB
JavaScript
/* Copyright (c) 2023 Read Write Tools. Legal use subject to the Thematic Earth Software License Agreement. */
import expect from'../softlib/expect.js';import terminal from'../softlib/terminal.js';export default class EncodedSerializer{constructor(e,t,i,s,o,r){expect(e,'GcsHoldingArea'),expect(t,'String'),expect(i,'Number'),expect(s,'String'),expect(o,'Array'),expect(r,'Map'),this.gcsFeaturePoints=e.gcsFeaturePoints,this.gcsFeatureLines=e.gcsFeatureLines,this.gcsFeaturePolygons=e.gcsFeaturePolygons,this.indexedCoordinates=e.indexedCoordinates,this.topology=e.topology,this.format=t,this.accuracy=i,this.datasetId=s,this.propertiesToInclude=o,this.declarations=r,this.debugTopology=!1,this.buildDeclarations()}serialize(){try{switch(this.writeProlog(),this.format){case'ice':case'icebin':this.buildPoints(),this.buildLines(),this.buildPolygons(),this.writeMeridiansAndParallels();break;case'tae':case'taebin':this.gcsFeaturePoints.length>0&&terminal.abnormal(`TAE and TAEBIN output format are only useful for polygons, ignoring ${this.gcsFeaturePoints.length} points`),this.gcsFeatureLines.length>0&&terminal.abnormal(`TAE and TAEBIN output format are only useful for polygons, ignoring ${this.gcsFeatureLines.length} lines`),this.topology.buildTopology(this.gcsFeaturePolygons),this.writeCoordinates(),this.writeArcs()}this.writePointDataset(),this.writeLineDataset(),this.writePolygonDataset()}catch(e){return terminal.caught(e),null}}buildPoints(){for(let e of this.gcsFeaturePoints)this.indexedCoordinates.registerLongitude(e.discretePoint.longitude),this.indexedCoordinates.registerLatitude(e.discretePoint.latitude)}buildLines(){for(let e of this.gcsFeatureLines)for(let t=0;t<e.lineSegment.length;t++)this.indexedCoordinates.registerLongitude(e.lineSegment[t].longitude),this.indexedCoordinates.registerLatitude(e.lineSegment[t].latitude)}buildPolygons(){for(let t of this.gcsFeaturePolygons){for(let e=0;e<t.outerRing.length;e++)this.indexedCoordinates.registerLongitude(t.outerRing[e].longitude),this.indexedCoordinates.registerLatitude(t.outerRing[e].latitude);for(let i=0;i<t.innerRings.length;i++){var e=t.innerRings[i];for(let t=0;t<e.length;t++)this.indexedCoordinates.registerLongitude(e[t].longitude),this.indexedCoordinates.registerLatitude(e[t].latitude)}}}getPropertyNamesAndTypes(e,t){expect(e,'Array'),expect(t,'String');var i=[...e];i=e.includes('all')?this.examineHoldingAreaForProperties(t):e.includes('none')?[]:[...e];var s=[];'ice'==this.format||'icebin'==this.format?'Point'==t?s=['xCoord','yCoord',...i]:'Line'==t?s=['xSegment','ySegment',...i]:'Polygon'==t&&(s=['xRings','yRings',...i]):'gfe'==this.format||'gfebin'==this.format?'Point'==t?s=['lngCoord','latCoord',...i]:'Line'==t?s=['lngSegment','latSegment',...i]:'Polygon'==t&&(s=['lngRings','latRings',...i]):'tae'==this.format||'taebin'==this.format?'Polygon'==t&&(s=this.debugTopology?['edgeRefs','edgePairs','arcRefs','arcCoords',...i]:['arcRefs',...i]):terminal.logic(`expected 'gfe' or 'gfebin', 'ice', 'icebin', 'tae' or 'taebin' but got ${this.format}`);var o=[];for(let e=0;e<s.length;e++)o.push(this.getPropertyType(s[e]));return{userRequestedProperties:i,propertyNames:s,propertyTypes:o}}buildDeclarations(){this.declarations.set('xCoord','xCoord'),this.declarations.set('yCoord','yCoord'),this.declarations.set('xSegment','xSegment'),this.declarations.set('ySegment','ySegment'),this.declarations.set('xRings','xRings'),this.declarations.set('yRings','yRings'),this.declarations.set('lngCoord','lngCoord'),this.declarations.set('latCoord','latCoord'),this.declarations.set('lngSegment','lngSegment'),this.declarations.set('latSegment','latSegment'),this.declarations.set('lngRings','lngRings'),this.declarations.set('latRings','latRings'),this.debugTopology&&(this.declarations.set('edgeRefs','edgeRefs'),this.declarations.set('edgePairs','edgePairs'),this.declarations.set('arcCoords','arcCoords')),this.declarations.set('arcRefs','arcRefs')}getPropertyType(e){return this.declarations.has(e)?this.declarations.get(e):'string'}isPropertyWanted(e){return!this.propertiesToInclude.includes('none')&&(!!this.propertiesToInclude.includes('all')||!!this.propertiesToInclude.includes(e))}examineHoldingAreaForProperties(e){expect(e,'String');var t={};'Point'==e&&this.gcsFeaturePoints.length>0?t=this.gcsFeaturePoints[0].kvPairs:'Line'==e&&this.gcsFeatureLines.length>0?t=this.gcsFeatureLines[0].kvPairs:'Polygon'==e&&this.gcsFeaturePolygons.length>0?t=this.gcsFeaturePolygons[0].kvPairs:terminal.logic(`Unexpected geometry type ${e} (or no features)`);var i=[];for(let e in t)i.push(e);return i}}