dongrang2186 2014-06-04 18:41
浏览 78
已采纳

从PHP查询创建CSV文件,然后转换为GEOJSON文件

I have a PHP method which returns a json string as follows:

function getjson(){
$mun = $this->input->POST('municipality');
$sl  = $this->input->POST('saleslimit'); 
$q = $this->db->query("SELECT RollNum , Address, v2_lat, v2_lng  
    FROM tblontario WHERE Municipality = '".$mun."'"." LIMIT ".$sl);            
        $json_string = json_encode($q->result(), JSON_PRETTY_PRINT);
        file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.json', $json_string);
        echo $json_string;
}

Rather than return a json string, I would like to be able to return a CSV file so that I can then take that CSV file and somehow turn it into a geojson file that can be loaded into a google map.

Any ideas on how to accomplish this? Ultimately my goal is to create a geojson file, but I am uncertain of the best way to do this. Thanks.

  • 写回答

2条回答 默认 最新

  • dpt1712 2014-06-04 19:29
    关注

    If $row is an array you can just do something like:

    function getCSV() {
        $mun = $this->input->POST('municipality');
        $sl  = $this->input->POST('saleslimit'); 
        $q = $this->db->query("
            SELECT 
                RollNum, 
                Address,
                v2_lat,
                v2_lng 
            FROM
                tblontario 
            WHERE 
                Municipality = '".$mun."'"." 
            LIMIT ".$sl
        );
        $row = $q->result();
        $csvRow = implode(",",$row);
        file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.csv', $csvRow);
        echo $csvRow;
    }
    

    Since I see a limit there, I'm guessing you're getting more than one result and that $q->result(); returns an array of results instead of just a row, in which case..

    function getCSV() {
        $mun = $this->input->POST('municipality');
        $sl  = $this->input->POST('saleslimit'); 
        $q = $this->db->query("
            SELECT 
                RollNum, 
                Address,
                v2_lat,
                v2_lng 
            FROM
                tblontario 
            WHERE 
                Municipality = '".$mun."'"." 
            LIMIT ".$sl
        );
        $res = $q->result();
        if(!empty($res)){
            $fh = fopen('/home/apts/Dropbox/ptax.ca/js/salecomps.csv','w');
            foreach($res as $row){
                // as suggested by @GigaWatt, swapped fwrite for fputcsv
                fputcsv($fh,$row);
            }
            fclose($fh);
            echo file_get_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.csv');
        } else {
            echo "";
        }
    
    }
    

    EDIT: Looking a GeoJSON spec, what you prob want is something like:

    function getGeoJSON() {
        $mun = $this->input->POST('municipality');
        $sl  = $this->input->POST('saleslimit'); 
        $q = $this->db->query("
            SELECT 
                RollNum, 
                Address,
                v2_lat,
                v2_lng 
            FROM
                tblontario 
            WHERE 
                Municipality = '".$mun."'"." 
            LIMIT ".$sl
        );
        $res = $q->result();
        if(!empty($res)){
            $geoArr = Array(
                "type" => "MultiPoint",
                "coordinates" => Array()
            );
            foreach($res as $row) {
                $geoArr["coordinates"][] = Array($row['v2_lat'],$row['v2_lng']);
            }
            $geoJSON = json_encode($geoArr);
            file_put_contents('/home/apts/Dropbox/ptax.ca/js/salecomps.geojson',$geoJSON);
            echo $geoJSON;
        } else {
            echo "{}";
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机