Skip to content

Instantly share code, notes, and snippets.

@kiang
Created April 14, 2021 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiang/facfa5bb14f0dd9529a4b5c3e995aa60 to your computer and use it in GitHub Desktop.
Save kiang/facfa5bb14f0dd9529a4b5c3e995aa60 to your computer and use it in GitHub Desktop.
script to extract large geojson as separated kml file
<?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