Hexagon Geospatial
MENU

Smart M.App

Looking for answers in regards to M.Apps, M.App Exchange or M.App Studio? Smart M.App Tech Discussion board is where you can search, ask your questions and assist others by sharing your knowledge. Join the conversation, connect, contribute and share.
Showing results for 
Search instead for 
Do you mean 
Reply
Occasional Contributor
Posts: 8
Registered: ‎04-01-2016
Accepted Solution

catalog items access from custom js

Hello!

 

I'm trying to access items available in catalog with custom js.

The data is a gps track in a form of geojson. Since for now standart ways of showing such tracks and interacting with it are quite limited, I'm using my map based on leaflet api too. There is a code snippet for browsing catalog items:

 

$GP.m_app.platform.catalog.getList(function (catalogItems) { ... });

 

But I don't see any way to access the contents of said item. I see a lot of properties and even one of them looks like what I need (onlineStoragePath) but it isn't since no url can be created of that.

 

When I try to publish geojson the callback is never called and no error thrown.

 

What am I doing wrong? How can I proceed from properties to actual data?

 

Sergey

 

Technical Evangelist
Posts: 1,063
Registered: ‎09-11-2015

Re: catalog items access from custom js

Hi Sergey,

 

$GP.m_app.platform.catalog.getList(function (catalogItems) { ... }); correctly returns list of catalog items and their properties. It can be used for instance for searching. Once you find an specific catalog item, you must download it. Did you try $GP.m_app.platform.catalog.get(itemID, function(result) {})

?

 

Jan

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial

Occasional Contributor
Posts: 8
Registered: ‎04-01-2016

Re: catalog items access from custom js

Hello, Jan!

 

catalog.get gives you in fact the same set of properties that you get from catalogItems.datasets[index] (members of array). To get actual file you need to access attachment directly somehow.

 

Sergey

Occasional Contributor
Posts: 9
Registered: ‎06-22-2016

Re: catalog items access from custom js

[ Edited ]

$GP.m_app.platform.catalog.get('e18b0308-47c3-42ef-a9a9-fabf56ceda19_2c91808355e0c2b5015600569d9521c2', function(result) { });

the above command results into below object 

 

 

I agree with Sergey, there will be still another step needed so actually retrive the data so it can be used for display or attach it to chart

 

Staff
Posts: 106
Registered: ‎11-24-2015

Re: catalog items access from custom js

Picture attached for previous post

Technical Evangelist
Posts: 1,063
Registered: ‎09-11-2015

Re: catalog items access from custom js

Hm, I was a bit afraid that it ($GP.m_app.platform.catalog.get) will just return object information but I hoped for better :-)

 

Be aware that you cannot simply get an object from the catalog (except published imagery to a web service). You must firstly issue a download request, get the download job ID and finally get the file downloaded to your machine. Just like what you see in M.App Chest. It looks like there's no function for that in JS API and you will have to use the Foundation API (REST interface).

 

Jan

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial

Occasional Contributor
Posts: 8
Registered: ‎04-01-2016

Re: catalog items access from custom js

So, guys, I found some help on the inside. This is what I end up doing:

 

$GP.m_app.platform.catalog.getList(function (catalogItems) {
	for (var i = 0; i < catalogItems.datasets.length; i += 1) {
		if (catalogItems.datasets[i].properties...........some selection logic) {
			var url = 'api/v1/items/' + catalogItems.datasets[i].id + '/attachments/' +
						catalogItems.datasets[i].attachments[catalogItems.datasets[i].title + '.attachment'].name;
			$GP.m_app.utils.connection(url).then(function (response) {
				console.log(JSON.parse(response.entity));
			}).catch(function (err) {
				console.log(err);
			});
		}
	}
});

 There is JSON.parse since I know it is json. As far as I know this is now the official way to do that. What you need is attachment. Catalog item ordinarily has three of them: thumbnail, largethumbnal and file itself. Attachments information is returned in this catalog properties object.

 

Hope this helps others. Smiley Happy

 

Sergey

Highlighted
Occasional Contributor
Posts: 8
Registered: ‎04-01-2016

Re: catalog items access from custom js

Or maybe title is a bad way to costruct attachment's name since it can be cahnged by user. But I hope you get the idea.

Technical Evangelist
Posts: 1,063
Registered: ‎09-11-2015

Re: catalog items access from custom js

[ Edited ]

Good job Sergey! This will be really handy to know for other folks. Smiley Happy

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial

Contributor
Posts: 34
Registered: ‎10-21-2016

Re: catalog items access from custom js

[ Edited ]

Thanks Sergey! It certainly does Smiley Happy

For anyone for who it might be useful: the following code just displays the information + once again (but separate) the title for every catalog item. Makes it easy to look through your Chest in the developer console.

 

$GP.m_app.platform.catalog.getList(function (catalogItems) {
    console.log("catalogItems: ", catalogItems);
	
	for (var i = 0; i < catalogItems.datasets.length; i += 1) {
	    console.log('%c name of catalogItem:  ', 'background: #222; color: #bada55');
	    console.log(catalogItems.datasets[i].title);
            console.log("catalogItem content: ", catalogItems.datasets[i]);
	}
});

Result:

Screenshot Chest Access.png

Hope this helps others. 

 

Vincent

 

Do you need immediate support?
Please submit a Ticket through our
Development Ticket Portal.