UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 10.3 kB
import e from"../../../../../core/Error.js";import t from"../../../../../core/Logger.js";import{isNone as r,isSome as s,assertIsSome as i}from"../../../../../core/maybe.js";import n from"../../../../../core/pbf.js";import{convertToGeometry as a,unquantizeOptimizedGeometry as h}from"../../../../../layers/graphics/featureConversionUtils.js";import{OptimizedFeature as o}from"../../../../../layers/graphics/OptimizedFeature.js";import d from"../../../../../layers/graphics/OptimizedGeometry.js";import{FeatureSetReader as u}from"./FeatureSetReader.js";import{parseHeader as c}from"./FeatureSetReaderPBFHeader.js";const g=!0,l=268435455,_=128,f=128e3,y={small:{delta:new Int32Array(_),decoded:new Int32Array(_)},large:{delta:new Int32Array(f),decoded:new Int32Array(f)}};function I(e){return e<=y.small.delta.length?y.small:(e<=y.large.delta.length||(y.large.delta=new Int32Array(Math.round(1.25*e)),y.large.decoded=new Int32Array(Math.round(1.25*e))),y.large)}function p(e){return e.toLowerCase().trim()}function m(r){try{const e=2,t=new n(new Uint8Array(r),new DataView(r));for(;t.next();){if(t.tag()===e)return x(t.getMessage());t.skip()}}catch(s){const r=new e("query:parsing-pbf","Error while parsing FeatureSet PBF payload",{error:s});t.getLogger("esri.view.2d.layers.features.support.FeatureSetReaderPBF").error(r)}return null}function x(e){const t=1;for(;e.next();){if(e.tag()===t)return e.getMessage();e.skip()}return null}function S(e){const t=1,r=2,s=3,i=4,n=5,a=6,h=7,o=8,d=9,u=e.getLength(),c=e.pos()+u;for(;e.pos()<c&&e.next();)switch(e.tag()){case t:return e.getString();case r:return e.getFloat();case s:return e.getDouble();case i:return e.getSInt32();case n:return e.getUInt32();case a:return e.getInt64();case h:return e.getUInt64();case o:return e.getSInt64();case d:return e.getBool();default:return e.skip(),null}return null}function F(e,t,r,s,i,n){return.5*Math.abs(e*s+r*n+i*t-e*n-r*t-i*s)}function v(e,t,r,s){return 0===e*s-r*t&&e*r+t*s>0}class G extends u{static fromBuffer(e,t,r=!1){const s=t.geometryType,i=m(e),n=c(i,"esriGeometryPoint"===s,r),a=u.createInstance();return new G(a,i,n,t)}constructor(e,t,r,s){super(e,s),this._hasNext=!1,this._isPoints=!1,this._featureIndex=-1,this._featureOffset=0,this._cache={area:0,unquantGeometry:void 0,geometry:void 0,centroid:void 0,legacyFeature:void 0,optFeature:void 0},this._geometryType=s.geometryType,this._reader=t,this._header=r,this._hasNext=r.hasFeatures,this._isPoints="esriGeometryPoint"===s.geometryType}get geometryType(){return this._geometryType}get _size(){return this._header.featureCount}get hasZ(){return!1}get hasM(){return!1}get stride(){return 2+(this.hasZ?1:0)+(this.hasM?1:0)}get hasFeatures(){return this._header.hasFeatures}get hasNext(){return this._hasNext}get exceededTransferLimit(){return this._header.exceededTransferLimit}hasField(e){return this._header.hasField(e)||this._header.hasField(p(e))}getFieldNames(){return this._header.fields.map((e=>e.fieldName))}getSize(){return this._size}getQuantizationTransform(){return this._header.transform}getCursor(){return this.copy()}getIndex(){return this._featureIndex}setIndex(e){this._cache.area=0,this._cache.unquantGeometry=void 0,this._cache.geometry=void 0,this._cache.centroid=void 0,this._cache.legacyFeature=void 0,this._cache.optFeature=void 0,this._featureIndex=e}getAttributeHash(){let e="";return this._header.fields.forEach((({index:t})=>{e+=this._readAttributeAtIndex(t)+"."})),e}getObjectId(){return this._readAttributeAtIndex(this._header.objectIdFieldIndex)}getDisplayId(){return this._header.displayIds[this._featureIndex]}setDisplayId(e){this._header.displayIds[this._featureIndex]=e}getGroupId(){return this._header.groupIds[this._featureIndex]}setGroupId(e){this._header.groupIds[this._featureIndex]=e}readLegacyFeature(){if(void 0===this._cache.legacyFeature){const e=this.readCentroid(),t={attributes:this.readAttributes(),geometry:this._isPoints?this.readLegacyPointGeometry():this.readLegacyGeometry(),centroid:(e&&{x:e.coords[0],y:e.coords[1]})??null};return this._cache.legacyFeature=t,t}return this._cache.legacyFeature}readOptimizedFeature(){if(void 0===this._cache.optFeature){const e=new o(this.readGeometry(),this.readAttributes(),this.readCentroid());return e.objectId=this.getObjectId(),e.displayId=this.getDisplayId(),this._cache.optFeature=e,e}return this._cache.optFeature}getXHydrated(){const e=this._header.centroid[2*this._featureIndex],t=this.getQuantizationTransform();return r(t)?e:e*t.scale[0]+t.translate[0]}getYHydrated(){const e=this._header.centroid[2*this._featureIndex+1],t=this.getQuantizationTransform();return r(t)?e:t.translate[1]-e*t.scale[1]}getX(){return this._header.centroid[2*this._featureIndex]*this._sx+this._tx}getY(){return this._header.centroid[2*this._featureIndex+1]*this._sy+this._ty}readLegacyPointGeometry(){return{x:this.getX(),y:this.getY()}}readLegacyGeometry(e){const t=this.readGeometry(e);return a(t,this.geometryType,!1,!1)}readLegacyCentroid(){const e=this.readCentroid();if(!e)return null;const[t,r]=e.coords;return{x:t,y:r}}readGeometryArea(){return this._cache.area||this.readGeometry(!0),this._cache.area}readUnquantizedGeometry(e=!1){if(void 0===this._cache.unquantGeometry){const t=this.readGeometry(e);if(!t)return this._cache.unquantGeometry=void 0,null;const r=I(t.coords.length).decoded,s=t.clone(r),i=s.coords;let n=0;for(const e of s.lengths){for(let t=1;t<e;t++){const e=2*(n+t),r=2*(n+t-1);i[e]+=i[r],i[e+1]+=i[r+1]}n+=e}return this._cache.unquantGeometry=s,s}return this._cache.unquantGeometry}readHydratedGeometry(){if(this._isPoints){if(this._header.centroid[2*this._featureIndex]===l)return null;const e=this.getXHydrated(),t=this.getYHydrated();return new d([],[e,t])}const e=this.readGeometry();if(!e)return null;const t=e.clone(),r=this.getQuantizationTransform();return s(r)&&h(t,t,this.hasZ,this.hasM,r),t}readGeometry(e=!1){if(void 0===this._cache.geometry){let r=null;if(this._isPoints){if(this._header.centroid[2*this._featureIndex]===l)return null;const e=this.getX(),t=this.getY();r=new d([],[e,t])}else{const s=this._header.offsets.geometry[this._featureIndex],i=this._reader;if(0===s){const e=this._readServerCentroid();if(!e)return null;const[t,r]=e.coords;return this.createQuantizedExtrudedQuad(t,r)}i.move(s);try{if(r=e?this._parseGeometryForDisplay(i):this._parseGeometry(i),null===r){const e=this._readServerCentroid();if(!e)return null;const[t,r]=e.coords;return this.createQuantizedExtrudedQuad(t,r)}}catch(t){return console.error("Failed to parse geometry!",t),null}}return this._cache.geometry=r,r}return this._cache.geometry}readCentroid(){if(void 0===this._cache.centroid){let e;return e=this._computeCentroid(),e||(e=this._readServerCentroid()),this._cache.centroid=e??void 0,e??null}return this._cache.centroid}copy(){const e=this._reader.clone(),t=new G(this.instance,e,this._header,this.fullSchema());return this.copyInto(t),t}next(){for(this._cache.area=0,this._cache.unquantGeometry=void 0,this._cache.geometry=void 0,this._cache.centroid=void 0,this._cache.legacyFeature=void 0,this._cache.optFeature=void 0;++this._featureIndex<this._size&&!this._getExists(););return this._featureIndex<this._size}_readAttribute(e,t){const r=this._header.hasField(e)?e:p(e),s=this._header.getFieldIndex(r);if(null==s)return;const i=this._readAttributeAtIndex(s);if(!t)return i;if(null==i)return i;return this._header.isDateField(r)?new Date(i):i}_readAttributes(){const e={};return this._header.fields.forEach((({fieldName:t,index:r})=>{e[t]=this._readAttributeAtIndex(r)})),e}copyInto(e){super.copyInto(e),e._featureIndex=this._featureIndex,e._featureOffset=this._featureOffset,e._hasNext=this._hasNext}_readAttributeAtIndex(e){const t=this._header.offsets.attributes[this._featureIndex*this._header.fieldCount+e],r=this._reader;return r.move(t),S(r)}_readServerCentroid(){const e=this._header.centroid[2*this._featureIndex]+this._tx,t=this._header.centroid[2*this._featureIndex+1]+this._ty;return e===l?null:new d([],[e,t])}_parseGeometry(e){const t=2,r=3,s=e.asUnsafe(),i=s.getLength(),n=s.pos()+i,a=[],h=[];for(;s.pos()<n&&s.next();)switch(s.tag()){case t:{const e=s.getUInt32(),t=s.pos()+e;for(;s.pos()<t;)h.push(s.getUInt32());break}case r:{const e=s.getUInt32(),t=s.pos()+e;for(a.push(s.getSInt32()+this._tx),a.push(s.getSInt32()+this._ty),this.hasZ&&s.getSInt32(),this.hasM&&s.getSInt32();s.pos()<t;)a.push(s.getSInt32()),a.push(s.getSInt32()),this.hasZ&&s.getSInt32(),this.hasM&&s.getSInt32();break}default:s.skip()}return new d(h,a)}_parseGeometryForDisplay(e){const t=2,r=3,s=e.asUnsafe(),n=s.getLength(),a=s.pos()+n,h=[],o=[];let u=0,c=0,l=null,_=0;const f="esriGeometryPolygon"===this.geometryType;for(;s.pos()<a&&s.next();)switch(s.tag()){case t:{const e=s.getUInt32(),t=s.pos()+e;for(;s.pos()<t;){const e=s.getUInt32();h.push(e),u+=e}l=I(2*u).delta;break}case r:{s.getUInt32();const e=2+(this.hasZ?1:0)+(this.hasM?1:0);i(l);for(const t of h)if(c+e*t>l.length)for(let e=0;e<t;e++)s.getSInt32(),s.getSInt32(),this.hasZ&&s.getSInt32(),this.hasM&&s.getSInt32();else if(f&&g){const e=this.getAreaSimplificationThreshold(t,this._header.vertexCount);let r=2,i=1;const n=!1;let a=s.getSInt32(),h=s.getSInt32();l[c++]=a,l[c++]=h,this.hasZ&&s.getSInt32(),this.hasM&&s.getSInt32();let d=s.getSInt32(),u=s.getSInt32();for(this.hasZ&&s.getSInt32(),this.hasM&&s.getSInt32();r<t;){let t=s.getSInt32(),n=s.getSInt32();this.hasZ&&s.getSInt32(),this.hasM&&s.getSInt32();const o=a+d,g=h+u;F(a,h,o,g,o+t,g+n)>=e?(_+=-.5*(o-a)*(g+h),i>1&&v(l[c-2],l[c-1],d,u)?(l[c-2]+=d,l[c-1]+=u):(l[c++]=d,l[c++]=u,i++),a=o,h=g):(t+=d,n+=u),d=t,u=n,r++}i<3||n?c-=2*i:(_+=-.5*(a+d-a)*(h+u+h),v(l[c-2],l[c-1],d,u)?(l[c-2]+=d,l[c-1]+=u,o.push(i)):(l[c++]=d,l[c++]=u,o.push(++i)))}else{let e=0,r=s.getSInt32(),i=s.getSInt32();this.hasZ&&s.getSInt32(),this.hasM&&s.getSInt32(),l[c++]=r,l[c++]=i,e+=1;for(let n=1;n<t;n++){const t=s.getSInt32(),a=s.getSInt32(),h=r+t,o=i+a;_+=-.5*(h-r)*(o+i),this.hasZ&&s.getSInt32(),this.hasM&&s.getSInt32(),n>2&&v(l[c-2],l[c-1],t,a)?(l[c-2]+=t,l[c-1]+=a):(l[c++]=t,l[c++]=a,e+=1),r=h,i=o}o.push(e)}break}default:s.skip()}if(this._cache.area=_,!o.length)return null;if(this._tx||this._ty){let e=0;i(l);for(const t of o)l[2*e]+=this._tx,l[2*e+1]+=this._ty,e+=t}return new d(o,l)}}export{G as FeatureSetReaderPBF};