【问题标题】:How to merge 2 JSON files into one JSON FeatureCollection如何将 2 个 JSON 文件合并为一个 JSON FeatureCollection
【发布时间】:2019-04-13 12:10:16
【问题描述】:

我有两个 JSON 文件,每个文件都包含一个具有相同结构但数据不同的 FeatureCollection。我正在尝试将它们组合成一个 JSON 文件作为包含所有数据的单个 FeatureCollection。我几乎做到了这一点,但“FeatureCollection”在文件开头重复,使其无效 JSON。

我认为这与我分别对两个文件进行 JSON 编码的方式有关,然后在我组合它们时再次编码,但经过一天尝试各种组合后,我还没有设法弄明白。

这是创建第一个 JSON 文件的代码(其中 $results 由数据库查询生成):

$geojson = array( 'type' => 'FeatureCollection', 'features' => array());

while ( $results->fetch() ) {
    $feature = array(
        'type' => 'Feature', 
              'properties' => array(
            'name' => $results->field('name')
            ),
      'geometry' => array(
        'type' => 'Point',
        'coordinates' => array((float)$results->field('long'), (float)$results->field('lat'))
            )
        );
    array_push($geojson['features'], $feature);
};

// // Create JSON file
    $fp = fopen('file1.json', 'w');
fwrite($fp, json_encode($geojson));
fclose($fp);

第二个文件(file2.json)同样创建,eg:

$geojson = array( 'type' => 'FeatureCollection', 'features' => array());

while ( $results->fetch() ) {
    $feature = array(
        'type' => 'Feature', 
              'properties' => array(
            'name' => $results->field('name')
            ),
      'geometry' => array(
        'type' => 'Point',
        'coordinates' => array((float)$results->field('long'), (float)$results->field('lat'))
            )
        );
    array_push($geojson['features'], $feature);
};

// // Create JSON file
    $fp = fopen('file2.json', 'w');
fwrite($fp, json_encode($geojson));
fclose($fp);

然后我使用以下代码将它们组合起来:

    $jsonString = file_get_contents('file2.json');
    $jsonString2 = file_get_contents('file1.json');   
    $data = json_decode($jsonString, true);
    $data2 = json_decode($jsonString2, true);

    $op = array_merge_recursive( $data, $data2 );

    $fp = fopen('file3.json', 'w');
    fwrite($fp, json_encode($op));
    fclose($fp); 

生成的文件基本上没问题,它包含我需要的所有数据并且格式正确,除了文件开头它具有以下事实:

{"type":["FeatureCollection","FeatureCollection"],"features":[{"type":"Feature","properties":{"name":"......etc

代替:

{"type":["FeatureCollection"],"features":[{"type":"Feature","properties":{"name":"......etc

我不明白为什么一开始有两个“FeatureCollection”实例,或者如何只产生一个。

【问题讨论】:

    标签: php json merge geojson


    【解决方案1】:

    当您合并两组数据时,都带有自己的“类型”副本,合并将创建一个包含这两个项目的输出。

    考虑...

    $data = [ "type" => "FeatureCollection"];
    $data2 = [ "type" => "FeatureCollection"];
    
    $op = array_merge_recursive( $data, $data2 );
    
    print_r($op);
    

    这将输出

    Array
    (
        [type] => Array
            (
                [0] => FeatureCollection
                [1] => FeatureCollection
            )
    
    )
    

    因为这是两个源数组的组合。

    解决此问题的一种简单方法是重新设置数组中的值,此代码只是选择第一个值并将其设置为那个...

    $op["type"] = $op["type"][0];
    print_r($op);
    

    会给...

    Array
    (
        [type] => FeatureCollection
    )
    

    【讨论】:

    • 太好了,非常感谢,这正是我所需要的。
    猜你喜欢
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多