简体   繁体   English

将具有 featurecollection 的 pandas 列转换为 GeoJSON

[英]Convert pandas column with featurecollection to GeoJSON

I downloaded a CSV that contains a column which has a GeoJSON format, and imported it as a pandas dataframe. How can I convert this to a GeoJSON (.geojson)?我下载了一个 CSV,其中包含一个具有 GeoJSON 格式的列,并将其导入为 pandas dataframe。如何将其转换为 GeoJSON (.geojson)? I have about 10,000 rows, each with information as shown below:我有大约 10,000 行,每行的信息如下所示:

This is an example of a cell in the column: {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-0.0903517,9.488375],[-0.0905786,9.488523],[-0.0909767,9.48913],[-0.09122,9.4895258],[-0.0909733,9.4901503],[-0.0908833,9.4906802],[-0.0906984,9.4905612],[-0.0907146,9.4898184],[-0.090649,9.4895175],[-0.0907516,9.489142],[-0.0906146,9.4889654],[-0.0903517,9.488375]]]},"properties":{"pointCount":"11","length":"502.9413","area":"8043.091133117676"}}]}这是列中单元格的示例:{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates" :[[[[ - 0.0903517,9.488375],[ - 0.0905786,9.488523],[ - 0.0909767,9.48913],[-0.09122,9912,4895258] ],[-0.0907146,9.4898184],[-0.090649,9.4895175],[-0.0907516,9.489142],[-0.0906146,9.4889654],[-0.0903517,9.488375]]]},"属性":{"pointCount":" 11","长度":"502.9413","面积":"8043.091133117676"}}]}

Overview of my pandas dataframe print now:                             site_registration_gps_area  ...                   geometry
11    {"type":"FeatureCollection","features":[{"type...  ...  POINT (-76.75880 2.38031)
14    {"type":"FeatureCollection","features":[{"type...  ...  POINT (-76.73718 2.33163)
40    {"type":"FeatureCollection","features":[{"type...  ...   POINT (-0.15727 9.69560)
42    {"type":"FeatureCollection","features":[{"type...  ...   POINT (-0.11686 9.65522)
44    {"type":"FeatureCollection","features":[{"type...  ...   POINT (-0.10379 9.65226)

I would suggest to use GeoPandas for this task.我建议使用GeoPandas来完成这项任务。 The simplest solution would be to read your CSV file directly with GeoPandas using a GeoDataFrame .最简单的解决方案是使用GeoDataFrame直接使用GeoPandas读取 CSV 文件。 If you do this you need something like this:如果你这样做,你需要这样的东西:

import geopandas as gpd

# read the CSV file into a GeoDataFrame
gdf = gpd.read_file('myFile.csv')

# convert the GeoDataFrame to a geojson object
geo_json = gdf.to_json()

# however if the objects become very big, store the GeoDataFrame to a .geojson file
gdf.to_file('path', driver='GeoJSON')

However, if you work with the DataFrame from Pandas you can convert it to a GeoDataFrame and then use the same steps as in the above snippet.但是,如果您使用DataFrame中的Pandas ,您可以将其转换为GeoDataFrame ,然后使用与上述代码段相同的步骤。

import pandas as pd
import geopandas as gpd

# your code here ...

# extract the geojson column as a list 
geo_j_list = df['geometry'].tolist()

# temporary list for GeoDataFrames
gdfs = []

# iterate over the geojson objects in the list
for geom in geo_j_list:
    gdfs.append(gpd.GeoDataFrame.from_features(geom['features']))

# merge the list of GeoDataFrames into one
gdf = gpd.GeoDataFrame(pd.concat(gdfs, ignore_index=True))

# convert the gdf to a json object
geo_json = gdf.to_json()

# store the gdf in a geojson file
gdf.to_file('path', driver='GeoJSON')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM