如何从转为GeoDataFrame的.geojson中获取timestamp等顶层属性?
提取GeoJSON顶层属性的高效方法
直接用geopandas.read_file()确实只会解析features节点的内容,顶层的timestamp这类属性不会自动存入GeoDataFrame。不用重新加载整个JSON文件,借助geopandas依赖的底层库fiona就能高效解决:
方法一:一次打开文件同时获取属性和GeoDataFrame
import fiona import geopandas as gpd with fiona.open("your_file.geojson") as src: # 提取顶层的timestamp属性 timestamp = src.extra.get("timestamp") # 同时读取GeoDataFrame,无需重复加载文件 gdf = gpd.GeoDataFrame.from_features(src, crs=src.crs)
这种方式只打开一次文件,既拿到了顶层属性,又生成了GeoDataFrame,完全避免重复读取的资源浪费。
方法二:已生成GeoDataFrame后补取属性
如果已经用read_file()生成了GeoDataFrame,也可以通过其内部的fiona读取器获取属性(注意:_reader是内部属性,后续版本可能变动,谨慎使用):
import geopandas as gpd gdf = gpd.read_file("your_file.geojson") with gdf._reader as src: timestamp = src.extra.get("timestamp")
两种方法都能直接拿到Overpass API返回的GeoJSON顶层timestamp、generator等属性,不用额外加载整个JSON文件。
内容的提问来源于stack exchange,提问作者jnhck
相关产品推荐
相关产品推荐

