我想将几个Json对象与LineStrings
组合成一个GeoJSON特征集合例如,我有以下格式错误的json对象:
{"lat":16.0269337,"lon":40.073042,"score":1,"ID":"13800006252028","TYPES":"Regional","N2C":"2","NAME":"Strada Statale della Val Sinni"}
{"lat":16.0316522,"lon":41.6808931,"score":1,"ID":"13800006260148","TYPES":"Scenic","N2C":"2"}
{"lat":16.0316523,"lon":41.6808932,"score":1,"ID":"13800006260148","TYPES":"Scenic","N2C":"2","NAME":"Giro del Gargano"}
{"lat":16.0316522,"lon":41.6808931,"score":1,"ID":"13800006203620","TYPES":"Scenic","N2C":"2"}
{"lat":16.0316522,"lon":41.6808931,"score":1,"ID":"13800006203620","TYPES":"Regional","N2C":"2","NAME":"Giro del Gargano"}
{"lat":16.032322,"lon":40.6097567,"score":1,"ID":"13800005963252","TYPES":"Scenic","N2C":"2"}
{"lat":16.032322,"lon":40.6097567,"score":1,"ID":"13800005963252","TYPES":"Regional","N2C":"2","NAME":"Via Appia"}
我的目标是做两件事
使用ID创建GeoJson线串,如果ID和&相同的类型是相同的
根据得分属性添加匹配的json文件的得分。
答案 0 :(得分:0)
我不是专家,但这是第一次削减。 This post有助于获得基础知识。对于每个“Geo”,“JSON”和pandas操作部件,可能有比我更优雅的方法。
目前还不能确定您的数据的确切格式,但是如果它真的是使用pd.DataFrame
的字典列表,或者直接从JSON文件中使用{{1},您将能够直接导入它。 }}。我只是和前者一起工作,但这很容易改变。
pd.from_json
然后使用pandas来操作数据并使用geojson包创建LineString对象
json_objs = [{"lat":16.0269337,"lon":40.073042,"score":1,"ID":"13800006252028","TYPES":"Regional","N2C":"2","NAME":"Strada Statale della Val Sinni"},
{"lat":16.0316522,"lon":41.6808931,"score":1,"ID":"13800006260148","TYPES":"Scenic","N2C":"2"},
{"lat":16.0316523,"lon":41.6808932,"score":1,"ID":"13800006260148","TYPES":"Scenic","N2C":"2","NAME":"Giro del Gargano"},
{"lat":16.0316522,"lon":41.6808931,"score":1,"ID":"13800006203620","TYPES":"Scenic","N2C":"2"},
{"lat":16.0316522,"lon":41.6808931,"score":1,"ID":"13800006203620","TYPES":"Regional","N2C":"2","NAME":"Giro del Gargano"},
{"lat":16.032322,"lon":40.6097567,"score":1,"ID":"13800005963252","TYPES":"Scenic","N2C":"2"},
{"lat":16.032322,"lon":40.6097567,"score":1,"ID":"13800005963252","TYPES":"Regional","N2C":"2","NAME":"Via Appia"}
输出:
import geojson
import pandas as pd
df = pd.DataFrame(json_objs)
grouped = df.groupby(["ID","TYPES"])
features = []
for name,indices in grouped.groups.items():
group = df.iloc[indices]
score = group["score"].sum()
points = list(zip(group["lat"].values,group["lon"].values))
properties = {"ID": name[0], "record_type": name[1], "score": score}
ls = geojson.LineString(coordinates = points, properties = properties)
print(ls)
features.append(ls)
然后将{"coordinates": [[16.032322, 40.6097567]], "properties": {"ID": "13800005963252", "record_type": "Scenic", "score": 1}, "type": "LineString"}
{"coordinates": [[16.0269337, 40.073042]], "properties": {"ID": "13800006252028", "record_type": "Regional", "score": 1}, "type": "LineString"}
{"coordinates": [[16.032322, 40.6097567]], "properties": {"ID": "13800005963252", "record_type": "Regional", "score": 1}, "type": "LineString"}
{"coordinates": [[16.0316522, 41.6808931], [16.0316523, 41.6808932]], "properties": {"ID": "13800006260148", "record_type": "Scenic", "score": 2}, "type": "LineString"}
{"coordinates": [[16.0316522, 41.6808931]], "properties": {"ID": "13800006203620", "record_type": "Scenic", "score": 1}, "type": "LineString"}
{"coordinates": [[16.0316522, 41.6808931]], "properties": {"ID": "13800006203620", "record_type": "Regional", "score": 1}, "type": "LineString"}
转换为LineStrings
:
FeatureCollection
显然你也可以手动构建这种格式,但我希望feature = geojson.FeatureCollection(features)
为你提供额外的灵活性。显然还有一个我无法安装的geojson
包。