简体   繁体   English

使用 python 将 geopandas dataframe 转换为 GEE 特征集合

[英]Convert geopandas dataframe to GEE feature collection using python

Given a geopandas dataframe (eg df that contains a geometry field), is the following a simplest way to convert it into ee.FeatureCollection?给定一个geopandas dataframe(例如包含几何字段的df),以下是将其转换为ee.FeatureCollection的最简单方法吗?

import ee
features=[]
for index, row in df.iterrows():
    g=ee.Geometry.Point([row['geometry'].x,row['geometry'].y])
    # Define feature with a geometry and 'name' field from the dataframe
    feature = ee.Feature(g,{'name':ee.String(row['name'])})
    features.append(feature)
fc = ee.FeatureCollection(features)

If you want convert points geodataframe (GeoPandas) to ee.FeatureCollection, you can use this function:如果要将点地理数据框 (GeoPandas) 转换为 ee.FeatureCollection,可以使用此函数:

import geopandas as gpd
import numpy as np
from functools import reduce
from geopandas import GeoDataFrame
from shapely.geometry import Point,Polygon

def make_points(gdf):
    g = [i for i in gdf.geometry]
    features=[]
    for i in range(len(g)):
        g = [i for i in gdf.geometry]
        x,y = g[i].coords.xy
        cords = np.dstack((x,y)).tolist()
        double_list = reduce(lambda x,y: x+y, cords)
        single_list = reduce(lambda x,y: x+y, double_list)

        g=ee.Geometry.Point(single_list)
        feature = ee.Feature(g)
        features.append(feature)
        #print("done")
        ee_object = ee.FeatureCollection(features)
    return ee_object

points_features_collections = make_points(points_gdf)

to do this function I based on this Reference做这个功能我基于这个参考

You can build a FeatureCollection from a json object.您可以从json object 构建FeatureCollection So if your geometry data file type is GeoJson you can do the following:因此,如果您的几何数据文件类型是GeoJson ,您可以执行以下操作:

# import libraries
import ee
import json

# initialize earth engine client
ee.Initialize()

# load your gemotry data (which should be in GeoJson file)
with open("my_geometry_data.geojson") as f:
    geojson = json.load(f)

# construct a FeatureCollection object from the json object
fc = ee.FeatureCollection(geojson)

If your geometry data is in different format ( shapefile , geopackage ), you can first save it to GeoJson then build a FeatureCollection object.如果您的几何数据格式不同( shapefilegeopackage ),您可以先将其保存到GeoJson ,然后构建FeatureCollection object。

Finally, if you don't want to write any conversion code, and want just to convert your Geopandas.GeoDataFrame instantly to ee.FeatureCollection you can use the python package: geemap最后,如果您不想编写任何转换代码,只想将Geopandas.GeoDataFrame立即转换为ee.FeatureCollection ,您可以使用 python package: geemap

geemap has several function for converting geometry data to FeatureCollection , and vice versa. geemap有几个 function 用于将几何数据转换为FeatureCollection ,反之亦然。 You can see examples here .您可以在此处查看示例。 In your case, you need to use the geopandas_to_ee function, so your code would look like this:在您的情况下,您需要使用geopandas_to_ee function,因此您的代码如下所示:

# import libraries
import ee
import geemap
import geopandas as gpd

# initialize earth engine client
ee.Initialize()

# load your gemotry data using GeoPandas (which can be stored in different formats)
gdf = gpd.read_file("my_geometry_file.geojson")

# convert to FeatureCollection using one line of code
fc = geemap.geopandas_to_ee(gdf)

Note that under the hood, geemap is converting the GeoDataFrame to a json file, and then following the first approach I mentioned above.请注意,在后台, geemap正在将GeoDataFrame转换为json文件,然后遵循我上面提到的第一种方法。

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

相关问题 将地球引擎特征集合转换为 python 中的 dataframe - Convert earth engine feature collection to a dataframe in python Python:如何将字典列表转换为地理熊猫 dataframe? - Python: how to convert a list of dictionary to a geopandas dataframe? 使用Python脚本将'String Feature'数据框架转换为Azure ML中的Float - Convert `String Feature` DataFrame into Float in Azure ML Using Python Script 使用 Python API 对 GEE 图像集合进行阈值处理 - Thresholding a GEE Image Collection with Python API 在python中使用geopandas将数据帧保存到shapefile会引发bool的ValueError - Saving dataframe to shapefile using geopandas in python raises ValueError for bool 使用 geopandas (Python) 从空间 dataframe 进行空间分箱 - Spatial binning from a spatial dataframe using geopandas (Python) Geopandas:如何读取 csv 并转换为带有多边形的 geopandas dataframe? - Geopandas: how to read a csv and convert to a geopandas dataframe with polygons? 使用以下 function 将 GEE javascript 转换为 Python 脚本时出现错误 - Using the below function I am getting an error when I convert the GEE javascript to Python script 使用 plotly 绘制地理熊猫 dataframe - Plotting a geopandas dataframe using plotly Python:如何将 geotiff 转换为 geopandas? - Python: how to convert geotiff to geopandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM