Skip to content

Instantly share code, notes, and snippets.

@wilxsv
Last active March 31, 2018 20:18
Show Gist options
  • Save wilxsv/206bc282cec9b6528e6035219e807304 to your computer and use it in GitHub Desktop.
Save wilxsv/206bc282cec9b6528e6035219e807304 to your computer and use it in GitHub Desktop.
bajar peso a geojson
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Crea conexion
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
lista_entidad( $conn, "tabla", "geojson");
$conn->close();
/*
Crea un archivo geojson por cada entidad que se le envie
*/
function crea_json( $file, $tabla, $contenido){
$myfile = fopen($file, "w") or die("Unable to open file!");
fwrite($myfile, '{ "type": "MultiPolygon", "coordinates": [[['.$contenido.']]]}');
fclose($myfile);
}
/*
Recorre la tabla por cada entidad
*/
function lista_entidad( $conn, $tabla, $campo){
$sql = "SELECT * FROM $tabla";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
crea_json( $row["id"].".json", "", $row[$campo] );
}
} else {
echo "0 results";
}
}
?>
#!/bin/bash
echo "">departamentos.sql
for entry in *.json
do
out=$entry"_o.json"
echo ".bin/geojson-precision -p 7 $entry geojson/$entry" | bash
done
for entry in geojson/*.json
do
json="$(cat $entry)"
id="${entry##*/}"
id="${id%.*}"
echo " UPDATE tabla SET geojson_7 = '$json' WHERE id = $id; ">>departamentos.sql
done
mysql -u USER -pPASSWORD BASE < departamentos.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment