我正在尝试将csv文件转换为GeoJSON,然后在Google地图上显示结果。
功能集合中的最后一个功能后面不应该有逗号,所以我需要检测哪个是fgetcsv中的最后一行,并且不用逗号,我正在努力解决这个问题。
这是我到目前为止的代码:
{
"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"
}
}
]
}
这是我想要实现的输出的一个例子:
{{1}}
感谢您的帮助。
答案 0 :(得分:0)
我会建议更像这样的东西而不是手动写出你自己的JSON。
将数据附加到PHP数组,并使用内置的json_encode
函数创建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]);