Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

Your JSON isn't proper GeoJSON. Here's a PHP script to translate to GeoJSON:


<?php

/*
    when importing into QGIS, you want to simplify the linestring with a tolerance of about 0,00001 degrees
*/

if ($argc < 4 ) {
    exit( "Usage: place_village lat lon scale - with scale in s, m, l\n" );
}

$fp = fopen("php://stdin", "r") or die("cannot open stdin");
$lat = floatval($argv[1]);
$lon = floatval($argv[2]);

    // conversion:
    //   latitude = distance * 360 / (2*PI * 6400000)
    //   longitude = distance *360 * / (2*PI* cos(latitude) )
    switch ($argv[3]) {
        case 's':
        case 'm':
            $scale_lon = 0.5 * 360 / (2 * M_PI * 6400000); // for projection correction: ... 6400000 * cos($lat));
            $scale_lat = 0.5 * 360 / (2 * M_PI * 6400000);
            break;
        case 'l':
        default:
            exit("scale must be one of s, m, l - instead of '".$argv[3]."'\n");
    }

$input_data = file_get_contents("php://stdin");
$json = json_decode($input_data);

foreach ($json->features as &$feature) {
    if (isset($feature->coordinates)) foreach ($feature->coordinates as &$coordinate) {
        move_this($coordinate);
    }
    if (isset($feature->geometries)) foreach ($feature->geometries as &$geom) {
        if (isset($geom->coordinates)) foreach ($geom->coordinates as &$coordinate) {
            move_this($coordinate);
        }
    }
}

function move_this(&$coordinate) {
    if (is_array($coordinate[0])) {
        foreach ($coordinate as &$c) {
            move_this($c);
        }
    } else {
        global $lat, $lon, $scale_lat, $scale_lon;
        $coordinate[0] = floatval($coordinate[0]) * $scale_lon + $lon;
//        $coordinate[1] = -1 * floatval($coordinate[1]) + $y; // old pre-json-export files needed Y flipped
        $coordinate[1] = floatval($coordinate[1]) * $scale_lat + $lat;
    }
}

// now convert to GeoJSON
$new_features = array();
foreach ($json->features as &$feature) {

    if (isset($feature->coordinates) && $feature->id != "earth") {
        // buildings
        $coords = close_rings($feature->coordinates);
        $new_features[] = array("type"=>"Feature", "properties"=>array("id"=>$feature->id), "geometry"=>array("type"=>$feature->type, "coordinates"=>$coords));
    }

    if (isset($feature->geometries)) {
        // roads
        $coords = [];
        foreach ($feature->geometries as $geom) {
            $coords[] = $geom->coordinates;
        }
        // "type" is the GQIS road type string - here "local" for local roads
        $new_features[] = array("type"=>"Feature", "properties"=>array("id"=>$feature->id, "type"=>"local"), "geometry"=>array("type"=>"MultiLineString", "coordinates"=>$coords));
    }
}
$new_json = array("type"=>"FeatureCollection", "features"=>$new_features);

function close_rings($coordinates) {
    if (is_array($coordinates[0][0])) {
        $all = [];
        foreach ($coordinates as $c) {
            $all[] = close_rings($c);
        }
        return $all;
    } else {
        $coordinates[] = $coordinates[0];
    }
    return $coordinates;
}

echo json_encode($new_json, JSON_PRETTY_PRINT);

?>

It is not: https://www.patreon.com/posts/37407181