I'm trying to convert a csv file into GeoJSON and then display the results on a Google map.
The last feature in the feature collection shouldn't have a comma after it so I need to detect which is the last line in fgetcsv and have leave off the comma and I'm struggling with this.
This is the code I have so far:
<?php
$file = fopen("Incident_List.csv","r");
echo "{<br>";
echo "\"type\": \"FeatureCollection\",<br>";
echo "\"features\": [<br>";
$csvflag = true;
while(! feof($file))
{
while (($data = fgetcsv($file)) !== FALSE) {
if($csvflag) { $csvflag = false; continue; }
echo "{<br>";
echo "\"geometry\": {<br>";
echo "\"type\": \"Point\",<br>";
echo "\"coordinates\": [<br>";
echo $data[2] . "," . $data[1] . "<br>";
echo "]<br>";
echo "},<br>";
echo "\"type\": \"Feature\",<br>";
echo "\"properties\": {<br>";
echo "\"Category\":\"" . $data[4] . "\",<br>";
echo "\"Grid Reference\":\"" . $data[0] . "\",<br>";
echo "\"Day\":\"" . $data[3] . "\",<br>";
echo "\"Date\":\"" . $data[4] . "\",<br>";
echo "\"Type Of Incident\":\"" . $data[5] . "\",<br>";
echo "\"Area\":\"" . $data[7] . "\",<br>";
echo "\"Sex\":\"" . $data[8] . "\",<br>";
echo "\"Age\":\"" . $data[9] . "\",<br>";
echo "\"Members\":\"" . $data[10] . "\",<br>";
echo "\"Hours\":\"" . $data[11] . "\",<br>";
echo "\"Notes\":\"" . $data[12] . "\",<br>";
echo "}<br>";
echo "},<br>";
}
}
fclose($file);
?>
And this is an example of the output I am trying to achieve:
{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Point",
"coordinates": [
-3.224752,
55.891831
]
},
"type": "Feature",
"properties": {
"category": "misper",
"hours": "10am - 6pm",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam aliquam erat non tellus pellentesque, non euismod quam lacinia. Sed ac ex non eros aliquam sagittis. Etiam iaculis turpis sit amet arcu viverra, ac cursus sem feugiat. Sed fermentum dolor non mauris semper, eu pulvinar dolor feugiat.",
"name": "Missing Person",
"phone": "+44 20 1234 5678"
}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-2.631056,
55.243980
]
},
"type": "Feature",
"properties": {
"category": "walker",
"hours": "10am - 6pm",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam aliquam erat non tellus pellentesque, non euismod quam lacinia. Sed ac ex non eros aliquam sagittis. Etiam iaculis turpis sit amet arcu viverra, ac cursus sem feugiat. Sed fermentum dolor non mauris semper, eu pulvinar dolor feugiat.",
"name": "Injured Walker",
"phone": "+44 20 1234 5678"
}
}
]
}
Thanks for your help.
I would suggest something more like this rather than manually writing out your own JSON.
Append your data to a PHP array, and use the built-in json_encode
function to create the JSON.
$file = fopen("Incident_List.csv","r");
$features = [];
while (false !== $data = fgetcsv($file)) {
$geometry = [
'type' => 'Point',
'coordinates' => [$data[2], $data[1]]
];
$properties = [
'Category' => $data[4],
'Grid Reference' => $data[0],
'Day' => $data[3],
'Date' => $data[4],
'Type of Incident' => $data[5],
'Area' => $data[7],
'Sex' => $data[8],
'Age' => $data[9],
'Members' => $data[10],
'Hours' => $data[11],
'Notes' => $data[12]
];
$features[] = [
'geometry' => $geometry,
'type' => 'Feature',
'properties' => $properties
];
}
echo json_encode(['type' => 'FeatureCollection','features' => $features]);