python - 如何从字典列表创建 .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:“浮动”对象没有属性“替换”
解决方案
推荐阅读
- amazon-web-services - AWS 错误消息:使用 AWS KMS 托管密钥指定服务器端加密的请求需要 AWS 签名版本 4
- python - 如何在渲染页面之前进行过滤?
- c++ - C++ 2D 数组和函数
- flowtype - 我可以在 Flow 中使用 JSDoc 注释来强制按文件输入吗?
- unison-lang - 将文本列表统一映射到其字母数
- ansible - Ansible wait_for_connection 直到主机准备好使用ansible?
- python - 我无法从本地机器上生成的 url 访问我的烧瓶应用程序
- solr - 如何在 Solr 中使用 Config API 添加具有多个字典的建议器组件?
- java - SpringBoot Bean Validation 注解
- r - 使用for循环从r中的数据框中删除特定列