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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀