Created
April 14, 2021 14:05
-
-
Save kiang/facfa5bb14f0dd9529a4b5c3e995aa60 to your computer and use it in GitHub Desktop.
script to extract large geojson as separated kml file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// exec first: ogr2ogr -f "GeoJSON" -s_srs EPSG:3826 -t_srs EPSG:4326 hello.json 全市潛力基地地籍_20210414.shp | |
$json = json_decode(file_get_contents(__DIR__ . '/hello.json'), true); | |
$path = __DIR__ . '/output'; | |
if(!file_exists($path)) { | |
mkdir($path, 0777, true); | |
} | |
$fc = []; | |
foreach($json['features'] AS $f) { | |
$k = $f['properties']['管理單位']; | |
if(!isset($fc[$k])) { | |
$fc[$k] = [ | |
'type' => 'FeatureCollection', | |
'name' => $k, | |
'features' => [], | |
]; | |
} | |
$fc[$k]['features'][] = $f; | |
} | |
foreach($fc AS $k => $j) { | |
$targetJson = $path . '/' . $k . '.json'; | |
$targetFile = $path . '/' . $k . '.kml'; | |
file_put_contents($targetJson, json_encode($j)); | |
exec("ogr2ogr -f KML {$targetFile} {$targetJson} -dsco NameField=fid_key"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment