dictionary - how to convert python dict to geojson file in form standard? -
i want convert excel file geojson file. dictionary like:
[ {"noadresse": 42537006584, "nousager": 42537000086, "lateffective": 45.83675, "longdebut": 4.91956, "latdebut": 45.75529, "longeffective": 4.84574, "idvehicule": "246veh", "latarrivee": 45.83492, "nodemande": 42537000003, "longarrivee": 4.84762}, {"noadresse": 42537007718, "nousager": 42537002720, "lateffective": 45.83955, "longdebut": 4.84574, "latdebut": 45.83675, "longeffective": 4.83098, "idvehicule": "246veh", "latarrivee": 45.83935, "nodemande": 42537000004, "longarrivee": 4.83084}, {"noadresse": 42537005803, "nousager": 42537002424, "lateffective": 45.98730, "longdebut": 4.83098, "latdebut": 45.83955, "longeffective": 4.72695, "idvehicule": "246veh", "latarrivee": 45.98174, "nodemande": 42537000006, "longarrivee": 4.73942}, {"noadresse": 42537005803, "nousager": 42537003576, "lateffective": 45.98730, "longdebut": 4.83098, "latdebut": 45.83955, "longeffective": 4.72695, "idvehicule": "246veh", "latarrivee": 45.98174, "nodemande": 42537000005, "longarrivee": 4.73942}, {"noadresse": 42537004215, "nousager": 42537003576, "lateffective": 45.93778, "longdebut": 4.72695, "latdebut": 45.9873, "longeffective": 4.62676, "idvehicule": "246veh", "latarrivee": 45.93784, "nodemande": 42537000005, "longarrivee": 4.62625}, {"noadresse": 42537004215, "nousager": 42537002424, "lateffective": 45.93778, "longdebut": 4.72695, "latdebut": 45.9873, "longeffective": 4.62676, "idvehicule": "246veh", "latarrivee": 45.93784, "nodemande": 42537000006, "longarrivee": 4.62625}, {"noadresse": 42537004215, "nousager": 42537002720, "lateffective": 45.93778, "longdebut": 4.72695, "latdebut": 45.9873, "longeffective": 4.62676, "idvehicule": "246veh", "latarrivee": 45.93784, "nodemande": 42537000004, "longarrivee": 4.62625}, {"noadresse": 42537004215, "nousager": 42537000086, "lateffective": 45.93778, "longdebut": 4.72695, "latdebut": 45.9873, "longeffective": 4.62676, "idvehicule": "246veh", "latarrivee": 45.93784, "nodemande": 42537000003, "longarrivee": 4.62625}, {"noadresse": 42537000007, "nousager": 42537002425, "lateffective": 45.72941, "longdebut": 4.77845, "latdebut": 45.77335, "longeffective": 4.88396, "idvehicule": "164veh", "latarrivee": 45.72815, "nodemande": 42537000070, "longarrivee": 4.88241}, {"noadresse": 42537000007, "nousager": 42537002425, "lateffective": 45.69349, "longdebut": 4.88396, "latdebut": 45.72941, "longeffective": 4.94466, "idvehicule": "164veh", "latarrivee": 45.69429, "nodemande": 42537000070, "longarrivee": 4.94216}]
and use code achieve this:
import json xlrd import open_workbook book = open_workbook('forum.xlsx') sheet = book.sheet_by_index(0) keys = [sheet.cell(0,col_index).value col_index in xrange(sheet.ncols)] dict_list = [] row_index in xrange(1,sheet.nrows): d = {keys[col_index]: sheet.cell(row_index,col_index).value col_index in xrange(sheet.ncols)} dict_list.append(d) j = json.dumps(dict_list) open('data.json','w') f: f.write(j)
then want make geojson file in form:
{ "type": "featurecollection", "features": [{ "type": "feature", "geometry": { "type": "linestring", "coordinates": [[latdebut, longdebut],[lateffective,longeffective]] }, "properties": { "noadresse": "xxx", "nousager": "xxx", "nodemand":"xxx", "idvehicule":"xxx" } }, { ... }] }
i don't know how , if there way directly convert excel geojson file. besides, want add property "tour", changes every time "idvehicule" changes. know it's ask i'm stuck long , appreciated.
thanks
so if know how "massage" dict wanted final structure - moving "other elements" properties member - think simple
# work on dict_list produce dict_in_geojson_form # ... then: open('data.json','w') f: json.dump(dict_in_geojson_form, f)
in case serialization still unclear, might want @ python dump dict json file or how write json data file in python? ...
advanced update: per amended request simple transformations , tour enumeration added. please see below sample code (split in different code boxes not have scroll ...:
#! /usr/bin/env python """map specific stream seom geojson target structure , stream output in chunks elements complete (any new vehicle id increments tour label works enumeration in properties of features geojson representation).""" __future__ import print_function import copy import json import sys
# declarations confine literals , prepare structure
no_address = 'noadresse' no_usage = 'nousager' id_vehicle = 'idvehicule' no_demand = 'nodemande' tour_label = 'tour' property_keys = (no_address, no_usage, id_vehicle, no_demand) lat_debut = 'latdebut' long_debut = 'longdebut' lat_effective = 'lateffective' long_effective = 'longeffective' coord_keys_debut = (lat_debut, long_debut) coord_keys_effective = (lat_effective, long_effective) properties_key = 'properties' geometry_key = 'geometry' coordinates_key = 'coordinates' features_key = 'features' geojson_frame_prefix = """{ "type": "featurecollection", "features": [ """ feature_template = { "type": "feature", geometry_key: { "type": "linestring", "coordinates": [] }, "properties": { no_address: none, no_usage: none, no_demand: none, id_vehicle: none, tour_label: none }} geojson_list_sep = ',\n' geojson_frame_postfix = ']\n}\n'
# simple feature emitting source mock
def event_source(): """sample input line source generator. might yielding python dictionaries matching "this questions" event specification.""" event_seq = [ {no_address: 42537006584, no_usage: 42537000086, lat_effective: 45.83675, long_debut: 4.91956, lat_debut: 45.75529, long_effective: 4.84574, id_vehicle: "246veh", "latarrivee": 45.83492, no_demand: 42537000003, "longarrivee": 4.84762}, {no_address: 42537007718, no_usage: 42537002720, lat_effective: 45.83955, long_debut: 4.84574, lat_debut: 45.83675, long_effective: 4.83098, id_vehicle: "246veh", "latarrivee": 45.83935, no_demand: 42537000004, "longarrivee": 4.83084}, # ... {no_address: 42537000007, no_usage: 42537002425, lat_effective: 45.69349, long_debut: 4.88396, lat_debut: 45.72941, long_effective: 4.94466, id_vehicle: "164veh", "latarrivee": 45.69429, no_demand: 42537000070, "longarrivee": 4.94216}] event in event_seq: yield event
# context free transformations:
def feature_from(event): """transform event feature, applying "business" rules.""" feature = copy.deepcopy(feature_template) property_key in property_keys: feature[properties_key][property_key] = event[property_key] coords_debut = [event[k] k in coord_keys_debut] coords_effective = [event[k] k in coord_keys_effective] feature[geometry_key][coordinates_key].append(coords_debut) feature[geometry_key][coordinates_key].append(coords_effective) return feature
# separated emitter of features (overkill here, ...)
def feature_gen(events): """generator creates features events (might place hook validty checks in real lif processing).""" event in events: yield feature_from(event)
# context aware generator feature sequences sharing tour
def tour_gen(features): """generator emits feature in chunks per complete tour detected change in vehicle id.""" id_vehicle_active = none tour_enumeration = none feature in features: id_vehicle_received = feature[properties_key][id_vehicle] if id_vehicle_active none: id_vehicle_active = id_vehicle_received tour_enumeration = 1 tour = [] if id_vehicle_active != id_vehicle_received: yield tour tour = [] tour_enumeration += 1 id_vehicle_active = id_vehicle_received feature[properties_key][tour_label] = tour_enumeration tour.append(feature) else: feature[properties_key][tour_label] = tour_enumeration tour.append(feature) if tour: yield tour
# mock function channel output stdout or file ...
def geojson_out(text, stream=sys.stdout): """expected json text output here.""" stream.write(text)
# sample processing logic driving transformation
def main(): """poor man's streaming falls on hardcoded geojson "frame" string pre , post fix feature stream. latter elements accumulated in chunks carrying common "tour" enumeration label. frame structure in python lingo is: geo_dict = {"type": "featurecollection", "features": []} features injected in list , in implementation need stateful separator injection hack, yield valid json (which not allow trailing comma after last array elememt). """
# ... here sample writing hard coded file path
open('yumyum.geojson', 'wt') f_out: geojson_out(geojson_frame_prefix, stream=f_out) json_array_nanny_needed = false # semi-auto, means semi-manual =( features in tour_gen(feature_gen(event_source())): feature in features: if json_array_nanny_needed: geojson_out(geojson_list_sep, stream=f_out) geojson_out(json.dumps(feature, sort_keys=true), stream=f_out) json_array_nanny_needed = true # hack did ack geojson_out(geojson_frame_postfix, stream=f_out) if __name__ == '__main__': main()
the idea in processing should in docstrings ...
one collect/aggregate/filter coordinates per id of vehicle, or ... it's python general purpose programming language ;-)
on machine output inside yumyum.txt is:
$ cat yumyum.geojson { "type": "featurecollection", "features": [ {"geometry": {"coordinates": [[45.75529, 4.91956], [45.83675, 4.84574]], "type": "linestring"}, "properties": {"idvehicule": "246veh", "noadresse": 42537006584, "nodemande": 42537000003, "nousager": 42537000086, "tour": 1}, "type": "feature"}, {"geometry": {"coordinates": [[45.83675, 4.84574], [45.83955, 4.83098]], "type": "linestring"}, "properties": {"idvehicule": "246veh", "noadresse": 42537007718, "nodemande": 42537000004, "nousager": 42537002720, "tour": 1}, "type": "feature"}, {"geometry": {"coordinates": [[45.72941, 4.88396], [45.69349, 4.94466]], "type": "linestring"}, "properties": {"idvehicule": "164veh", "noadresse": 42537000007, "nodemande": 42537000070, "nousager": 42537002425, "tour": 2}, "type": "feature"}] }
Comments
Post a Comment