geojson 文件生成
线要素 (注意大括号后面的多余逗号要去掉,不然QGIS会打不开)
{"type": "FeatureCollection", "features": [ {"type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [115.1866, 30.9716], [114.8311, 30.8155], [114.7447, 30.3861], [114.6661, 29.9919], [115.1036, 29.9252], [115.5574, 30.2716], [115.5325, 30.8211], [115.1866, 30.9716] ] } } ] }
多点要素
{"type": "FeatureCollection", "features": [ {"type": "Feature", "geometry": { "type": "MultiPoint", "coordinates": [ [114.7516, 30.1502], [114.9844, 30.6019], [115.2669, 30.4338] ] } } ] }
点要素
{"type": "FeatureCollection", "features": [ {"type": "Feature", "geometry": { "type": "Point", "coordinates": [114.7447, 30.3861] } } ] }
通过代码把数据生成 geojson 文件
"""生成 Geojson""" import json import pandas as pd from geojson import Feature, FeatureCollection, Point ddcit = {'lat': lat.flatten(), 'lon': lon.flatten(), 'val': data.flatten()} df = pd.DataFrame(ddcit) print(df) print(df['val'][np.isnan(df['val'])]) df.drop(index=df['val'][np.isnan(df['val'])].index.tolist(), inplace=True) # 注意数据值,不能有 nan print(df) # columns used for constructing geojson object features = df.apply( lambda row: Feature(geometry=Point((float(row['lon']), float(row['lat'])))), axis=1).tolist() print(features) # all the other columns used as properties properties = df.drop(['lat', 'lon'], axis=1).to_dict('records') print(properties) # whole geojson object feature_collection = FeatureCollection(features=features, properties=properties) print(feature_collection) with open('file.geojson', 'w', encoding='utf-8') as f: json.dump(feature_collection, f, ensure_ascii=False)
通过 geopandas 生成 geojson
import geopandas ss = np.stack((lon.flatten(), lat.flatten()), 1) ss1 = [Point(ss[0].tolist()) for i in ss] print(ss) print(ss1) cq = geopandas.GeoDataFrame({'val': data.flatten(), 'geometry': ss1}, crs='EPSG:4326') print(cq) cq.to_file('output.geojson', driver='GeoJSON', # 默认 geojson 可以修改成 ESRI Shapefile encoding='utf-8')
参考:https://www.jianshu.com/p/852d7ad081b3
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!