11import { ArcGISPluginConfig } from "./ArcGISPluginConfig" ;
22import { LayerInfo } from "./LayerInfo" ;
33import { QueryObjectResult } from "./QueryObjectResult" ;
4- import { ArcGISIdentityManager , request } from "@esri/arcgis-rest-request" ;
4+ import { ArcGISIdentityManager } from "@esri/arcgis-rest-request" ;
5+ import { queryFeatures } from '@esri/arcgis-rest-feature-service' ;
56
67/**
78 * Performs various queries on observations for a specific arc feature layer.
@@ -52,22 +53,17 @@ export class FeatureQuerier {
5253 * @param geometry query the geometry, default is true
5354 */
5455 async queryObservation ( observationId : string , response : ( result : QueryObjectResult ) => void , fields ?: string [ ] , geometry ?: boolean ) {
55- const queryUrl = new URL ( this . _url )
56- if ( this . _config . eventIdField == null ) {
57- queryUrl . searchParams . set ( 'where' , `${ this . _config . observationIdField } LIKE '${ observationId } ${ this . _config . idSeparator } %'` ) ;
58- } else {
59- queryUrl . searchParams . set ( 'where' , `${ this . _config . observationIdField } = ${ observationId } ` ) ;
60- }
61- queryUrl . searchParams . set ( 'outFields' , this . outFields ( fields ) )
62- queryUrl . searchParams . set ( 'returnGeometry' , geometry === false ? 'false' : 'true' )
63- this . _console . info ( 'ArcGIS query: ' + queryUrl )
64-
65- const queryResponse = await request ( queryUrl . toString ( ) , {
56+ const where = ! this . _config . eventIdField
57+ ? `${ this . _config . observationIdField } LIKE '${ observationId } ${ this . _config . idSeparator } %'`
58+ : `${ this . _config . observationIdField } = '${ observationId } '` ;
59+ this . _console . info ( 'ArcGIS query observation: ' + this . _url . toString ( ) + where ) ;
60+ await queryFeatures ( {
61+ url : this . _url . toString ( ) ,
6662 authentication : this . _identityManager ,
67- params : { f : 'json' }
68- } ) ;
69-
70- response ( queryResponse as QueryObjectResult ) ;
63+ where ,
64+ returnGeometry : geometry ,
65+ outFields : fields ?. length ? fields : '*'
66+ } ) . then ( ( queryResponse ) => response ( queryResponse as QueryObjectResult ) ) . catch ( ( error ) => this . _console . error ( 'Error in FeatureQuerier.queryObservation :: ' + error ) ) ;
7167 }
7268
7369 /**
@@ -77,19 +73,14 @@ export class FeatureQuerier {
7773 * @param geometry query the geometry, default is true
7874 */
7975 async queryObservations ( response : ( result : QueryObjectResult ) => void , fields ?: string [ ] , geometry ?: boolean ) {
80- const queryUrl = new URL ( this . _url )
81- queryUrl . searchParams . set ( 'where' , `${ this . _config . observationIdField } IS NOT NULL` ) ;
82- queryUrl . searchParams . set ( 'outFields' , this . outFields ( fields ) ) ;
83- queryUrl . searchParams . set ( 'returnGeometry' , geometry === false ? 'false' : 'true' ) ;
84-
85- this . _console . info ( 'ArcGIS query: ' + queryUrl )
86-
87- const queryResponse = await request ( queryUrl . toString ( ) , {
76+ this . _console . info ( 'ArcGIS query observation: ' + this . _url . toString ( ) ) ;
77+ await queryFeatures ( {
78+ url : this . _url . toString ( ) ,
8879 authentication : this . _identityManager ,
89- params : { f : 'json' }
90- } ) ;
91-
92- response ( queryResponse as QueryObjectResult ) ;
80+ where : ` ${ this . _config . observationIdField } IS NOT NULL` ,
81+ returnGeometry : geometry ,
82+ outFields : fields ?. length ? fields : '*'
83+ } ) . then ( ( queryResponse ) => response ( queryResponse as QueryObjectResult ) ) . catch ( ( error ) => this . _console . error ( 'Error in FeatureQuerier.queryObservations :: ' + error ) ) ;
9384 }
9485
9586 /**
@@ -98,37 +89,14 @@ export class FeatureQuerier {
9889 * @param field field to query
9990 */
10091 async queryDistinct ( response : ( result : QueryObjectResult ) => void , field : string ) {
101- const queryUrl = new URL ( this . _url ) ;
102- queryUrl . searchParams . set ( 'where' , `${ field } IS NOT NULL` ) ;
103- queryUrl . searchParams . set ( 'returnDistinctValues' , 'true' ) ;
104- queryUrl . searchParams . set ( 'outFields' , this . outFields ( [ field ] ) ) ;
105- queryUrl . searchParams . set ( 'returnGeometry' , 'false' ) ;
106- this . _console . info ( 'ArcGIS query: ' + queryUrl )
107-
108- try {
109- const queryResponse = await request ( queryUrl . toString ( ) , {
110- authentication : this . _identityManager ,
111- params : { f : 'json' }
112-
113- } ) ;
114-
115- response ( queryResponse as QueryObjectResult ) ;
116- } catch ( err ) {
117- console . error ( "could not query" , err )
118- }
119- }
120-
121- /**
122- * Build the out fields query parameter
123- * @param fields query fields
124- * @returns out fields
125- */
126- private outFields ( fields ?: string [ ] ) : string {
127- if ( fields != null && fields . length > 0 ) {
128- return fields . join ( ',' ) ;
129- } else {
130- return '*' ;
131- }
92+ this . _console . info ( 'ArcGIS query observation: ' + this . _url . toString ( ) ) ;
93+ await queryFeatures ( {
94+ url : this . _url . toString ( ) ,
95+ authentication : this . _identityManager ,
96+ where : `${ field } IS NOT NULL` ,
97+ returnGeometry : false ,
98+ outFields : field ? [ field ] : '*' ,
99+ returnDistinctValues : true
100+ } ) . then ( ( queryResponse ) => response ( queryResponse as QueryObjectResult ) ) . catch ( ( error ) => this . _console . error ( 'Error in FeatureQuerier.queryDistinct :: ' + error ) ) ;
132101 }
133-
134102}
0 commit comments