Generate dynamic CZML data using NodeJS - node.js

Cesium has the ability to render dynamic data that originates from a czml file. They have an example on their website showing how use the czml data on the client-side.
I'd like to generate czml data dynamically on the server-side using nodejs. Are there any pointers or examples on how that can be achieved?

There's an official czml-writer for C# and Java, and a 3rd-party czml writer for Python, but I'm not aware of any yet available for NodeJS.
It seems like one could easily output CZML from NodeJS just by calling JSON.stringify on an array of CZML packets (specially-formatted JavaScript objects). For dates and intervals, one could even import Cesium (or Cesium Core) into NodeJS for access to JulianDate and related time interval classes, to store in the appropriate fields.
You'll need to make sure the first element in the array is the "id" : "document" packet (as shown in the guide) with the settings for the rest of the CZML data, and that each subsequent packet provides a unique id of its own. After that just push some entity description packets into the array, and you should be good to go.

For earth orbiting objects, you can use the czml-writer npm package. Here's some basic usage:
var czml = require("czml-writer");
var orbit = new czml.orbit.fromParams({
apogee: 426.9, // km
perigee: 416.2, // km
inclination: 51.65, // deg
rightAscension: 304.1, // deg
argumentOfPeriapsis: 117.8 // deg
});
var output = orbit.czml();
var czml = require("czml-writer");
var tle = 'NOAA 14\n' +
'1 23455U 94089A 97320.90946019 .00000140 00000-0 10191-3 0 2621\n' +
'2 23455 99.0090 272.6745 0008546 223.1686 136.8816 14.11711747148495';
var orbit = new czml.orbit.fromTle(tle);
var output = orbit.czml();

Related

Find HTML Element value in Inline Code Connector Logic App

I am trying to get the value from an API multipart Response which contains json, xml and html. Plus HTML doesn't have any id and any class on its elements in that response. I want to get the complete table in an element and want to get the values from its tr. Please suggest me how can I do this in inline code in logic apps.
var response = workflowContext.trigger.outputs.body; // API Response
var multiBody = response.body.$multipart[4].body; // multipartBody
var table = multiBody. // How to get table section and get its tr values
return table;
I have already written the javascript code which I want to implement in inline code connector. My code is
var rows = document.getElementsByTagName('table')[4].rows;
var firstRow = rows[0]; // LTV/CLTV Row
var firstCell = firstRow.cells[1]; // LTV/CLTV Index
var firstText = firstCell.innerText;
var firstValue = firstText.substring(0, firstText.indexOf('/') - 1); // LTV
var secondValue = firstText.substring(firstText.indexOf('/') + 2, firstText.lastIndexOf('/') - 1); // CLTV
Please help me how can I do this.
Plus as it is mentioned in the documentation that we can write the javascript in inline code connector then why getElementById, getElementByTagname functions are not working here.
The Inline Code Actions runs NodeJS for JavaScript which doesn't really have a DOM Object to work with like a browser.
Instead, you will have to parse the DOM or just perform string searches to get what you need.
Parsing the DOM wouldn't be that simple of course and it would be best to use something like jsdom inside an Azure Function instead, if string/regex searches won't cut it for you.

Is it possible to store Cytoscape.js layout data directly to a file format in app/web server and re-launch the same layout to minimize re-computation?

Some of the cytoscape layout is randomize where the position is not fix every time we launch it. I understand from multiple stack overflow questions where we can save the layout data (i.e. including its position x and y) into browser local storage or session storage so that we can display the same layout using the same data.
However, the problem with local storage or session storage is good for one users. But, imagine if there are thousands of users using the same app, the server will undergo mass computation for each user to store respective data to individual browsers. Can we save the data into a file format directly into app/web server so that 1000 users will see the same layout and this reduces the computation of different data set as well.
Thank you. Would like to know the possibility to convert data into a file and store in the web/app server.
Yes, you can store position data. Actually, there are 2 options in my mind.
Use cy.json(). You can store the elements as JSON like JSON.stringify(cy.json().elements) and then save this JSON string.
cy.json().elements is something like the below image
You can restore this data easily like cy.json({elements: JSON.parse(jsonStr));
As you could see cy.json().elements is a bit big thing. Position data is just a small object like {x: 0, y: 0}. Additional to position it contains many other data. So if you only need to restore the positions, you could store them manually easily with a code like below. You can use ele.id and node.position() functions.
function storePositions() {
const nodes = cy.nodes();
const nodePositions = {};
for (let i = 0; i < nodes.length; i++) {
nodePositions[nodes[i].id()] = nodes[i].position();
}
return nodePositions;
}
You can also restore node positions easily. You can use getElementById and node.position() functions.
function restorePositions(nodePositions) {
const nodes = cy.nodes();
const nodePositions = {};
for (let k in nodePositions) {
const node = cy.getElementById(k);
if (node && node.length > 0) {
node.position(nodePositions[k]);
}
}
return nodePositions;
}

Creating Node.js enum in code to match list of values in database

I have a list of valid values that I am storing in a data store. This list is about 20 items long now and will likely grow to around 100, maybe more.
I feel there are a variety of reasons it makes sense to store this in a data store rather than just storing in code. I want to be able to maintain the list and its metadata and make it accessible to other services, so it seems like a micro-service data store.
But in code, we want to make sure only values from the list are passed, and they can typically be hardcoded. So we would like to create an enum that can be used in code to ensure that valid values are passed.
I have created a simple node.js that can generate a JS file with the enum right from the data store. This could be regenerated anytime the file changes or maybe on a schedule. But sharing the enum file with any node.js applications that use it would not be trivial.
Has anyone done anything like this? Any reason why this would be a bad approach? Any feedback is welcome.
Piggy-backing off of this answer, which describes a way of creating an "enum" in JavaScript: you can grab the list of constants from your server (via an HTTP call) and then generate the enum in code, without the need for creating and loading a JavaScript source file.
Given that you have loaded your enumConstants from the back-end (here I hard-coded them):
const enumConstants = [
'FIRST',
'SECOND',
'THIRD'
];
const temp = {};
for (const constant of enumConstants) {
temp[constant] = constant;
}
const PlaceEnum = Object.freeze(temp);
console.log(PlaceEnum.FIRST);
// Or, in one line
const PlaceEnum2 = Object.freeze(enumConstants.reduce((o, c) => { o[c] = c; return o; }, {}));
console.log(PlaceEnum2.FIRST);
It is not ideal for code analysis or when using a smart editor, because the object is not explicitly defined and the editor will complain, but it will work.
Another approach is just to use an array and look for its members.
const members = ['first', 'second', 'third'...]
// then test for the members
members.indexOf('first') // 0
members.indexOf('third') // 2
members.indexOf('zero') // -1
members.indexOf('your_variable_to_test') // does it exist in the "enum"?
Any value that is >=0 will be a member of the list. -1 will not be a member. This doesn't "lock" the object like freeze (above) but I find it suffices for most of my similar scenarios.

Save a date value in local storage from a resource file and retrieve from background.js using crossrider

I need to save the date value of a particular action into the local storage from a resource file. The date value should be compared with the current date in the background in regular intervals. the code i am using in the resource file to store the date value is
var x = new Date();
var enableDate = x.getFullYear()+"-"+(x.getMonth()+1)+"-"+x.getDate();
appAPI.db.async.set("ext_enable_date",enableDate);
and the code i used to retrieve the saved date from 'background.js' is
var enDate = appAPI.db.get("ext_enable_date");
But i get null value in 'enDate'. How can i get it correct? Any help would be appreciated.
The Crossrider framework provides 2 types of local storage, as follows:
appAPI.db - a synchronous database for saving small data (integers, booleans, and short strings of approximately 10 characters)
appAPI.db.async - an asynchronous database for saving larger data
The storages are completely distinct and one cannot access the other. Hence, the problem with code is that it saves the date using an asynchronous method (async.set) and then tries to retrieve it using a synchronous method (get).
As your data is small, simply resolve the issue by using the synchronous methods as follows:
var x = new Date();
var enableDate = x.getFullYear()+"-"+(x.getMonth()+1)+"-"+x.getDate();
appAPI.db.set("ext_enable_date",enableDate);
var enDate = appAPI.db.get("ext_enable_date");
[Disclosure: I am a Crossrider employee]

Parse attribute of Media Element

I want to parse url attribute from the XML and show image in image control (the one reffered to by the URL) in listbox from the following feed link: http://feeds.bbci.co.uk/news/rss.xml
My code is:
var ComingNewsFromUri = from rss in XElement.Parse(e.Result).Descendants("item")
select new NewsItems
{
Title = rss.Element("title").Value,
PubDate = rss.Element("pubDate").Value,
Description = rss.Element("description").Value
};
For RSS, I would recommend using SyndicationFeed and SyndicationItem....does all the parsing and converting to objects automatically and brilliantly for you.
http://ryanhayes.net/blog/how-to-build-an-rss-feed-reader-in-windows-phone-7part-i-retrieving-parsing-and-displaying-post-titles/
I have an RSS feed app on the store myself using SyndicationFeed and it is very reliable and convenient.
Here is another sample by Microsoft
http://code.msdn.microsoft.com/wpapps/RSS-Reader-Sample-1702775f

Resources