-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from esri-es/develop
Develop
- Loading branch information
Showing
3 changed files
with
121 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
This is where you can customize the transformation of your data from websockets | ||
If you want to add more steps in the final pipeline: | ||
- implement a function like this and add it to the array on the module.exports | ||
function your_step( context ) { | ||
return data => { | ||
...do your stuff here | ||
return result // result has to be an object like data or null to skip it | ||
} | ||
} | ||
context = | ||
{ | ||
geo : { lat : fieldLat, lon : fieldLon } || null, | ||
service : <serviceConf> | ||
} | ||
data = { | ||
key : <from the parser> | ||
value : <your actual payload> | ||
} | ||
*/ | ||
|
||
const proj4 = require('proj4'); | ||
|
||
module.exports = [adaptPayload]; | ||
|
||
function adaptPayload (context) { | ||
return data => { | ||
if(context.geo !== null) { | ||
let data_lat = data.value[context.geo.lat]; | ||
let data_lon = data.value[context.geo.lon]; | ||
if (data_lat !== 0 && data_lon !== 0 ) { | ||
// Reprojection according to conf. | ||
try { | ||
let [lon,lat] = proj4(proj4.defs(`EPSG:${context.service.out_sr.latestWkid}`),[data_lon,data_lat]) | ||
let fixed = { | ||
geometry : { | ||
x : lon, y : lat, | ||
spatialReference : context.service.out_sr | ||
}, | ||
attributes : data.value | ||
}; | ||
fixed.attributes.FltId = data.value.id_str; | ||
data.value = fixed; | ||
|
||
return data; | ||
} catch(err) { | ||
console.error(`Failed re-projection [${err}]`); | ||
console.log(`${data_lat} || ${data_lon}`); | ||
return null; | ||
} | ||
} else { | ||
return null | ||
} | ||
} else { | ||
return null | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const {parser} = require('stream-json/Parser'); | ||
const {streamValues} = require('stream-json/streamers/StreamValues'); | ||
const {chain} = require('stream-chain'); | ||
const CUSTOM_PIPELINE = require('./custom.js'); | ||
|
||
function _injectCtx (arr,ctx) { | ||
return arr.map(fn => fn(ctx)); | ||
} | ||
|
||
function sanityCheck(arr) { | ||
let length = arr.length; | ||
return Array.isArray(arr) && arr.filter(fn => typeof(fn) === "function").length === length; | ||
} | ||
|
||
function compose(ctx) { | ||
let pipeline = [ | ||
parser({jsonStreaming: true}), | ||
streamValues() | ||
]; | ||
if (sanityCheck(CUSTOM_PIPELINE)) { | ||
pipeline.push(..._injectCtx(CUSTOM_PIPELINE,ctx)); | ||
} else { | ||
console.log(`Default Pipeline setup...[Skipping custom pipeline]`); | ||
if (CUSTOM_PIPELINE.length > 0) { | ||
console.warn(`Something is wrong : Please review your custom pipeline`); | ||
process.exit(12); | ||
} | ||
} | ||
|
||
return pipeline; | ||
} | ||
|
||
module.exports = compose; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters