[英]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)
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.如果您的几何数据格式不同(
shapefile
、 geopackage
),您可以先将其保存到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.