I have an empty dictionary which will be in GeoJSON form:
fleet_geojson = {"type": "FeatureCollection", "features": []}
I have a pandas dataframe. Each row is a point in space: latitude, longitude and other information.
df_entire_fleet.to_csv('resources/entire_fleet.csv')
I want to fill the GeoJSON dictionay with the info of each point. Therefore, I do:
df_entire_fleet.apply(
lambda row: addVehicle2Geojson(fleet_geojson, row['company_id'], row[
'alarm'], row['battery_level'], row['id'], row['service_area_id'],
row['staff_state'], row['location']),
axis=1)
Where every string in brackets is the name of a column in the dataframe. The function:
def addVehicle2Geojson(fleet, comp, alarm, batt, id, serv_area, state,
coordinates):
fleet['features'].append({
"type": "Feature",
"properties": {
'company_id': comp,
'alarm': alarm,
'battery_level': batt,
'id': id,
'service_area_id': serv_area,
'staff_state': state,
},
"geometry": {
"type": "Point",
"coordinates": coordinates
}
})
And I get invalid GeoJSON where the order of all the elements is reversed:
{
"features": [
{
"geometry": {
"coordinates": "POINT(-0.00000 0.00000)",
"type": "Point"
},
"properties": {
"alarm": false,
"battery_level": 83,
"company_id": "xxxxxxxx",
"id": "xxxxxxxx",
"service_area_id": "xxxxxxxx",
"staff_state": "xxxxxxxx"
},
"type": "Feature"
},
{
"geometry": {
"coordinates": "POINT(-0.00000 0.00000)",
"type": "Point"
},
"properties": {
"alarm": false,
"battery_level": 1,
"company_id": "xxxxxxxx",
"id": "xxxxxxxx",
"service_area_id": "xxxxxxxx",
"staff_state": "xxxxxxxx"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
Why is append() changing the order of the keys?