How to Load a local KML from browser url ?

Hello,

I am modifiying my QGIS GEarthView plugin, calling Cesium, (together) instead of GoogleEarth.

Now, my question is: how is the correct syntax to load a KML, from the browser URL (that is, without an html page) ?

I noticed that Dragging (& Dropping) the KML file on the CesiumVIewer web page, it works, but with:

http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/index.html?source=file:///c:/Users/rrr/.qgis2/python/plugins/gearthview/temp/GEKml_Lines.kml

it answers an error:

An error occurred while loading the file: file:///c:/Users/rrr/.qgis2/python/plugins/gearthview/temp/GEKml_Lines.kml
An error occurred while loading the file, which may indicate that it is invalid. A detailed error report is below:

NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/Users/rrr/.qgis2/python/plugins/gearthview/temp/GEKml_Lines.kml'.
Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/Users/rrr/.qgis2/python/plugins/gearthview/temp/GEKml_Lines.kml'.
    at Error (native)
    at Function.u.load (http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:418:29100)
    at http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:418:28401
    at Object.then (http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:418:23643)
    at e (http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:418:23356)
    at u (http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:418:28356)
    at t (http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:424:17323)
    at ui.load (http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:426:2784)
    at Function.ui.load (http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:426:2290)
    at http://cesiumjs.org/Cesium/Build/Apps/CesiumViewer/CesiumViewerStartup.js:466:17690

Surely you have already dealt with this issue, you can you please send me a link about it?

Thank you

Roberto

In most use cases, what you want to do isn’t possible. For security reasons, browsers are heavily sandboxed and reading a file uri directly from disk is impossible without having the browser bringing up a file selection dialog itself for the user to choose which file to read.

That being said, there is one way to do what you want. You need to read the contents of the file you want to pass to the browser and convert it to a data uris You can then pass the data uri as part of the URL the same way you pass normal urls. The one caveat here is that if the file you are loading hasother file url references in it, those won’t be readable either. For that you can either make them embedded data uris as well or in the case of a kml file, you can use a kmz instead.

If you are using an embedded browser control rather than just popping up an external browser, then you might have other options, but that depends on the control being used.

Hope that helps,

Matt

In most use cases, what you want to do isn't possible. For security
reasons, browsers are heavily sandboxed and reading a file uri directly
from disk is impossible without having the browser bringing up a file
selection dialog itself for the user to choose which file to read.

OK. But, I do not understand why the same problem do not happens with the
drag & drop of the KML file.

That being said, there is one way to do what you want. You need to read
the contents of the file you want to pass to the browser and convert it to
a data uris <https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs>
You can then pass the data uri as part of the URL the same way you pass
normal urls. The one caveat here is that if the file you are loading
hasother file url references in it, those won't be readable either. For
that you can either make them embedded data uris as well or in the case of
a kml file, you can use a kmz instead.

MMmmm. It seems complex.

I have to go to Cesium a KML file created by my plugin for QGIS. Which
method do you suggest?

I can call a page of Cesium viewer that already includes' a KML, then I'm
going to rewrite dynamically?