In Earth Engine python, how to create a ee.FeatureCollection with a number of points, whose coordinates are obtained from a dataframe
To create an ee.FeatureCollection
in Google Earth Engine (GEE) using Python, where the coordinates of the points are obtained from a pandas DataFrame, you can follow these steps:
ee.Feature
objects.ee.FeatureCollection
from the list of ee.Feature
objects.Here's a step-by-step example:
import ee
import pandas as pd
# Initialize the Earth Engine API
ee.Initialize()
# Example DataFrame with coordinates
data = {
'latitude': [37.7749, 34.0522, 40.7128],
'longitude': [-122.4194, -118.2437, -74.0060],
'name': ['San Francisco', 'Los Angeles', 'New York']
}
df = pd.DataFrame(data)
# Function to convert a row of the DataFrame to an ee.Feature
def row_to_feature(row):
point = ee.Geometry.Point([row['longitude'], row['latitude']])
return ee.Feature(point, {'name': row['name']})
# Convert the DataFrame to a list of ee.Feature objects
features = df.apply(row_to_feature, axis=1).tolist()
# Create an ee.FeatureCollection from the list of ee.Feature objects
feature_collection = ee.FeatureCollection(features)
# Print the FeatureCollection to verify
print(feature_collection.getInfo())
ee
module for Earth Engine and pandas
for handling the DataFrame.ee.Initialize()
to initialize the Earth Engine API.row_to_feature
that takes a row of the DataFrame and converts it to an ee.Feature
with a Point
geometry and properties.apply
method to apply the row_to_feature
function to each row of the DataFrame, converting it to a list of ee.Feature
objects.ee.FeatureCollection
to create a FeatureCollection from the list of ee.Feature
objects.This code will create an ee.FeatureCollection
with points corresponding to the coordinates in the DataFrame, and each point will have properties as specified in the DataFrame.