UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 12.1 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import e from"../../request.js";import t from"../../core/Error.js";import{find as r,cache as n}from"../../core/iteratorUtils.js";import{isHTTPSProtocol as a,hasSameOrigin as o,toHTTPS as s}from"../../core/urlUtils.js";import i from"../../geometry/Extent.js";import{initializeProjection as p,project as u}from"../../geometry/projectionUtils.js";import l from"../../geometry/SpatialReference.js";import{equals as c,wgs84 as m}from"../../geometry/support/spatialReferenceUtils.js";import{featureGeometryTypeKebabDictionary as f}from"../../geometry/support/typeUtils.js";import{getGeometryType as y}from"../graphics/sources/geojson/geojson.js";import{visitXML as d,iterateXML as g}from"./xmlUtils.js";import{isArcGISUrl as w}from"../support/arcgisLayerUrl.js";import b from"../support/Field.js";import{getFieldDefaultLength as h}from"../support/fieldUtils.js";const T="xlink:href",S="2.0.0",F="__esri_wfs_id__",x="wfs-layer:getWFSLayerTypeInfo-error",C="wfs-layer:empty-service",E="wfs-layer:feature-type-not-found",R="wfs-layer:geojson-not-supported",k="wfs-layer:kvp-encoding-not-supported",P="wfs-layer:malformed-json",j="wfs-layer:unknown-geometry-type",A="wfs-layer:unknown-field-type",N="wfs-layer:unsupported-spatial-reference",G="wfs-layer:unsupported-wfs-version";async function v(t,r){const n=U((await e(t,{responseType:"text",query:{SERVICE:"WFS",REQUEST:"GetCapabilities",VERSION:S,...r?.customParameters},signal:r?.signal})).data);return D(t,n),n}function U(e){const t=te(e);ne(t),ae(t);const r=t.firstElementChild,a=n(O(r));return{operations:M(r),get featureTypes(){return Array.from(a())},readFeatureTypes:a}}const I=["json","application/json; subtype=geojson; charset=utf-8","application/json; subtype=geojson","application/json","geojson","application/geo+json"];function L(e){for(const t of I){const r=e.findIndex((e=>e.toLowerCase()===t));if(r>=0)return e[r]}return null}function M(e){let r=!0;const n={GetCapabilities:{url:""},DescribeFeatureType:{url:""},GetFeature:{url:"",outputFormat:null,supportsPagination:!1}},a=[],o=[];if(d(e,{OperationsMetadata:{Parameter:e=>{if("outputformat"===e.getAttribute("name")?.toLowerCase())return{AllowedValues:{Value:({textContent:e})=>{e&&a.push(e)}}}},Operation:e=>{switch(e.getAttribute("name")){case"GetCapabilities":return{DCP:{HTTP:{Get:e=>{n.GetCapabilities.url=e.getAttribute(T)}}}};case"DescribeFeatureType":return{DCP:{HTTP:{Get:e=>{n.DescribeFeatureType.url=e.getAttribute(T)}}}};case"GetFeature":return{DCP:{HTTP:{Get:e=>{n.GetFeature.url=e.getAttribute(T)}}},Parameter:e=>{if("outputformat"===e.getAttribute("name")?.toLowerCase())return{AllowedValues:{Value:({textContent:e})=>{e&&o.push(e)}}}}}}},Constraint:e=>{switch(e.getAttribute("name")){case"KVPEncoding":return{DefaultValue:e=>{r="true"===e.textContent.toLowerCase()}};case"ImplementsResultPaging":return{DefaultValue:e=>{n.GetFeature.supportsPagination="true"===e.textContent.toLowerCase()}}}}}}),n.GetFeature.outputFormat=L(o)??L(a),!r)throw new t(k,"WFS service doesn't support key/value pair (KVP) encoding");if(null==n.GetFeature.outputFormat)throw new t(R,"WFS service doesn't support GeoJSON output format");return n}function D(e,t){a(e)&&(o(e,t.operations.DescribeFeatureType.url,!0)&&(t.operations.DescribeFeatureType.url=s(t.operations.DescribeFeatureType.url)),o(e,t.operations.GetFeature.url,!0)&&(t.operations.GetFeature.url=s(t.operations.GetFeature.url)))}function V(e){const t=parseInt(e.textContent?.match(/(?<wkid>\d+$)/i)?.groups?.wkid??"",10);if(!Number.isNaN(t))return t}function O(e){return g(e,{FeatureTypeList:{FeatureType:e=>{const t={typeName:"undefined:undefined",name:"",title:"",description:"",extent:null,namespacePrefix:"",namespaceUri:"",defaultSpatialReference:4326,supportedSpatialReferences:[]},r=new Set;return d(e,{Name:e=>{const{name:r,prefix:n}=re(e.textContent);t.typeName=`${n}:${r}`,t.name=r,t.namespacePrefix=n,t.namespaceUri=e.lookupNamespaceURI(n)},Abstract:e=>{t.description=e.textContent},Title:e=>{t.title=e.textContent},WGS84BoundingBox:e=>{t.extent=i.fromJSON($(e))},DefaultCRS:e=>{const n=V(e);n&&(t.defaultSpatialReference=n,r.add(n))},OtherCRS:e=>{const t=V(e);t&&r.add(t)}}),t.title||(t.title=t.name),r.add(4326),t.supportedSpatialReferences.push(...r),t}}})}function $(e){let t,r,n,a;for(const o of e.children)switch(o.localName){case"LowerCorner":[t,r]=o.textContent.split(" ").map((e=>Number.parseFloat(e)));break;case"UpperCorner":[n,a]=o.textContent.split(" ").map((e=>Number.parseFloat(e)))}return{xmin:t,ymin:r,xmax:n,ymax:a,spatialReference:m}}function Y(e,t,n){return r(e,(e=>n?e.name===t&&e.namespaceUri===n:e.typeName===t||e.name===t))}async function W(e,t,r,n={}){const{featureType:a,extent:o}=await X(e,t,r,n),{spatialReference:s}=oe(e.operations.GetFeature.url,a,n.spatialReference),{fields:i,geometryType:p,swapXY:u,objectIdField:c,geometryField:m}=await q(e,a,s,n);return{url:e.operations.GetCapabilities.url,name:a.name,namespaceUri:a.namespaceUri,fields:i,geometryField:m,geometryType:p,objectIdField:c,spatialReference:n.spatialReference??new l({wkid:a.defaultSpatialReference}),extent:o,swapXY:u,wfsCapabilities:e,customParameters:n.customParameters}}async function X(e,r,n,a={}){const o=e.readFeatureTypes(),s=r?Y(o,r,n):o.next().value,{spatialReference:i=new l({wkid:s?.defaultSpatialReference})}=a;if(null==s)throw r?new t(E,`The type '${r}' could not be found in the service`):new t(C,"The service is empty");let m=s.extent;if(m&&!c(m.spatialReference,i))try{await p(m.spatialReference,i,void 0,a),m=u(m,i)}catch{throw new t(N,"Projection not supported")}return{extent:m,spatialReference:i,featureType:s}}async function q(e,r,n,a={}){const{typeName:o}=r,[s,i]=await Promise.allSettled([J(e.operations.DescribeFeatureType.url,o,a),_(e,o,n,a)]),p=e=>new t(x,`An error occurred while getting info about the feature type '${o}'`,{error:e});if("rejected"===s.status)throw p(s.reason);if("rejected"===i.status)throw p(i.reason);const{fields:u,errors:l}=s.value??{},c=s.value?.geometryType||i.value?.geometryType,m=i.value?.swapXY??!1;if(null==c)throw new t(j,`The geometry type could not be determined for type '${o}`,{typeName:o,geometryType:c,fields:u,errors:l});return{...z(u??[]),geometryType:c,swapXY:m}}function z(e){const t=e.find((e=>"geometry"===e.type));let r=e.find((e=>"oid"===e.type));return e=e.filter((e=>"geometry"!==e.type)),r||(r=new b({name:F,type:"oid",alias:F}),e.unshift(r)),{geometryField:t?.name??null,objectIdField:r.name,fields:e}}async function _(t,r,n,a={}){let o,s=!1;const[i,p]=await Promise.all([K(t.operations.GetFeature.url,r,n,t.operations.GetFeature.outputFormat,{...a,count:1}),e(t.operations.GetFeature.url,{responseType:"text",query:Z(r,n,void 0,{...a,count:1}),signal:a?.signal})]),u="FeatureCollection"===i.type&&i.features[0]?.geometry;if(u){let e;switch(o=f.fromJSON(y(u.type)),u.type){case"Point":e=u.coordinates;break;case"LineString":case"MultiPoint":e=u.coordinates[0];break;case"MultiLineString":case"Polygon":e=u.coordinates[0][0];break;case"MultiPolygon":e=u.coordinates[0][0][0]}const t=/<[^>]*pos[^>]*> *(-?\d+(?:\.\d+)?) (-?\d+(?:\.\d+)?)/.exec(p.data);if(t){const r=e[0].toFixed(3),n=e[1].toFixed(3),a=parseFloat(t[1]).toFixed(3);r===parseFloat(t[2]).toFixed(3)&&n===a&&(s=!0)}}return{geometryType:o,swapXY:s}}async function J(t,r,n){return Q(r,(await e(t,{responseType:"text",query:{SERVICE:"WFS",REQUEST:"DescribeFeatureType",VERSION:S,TYPENAME:r,TYPENAMES:r,...n?.customParameters},signal:n?.signal})).data)}function Q(e,n){const{name:a}=re(e),o=te(n);ae(o);const s=r(g(o.firstElementChild,{element:e=>e}),(e=>e.getAttribute("name")===a));if(null!=s){const e=s.getAttribute("type"),t=e?r(g(o.firstElementChild,{complexType:e=>e}),(t=>t.getAttribute("name")===re(e).name)):r(g(s,{complexType:e=>e}),(()=>!0));if(t)return B(t)}throw new t(E,`Type '${e}' not found in document`,{document:(new XMLSerializer).serializeToString(o)})}const H=new Set(["objectid","fid"]);function B(e){const r=[],n=[];let a;const o=g(e,{complexContent:{extension:{sequence:{element:e=>e}}}});for(const s of o){const o=s.getAttribute("name");if(!o)continue;let i,p;if(s.hasAttribute("type")?i=re(s.getAttribute("type")).name:d(s,{simpleType:{restriction:e=>(i=re(e.getAttribute("base")).name,{maxLength:e=>{p=+e.getAttribute("value")}})}}),!i)continue;const u="true"===s.getAttribute("nillable");let l=!1;switch(i.toLowerCase()){case"integer":case"nonpositiveinteger":case"negativeinteger":case"long":case"int":case"short":case"byte":case"nonnegativeinteger":case"unsignedlong":case"unsignedint":case"unsignedshort":case"unsignedbyte":case"positiveinteger":n.push(new b({name:o,alias:o,type:"integer",nullable:u,length:h("integer")}));break;case"float":case"double":case"decimal":n.push(new b({name:o,alias:o,type:"double",nullable:u,length:h("double")}));break;case"boolean":case"string":case"gyearmonth":case"gyear":case"gmonthday":case"gday":case"gmonth":case"anyuri":case"qname":case"notation":case"normalizedstring":case"token":case"language":case"idrefs":case"entities":case"nmtoken":case"nmtokens":case"name":case"ncname":case"id":case"idref":case"entity":case"duration":case"time":n.push(new b({name:o,alias:o,type:"string",nullable:u,length:p??h("string")}));break;case"datetime":case"date":n.push(new b({name:o,alias:o,type:"date",nullable:u,length:p??h("date")}));break;case"pointpropertytype":a="point",l=!0;break;case"multipointpropertytype":a="multipoint",l=!0;break;case"curvepropertytype":case"multicurvepropertytype":case"multilinestringpropertytype":a="polyline",l=!0;break;case"surfacepropertytype":case"multisurfacepropertytype":case"multipolygonpropertytype":a="polygon",l=!0;break;case"geometrypropertytype":case"multigeometrypropertytype":l=!0,r.push(new t(j,`geometry type '${i}' is not supported`,{type:(new XMLSerializer).serializeToString(e)}));break;default:r.push(new t(A,`Unknown field type '${i}'`,{type:(new XMLSerializer).serializeToString(e)}))}l&&n.push(new b({name:o,alias:o,type:"geometry",nullable:u}))}for(const t of n)if("integer"===t.type&&!t.nullable&&H.has(t.name.toLowerCase())){t.type="oid";break}return{geometryType:a,fields:n,errors:r}}async function K(r,n,a,o,s){let{data:i}=await e(r,{responseType:"text",query:Z(n,a,o,s),signal:s?.signal});i=i.replaceAll(/": +(-?\d+),(\d+)(,)?/g,'": $1.$2$3');try{return JSON.parse(i)}catch(p){throw new t(P,"Error while parsing the response",{response:i,error:p})}}function Z(e,t,r,n){const a="number"==typeof t?t:t.wkid;return{SERVICE:"WFS",REQUEST:"GetFeature",VERSION:S,TYPENAMES:e,OUTPUTFORMAT:r,SRSNAME:"EPSG:"+a,STARTINDEX:n?.startIndex,COUNT:n?.count,...n?.customParameters}}async function ee(t,r,n){const a=await e(t,{responseType:"text",query:{SERVICE:"WFS",REQUEST:"GetFeature",VERSION:S,TYPENAMES:r,RESULTTYPE:"hits",...n?.customParameters},signal:n?.signal}),o=/numberMatched=["'](?<numberMatched>\d+)["']/gi.exec(a.data);if(o?.groups)return+o.groups.numberMatched}function te(e){return(new DOMParser).parseFromString(e.trim(),"text/xml")}function re(e){const[t,r]=e.split(":");return{prefix:r?t:"",name:r??t}}function ne(e){const r=e.firstElementChild?.getAttribute("version");if(r&&r!==S)throw new t(G,`Unsupported WFS version ${r}. Supported version: ${S}`)}function ae(e){let r="",n="";if(d(e.firstElementChild,{Exception:e=>(r=e.getAttribute("exceptionCode"),{ExceptionText:e=>{n=e.textContent}})}),r)throw new t(`wfs-layer:${r}`,n)}function oe(e,t,r){const n={wkid:t.defaultSpatialReference},a=null!=r?.wkid?{wkid:r.wkid}:n;return{spatialReference:a,getFeatureSpatialReference:w(e)||a.wkid&&t.supportedSpatialReferences.includes(a.wkid)?{wkid:a.wkid}:{wkid:t.defaultSpatialReference}}}export{J as describeFeatureType,Y as findFeatureType,v as getCapabilities,K as getFeature,ee as getFeatureCount,X as getFeatureTypeInfo,oe as getGetFeatureSpatialReference,W as getWFSLayerInfo,Q as parseDescribeFeatureTypeResponse,U as parseGetCapabilitiesResponse,z as prepareWFSLayerFields,F as wfsOidFieldName};