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)

 参考: https://blog.csdn.net/MLH7M/article/details/121024141?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-121024141-blog-110056901.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-121024141-blog-110056901.pc_relevant_default&utm_relevant_index=7

 通过 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://geojson.org/

参考:https://www.jianshu.com/p/852d7ad081b3

 

posted on 2022-02-22 12:05  闹不机米  阅读(771)  评论(0编辑  收藏  举报

导航