formatage GeoJson par PHP
Bonjour,
J'extrais des données d'une base Postgis que je converti en GeoJson avec ce script :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| $conn = pg_connect($conn_string) or die ("erreur de connexion base");
# Build SQL SELECT statement and return the geometry as a GeoJSON element
$sql = 'SELECT pt_id, pt_name, public.ST_AsGeoJSON(public.ST_Transform((pt_geo),4326),6) AS geojson FROM point';
$req = pg_query($sql);
if (!$req) {
echo "Une erreur s'est produite.\n";
exit;
}
# Build GeoJSON feature collection array
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
# Loop through rows to build feature arrays
while ($row = pg_fetch_row($req)) {
$properties = $row;
# Remove geojson and geometry fields from properties
unset($properties['geojson']);
unset($properties['geom']);
$feature = array(
'type' => 'Feature',
'geometry' => json_decode($row['geojson'], true),
'properties' => $properties
);
# Add feature arrays to feature collection array
array_push($geojson['features'], $feature);
}
header('Content-type: application/json');
echo json_encode($geojson, JSON_NUMERIC_CHECK);
pg_close(); |
J'ai validé le GeoJson de sortie avec JSONLint, il est OK.
Ensuite, je veux lire ce fichier GeoJson dans une interface de webmapping, mais il est mal formaté.
Exemple :
Voici le GeoJson formaté par PHP :
Code:
1 2 3 4 5 6 7 8
| {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": null,
"properties": [2, "Point test", "{\"type\":\"Point\",\"coordinates\":[-4.5,48]}"]
}]
} |
Voici le formatage qui permettrait qu'il soit lu dans l'interface webmapping :
Code:
1 2 3 4 5 6 7
| {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {"type":"Point","coordinates":[-4.5,48]},
"properties": [2, "Point test"]}]
} |
Dans le formatage PHP, la geométrie est à null et est mise dans les properties.
Est-ce qu'il faut formater "à la main" le GeoJson dans le script PHP?
Comment faire pour avoir dans les properties le nom des variables correspondant aux données ?
Exemple :
Code:
"properties": ["ID":2, "Nom":"Point test"]
Merci,
Nico