首页 > 解决方案 > 如何从字典列表创建 .geojson?

问题描述

我在使用其他人所做的相同代码时遇到问题,但问题不同。有人可以向我解释几何部分是如何工作的吗?我想要的只是点数和字典列表。我需要输入任何其他信息吗?看起来它不喜欢 lat 和 lon 的浮动。

def convert_to_geojson(my_list): """ 此函数将字典列表转换为 GeoJSON 格式 它至少需要字典中的 "lon" 和 "lat" 键 但可能包含任何其他附加元数据 例如 my_list = [{" lat": 30.321, "lon": 20}] :param my_list: 字典列表 :return: GeoJSON 字符串 """

feature_list = []
for d in my_list:
    
    the_geom = {"type": "Point", "coordinates": [d["lon"], d["lat"]]}
    feature = {"type": "Feature", "geometry": the_geom, "properties": d}
    feature_list.append(feature)

feature_collection = {"type": "FeatureCollection", "features": feature_list}

return json.dumps(feature_collection)

the_list = [{“lat”:-32.038607,“lon”:115.893494,“位置”:“Riverton”},{“lat”:-32.064277,“lon”:115.79581,“acc_ID”:8063399,“严重性”: “主要”,“类型”:“交叉点”,“年份”:2012,“最远”:“否”},{“纬度”:-31.791455,“经度”:116.050658,“acc_ID”:8063291,“严重性” :“医院”,“类型”:“Midblock”,“年份”:2012,“最远”:“是”}]

convert_to_geojson(the_list)

导入 json,GeojsonConverter_3

# Now call the function, give it the list and assign what it returns to a variable
the_string = GeojsonConverter_3.convert_to_geojson(the_list)
# Print the GeoJSON to screen so you can see it
print(the_string)

file_pointer = open("GeoJSON_File.geojson", "w")

file_pointer.write(the_string)

结果

 37 
 38     except Exception as e:

---> 39 raise e 40 41 else:

24 try: 25 for d in my_list: ---> 26 if(d["lon"].replace(" ", "") is not "") and (d["lat"].replace(" ", "") 不是 ""): 27 float(d["lon"]) 28 float(d["lat"])

AttributeError:“浮动”对象没有属性“替换”

标签: pythongeojsonconverters

解决方案


推荐阅读