1

我正在 www.openwifinder.com/ 上开展一个开放数据项目。我们正在使用 firebase 存储我们将使用 javascript 加载到 ESRI 地图的数据。因为我们正在使用开放数据的理念,我们希望公众能够下载存储在 firebase 中的整个点云作为 .json 文件。我的团队最初从数据页面上的“导出数据”按钮复制了链接,但我们发现该链接是临时的,因为 url 中的身份验证令牌到期。我们还尝试链接到“Instance.firebaseio.com”网址,但是当您单击它(在另一个浏览器中)时,它表示您无权查看 Firebase。

理想情况下,我们想做一些像 Firebase 对它的 Open Data Set 页面所做的事情。当你点击那些开放的数据 url 时,你会被带到开放的 firebase 的数据页面,除了读取和导出数据之外你什么也做不了。它不一定必须是公共链接,但即使用户必须注册一个 firebase 帐户才能查看数据,它也可以。当然,真正的公共链接对于我们的目的来说是理想的。

有任何想法吗?我已经搜索了网站的帮助页面以获取线索。

谢谢!!

4

2 回答 2

1

我想不出一种使用 API 让用户下载数据的方法,所以我使用此处和网络上其他地方的示例以编程方式进行了操作。自从我编写以下代码(以及基于原始 OpenWifinder 代码的项目)以来已经有一段时间了,所以我不确定将其归因于何处,但我在这里发布我的解决方案,以便其他人找到这个问题他们可以得到这个工作。我确信它不像使用 API 那样优雅,但它确实有效。

解决方案可能来自这里,但无法判断此人是否在其他地方得到它:https ://gist.github.com/AnirbanKundu/26f2ba229174b1915873

导出.js:

// JavaScript Document
var myFirebase = new Firebase("FIREBASEURL");
var dataJSON;
var jsonDL;
require([
      "dojo/dom", 
      "dojo/on", 
      "dojo/_base/lang", "dojo/domReady!"
    ], function(
    dom, 
    on, 
    lang,
    domReady
    )  {
 var exportDataBtn = dom.byId("exportBtn");
      on(exportDataBtn, "click", JSONToCSVConvertor);
var exportJSONBtn = dom.byId("dlJSON");
      on(exportJSONBtn, "click", DownloadJSON);
function DownloadJSON(){
    //console.log(JSON.stringify(dataJSON));
    var jsonString = JSON.stringify(dataJSON, null, 4);
    //console.log(jsonString);
  jsonDL = exportJSONBtn;
  jsonDL.download = "BikeThefts.json";
  jsonDL.href = "data:application/json;charset=utf-8," + jsonString;
  jsonDL.innerHTML = "Download data as JSON";
};
var dataObject;
    //this function grabs a 'snapshot' of all the data in Firebase, then navigates down to the 'features' child. It then iterates through all the
    //children under 'attributes' and retrieves all attribute data. Then it converts them to strings or numbers and calls addPoint to map them
myFirebase.on("value", function(snapshot) {
dataObject = snapshot;
dataJSON = dataObject.val();
//console.log(JSON.stringify(dataJSON));

});

function JSONToCSVConvertor() {
    //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
    var arrData = typeof dataJSON != 'object' ? JSON.parse(dataJSON) : dataJSON;

    var CSV = '';    
    //Set Report title in first row or line
    var ShowLabel = true;
    var ReportTitle = "Data";
    CSV += ReportTitle + '\r\n\n';

    //This condition will generate the Label/Header
    if (ShowLabel) {
        var row = "";

        //This loop will extract the label from 1st index of on array
        for (var index in arrData[0]) {

            //Now convert each value to string and comma-seprated
            row += index + ',';
        }

        row = row.slice(0, -1);

        //append Label row with line break
        CSV += row + '\r\n';
    }

    //1st loop is to extract each row
    for (var i = 0; i < arrData.length; i++) {
        var row = "";

        //2nd loop will extract each column and convert it in string comma-seprated
        for (var index in arrData[i]) {
            row += '"' + arrData[i][index] + '",';
        }

        row.slice(0, row.length - 1);

        //add a line break after each row
        CSV += row + '\r\n';
    }

    if (CSV == '') {        
        alert("Invalid data");
        return;
    }   

    //Generate a file name
    var fileName = "BikeTheftIncidents_";
    //this will remove the blank-spaces from the title and replace it with an underscore
    fileName += ReportTitle.replace(/ /g,"_");   

    //Initialize file format you want csv or xls
    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);

    // Now the little tricky part.
    // you can use either>> window.open(uri);
    // but this will not work in some browsers
    // or you will not get the correct file extension    

    //this trick will generate a temp <a /> tag
    var link = document.createElement("a");    
    link.href = uri;

    //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden";
    link.download = fileName + ".csv";

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

});
于 2015-07-15T19:57:49.670 回答
0

您可以轻松拥有一个 URL,任何人都可以使用它通过 Firebase Cloud Functions 从 Firebase 获取您想要的数据:

https://medium.com/@dalenguyen/csv-or-json-export-with-firebase-functions-fa4a4b1416aa

于 2018-02-28T21:11:50.283 回答