Solved

How to export data as .js?

  • 12 September 2018
  • 5 replies
  • 17 views

I am using the QGIS plugin "QGIS2WEB" for publishing spatial data into webmaps, written in javascript. The plugin converts all the data showing into .js files, stored in a data registry. Now I want to replace/add spatial data manually, without using the plugin. Is there a way to convert and export spatial data, e.g. json as .js with FME?

Thank you.

icon

Best answer by nathanatsafe 12 September 2018, 21:02

View original

5 replies

Userlevel 4

I don't know what that QGIS plugin expects, but have you looked at the GeoJSON writer? Could that be it?

Badge +7

Hi @pascatl

 

The QGIS plugin expects a .js file with the GeoJSON feature(s) assigned to a variable. This .js file is included as a script in the output .html, which calls the variable and adds it to the map. (You can verify this in your qgis2web output folder in /data.) The fact that the .html uses a .js file with the GeoJSON assigned to a variable isn't necessary for the web app to work.

 

 

It might be possible to create this kind of .js file with FME, but since we don't have a "Javascript" writer, it might be a bit of a hack. Instead I would suggest writing out your GeoJSON to a .json file with FME, and consuming that in your application.

 

Hope this helps!

Nathan

Hi @pascatl

 

The QGIS plugin expects a .js file with the GeoJSON feature(s) assigned to a variable. This .js file is included as a script in the output .html, which calls the variable and adds it to the map. (You can verify this in your qgis2web output folder in /data.) The fact that the .html uses a .js file with the GeoJSON assigned to a variable isn't necessary for the web app to work.

 

 

It might be possible to create this kind of .js file with FME, but since we don't have a "Javascript" writer, it might be a bit of a hack. Instead I would suggest writing out your GeoJSON to a .json file with FME, and consuming that in your application.

 

Hope this helps!

Nathan

Thank you, I will give it a try and let you know, if it works for me.

 

 

Hi @pascatl

 

The QGIS plugin expects a .js file with the GeoJSON feature(s) assigned to a variable. This .js file is included as a script in the output .html, which calls the variable and adds it to the map. (You can verify this in your qgis2web output folder in /data.) The fact that the .html uses a .js file with the GeoJSON assigned to a variable isn't necessary for the web app to work.

 

 

It might be possible to create this kind of .js file with FME, but since we don't have a "Javascript" writer, it might be a bit of a hack. Instead I would suggest writing out your GeoJSON to a .json file with FME, and consuming that in your application.

 

Hope this helps!

Nathan

The solution was, as you mentioned, to export the new spatial data as GeoJSON and assigning the GeoJSON to a variable in my javascript file using ajax. Instead of using the converted .js, I can now replace new data easily with GeoJSONs. 

var geojson_data = JSON.parse($.ajax({'url': 'data/data.geojson', 'async': false}).responseText); 

 

"geojson_data" is the "new" .js file linked in the javascript code.

 

Thank you for your help.

 

Userlevel 6
Badge +33

The solution was, as you mentioned, to export the new spatial data as GeoJSON and assigning the GeoJSON to a variable in my javascript file using ajax. Instead of using the converted .js, I can now replace new data easily with GeoJSONs. 

var geojson_data = JSON.parse($.ajax({'url': 'data/data.geojson', 'async': false}).responseText); 

 

"geojson_data" is the "new" .js file linked in the javascript code.

 

Thank you for your help.

 

Awesome, thanks for sharing! I used this (QGIS2Web) a lot on a previous project (QGIS 2.x era). Especially for sharing a 3D view of contours to CAD specialists. Very useful!

Edit: I used QGIS2ThreeJS. Different plugin, 3D vs 2D, but same principle.

Reply