Skip to main content
Solved

How to export data as .js?

  • September 12, 2018
  • 5 replies
  • 49 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.

Best answer by nathanatsafe

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

View original
Did this help you find an answer to your question?

5 replies

david_r
Evangelist
  • September 12, 2018

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


nathanatsafe
Safer
Forum|alt.badge.img+7
  • Safer
  • Best Answer
  • September 12, 2018

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


  • Author
  • September 13, 2018
nathanatsafe wrote:

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.

 

 


  • Author
  • November 20, 2018
nathanatsafe wrote:

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.

 


nielsgerrits
VIP
pascatl wrote:

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.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings