attribute.js

  1. 'use strict';
  2. /*
  3. Structure and naming:
  4. this is a layer Package. it contains information about a single server-side layer.
  5. note this is not always 1-to-1 with client side. a client side DynamicLayer can have
  6. many server-side sublayers, each with their own attribute sets
  7. DO NOT access the ._attribData property directly, as it will not exist until the first
  8. request for attributes. use the function .getAttribs(), as it will properly handle the
  9. initial request, or return the previously loaded result (always as a promise)
  10. {
  11. "layerId": "<layerid>",
  12. "featureIdx": 3,
  13. "getAttribs": getAttribs(),
  14. "_attribData": Promise(
  15. <instance of a attribute data object, see below>
  16. ),
  17. "layerData": Promise(
  18. <instance of a layer data object, see below>
  19. )
  20. }
  21. this is an attribute data object. it resides in a promise (as the data needs to be downloaded)
  22. it contains the attribute data as an array, and an index mapping object id to array position
  23. {
  24. "features": [
  25. {
  26. "attributes": {
  27. "objectid": 23,
  28. "name": "Bruce",
  29. "age": 27
  30. }
  31. },
  32. ...
  33. ],
  34. "oidIndex": {
  35. "23": 0,
  36. ...
  37. }
  38. }
  39. this is a layer data object. it contains information describing the server-side layer
  40. {
  41. "fields: [
  42. {
  43. "name": "objectid",
  44. "type": "esriFieldTypeOID",
  45. "alias": "OBJECTID"
  46. },
  47. ...
  48. ],
  49. "oidField": "objectid",
  50. "renderer": {...},
  51. "geometryType": "esriGeometryPoint",
  52. "layerType": "Feature Layer",
  53. "minScale": 0,
  54. "maxScale": 0,
  55. "extent": {...}
  56. }
  57. */
  58. /**
  59. * Will generate an empty object structure to store attributes for a single layer of features
  60. * @private
  61. * @param {String} featureIdx server index of the layer
  62. * @param {Object} esriBundle bundle of API classes
  63. * @return {Object} empty layer package object
  64. */
  65. function newLayerPackage(featureIdx, esriBundle) {
  66. // only reason this is in a function is to tack on the lazy-load
  67. // attribute function. all object properties are added elsewhere
  68. const layerPackage = {
  69. featureIdx,
  70. getAttribs
  71. };
  72. /**
  73. * Return promise of attribute data object. First request triggers load
  74. * @private
  75. * @return {Promise} promise of attribute data object
  76. */
  77. function getAttribs() {
  78. if (layerPackage._attribData) {
  79. // attributes have already been downloaded.
  80. return layerPackage._attribData;
  81. }
  82. // first request for data. create the promise
  83. layerPackage._attribData = new Promise((resolve, reject) => {
  84. // first wait for the layer specific data to finish loading
  85. // NOTE: by the time the application has access to getAttribs(), the .layerData
  86. // property will have been created.
  87. layerPackage.layerData.then(layerData => {
  88. // FIXME switch to native Promise
  89. const defFinished = new esriBundle.Deferred();
  90. const params = {
  91. maxId: -1,
  92. batchSize: -1,
  93. layerUrl: layerData.load.layerUrl,
  94. oidField: layerData.oidField,
  95. attribs: layerData.load.attribs,
  96. supportsLimit: layerData.load.supportsLimit,
  97. esriBundle
  98. };
  99. // begin the loading process
  100. loadDataBatch(params, defFinished);
  101. // after all data has been loaded
  102. defFinished.promise.then(features => {
  103. delete layerData.load; // no longer need this info
  104. // resolve the promise with the attribute set
  105. resolve(createAttribSet(layerData.oidField, features));
  106. }, error => {
  107. console.warn('error getting attribute data for ' + layerData.load.layerUrl);
  108. // attrib data deleted so the first check for attribData doesn't return a rejected promise
  109. delete layerPackage._attribData;
  110. reject(error);
  111. });
  112. });
  113. });
  114. return layerPackage._attribData;
  115. }
  116. return layerPackage;
  117. }
  118. /**
  119. * Will generate attribute package with object id indexes
  120. * @private
  121. * @param {String} oidField field containing object id
  122. * @param {Array} featureData feature objects to index and return
  123. * @return {Object} object containing features and an index by object id
  124. */
  125. function createAttribSet(oidField, featureData) {
  126. // add new data to layer data's array
  127. const res = {
  128. features: featureData,
  129. oidIndex: {}
  130. };
  131. // make index on object id
  132. featureData.forEach((elem, idx) => {
  133. // map object id to index of object in feature array
  134. // use toString, as objectid is integer and will act funny using array notation.
  135. res.oidIndex[elem.attributes[oidField].toString()] = idx;
  136. });
  137. return res;
  138. }
  139. // skim the last number off the Url
  140. // TODO apply more edge case tests to this function
  141. function getLayerIndex(layerUrl) {
  142. const re = /\/(\d+)\/?$/;
  143. const matches = layerUrl.match(re);
  144. if (matches) {
  145. return parseInt(matches[1]);
  146. }
  147. throw new Error('Cannot extract layer index from url ' + layerUrl);
  148. }
  149. /**
  150. * Recursive function to load a full set of attributes, regardless of the maximum output size of the service.
  151. * Passes result back on the provided Deferred object.
  152. *
  153. * @private
  154. * @param {Object} opts options object that consists of these properties
  155. * - maxId: integer, largest object id that has already been downloaded.
  156. * - supportsLimit: boolean, indicates if server result will notify us if our request surpassed the record limit.
  157. * - batchSize: integer, maximum number of results the service will return. if -1, means currently unknown. only required if supportsLimit is false.
  158. * - layerUrl: string, URL to feature layer endpoint.
  159. * - oidField: string, name of attribute containing the object id for the layer.
  160. * - attribs: string, a comma separated list of attributes to download. '*' will download all.
  161. * - esriBundle: object, standard set of ESRI API objects.
  162. * @param {Object} callerDef deferred object that resolves when current data has been downloaded
  163. */
  164. function loadDataBatch(opts, callerDef) {
  165. // fetch attributes from feature layer. where specifies records with id's higher than stuff already
  166. // downloaded. no geometry.
  167. // FIXME replace esriRequest with a library that handles proxies better
  168. const defData = opts.esriBundle.esriRequest({
  169. url: opts.layerUrl + '/query',
  170. content: {
  171. where: opts.oidField + '>' + opts.maxId,
  172. outFields: opts.attribs,
  173. returnGeometry: 'false',
  174. f: 'json',
  175. },
  176. callbackParamName: 'callback',
  177. handleAs: 'json'
  178. });
  179. defData.then(dataResult => {
  180. if (dataResult.features) {
  181. const len = dataResult.features.length;
  182. if (len > 0) {
  183. // figure out if we hit the end of the data. different logic for newer vs older servers.
  184. let moreData;
  185. if (opts.supportsLimit) {
  186. moreData = dataResult.exceededTransferLimit;
  187. } else {
  188. if (opts.batchSize === -1) {
  189. // this is our first batch. set the max batch size to this batch size
  190. opts.batchSize = len;
  191. }
  192. moreData = (len >= opts.batchSize);
  193. }
  194. if (moreData) {
  195. // stash the result and call the service again for the next batch of data.
  196. // max id becomes last object id in the current batch
  197. const thisDef = new opts.esriBundle.Deferred();
  198. opts.maxId = dataResult.features[len - 1].attributes[opts.oidField];
  199. loadDataBatch(opts, thisDef);
  200. thisDef.then(dataArray => {
  201. // chain the next result to our current result, then pass back to caller
  202. callerDef.resolve(dataResult.features.concat(dataArray));
  203. },
  204. error => {
  205. callerDef.reject(error);
  206. });
  207. } else {
  208. // done thanks
  209. callerDef.resolve(dataResult.features);
  210. }
  211. } else {
  212. // no more data. we are done
  213. callerDef.resolve([]);
  214. }
  215. } else {
  216. // it is possible to have an error, but it comes back on the "success" channel.
  217. callerDef.reject(dataResult.error);
  218. }
  219. },
  220. error => {
  221. callerDef.reject(error);
  222. });
  223. }
  224. function loadServerAttribsBuilder(esriBundle, geoApi) {
  225. /**
  226. * fetch attributes from an ESRI ArcGIS Server Feature Layer Service endpoint
  227. * @param {String} mapServiceUrl an arcgis map server service endpoint (no integer index)
  228. * @param {String} featureIdx index of where the endpoint is.
  229. * @param {String} attribs an optional comma separated list of attributes to download. default '*' will download all
  230. * @return {Object} attributes in a packaged format for asynch access
  231. */
  232. return (mapServiceUrl, featureIdx, attribs = '*') => {
  233. const layerUrl = mapServiceUrl + '/' + featureIdx;
  234. const layerPackage = newLayerPackage(featureIdx, esriBundle);
  235. // get information about this layer, asynch
  236. layerPackage.layerData = new Promise((resolve, reject) => {
  237. const layerData = {};
  238. // extract info for this service
  239. const defService = esriBundle.esriRequest({
  240. url: layerUrl,
  241. content: { f: 'json' },
  242. callbackParamName: 'callback',
  243. handleAs: 'json',
  244. });
  245. defService.then(serviceResult => {
  246. if (serviceResult && (typeof serviceResult.error === 'undefined')) {
  247. // properties for all endpoints
  248. layerData.layerType = serviceResult.type;
  249. layerData.geometryType = serviceResult.geometryType || 'none'; // TODO need to decide what propert default is. Raster Layer has null gt.
  250. layerData.minScale = serviceResult.minScale;
  251. layerData.maxScale = serviceResult.maxScale;
  252. layerData.supportsFeatures = false; // saves us from having to keep comparing type to 'Feature Layer' on the client
  253. layerData.extent = serviceResult.extent;
  254. if (serviceResult.type === 'Feature Layer') {
  255. layerData.supportsFeatures = true;
  256. layerData.fields = serviceResult.fields;
  257. layerData.nameField = serviceResult.displayField;
  258. // find object id field
  259. // NOTE cannot use arrow functions here due to bug
  260. serviceResult.fields.every(function (elem) {
  261. if (elem.type === 'esriFieldTypeOID') {
  262. layerData.oidField = elem.name;
  263. return false; // break the loop
  264. }
  265. return true; // keep looping
  266. });
  267. // ensure our attribute list contains the object id
  268. if (attribs !== '*') {
  269. if (attribs.split(',').indexOf(layerData.oidField) === -1) {
  270. attribs += (',' + layerData.oidField);
  271. }
  272. }
  273. // add renderer and legend
  274. layerData.renderer = geoApi.symbology.cleanRenderer(serviceResult.drawingInfo.renderer,
  275. serviceResult.fields);
  276. layerData.legend = geoApi.symbology.rendererToLegend(layerData.renderer, featureIdx);
  277. geoApi.symbology.enhanceRenderer(layerData.renderer, layerData.legend);
  278. // temporarily store things for delayed attributes
  279. layerData.load = {
  280. // version number is only provided on 10.0 SP1 servers and up.
  281. // servers 10.1 and higher support the query limit flag
  282. supportsLimit: (serviceResult.currentVersion || 1) >= 10.1,
  283. layerUrl,
  284. attribs
  285. };
  286. }
  287. // return the layer data promise result
  288. resolve(layerData);
  289. } else {
  290. // case where error happened but service request was successful
  291. console.warn('Service metadata load error');
  292. if (serviceResult && serviceResult.error) {
  293. // reject with error
  294. reject(serviceResult.error);
  295. } else {
  296. reject(new Error('Unknown error loading service metadata'));
  297. }
  298. }
  299. }, error => {
  300. // failed to load service info. reject with error
  301. console.warn('Service metadata load error : ' + error);
  302. reject(error);
  303. });
  304. });
  305. return layerPackage;
  306. };
  307. }
  308. function loadFileAttribsBuilder(esriBundle, geoApi) {
  309. return layer => {
  310. // feature layer was loaded from a file.
  311. // this approach is inefficient (duplicates attributes in layer and in attribute store),
  312. // but provides a consistent approach to attributes regardless of where the layer came from
  313. const layerPackage = newLayerPackage('0', esriBundle); // files have no index (no server), so we use value 0
  314. // it's local, no need to lazy-load
  315. layerPackage._attribData = Promise.resolve(createAttribSet(layer.objectIdField, layer.graphics.map(elem => {
  316. return { attributes: elem.attributes };
  317. })));
  318. const renderer = layer.renderer.toJson();
  319. const legend = geoApi.symbology.rendererToLegend(renderer, 0);
  320. geoApi.symbology.enhanceRenderer(renderer, legend);
  321. // TODO revisit the geometry type. ideally, fix our GeoJSON to Feature to populate the property
  322. layerPackage.layerData = Promise.resolve({
  323. oidField: layer.objectIdField,
  324. fields: layer.fields,
  325. geometryType: layer.geometryType || JSON.parse(layer._json).layerDefinition.drawingInfo.geometryType,
  326. minScale: layer.minScale,
  327. maxScale: layer.maxScale,
  328. layerType: 'Feature Layer',
  329. renderer,
  330. legend
  331. });
  332. return layerPackage;
  333. };
  334. }
  335. // Attribute Loader related functions
  336. // TODO consider re-writing all the asynch stuff with the ECMA-7 style of asynch keywords
  337. module.exports = (esriBundle, geoApi) => {
  338. return {
  339. loadServerAttribs: loadServerAttribsBuilder(esriBundle, geoApi),
  340. loadFileAttribs: loadFileAttribsBuilder(esriBundle, geoApi),
  341. getLayerIndex
  342. };
  343. };