- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想将 Excel 文件转换为 geojson 文件。字典是这样的:
[
{"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}]
我使用这段代码来实现这一点:
import json
from xlrd import open_workbook
book = open_workbook('forum.xlsx')
sheet = book.sheet_by_index(0)
keys = [sheet.cell(0,col_index).value for col_index in xrange(sheet.ncols)]
dict_list = []
for row_index in xrange(1,sheet.nrows):
d = {keys[col_index]: sheet.cell(row_index,col_index).value
for col_index in xrange(sheet.ncols)}
dict_list.append(d)
j = json.dumps(dict_list)
with open('data.json','w') as f:
f.write(j)
然后我想将其放入以下形式的 geojson 文件中:
{ "type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [[LatDebut, LongDebut],[LatEffective,LongEffective]]
},
"properties": {
"NoAdresse": "XXX",
"NoUsager": "XXX",
"NoDemand":"XXX",
"IdVehicule":"XXX"
}
}, {
...
}]
}
我不知道该怎么做,也不知道是否有另一种方法可以直接从 Excel 转换为 geojson 文件。此外,我想添加一个属性“Tour”,每次“IdVehicle”更改时它都会更改。我知道这个要求太多了,但我被困了这么久,任何帮助将不胜感激。
谢谢
最佳答案
因此,如果您知道如何将字典“按摩”到您想要的最终结构中 - 将所有“其他元素”移动到某些属性成员中 - 那么我认为它就像
# work on dict_list to produce some dict_in_geojson_form
# ... then:
with open('data.json','w') as f:
json.dump(dict_in_geojson_form, f)
如果序列化仍然不清楚,您可能还想看看 Python dump dict to json file或How do I write JSON data to a file in Python? ...
高级更新:根据修改后的请求进行一些简单的转换并添加了游览枚举。请参阅下面的示例代码(拆分在不同的代码框中,以便不必滚动...:
#! /usr/bin/env python
"""Map a specific stream to seom GeoJSON target structure and stream the
output in chunks as elements are complete (any new vehicle id increments
the tour label which works like an enumeration in the properties of the
features GeoJSON representation)."""
from __future__ import print_function
import copy
import json
import sys
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'
def event_source():
"""Sample input line source generator. Might anything 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}]
for event in event_seq:
yield event
def feature_from(event):
"""Transform event to feature, applying the "business" rules."""
feature = copy.deepcopy(FEATURE_TEMPLATE)
for property_key in PROPERTY_KEYS:
feature[PROPERTIES_KEY][property_key] = event[property_key]
coords_debut = [event[k] for k in COORD_KEYS_DEBUT]
coords_effective = [event[k] for k in COORD_KEYS_EFFECTIVE]
feature[GEOMETRY_KEY][COORDINATES_KEY].append(coords_debut)
feature[GEOMETRY_KEY][COORDINATES_KEY].append(coords_effective)
return feature
def feature_gen(events):
"""Generator creates features from events (might be a good place
to hook into validty checks in real lif processing)."""
for event in events:
yield feature_from(event)
def tour_gen(features):
"""Generator emits the feature in chunks per complete tour as detected
by a change in vehicle id."""
id_vehicle_active = None
tour_enumeration = None
for feature in features:
id_vehicle_received = feature[PROPERTIES_KEY][ID_VEHICLE]
if id_vehicle_active is 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
def geojson_out(text, stream=sys.stdout):
"""Expected JSON text is output here."""
stream.write(text)
def main():
"""Poor man's streaming falls back on hardcoded GeoJSON "frame"
string as pre and post fix to the feature stream. the latter
elements are accumulated in chunks carrying common "tour"
enumeration label.
The frame structure in Python lingo is:
geo_dict = {"type": "FeatureCollection", "features": []}
The features will be injected in the list and in this implementation
need some stateful separator injection hack, to yield valid JSON
(which does not allow trailing comma after last array elememt).
"""
with open('yumyum.geojson', 'wt') as f_out:
geojson_out(GEOJSON_FRAME_PREFIX, stream=f_out)
json_array_nanny_needed = False # Semi-auto, means semi-manual =(
for features in tour_gen(feature_gen(event_source())):
for 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 A DID ACK
geojson_out(GEOJSON_FRAME_POSTFIX, stream=f_out)
if __name__ == '__main__':
main()
处理的想法应该在文档字符串中......
人们还可以收集/聚合/过滤每个车辆 ID 的坐标,或者......它是 Python 一种通用编程语言;-)
在我的机器上 yumyum.txt 内的输出是:
$ 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"}]
}
关于python - 如何将 python dict 转换为标准形式的 geojson 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38072738/
我最近在读 CSAPP。在 10.9 节中,它说标准 I/O 不应该与 socket 一起使用,原因如下: (1) The restrictions of standard I/O Restricti
似乎是一个足够标准的问题,可以保证解决方案中的标准设计: 假设我想在文件中写入 x+2(或更少)个字符串。 x 字符串构成一个部分的内容,这两个字符串构成该部分的页眉和页脚。要注意的是,如果内容中没有
代码版本管理 在项目中,代码的版本管理非常重要。每个需求版本的代码开发在版本控制里都应该经过以下几个步骤。 在master分支中拉取该需求版本的两个分支,一个feature分支,
我有以下sql查询,我需要获取相应的hibernate条件查询 SELECT COUNT(DISTINCT employee_id) FROM erp_hr_payment WHERE payment
所以我正在编写一些代码,并且最近遇到了实现一些 mixin 的需要。我的问题是,设计混音的正确方法是什么?我将使用下面的示例代码来说明我的确切查询。 class Projectile(Movable,
我的环境变量包含如下双引号: $echo $CONNECT_SASL_JAAS_CONFIG org.apache.kafka.common.security.plain.PlainLoginModu
示例: /** * This function will determine whether or not one string starts with another string. * @pa
有没有办法在 Grails 中做一个不区分大小写的 in 子句? 我有这个: "in"("name", filters.tags) 我希望它忽略大小写。我想我可以做一个 sqlRestriction
我搜索了很长时间,以查找将哪些boost库添加到std库中,但是我只找到了一个新库的完整列表(如此处:http://open-std.org/jtc1/sc22/wg21/docs/library_t
我已经通过使用这个肮脏的黑客解决了我的问题: ' Filter managerial functions ActiveSheet.Range("$A$1:$BW$2211").Auto
因此,我很难理解我需要遵循的标准,以便我的 Java 程序能够嵌入 HTML。我是否只需将我的主类扩展到 Applet 类,或者我还需要做更多的事情吗?另外,在我见过的每个 Applet 示例中,它都
我对在 Hibernate 中使用限制有疑问。 我必须创建条件,设置一些限制,然后选择日期字段最大值的记录: Criteria query = session.createCriteria(Stora
我有标准: ICriteria criteria = Session.CreateCriteria() .SetFetchMode("Entity1", FetchMo
我很难编写条件来选择所有子集合或孙集合为空的实体。我可以将这些作为单独的条件来执行,但我无法将其组合成一个条件。 类结构: public class Component { p
@Entity class A { @ManyToMany private List list; ... } @Entity class B { ... } 我想使用条件(不是 sql 查询)从 A
我的数据库中有以下表结构: Table A: Table B: Table C: _______________
请帮助我: 我有下一张 table : 单位 ID 姓名 用户 ID 姓名 利率 单位 ID 用户 ID 我不明白如何从 SQL 创建正确的条件结构: 代码: SELECT * FROM Unit W
我正在构建一个包含项目的网站,每个项目都有一个页面,例如: website.com/book/123 website.com/film/456 website.com/game/789 每个项目都可以
我需要使用两个属性的组合来过滤结果列表。一个简单的 SQL 语句如下所示: SELECT TOP 10 * FROM Person WHERE FirstName + ' ' + LastName L
我有一个“ super 实体”SuperEntity 和三个扩展父类(super class)的实体 ChildEntity1、...、ChildEntity3。 搜索数据库中的所有实体很容易,即我们
我是一名优秀的程序员,十分优秀!